前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytesser模块WindowsError错误解决方法

pytesser模块WindowsError错误解决方法

作者头像
jhao104
发布2018-03-20 17:00:06
1.4K0
发布2018-03-20 17:00:06
举报
文章被收录于专栏:烂笔头烂笔头

目录[-]

在使用pytesser做图片文字识别时遇到 WindowsError: [Error 2] 错误,报错内容如下:

代码语言:javascript
复制
Traceback (most recent call last):
  File "E:/Code/Captcha/ChinaMobileEC/recogCaptcha.py", line 37, in <module>
    print pytesser.image_to_string(out)
  File "E:\Code\Captcha\pytesser\pytesser.py", line 36, in image_to_string
    call_tesseract(scratch_image_name, scratch_text_name_root)
  File "E:\Code\Captcha\pytesser\pytesser.py", line 25, 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] 的意思是系统找不到指定的文件。

查看pytesser中的代码,其实就是一个调用 tesseract.exe 识别图片的过程,其中代码如下:

代码语言:javascript
复制
tesseract_exe_name = 'tesseract'

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()

就是调用 subprocess.Popen() 执行 tesseract input_filename output_filename, 这样会将识别结果写到out_filename的txt文件。这条命令你可以直接在cmd中到 tesseract.exe 目录下运行,也能看到识别结果。pytesser其实就是调用这个命令识别,然后读取结果返回。

所以上面错误中所说的找不到的文件就是指 tesseract.exe。所以解决这个问题的方法有两种:

第一种时将 tesseract.exe 所在路径添加到搜索路径,这样无论在哪儿执行 tesseract 系统都能找到。

第二种是修改 pytesser 中的代码, 将 tesseract 换成绝对路径即可:

代码语言:javascript
复制
import os

pwd = os.path.dirname(__file__)

# 将原来的 tesseract_exe_name='tesseract' 改成绝对路径
tesseract_exe_name = os.path.join(pwd,'tesseract')
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年11月23日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档