RSiopa / SAVI_TP1

First Practical Work from SAVI
0 stars 0 forks source link

Code Breaks when face_recognition fails to detect a face after taking new photo. #3

Closed gilviegas closed 1 year ago

gilviegas commented 1 year ago
Traceback (most recent call last):
  File "/home/topg/Documents/SAVI/SAVI_TP1/./main.py", line 168, in <module>
    main()
  File "/home/topg/Documents/SAVI/SAVI_TP1/./main.py", line 33, in main
    sfr.load_encoding_images(database_path)
  File "/home/topg/Documents/SAVI/SAVI_TP1/simple_facerec.py", line 36, in load_encoding_images
    img_encoding = face_recognition.face_encodings(rgb_img)[0]
IndexError: list index out of range
gilviegas commented 1 year ago

This happens when we have a photo stored in the folder /Faces that the module Face_recognition fails to detect a face.

This means that the face_recognition module couldn't find any faces in the image. face_recognition.face_encodings(known_image) basically returns a list of all the faces found in the photo. Now, you are using the index [0] to get the first found face. However, when there is no face in the image, you are trying to get a non-existent index, hence the IndexError.

gilviegas commented 1 year ago

FIXED!

Added Condition to skip picture.

            # Get encoding
            if len(face_recognition.face_encodings(rgb_img)) == 0:
                continue