简体   繁体   English

TypeError:需要一个 integer(获取类型元组)<python><opencv><tesseract></tesseract></opencv></python>

[英]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.

相关问题 Python TypeError:需要一个整数(得到类型元组)-(OpenCV / Numpy) - Python TypeError: an integer is required (got type tuple) - (OpenCV / Numpy) 类型错误:需要一个 integer(获取类型元组)日期时间 Python - TypeError: an integer is required (got type tuple) datetime Python 类型错误:需要一个 integer(获取类型元组)|| python 中的网站阻止代码 - TypeError: an integer is required (got type tuple) || website blocking code in python Python TypeError:整数是必需的(got类型列表) - Python TypeError: an integer is required (got type list) Python:类型错误:需要一个整数(得到类型 str) - Python: TypeError: an integer is required (got type str) TypeError:Python中需要一个整数(类型为str) - TypeError: an integer is required (got type str) in python Python: TypeError: an integer is required (got type module) - Python: TypeError: an integer is required (got type module) Python:TypeError 需要 integer(获取类型 str) - Python: TypeError an integer is required(got type str) 枕头图像TypeError:需要一个整数(元组类型元组) - pillow image TypeError: an integer is required (got type tuple) 错误:必须为整数(元组类型为元组) - Error : an integer is required (got type tuple)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM