Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 807
Aman Relan Avatar asked Oct 01 '18 20:10

Aman Relan


2 Answers

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.

like image 148
Shubham Chechani Avatar answered Sep 16 '22 15:09

Shubham Chechani


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 )
like image 28
Vijay Anand Avatar answered Sep 20 '22 15:09

Vijay Anand