Home > Software design >  list/numpy array out of range
list/numpy array out of range

Time:11-01

I'm working on a facial recognition project. The app works great, until a large amount of faces are added to my database and then I am getting an out of range error that I cannot solve. I initially tried to do this project in c#, but found the api's were lacking so I switched to python, but I am quite bad at it.

The error is somewhere in this piece of code that generates the database.

def encode_photos():
encoded = {}

for dirpath, dnames, fnames in os.walk("./datasets"):
    for f in fnames:
        if f.endswith(".jpg") or f.endswith(".png"):
            face = face_recognition.load_image_file("datasets/"   f)
            encoding = face_recognition.face_encodings(face, model="cnn")[0]
            encoded[f.split(".")[0]] = encoding
            print("Encoding image", f)

with open('dataset_faces.dat', 'wb') as f:
    pickle.dump(encoded, f)
    print("Encoding complete and saved in dataset_faces.dat")

It never gets to the data dump part because it is encountering this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1702, in __call__
    return self.func(*args)
  File "cnn.py", line 42, in encode_photos

encoding = face_recognition.face_encodings(face, model="cnn")[0]

IndexError: list index out of range

The folder has about 2600 photos in it but it is only getting through a few hundred before this error occurs.

Any ideas?

CodePudding user response:

In the end it turned out the easiest way to solve this was to add exception handling. The database was created without any issues with this addition to the code.

try:
    face = face_recognition.load_image_file("datasets/"   f)
    encoding = face_recognition.face_encodings(face, model="cnn")[0]
    encoded[f.split(".")[0]] = encoding
    print("Encoding image", f)
except Exception:
    pass
  • Related