Search code examples
pythonopencvtesseract

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


Solution

  • 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

    (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
    

    The problem should be solved