【发布时间】:2021-03-09 09:28:32
【问题描述】:
我正在尝试对发票进行文本识别。
import pytesseract
from pytesseract import Output
import cv2
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
img = cv2.imread('bill_copy.jpg')
d = pytesseract.image_to_data(img, output_type=Output.DICT)
n_boxes = len(d['level'])
for i in range(n_boxes):
(x, y, w, h) = (d['left'], d['top'], d['width'], d['height'])
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.imshow(img, 'img')
当我运行它时,我得到 enter image description here
【问题讨论】:
-
我不知道 pytesseract 究竟是如何工作的,但我认为它会返回一个盒子的坐标元组,你正在循环 n_boxes,但你没有使用索引,即。我想您正在传递框坐标的元组,而不是其中一个坐标。尝试打印 x 的值,看看我的怀疑是否正确。
-
@Leander,我明白了 [0, 548, 548, 548, 548, 1146, 624, 624, 624, 624, 932, 1209, 0, 0, 0, 0, 2047, 2047 , 2047, 2047] Traceback(最近一次通话最后):文件“F:/BashundharaIT/Bill OCR Python OpenCV/align_documents.py”,第 13 行,在
img = cv2.rectangle(img, (x, y) , (x + w, y + h), (0, 0, 255), 2) TypeError: an integer is required (got type tuple) -
我为这么多编辑道歉,我的网络正在苦苦挣扎。
-
没错,您正在传递多个框的坐标,请查看下面的 Peters 回答:)