Pytesseract 没有此文件或目录错误。

5

首先,我按照pytesseract-无此文件或目录错误中提到的步骤进行操作。

但仍然没有解决问题。现在我使用 Pycharm IDE,并编写了以下代码:

from PIL import Image
import pytesseract
import subprocess

im = Image.open('test.png')
im.show()
subprocess.call(['tesseract','test.png','out'])
print pytesseract.image_to_string(Image.open('test.png'))
  • im.show() 成功打开了图片。
  • subprocess.call() 与 tesseract test.png out 同样可以从图片中提取出文本。
  • 但是 pytesseract.image_to_string() 失败了。

我不明白,为什么我能在shell中使用tesseract,但在python中却不能。而且在python中我可以打开同一张图片,但使用tesseract却找不到图片。

下面可以看到错误输出。

 File "/home/hamza-c/Schreibtisch/Android/JioShare/orc.py", line 7, in <module>
    print pytesseract.image_to_string(Image.open('/home/hamza-c/Schreibtisch/Android/JioShare/test.png'))
  File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 162, in image_to_string
    config=config)
  File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 95, in run_tesseract
    stderr=subprocess.PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1340, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

我曾经遇到同样的问题,并在这里找到了解决方案。 - Ícaro Magalhães
4个回答

5
我测试了您在问题中提到的代码,它可以正常工作。我遇到了相同的错误

未找到此类文件或目录

问题是包含“tesseract.exe”的目录未添加到环境变量中。您应该能够在命令提示符中运行“tesseract”命令。
如果未安装tesseract,则可以从tesseract下载1https://github.com/tesseract-ocr/tesseract/wiki,对于Windows,请使用可用的第三方安装程序here

如何添加环境变量?你是如何解决的? - agent18

0

我使用了以下命令,这个方法对我来说有效:

brew install tesseract

0

也许你需要安装tesseract,如果你的操作系统是CentOS,请输入以下命令

yum install tesseract

-6

我解决了自己的问题。

im = Image.open('test.png')
print pytesseract.image_to_string(im)

为什么当传递引用时它可以工作,但是直接尝试打开参数内的图像时却不清楚。


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