pytesseract subprocess file not found

pytesseract subprocess file not found

Python识别验证码的神器pytesseract。这是一个现成的库,直接pip就可以安装。兼容python3。

但是网上许多人都舍近求远,要去整pytesser。pytesser呢,只有0.01版本。而且原生态是python2下的,python3下还不能直接使用。要使用还需要做些修改。而且,这库还没有pip版本。需要自己手动下载安装,修改。许多人还不是乐此不疲的整pytesser。看到这样的,我只想说一句,为毛一个个的都舍近求远。

当然,pytesser我也整了一把。。。。闲的。。。还改成了Python3下能用的。要下载可以去我的下载资源看看。。。

好了,闲话不提了,切入正题。这篇博客是我的一个debug日志吧。写出来与大家分享。

problem

代码如下:

# encoding:utf8

import pytesseract
import pytesser
from PIL import Image, ImageGrab

def read_img(path):
    img = Image.open(path)
    # txt = pytesseract.image_to_string(img)
    txt = pytesser.image_to_string(img)
    print(txt)
    return txt

if __name__ == "__main__":
    path="D:/img.bmp"
    read_img(path)
    pass

报错如下:

Connected to pydev debugger (build 172.3968.37)
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/SoftwareByMyself/PythonPro/PyStockDealer/util/ImgReader.py", line 27, in <module>
    read_img(path)
  File "D:/SoftwareByMyself/PythonPro/PyStockDealer/util/ImgReader.py", line 12, in read_img
    txt = pytesser.image_to_string(img)
  File "D:\Python35-32\lib\site-packages\pytesser\__init__.py", line 32, in image_to_string
    call_tesseract(scratch_image_name, scratch_text_name_root)
  File "D:\Python35-32\lib\site-packages\pytesser\__init__.py", line 22, in call_tesseract
    proc = subprocess.Popen(args)
  File "D:\Python35-32\lib\subprocess.py", line 676, in __init__
    restore_signals, start_new_session)
  File "D:\Python35-32\lib\subprocess.py", line 957, in _execute_child
    startupinfo)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 420, in new_CreateProcess
    return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

由于我切换电脑的缘故,在windows下碰到这个问题也有几次了。前几次都没找到门路,捣腾到最后解决了也不知道是为啥。这次算是知道问题的症结所在了。

开始的时候看到subprocess报错。以为是subprocess相关的库不兼容之类的问题。因为同样的代码我在linux下执行是没有问题的。一拿到windows下就过不去。

经过几番详细探查分析,总算发现问题所在。先看一张截图。
这里写图片描述

看到阴影处下方的提示框了没有。有一个变量的值。

cmd_line={str} 'tesseract temp.bmp temp'

cause

从这里,我们可以知道些什么呢?
首先,pytesser底层也是调用的tesseract命令行来识别的图片。
其次,pytesser是调用了子进程来识别图片,然后等待返回。

看到第一条,有些失望了啊,原来之前略显神秘有些高大让人欣喜的pytesseract底层却是这么赤裸裸的调用的别人的库。早知道这样,我自己直接用tesseract不就完了么。干嘛还用pytesseract转一道手。

好吧,回到主题。既然知道了其底层的原理,结合错误提示信息“file not found”,不难联想到可能是tesseract这个命令所在文件夹没有加入到环境变量的path中,导致执行一个命令行启动子进程时报找不到文件的错误。

在cmd中输入tesseract,果然提示找不到文件。

嗯,这个问题的症结就在此处。。。。:) 简单吧。。。。

solution

解决方案很简单。安装tesseract-ocr,将其目录加入环境变量,创建TESSDATA_PREFIX环境变量并设置为tessdata目录
如果不设置TESSDATA_PREFIX的话,windows下会报错的。报错信息如下:

print(pytesseract.image_to_string(Image.open('d:/testimages/name.gif'), lang='chi_sim'))
 File "C:\Users\dell\AppData\Local\Programs\Python\Python35\lib\site-packages\pytesseract\pytesseract.py", line 165, in image_to_string
   raise TesseractError(status, errors)
pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Program Files (x86)\\Tesseract-OCR\\tessdata/chi_sim.traineddata')

tesseract-ocr下载地址。
官网github地址:https://github.com/tesseract-ocr/tesseract/wiki/Downloads

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值