Pytesseract:FileNotFound

Posted

技术标签:

【中文标题】Pytesseract:FileNotFound【英文标题】:Pytesseract: FileNotFound 【发布时间】:2017-11-06 14:35:33 【问题描述】:

我在使用 Pytesser 进行测试时遇到了一些问题:

from PIL import Image
import pytesseract

img = Image.open('pic.png')
img.load()
text = pytesseract.image_to_string(img)
print(text)

在 Windows 中的 Python 3.4 上运行

运行时我收到来自 Pytesseract 模块的错误:

Traceback (most recent call last):
   File "C:/Users/Gamer/Documents/Python/Bot/test.py", line 6, in <module>
      text = pytesseract.image_to_string(img)
   File "C:\Python34\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
      config=config)
File "C:\Python34\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
   proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "C:\Python34\lib\subprocess.py", line 859, in __init__
   restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child
   startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

我是安装模块的新手,这可能源于 Tesseract-OCR 或模块的错误安装或设置。

任何帮助将不胜感激,

-尼尔

【问题讨论】:

您的路径中是否有来自 Tesseract-OCR 的 tesseract?如果没有,请将其添加到您的 PATH 环境变量中或使用 this 变量来提供自定义路径。 【参考方案1】:

我在安装 tesseract 时没有任何问题,但我在 UB Mannheim 安装程序中使用了 Tesseract:

https://github.com/UB-Mannheim/tesseract/wiki

您还需要安装 pytesseract:

pip3.6 安装 pytesseract

看来 Python 在查找图像位置时遇到了问题。我建议使用带有图像路径的变量集来排除任何与 PATH 相关的问题。这是一个例子:

#Path to image folder    
src_path = "C:\\Users\\USERNAME\\Documents\\OCR\\"

#Run OCR on image    
text = pytesseract.image_to_string(Image.open(src_path + "pic.png"))

#Print OCR result
print (text)

【讨论】:

【参考方案2】:

正如“st0le”所评论的那样,事实证明,需要正确定义 OCR-Tesseract 的路径,而不是图像的路径。

我想我会为其他遇到类似问题的人解决这个问题,因为我现在已经解决了这个问题。

【讨论】:

以上是关于Pytesseract:FileNotFound的主要内容,如果未能解决你的问题,请参考以下文章

Pytesseract.TesseractError '用法:python pytesseract.py [-l lang] input_file

Pytesseract:FileNotFound

pytesseract:将 7 段数字的图片转换为文本

解决pytesseract.pytesseract.TesseractError: (1, ‘Error opening data file C:\Program Files\Tesseract-(代

解决pytesseract.pytesseract.TesseractError: (1, ‘Error opening data file C:\Program Files\Tesseract-(代

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your p