您的位置:首页 > 其它

关于pytesser的一些问题

2016-03-17 10:51 253 查看
最近几天在做python验证码的识别,主要是运用python的ocr库,可惜在安装的时候就出现了很多问题,在google,stackoverflow,baidu上都没有有效解决,无奈自力更生,从源码层面开始,一步一步开始排除bug。

一、运行的主要问题如下:

Traceback (most recent call last):
File "C:\Users\TF-2016\Desktop\spider\ruijie\ruijie.py", line 33, in <module>
print image_file_to_string('11.png', graceful_errors=True)
File "C:\Python27\lib\site-packages\pytesser\pytesser.py", line 48, in image_file_to_string
call_tesseract(filename, scratch_text_name_root)
File "C:\Python27\lib\site-packages\pytesser\pytesser.py", line 23, in call_tesseract
proc = subprocess.Popen(args)
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2]


WindowsError: [Error 2] 的意思一般是找不到可靠的文件。

解决方案:

1、将需要运行的文件,直接放在pytesser的包下

2、如果包在site-packages下,可能如上问题会一直存在,弄了几次还是如此,问题应该是tesseract.exe执行程序不在环境变量中,大家可以自行更改,将其直接添加至环境变量中,懒的话就像方法1那么操作就行。

在pytesser.py文件中有:

This file must be .bmp or other Tesseract-compatible format

所以如果自己给的输入文件不能被很好支持,那么

- 直接使用
image\_to_string()
函数容易失败,

- 尽量使用
image\_file\_to\_string(filename, cleanup=cleanup\_scratch\_flag, graceful\_errors=True)
,注意最后的graceful_errors=True,很有用,当然它已经设定了默认值,可以不用手工赋值。

二、还有一种错误是errors.py文件找不到tesseract.log文件

这个是因为,在pytesser.py文件中调用subprocess时,执行tesseract.exe引擎没有返回错误(即没有生成tesseract.log文件),虽然也没有输出所要的文件结果(即进入了需要参数为tesseract.log的函数)———听着很绕,总之就是出bug了。

部分代码:

def check_for_errors(logfile="tesseract.log"):
inf = file(logfile)
text = inf.read()
inf.close()
# All error conditions result in "Error" somewhere in logfile
if text.find("Error") != -1:
raise Tesser_General_Exception, text


感兴趣的同学可以去看看程序源码,不长。

那该怎么着解决呢?

很简单 ,去包里找到tesseract.exe,双击它就行,会自动产生一个tesseract.log错误日志。

如果想使用tesseract.exe直接识别,格式为

tesseract.exe input_filename output_filename
(eg:tesseract.exe default.png default)


最后,附点文件:

1、 PyTesser

2、 Tesseract

3、 或者我早csdn上的PyTesser资源

注:网上有教程说换tessdata的内容,不用换

Dependencies:

pytesser.py Main module for importing

util.py Utility functions used by pytesser.py

errors.py Interprets exceptions thrown by Tesseract

tesseract.exe Executable called by pytesser.py

tessdata/ Resources used by tesseract.exe
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: