【问题标题】:How to get the letter coordinate retrieved by Tesseract ocr如何获取 Tesseract ocr 检索到的字母坐标
【发布时间】:2020-02-16 16:50:33
【问题描述】:

我正在尝试在 python 中处理 tesseract 来做简单的工作: - 打开一张图片 - 运行 ocr - 获取字符串 - 获取字符坐标

最后一个是我的痛!

这是我的第一个代码:

import tesseract
import glob
import cv2

api = tesseract.TessBaseAPI()
api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZéèô%")
api.SetPageSegMode(tesseract.PSM_AUTO)

imagepath = "C:\\Project\\Bob\\"
imagePathList = glob.glob(imagepath + "*.jpg")

for image in imagePathList:
    mBuffer=open(imagePathList[10],"rb").read()
    result = tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api)
    img = cv2.imread(image)
    cv2.putText(img,result,(20,20), cv2.FONT_HERSHEY_PLAIN, 1.0,(0,255,0))       
    cv2.imshow("Original",img)
    cv2.waitKey()

由于我的图片有不同的布局,不同的位置有不同的单词,我想为每个字符设置一个框。

我见过谈论: - api.getBoxText - 霍克

但是没有找到用 Python 实现它的方法。

【问题讨论】:

    标签: python ocr tesseract


    【解决方案1】:

    tesserocr 提供了访问几乎所有 tesseract 的 API 功能的能力。这是您可能想要的example

    from PIL import Image
    from tesserocr import PyTessBaseAPI, RIL
    
    image = Image.open('/usr/src/tesseract/testing/phototest.tif')
    with PyTessBaseAPI() as api:
        api.SetImage(image)
        boxes = api.GetComponentImages(RIL.TEXTLINE, True)
        print 'Found {} textline image components.'.format(len(boxes))
        for i, (im, box, _, _) in enumerate(boxes):
            # im is a PIL image object
            # box is a dict with x, y, w and h keys
            api.SetRectangle(box['x'], box['y'], box['w'], box['h'])
            ocrResult = api.GetUTF8Text()
            conf = api.MeanTextConf()
            print (u"Box[{0}]: x={x}, y={y}, w={w}, h={h}, "
                   "confidence: {1}, text: {2}").format(i, conf, ocrResult, **box)
    

    您还可以访问其他 API 方法,例如 GetHOCRTextGetBoxText 等。

    不过,目前它仅支持 *nix 系统,尽管用户 successfully compiled it on Windows 并提供了二进制文件,如果您想尝试一下。

    免责声明:tesserocr 作者在这里。

    【讨论】:

    • @iMath 这是一个使用示例。您可以使用RIL.WORD 来迭代单词,还有RIL.SYMBOL 来迭代字母...
    • 您的提示确实有效,但似乎如果您使用api.SetRectangle(box['x'], box['y'], box['w'], box['h']) 限制识别区域,则文本识别校正率低于免费限制方式,即self.tessBaseAPI.SetImage(image);print('-----all text------',self.tessBaseAPI.GetUTF8Text()),那么引擎盖下是什么?
    • 你最好问一个新问题。
    【解决方案2】:

    如果 Python 包装器支持,您可能需要调用 GetHOCRText 方法。

    【讨论】:

      猜你喜欢
      • 2014-05-20
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      相关资源
      最近更新 更多