【问题标题】:TypeError: an integer is required (got type tuple) <python> <OpenCV> <tesseract>TypeError: an integer is required (got type tuple) <python> <OpenCV> <tesseract>
【发布时间】: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 回答:)

标签: python opencv tesseract


【解决方案1】:

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)

【讨论】:

  • 谢谢,我现在明白我的错误了
  • 不客气,记得使用cv2.waitkey(0)成功显示你的图片。
【解决方案2】:

您的代码中的问题在于以下语句:

(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])

问题应该解决了

【讨论】:

    猜你喜欢
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 2016-11-17
    • 2022-01-08
    • 2018-06-24
    相关资源
    最近更新 更多