我正在尝试以下代码,类似于tesserocr's docs中的代码:基本上我有一个"hello world" image,并且我想获取一个列表框及其单词。但是它没有按预期方式工作:第一个getUTF8Text()返回整个图像的“ Hello World”,但是循环内的第二个getUTF8Text始终返回一个空字符串,尽管确实标识了2个框,正确地围绕了每个单词。 我究竟做错了什么?谢谢!
from PIL import Image
from tesserocr import PyTessBaseAPI, RIL
image = Image.open('helloworld.jpg')
with PyTessBaseAPI() as api:
api.SetImage(image)
print(api.GetUTF8Text()) # <==== "hello world"
boxes = api.GetComponentImages(RIL.WORD, True)
print('Found {} textline image components.'.format(len(boxes)))
for i, (im, box, _, _) in enumerate(boxes):
api.SetRectangle(box['x'], box['y'], box['w'], box['h'])
ocrResult = api.GetUTF8Text() # <==== empty string
conf = api.MeanTextConf()
print("Box[{0}]: x={x}, y={y}, w={w}, h={h}, confidence: {1}, text: {2}".format(i, conf, ocrResult, **box))
输出为:
Hello World
Found 2 textline image components.
Box[0]: x=42, y=148, w=111, h=37, confidence: 95, text:
Box[1]: x=170, y=148, w=129, h=37, confidence: 95, text:
请注意,输出文本包含很多换行符,我不知道为什么。