Python 如何获取Tesseract ocr检索到的字母坐标

Python 如何获取Tesseract ocr检索到的字母坐标,python,ocr,tesseract,Python,Ocr,Tesseract,我试图在python中处理tesseract,以完成简单的工作: -打开一幅画 -运行ocr -获取字符串 -获取字符坐标 最后一个是我的痛苦 这是我的第一个代码: import tesseract import glob import cv2 api = tesseract.TessBaseAPI() api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP

我试图在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 -Hocr


但是还没有找到在Python中实现它的方法。

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

提供了访问几乎所有tesseract API功能的功能。下面是一个可能正是您想要的:

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方法,例如
GetHOCRText
GetBoxText

但是,现在它只支持*nix系统,如果您愿意尝试的话,它需要一个用户和提供的二进制文件


免责声明:tesserocr作者在此。

@iMath这是一个使用示例。您可以使用
RIL.WORD
来迭代单词,也可以使用
RIL.SYMBOL
来迭代字母…您的提示确实有效,但如果您使用
api.SetRectangle(box['x']、box['y']、box['w']、box['h'])
限制识别区域,文本识别正确率似乎低于自由限制方式,i、 e.
self.tessBaseAPI.SetImage(图像);打印('----all text-----',self.tessBaseAPI.GetUTF8Text())
,那么引擎盖下面是什么呢?你最好问一个新问题。