Pytesseract错误 Windows错误[错误2]

3

你好,我正在尝试使用Python库pytesseract从图片中提取文本。以下是代码:

from PIL import Image
from pytesseract import image_to_string
print image_to_string(Image.open(r'D:\new_folder\img.png'))

但是出现了以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
config=config)
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
stderr=subprocess.PIPE)
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] The system cannot find the file specified

我没有找到针对此问题的具体解决方法。有人可以帮助我该怎么做吗?需要下载更多东西或者从哪里下载等等…

提前感谢 :)

4个回答

4

我曾经也遇到过同样的问题,后来在阅读了这篇文章之后便找到了解决方案:

OSError: [Errno 2] No such file or directory using pytesser

只需要将下面的代码替换为适用于Windows的代码即可:

tesseract_cmd = 'tesseract'

使用:

tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'

(需要双\\来转义字符串中的第一个\)


2
您之所以收到异常,是因为子进程无法找到二进制文件(tesser可执行文件)。
安装过程包括三个步骤:
1. 下载/安装系统级别的库/二进制文件:
不同操作系统可以使用这里进行下载/安装。对于MacOS用户,可以直接使用brew进行安装。

安装Google Tesseract OCR(如何在Linux、Mac OSX和Windows上安装引擎的其他信息)。你必须能够调用tesseract命令作为tesseract。如果不是这种情况,例如因为tesseract不在您的PATH中,您将不得不更改tesseract.py顶部的“tesseract_cmd”变量。在Debian/Ubuntu下,您可以使用tesseract-ocr软件包。对于Mac OS用户,请安装homebrew软件包tesseract。

对于Windows用户:
我们的下载页面提供了适用于Windows的旧版本3.02的安装程序。其中包括英语训练数据。如果您想使用其他语言,请下载相应的训练数据,使用7-zip解压缩,并将.traineddata文件复制到“tessdata”目录中,可能是C:\Program Files\Tesseract-OCR\tessdata
要从任何位置访问tesseract-OCR,您可能需要将tesseract-OCR二进制文件所在的目录添加到Path变量中,可能是C:\Program Files\Tesseract-OCR
您可以从这里下载.exe文件。
2.安装Python软件包。
pip install pytesseract

"最后,您需要在PATH中拥有Tesseract二进制文件。或者,您可以在运行时设置它:"
import pytesseract

pytesseract.pytesseract.tesseract_cmd = '<path-to-tesseract-bin>'

对于Windows:
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'

上述行将使其临时工作,要进行永久解决方案,请将 tesseract.exe 添加到 PATH 中,例如:PATH=%PATH%;"C:\Program Files (x86)\Tesseract-OCR"。
此外,请确保设置了 TESSDATA_PREFIX Windows 环境变量为包含 tessdata 目录的目录。例如:

TESSDATA_PREFIX=C:\Program Files (x86)\Tesseract-OCR

即 tessdata 的位置为:C:\Program Files (x86)\Tesseract-OCR\tessdata
你的例子:
from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
print pytesseract.image_to_string(Image.open(r'D:\new_folder\img.png'))

0

您需要在计算机上安装Tesseract OCR引擎(“Tesseract.exe”)。如果路径未在计算机中配置,请在pytesseract.py(tesseract.py)中提供完整路径。

README

安装Google Tesseract OCR(有关如何在Linux、Mac OSX和Windows上安装引擎的其他信息)。您必须能够调用tesseract命令作为tesseract。如果不是这种情况,例如因为tesseract不在您的PATH中,则必须更改tesseract.py顶部的“tesseract_cmd”变量。在Debian / Ubuntu下,您可以使用tesseract-ocr软件包。对于Mac OS用户,请安装homebrew软件包tesseract。

Another thread


0

我也遇到了关于pytesseract的同样问题。 我建议您在Linux环境下工作,以解决此类错误。 在Linux中执行以下命令:

pip install pytesseract
sudo apt-get update
sudo apt-get install pytesseract-ocr

希望这能完成工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接