Python 如何使用Tesseract API迭代单词?

Python 如何使用Tesseract API迭代单词?,python,python-tesseract,Python,Python Tesseract,我正在尝试与Tesseract API并行学习Python。我的最终目标是学习如何使用Tesseract API来读取文档并执行一些基本的错误检查。我发现了一些似乎是很好的开始的例子,但我很难理解两段代码之间的区别,虽然它们在行为上不同,但在我看来它们应该是等价的。这两个都是从 第一个示例生成此输出: $ time ./GetComponentImagesExample2.py|tail -2 symbol MISSISSIPPI,conf: 88.3686599731 real 0m

我正在尝试与Tesseract API并行学习Python。我的最终目标是学习如何使用Tesseract API来读取文档并执行一些基本的错误检查。我发现了一些似乎是很好的开始的例子,但我很难理解两段代码之间的区别,虽然它们在行为上不同,但在我看来它们应该是等价的。这两个都是从

第一个示例生成此输出:

$ time ./GetComponentImagesExample2.py|tail -2
symbol MISSISSIPPI,conf: 88.3686599731


real    0m14.227s
user    0m13.534s
sys 0m0.397s
这是准确的,在14秒内完成。回顾其余的输出,它非常好——我可能需要一些SetVariable命令才能达到99%以上的精度

$ ./GetComponentImagesExample2.py|wc -l
    1289
手动查看结果,它似乎可以获取所有文本

#!/usr/bin/python
from PIL import Image
Image.MAX_IMAGE_PIXELS=1000000000
from tesserocr import PyTessBaseAPI, RIL, iterate_level

image = Image.open('/Users/chrysrobyn/tess-install/tesseract/scan_2_new.tif')
with PyTessBaseAPI() as api:
    api.SetImage(image)
    api.Recognize()
    api.SetVariable("save_blob_choices","T")
    ri=api.GetIterator()
    level=RIL.WORD
    boxes = api.GetComponentImages(RIL.WORD, True)
    print 'Found {} textline image components.'.format(len(boxes))
    for r in iterate_level(ri, level):
        symbol = r.GetUTF8Text(level)
        conf = r.Confidence(level)
        if symbol:
            print u'symbol {},conf: {}\n'.format(symbol,conf).encode('utf-8')
第二个示例生成此输出

$ time ./GetComponentImagesExample4.py|tail -4
symbol MISSISS IPPI
,conf: 85


real    0m17.524s
user    0m16.600s
sys 0m0.427s
这不太准确(在一个单词中检测到额外的空间),速度较慢(需要17.5秒)

这是非常缺乏大量的文本,我不明白为什么它遗漏了一些东西

#!/usr/bin/python
from PIL import Image
Image.MAX_IMAGE_PIXELS=1000000000
from tesserocr import PyTessBaseAPI, RIL

image = Image.open('/Users/chrysrobyn/tess-install/tesseract/scan_2_new.tif')
with PyTessBaseAPI() as api:
    api.SetImage(image)
    api.Recognize()
    api.SetVariable("save_blob_choices","T")
    boxes = api.GetComponentImages(RIL.WORD, True)
    print 'Found {} textword 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()
        conf = api.MeanTextConf()
        if ocrResult:
            print u'symbol {},conf: {}\n'.format(ocrResult,conf).encode('utf-8')
#        print (u"Box[{0}]: x={x}, y={y}, w={w}, h={h}, "
#               "confidence: {1}, text: {2}").format(i, conf, ocrResult, **box).encode('utf-8')
我的最终目标依赖于理解文本在文档中的位置,因此我需要像第二个示例那样的边界框。据我所知,iterate_级别不公开找到的文本的坐标,因此我需要GetComponentImage。。。但产出并不相等

为什么这些代码在速度和准确性上表现不同?我可以让GetComponentImage与GetIterator匹配吗

api.Recognize()
api.SetVariable("save_blob_choices","T")
ri=api.GetIterator()
level=tesserocr.RIL.WORD
boxes = api.GetComponentImages(tesserocr.RIL.TEXTLINE, True)
text_list = []
print 'Found {} textline image components.'.format(len(boxes))
i = 0
for r in tesserocr.iterate_level(ri, level):
    symbol = r.GetUTF8Text(level)
    conf = r.Confidence(level)
    bbox = r.BoundingBoxInternal(level)
    im = Image.fromarray(img[bbox[1]:bbox[3], bbox[0]:bbox[2]])
    im.save("../out/" + str(i) + ".tif")
    text_list.append(symbol + " " + str(conf) + "\n")
    i += 1
我认为函数r.BoundingBoxInternal(level)将给出检测到的单词的边界框

api.Recognize()
api.SetVariable("save_blob_choices","T")
ri=api.GetIterator()
level=tesserocr.RIL.WORD
boxes = api.GetComponentImages(tesserocr.RIL.TEXTLINE, True)
text_list = []
print 'Found {} textline image components.'.format(len(boxes))
i = 0
for r in tesserocr.iterate_level(ri, level):
    symbol = r.GetUTF8Text(level)
    conf = r.Confidence(level)
    bbox = r.BoundingBoxInternal(level)
    im = Image.fromarray(img[bbox[1]:bbox[3], bbox[0]:bbox[2]])
    im.save("../out/" + str(i) + ".tif")
    text_list.append(symbol + " " + str(conf) + "\n")
    i += 1