Skip to main content
 首页 » 编程设计

python之如何使用 Tesseract API 迭代单词

2025年02月15日12bluestorm

我正在尝试在学习 Tesseract API 的同时学习 Python。我的最终目标是学习如何使用 Tesseract API 来读取文档并进行一些基本的错误检查。我发现了一些例子,这些例子似乎是很好的起点,但我很难理解两段代码之间的区别,虽然行为不同,但在我看来它们应该是等价的。这些都从 https://pypi.python.org/pypi/tesserocr 稍作修改.

第一个示例产生以下输出:

$ 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 秒)。

$ ./GetComponentImagesExample4.py|wc -l 
     223 

这严重缺少大量文本,我不明白为什么它会遗漏一些内容。

#!/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_level 不公开找到的文本的坐标,因此我需要 GetComponentImages...但输出不相等。

为什么这些代码片段在速度和准确性方面表现不同?我可以让 GetComponentImages 匹配 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) 会给出检测到的单词的边界框。