从tesseract导入image_to_string时出现Python错误

14

最近我在使用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”。 - monkut
我认为你使用了错误的Python绑定... 请问在 vars(tesseract) 中有什么内容? - wim
4个回答

9

我发现另一种可能性是修改pytesseract,将import Image改为from PIL import Image。

在修改过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。

1
我收到一个错误提示 - OSError: [Errno 2] 没有该文件或目录 在文件“/usr/lib/python2.7/subprocess.py”的第679行,“__init__”函数出错, errread、errwrite) 在“/usr/lib/python2.7/subprocess.py”的第1249行,执行子进程时出错。 - Hussain
1
@C.R.Sharat 是的,很久以前。我不记得是什么解决了它。如果有帮助的话,我正在使用 PIL==1.1.7、pytesseract==0.1.6、Pillow==2.9.0,并且我已经安装了 sudo apt-get install python-opencv - Hussain
2
apt-get install tesseract-ocr # 这可能会解决这个问题,@C.R.Sharat - shantanuo

1
对于Windows,请按照以下步骤进行操作。
pip3 install pytesseract 
pip3 install pillow

安装tessaract-ocr也是必须的https://github.com/tesseract-ocr/tesseract/wiki,否则你将会得到一个错误提示:Tessract不在路径上。
Python代码。
from PIL import Image
from pytesseract import image_to_string

print ( image_to_string(Image.open('test.tif'),lang='eng')  )

1

0

适用于我的方法:

在我安装了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社区版2018.2.3 x64


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