[英]TypeError: an integer is required (got type tuple) <python> <OpenCV> <tesseract>
I am trying to do a text recognition on invoices.我正在尝试对发票进行文本识别。
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')
When i run it, i get enter image description here当我运行它时,我会在此处输入图像描述
The parameter of x, y, w, h is an array of every divided character, But in the loop it draws the rectangle one by one. x,y,w,h的参数是每个分割字符的数组,但是在循环中它一个一个地绘制矩形。
So you need to send an integer for those parameter(x, y, w, h) every loop.因此,您需要在每个循环中为这些参数(x,y,w,h)发送一个 integer。
And there is plenty of error in your code.而且您的代码中有很多错误。 The right code should be like that:正确的代码应该是这样的:
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)
The problem in your code is in the following statement:您的代码中的问题在以下语句中:
(x, y, w, h) = (d['left'], d['top'], d['width'], d['height'])
You need to get ith value of each region您需要获取每个区域的第 i 个值
(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
The problem should be solved问题应该解决
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.