【问题标题】:Python error when importing image_to_string from tesseract从 tesseract 导入 image_to_string 时出现 Python 错误
【发布时间】:2013-01-16 10:10:04
【问题描述】:

我最近在 python 中使用了 tesseract OCR,当我尝试从 tesseract 导入 image_to_string 时,我一直收到错误消息。

导致问题的代码:

# Perform OCR using tesseract-ocr library
from tesseract import image_to_string
image = Image.open('input-NEAREST.tif')
print image_to_string(image)

以上代码导致的错误:

Traceback (most recent call last):  
file "./captcha.py", line 52, in <module>  
from tesseract import image_to_string  
ImportError: cannot import name image_to_string

我已验证安装了 tesseract 模块:

digital_alchemy@roaming-gnome /home $ pydoc modules | grep 'tesseract'
Hdf5StubImagePlugin _tesseract          gzip                sipconfig
ORBit               cairo               mako                tesseract

我相信我已经获得了所有必需的包,但不幸的是我只是停留在这一点上。看来该功能不在模块中。

非常感谢任何帮助。

【问题讨论】:

  • 尝试“import tesseract.image_to_string”,甚至只是“import tesseract”。
  • 我认为你有错误的 python 绑定...vars(tesseract) 中有什么?

标签: python ocr tesseract


【解决方案1】:

似乎对我有用的另一种可能性是修改 pytesseract 以便从 PIL 导入图像而不是导入图像

修改pytesseract后在PyCharm中工作的代码:

from pytesseract import image_to_string
from PIL import Image

im = Image.open(r'C:\Users\<user>\Downloads\dashboard-test.jpeg')
print(im)

print(image_to_string(im))

我通过 PyCharm 内置的包管理安装的 Pytesseract

【讨论】:

  • 我收到一条错误消息 - OSError: [Errno 2] No such file or directory In File "/usr/lib/python2.7/subprocess.py", line 679, in init errread, errwrite) 文件“/usr/lib/python2.7/subprocess.py”,第 1249 行,在 _execute_child 中
  • @C.R.Sharat 是的,很久以前。我不记得是什么解决了它。如果有帮助,我正在使用PIL==1.1.7 pytesseract==0.1.6 Pillow==2.9.0,并且我还安装了sudo apt-get install python-opencv
  • apt-get install tesseract-ocr #这可能会解决这个问题,@C.R.Sharat
【解决方案2】:

对于已安装的模块,您的语法是否正确?根据此页面上的使用示例,image_to_string 函数看起来像是来自 PyTesser: https://code.google.com/p/pytesser/

您的导入看起来像是针对 python-tesseract 的,其中列出了更复杂的用法示例: https://code.google.com/p/python-tesseract/

【讨论】:

    【解决方案3】:

    对于 Windows,请按照以下步骤操作

    pip3 install pytesseract 
    pip3 install pillow
    

    还需要安装 tessaract-ocr https://github.com/tesseract-ocr/tesseract/wiki 否则你会得到一个错误 Tessract is not on path

    Python 代码

    from PIL import Image
    from pytesseract import image_to_string
    
    print ( image_to_string(Image.open('test.tif'),lang='eng')  )
    

    【讨论】:

      【解决方案4】:

      什么对我有用:

      在我安装了 pytesseract 表单后tesseract-ocr-setup-3.05.02-20180621.exe 我添加行 pytesseract.pytesseract.tesseract_cmd="C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe" 并使用上面的代码,这就是所有代码:

      import pytesseract
      from PIL import Image
      
      pytesseract.pytesseract.tesseract_cmd="C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe"
      im=Image.open("C:\\Users\\<user>\\Desktop\\ro\\capt.png")
      print(pytesseract.image_to_string(im,lang='eng'))
      

      我正在使用 Windows 10 和 PyCharm Community Edition 2018.2.3 x64

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-11
        • 2019-09-07
        • 1970-01-01
        相关资源
        最近更新 更多