-2

This code was working few days ago. But now getting typeerror

CODE:

import cv2
import numpy as np
import pytesseract
from langdetect import detect_langs
from pytesseract import *
from flask import Flask,request
import requests 

try:
    from PIL import Image
except ImportError:
    import Image

#pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'


img = Image.open('G:/Agrima/example_code_API/OCR/FDA.png')

#h, w, c = img.shape

d = pytesseract.image_to_data(img,output_type=Output.DICT)


detected_ocr = image_to_string(img)
points = []
n_boxes = len(d['text'])
for i in range(n_boxes):
    if int(d['conf'][i]) > 60:
        (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i],d['height'][i])
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
        mark = {'x':x,'y':y,'width':w,'height':h}
        points.append({'mark':mark})

# print(points)
cv2.imshow('img', img)
cv2.waitKey(0)

Error in img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

Also tried changing to img = cv2.rectangle(img, int((x, y)), int((x + w, y + h)), (0, 255, 0), 2)

Error log :

img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) TypeError: an integer is required (got type tuple)

Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69
  • 1
    Can you show a print of `(x, y, w, h)` – Rakesh Feb 06 '20 at 09:04
  • @Rakesh 0 0 220 1 – Sai Krishnadas Feb 06 '20 at 09:11
  • @Georgy Thanks for fetching me the answer. That Answer took me to someother answer which solved my problem. The problem was OpenCV doesn't appear to work directly with PIL image. So, had to convert it to RGB and store them as a numpy array and conver it to BGR – Sai Krishnadas Feb 06 '20 at 09:29
  • 1
    No idea why my comment with the link was deleted. I'll leave it here again: [Getting an “integer is required (got type tuple)” error, drawing a rectangle using cv2](https://stackoverflow.com/q/54460134/7851470) – Georgy Feb 06 '20 at 09:36

1 Answers1

0

I believe the error statement might be misleading. I remember encountering this problem once due to my coordinates (x, y) being float and not int. Check to see if your variables x, y, w, h are indeed int.

  • yeah. I tried printing (x,y,w,h) . Output : 0 0 220 1 ......Even double checked with type(x) .. Which is 'int' – Sai Krishnadas Feb 06 '20 at 09:13
  • When doing `x+w` and `y+h` do you still end up with integers or floats? If you for example do `1.1+2.2` in Python it'll return a float with the value `3.3000000000000003` This error occurs due to floating point numbers generally aren't exact representations of decimal numbers but instead are approximations. –  Feb 06 '20 at 09:18
  • yeah it stills gives me 'int'.... type(x+w) is 'int' – Sai Krishnadas Feb 06 '20 at 09:21
  • 1
    Hey Thanks for your time. I solved the issue.... The problem was OpenCV doesn't appear to work directly with PIL image. So, had to convert it to RGB and store them as a numpy array and conver it to BGR. Cheers !! – Sai Krishnadas Feb 06 '20 at 09:31