1

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?

novaconta
  • 21
  • 1
  • ok i already know, i can do " if len(faces_haar ) == 0: return -1, -1 – novaconta May 06 '22 at 15:18
  • 1
    I would rather return None than -1. –  May 06 '22 at 15:34
  • more popular `if not faces_haar: return None`. OR you should do `if faces_haar: return image[y:y+h, x:x+w] , faces_haar[0]` and then it will automatically run `return None` – furas May 06 '22 at 15:51

1 Answers1

0

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

furas
  • 134,197
  • 12
  • 106
  • 148