我正在尝试对发票进行文本识别。
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
发布于 2021-03-09 09:50:36
代码中的问题出现在以下语句中:
(x, y, w, h) = (d['left'], d['top'], d['width'], d['height'])
你需要得到每个区域的第i个值。
(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
这个问题应该得到解决
发布于 2021-03-09 09:44:47
x,y,w,h的参数是每个分割字符的数组,但是在循环中它一个接一个地绘制矩形。
所以你需要在每个循环中为这些参数(x,y,w,h)发送一个整数。
而且你的代码中有很多错误。正确的代码应该是这样的:
import pytesseract
from pytesseract import Output
import cv2
pytesseract.pytesseract.tesseract_cmd = r'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'])
(x, y, w, h) = (d['left'], d['top'], d['width'], d['height'])
for i in range(n_boxes):
img = cv2.rectangle(img, (x[i], y[i]), (x[i] + w[i], y[i] + h[i]), (0, 0, 255), 2)
cv2.imshow('img',img)
cv2.waitKey(0)
https://stackoverflow.com/questions/66544195
复制