如何使用python3.x检测字符的位置

2024-05-09 23:15:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我按照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'

我知道它是为Python2.7开发的,有人能指导我或者给我一个如何在Python3.x上实现它的方法吗

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

^{pr2}$

Tags: theruninimportptimgoutputas
2条回答

要使用pytesseract和方法image_To_box获取字符及其在图像中的位置:

ret = pt.image_to_boxes(pil_img)

结果是一个字符串,其中每一行都是一个字符,其位置(x1,y1,X2,y2)用空格隔开:

^{pr2}$

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

相关问题 更多 >