我正在使用https://pypi.python.org/pypi/pyocr/0.1.2从图片中识别文字 我的脚本如下:
from PIL import Image
import sys
import pyocr
import pyocr.builders
tools = pyocr.get_available_tools()
if len(tools) == 0:
print("No OCR tool found")
sys.exit(1)
tool = tools[0]
print("Will use tool '%s'" % (tool.get_name()))
langs = tool.get_available_languages()
print("Available languages: %s" % ", ".join(langs))
lang = langs[0]
print("Will use lang '%s'" % (lang))
txt = tool.image_to_string(Image.open('http://www.domain.com/fr/i/3518721/phone'),
lang=lang,
builder=pyocr.builders.TextBuilder())
word_boxes = tool.image_to_string(Image.open('http://www.domain.com/fr/i/3518721/phone'),
lang=lang,
builder=pyocr.builders.WordBoxBuilder())
line_and_word_boxes = tool.image_to_string(
Image.open('http://www.domain.com/fr/i/3518721/phone'), lang=lang,
builder=pyocr.builders.LineBoxBuilder())
当我运行脚本时,我收到此错误消息:
追踪(最近一次通话): 文件“./test.py”,第6行,in tools = pyocr.get_available_tools() AttributeError:'module'对象没有属性'get_available_tools'
什么似乎是问题官员?
答案 0 :(得分:1)
将您的导入更改为:
from PIL import Image
import sys
from pyocr import pyocr
from pyocr import builders
现在pyocr.get_available_tools()
将起作用,因为您已导入模块。但是pyocr.builders.WordBoxBuilder()
不会工作,因为构建器是在其自己的命名空间中导入的。对于TextBuilder和LineBoxBuilder,您需要将它们更改为builders.WordBoxBuilder()
同上。