最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】尝试使用PyTesser解析网易163博客中的验证码

Python crifan 3670浏览 0评论

【背景】

从:

求一个验证码识别代码

得知了一个Python库:

PyTesser

http://code.google.com/p/pytesser/

https://pypi.python.org/pypi/PyTesser/0.0.1

所以,打算试试,用其能否解析之前解决不了的,网易163博客中的验证码。

【折腾过程】

1.从

http://code.google.com/p/pytesser/downloads/list

下载得到pytesser_v0.0.1.zip,解压后,看到

pytesser_v0.0.1\README

说是,无需安装,直接解决即可使用。

2.试了试下面代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
Function:
【记录】尝试使用PyTesser解析网易163博客中的验证码
https://www.crifan.com/try_use_pytesser_decode_netease_163_captcha

Autor:  Crifan Li
Date:   2013-04-02

-------------------------------------------------------------------------------
"""

#---------------------------------import---------------------------------------
import sys;
sys.path.append("pytesser_v0.0.1");
from PIL import Image;
from pytesser import *

def pytesser_try_decode_163captcha():
    """
    try to use PyTesser to decode 163 captcha
    """
    testCaptchaPic = "test_captcha_pic/captcha.gif";
    image = Image.open(testCaptchaPic); # Open image object using PIL
    print image_to_string(image); # Run tesseract.exe on image
    print image_file_to_string(testCaptchaPic);


###############################################################################
if __name__=="__main__":
    pytesser_try_decode_163captcha();

去运行,结果直接导致,Win7弹出出错的对话框,告诉当前程序停止运行:

python.exe has stopped working

后来发现在当前文件夹下面,生成一个temp.bmp图片文件,是黑色背景的,空的图片。

3.发现其下有那个测试文件:

fnord.tif

所以去试试:

    #testCaptchaPic = "test_captcha_pic/captcha.gif";
    testCaptchaPic = "pytesser_v0.0.1/fnord.tif";
    image = Image.open(testCaptchaPic); # Open image object using PIL
    print image_to_string(image); # Run tesseract.exe on image
    print image_file_to_string(testCaptchaPic);

看看结果如何,结果却是和上面错误一样。

4.才注意到,人家说是此PyTesser,用到了:

Tesseract OCR engine

但是很明显,此处没有安装。

所以先去

http://code.google.com/p/tesseract-ocr/downloads/list

下载得到,tesseract-ocr-setup-3.02.02.exe

双击安装,完毕后,却又发现,说是此

tesseract-ocr

又依赖于Leptonica:

Dependencies and Licenses

=========================

Leptonica is required. (www.leptonica.com). Tesseract no longer compiles

without Leptonica.

Libtiff is no longer required as a direct dependency.

所以,貌似又需要去安装Leptonica。

5.找到Leptonica.com的下面页面:

http://leptonica.com/download.html

从中找到了googlecode:

http://code.google.com/p/leptonica/downloads/list

下载得到vs2008-1.68.zip。

但是对于如何使用,却没完全搞懂。

是编译成dll库?还是其他方式,才能使得,tesseract能够引用到此库?

6.后来是看了说明文档:

leptonica_vs2008-1.68/vs2008/doc/quickstart.html

貌似是需要,编译为dll库,然后供tesseract使用?

貌似需要,去看看PyTesser代码,或许才能搞懂,如何调用tesseract以及tesseract如何用到此Leptonica。

后来是看了:

pytesser_v0.0.1\pytesser.py

中的:

tesseract_exe_name = 'tesseract' # Name of executable to be called at command line
scratch_image_name = "temp.bmp" # This file must be .bmp or other Tesseract-compatible format
scratch_text_name_root = "temp" # Leave out the .txt extension
cleanup_scratch_flag = True  # Temporary files cleaned up after OCR operation

def call_tesseract(input_filename, output_filename):
	"""Calls external tesseract.exe on input file (restrictions on types),
	outputting output_filename+'txt'"""
	args = [tesseract_exe_name, input_filename, output_filename]
	proc = subprocess.Popen(args)
	retcode = proc.wait()
	if retcode!=0:
		errors.check_for_errors()

才知道,PyTesser是调用同路径下的tesseract.exe,去执行,获得相应的结果的。

那看起来,可以自己此处通过手动执行此tesseract.exe,试试效果。

7.结果却没有任何输出:

D:\tmp\tmp_dev_root\python\tutorial_summary\PyTesser_test\pytesser_v0.0.1>tesseract.exe fnord.tif outputfile.bmp

D:\tmp\tmp_dev_root\python\tutorial_summary\PyTesser_test\pytesser_v0.0.1>

也没看到有outputfile.bmp文件生成。

 

【总结】

够麻烦的。

算了,暂时不弄了。

等以后有兴趣,有空,再说。

转载请注明:在路上 » 【记录】尝试使用PyTesser解析网易163博客中的验证码

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

  1. 在文件pytesser.py加入 import os os.chdir(os.path.dirname(__file__)) 这样就能正常使用
    RobotCan10年前 (2014-05-19)回复
  2. 运行cmd进入控制台命令行 C:\>e: # 切换到pytesser所在盘 E:\>cd pytesser # 切换到pytesser所目录 E:\pytesser>python # 不用sys.path.append # 直接运行: >>> from PIL import Image >>> from pytesser import * >>> im = Image.open("e:/图片.jpg") >>> image_to_string(im) '7025\n\n' >>> 以上是在XP上测试成功,用你的方法也是出现一堆错误。 初学python,两种方法的区别还不懂,求指点,谢谢!
    ccelee11年前 (2013-07-08)回复
93 queries in 0.189 seconds, using 22.20MB memory