Jason Rieder
Jason Rieder

Reputation: 197

IndexError: list index out of range, face_recognition

I am using open cv and face recognition together, however this line of code:

biden_encoding = face_recognition.face_encodings(known_image)[0]

is giving me the following error:

IndexError: list index out of range

I have read up on this error and most conclude that it means that face_recognition is not detecting any faces in the frame. However, open cv is detecting faces within that same frame, so I am not sure if face_recognition is indeed not detecting any faces or I am receiving an IndexError for some other reason?

all the code needed to get a background of the issue:

check, frame = video.read()
faceCascade = cv2.CascadeClassifier(
    'C:\\Users\\Astroid\\Desktop\\face detection software\\data\\haarcascade_frontalface_alt.xml')

frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(
    frame,
    scaleFactor=1.2,
    minNeighbors=5,
)

for x, y, w, h in faces:
    img = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 1)

    if len(os.listdir("C:\\Users\\Astroid\\Desktop\\face detection software\\saved faces\\")) == 0:

        cv2.imwrite(
            "C:\\Users\\Astroid\\Desktop\\face detection software\\saved faces\\" + "1 faces.jpg", cropped)
    else:
        cv2.imwrite(
            "C:\\Users\\Astroid\Desktop\\face detection software\\unknown faces\\" + " unknown_faces.jpg", cropped)

        known_image = face_recognition.load_image_file(
            "C:\\Users\\Astroid\\Desktop\\face detection software\\saved faces\\" + "1 faces.jpg")

        unknown_image = face_recognition.load_image_file(
           "C:\\Users\\Astroid\Desktop\\face detection software\\unknown faces\\" + " unknown_faces.jpg"

        biden_encoding = face_recognition.face_encodings(known_image)[0]
        print(biden_encoding)#

        unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
        print(unknown_encoding)#

        results = face_recognition.compare_faces([biden_encoding], [unknown_encoding])

        if results >= (60):
            shutil.move(
                "C:\\Users\\Astroid\Desktop\\face detection software\\unknown faces\\" + " unknown_faces.jpg",
                "C:\\Users\\Astroid\\Desktop\\face detection software\\saved faces\\" + (face_num) + (" faces.jpg"))
        else:
            pass

Upvotes: 3

Views: 14219

Answers (4)

Kedar K
Kedar K

Reputation: 73

I was loading an animated image (it was in .jpg format, but animated image), hence I was getting this error. I changed the image to regular non-animated image, and this time same code worked without any errors.

Upvotes: 0

The Pilot Dude
The Pilot Dude

Reputation: 2237

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.

The only real solution to this is to use a new image. face_recognition cannot find any faces, so either you can make your own algorithm to find a face, which I highly do not recommend, or you use a different image.

Upvotes: 4

Oussama
Oussama

Reputation: 11

i solve that problem by changing the images to jpeg, it means you should include a clean quality of such an image with jpeg type, Regards

Upvotes: 1

bkmalan
bkmalan

Reputation: 183

It means, the dlib face detector couldn't detect a face in the image you passed in. You could add a try exception block something like this:

try:
    image_to_be_matched_encoded = face_recognition.face_encodings(known_image)[0]
except IndexError as e:
    print(e)
    sys.exit(1) # stops code execution in my case you could handle it differently

More on this can be found here: https://github.com/ageitgey/face_recognition/issues/100#issuecomment-307590846

Upvotes: 1

Related Questions