Aman Relan
Aman Relan

Reputation: 393

TypeError: an integer is required (got type tuple)

I am trying to set up my detector for a face recognition project or a program, but I keep getting this error:

TypeError: an integer is required (got type tuple)

Also I tried with changing:

cv2.putText(img, str(id), (x, y + h), font, 255)

to

cv2.putText(img, name, (x, y + h), font, 2, (0, 255, 0), 2)

Here's my code:

import cv2
import numpy as np

faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam=cv2.VideoCapture(0)

rec = cv2.face.LBPHFaceRecognizer_create()
rec.read("trainer/training_data.yml")
id=0
font=(cv2.FONT_HERSHEY_SIMPLEX,1,1,0,1)

while(True):
    ret,img=cam.read()
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces=faceDetect.detectMultiScale(gray,1.3,5)
    for(x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        id,conf=rec.predict(gray[y:y+h,x:x+w])
        cv2.putText(img,str(id),(x,y+h),font,255)
    cv2.imshow("FACEDETECTIONPT1",img)
    if(cv2.waitKey(1)==ord('q')):
        break
cam.release()
cv2.destroyAllWindows

Upvotes: 15

Views: 39025

Answers (9)

Thunder
Thunder

Reputation: 479

In my case, the PIL image had a mode of 'P' stands for palettised. I converted it into RGB & now the error is gone.

Upvotes: 0

Vijay Anand
Vijay Anand

Reputation: 31

I solved it by entering Thickness in int instead of float

For eg, I changed this

cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color, 0.6 )

to

cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color, 2 )

Upvotes: 1

Nouman Ahsan
Nouman Ahsan

Reputation: 367

I had the same issue but I resolved it. The message can arise from different types of errors but the content of the message is not what it says actually.

In my case, I read the image using PIL and tried to apply openCV operations on it which lead to this problem. You must have to use cv2.imread(image_path) to draw rectangle using cv2.rectangle(). Another reason to this error can be when your point coordinates are in float or double instead of integers.

Upvotes: 0

code-freeze
code-freeze

Reputation: 485

cv2.putText(img, name, (x, y + h), font, 2, color, 2)
and the color should be mentioned in RGB format
for example : (255,255,255)

i think this should help you

Upvotes: 0

Rohan
Rohan

Reputation: 1352

As others have discussed, this error is misleading. I received this error when I tried applying open cv functions on an image that was opened with PIL

For example:

from PIL import Image
import cv2

# error if we use PIL to read image and apply cv2 functions
image = Image.open(path)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# resolve error by reading image with cv2
image = cv2.imread(path)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

Upvotes: 1

eudoxos
eudoxos

Reputation: 19065

Another possible reason for this misleading error message is if the img is a view into np.ndarray, not contiguous memory data. So e.g. if your code worked with img, it will fail if you do something like img=np.flipud(img).

The solution is to make deep copy, like img=np.flipud(img).copy().

Upvotes: 9

Shubham Chechani
Shubham Chechani

Reputation: 371

In my experience the error statement was misleading. In my case, the coordinates (x, y) were in float instead of int and fixing that fixed this issue.

Upvotes: 27

Aman Relan
Aman Relan

Reputation: 393

removed the parameters from the font and edited my putText to cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) and this seems to have worked for me..

Upvotes: 1

Sunny Patel
Sunny Patel

Reputation: 8076

Looking at the putText documentation, I see that your font is a tuple, and you're trying to fill in multiple parameters for putText: fontFace, fontScale, color, thickness, and lineType.

Perhaps you can unpack that parameter to get what you need (with the * operator):

cv2.putText(img, name, (x, y + h), *font, 2, (0, 255, 0), 2)

Upvotes: 0

Related Questions