python - 如何使用 python 3.x 检测字符的位置

标签 python opencv ocr tesseract python-tesseract

我想检测图像中每个字符的位置。

我按照 how to get character position in pytesseract 中的建议尝试了 pytesseract但给了我一个错误

import csv
import cv2
from pytesseract import pytesseract as pt

pt.run_tesseract('bw.png', 'output', lang=None, boxes=True, config="hocr")

# To read the coordinates
boxes = []
with open('output.box', 'rb') as f:
    reader = csv.reader(f, delimiter = ' ')
    for row in reader:
        if(len(row)==6):
            boxes.append(row)

# Draw the bounding box
img = cv2.imread('bw.png')
h, w, _ = img.shape
for b in boxes:
    img = cv2.rectangle(img,(int(b[1]),h-int(b[2])),(int(b[3]),h-int(b[4])),(255,0,0),2)

cv2.imshow('output',img)



---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-bad5c94b3e13> in <module>()
      3 from pytesseract import pytesseract as pt
      4 
----> 5 pt.run_tesseract('input/1230.jpg', 'output', lang=None, boxes=True, config="hocr")
      6 
      7 # To read the coordinates

TypeError: run_tesseract() got an unexpected keyword argument 'boxes'

我确实意识到它适用于 python 2.7,有人可以指导我或让我开始了解如何为 python 3.x 做到这一点

然后我尝试了 tesserocr,无论如何它似乎都无法解析 tessdata 的路径,我尝试使用 tesseracct-ocr,尝试手动下载 tessdata 但仍然出现相同的错误。

from PIL import Image
from tesserocr import PyTessBaseAPI, RIL

image = Image.open('train/1230.jpg')
with PyTessBaseAPI(path="C:\\Users\light\Desktop\tessdata-master") 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)


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-11-68789b2bbe93> in <module>()
      3 
      4 image = Image.open('train/1230.jpg')
----> 5 with PyTessBaseAPI(path="C:\\Users\light\Desktop\tessdata-master") as api:
      6     api.SetImage(image)
      7     boxes = api.GetComponentImages(RIL.TEXTLINE, True)

tesserocr.pyx in tesserocr._tesserocr.PyTessBaseAPI.__cinit__()

tesserocr.pyx in tesserocr._tesserocr.PyTessBaseAPI._init_api()

RuntimeError: Failed to init API, possibly an invalid tessdata path: C:\Users\light\Desktop essdata-master

最佳答案

boxes 参数仅对 pytesseract 版本 0.1.x 有效。它已在 0.2.0 版本中被删除。

关于python - 如何使用 python 3.x 检测字符的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49614028/

相关文章:

python - 简化座标 list

opencv - python opencv 从 bytearray 创建图像

java - 没有安装 Tesseract 的 Apache Tika OCR

python - MANIFEST.IN中包含的数据文件存储在哪里?

python - 为什么 sorted() 和 reversed() 是内置函数而不是序列方法?

javascript - 使用 jquery 或 javascript 单击时切换复选框标签周围的边框

python - 为什么 python 不引发 NameError

c# - OpenCv:查找多个匹配项

parsing - 如何读取图像上的文字?

php - 如何在 wamp 服务器上使用 Tesseract PHP 包装器