novaconta
novaconta

Reputation: 21

Face detection return value without face

I' using haar cascade for face detection

faces_haar = face_cascade.detectMultiScale(image, scaleFactor=1.3,  minNeighbors=4, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)

and i save the face in this variables

(x, y, w, h) = faces_haar[0]

and in my fucntion i return

return image[y:y+h, x:x+w] , faces_haar[0]

when he dont find any face gives me the error "IndexError: tuple index out of range" because i dont have any face.

How i can return only when he find some face?

Upvotes: 1

Views: 95

Answers (1)

furas
furas

Reputation: 142631

You should use if to check if list is not empty

if faces_haar:
   (x, y, w, h) = faces_haar[0]
   return image[y:y+h, x:x+w] , faces_haar[0]

and it will automatically run return None

Upvotes: 0

Related Questions