list/numpy 数组超出范围

list/numpy array out of range

我正在从事面部识别项目。该应用程序运行良好,直到将大量面孔添加到我的数据库中,然后我收到无法解决的超出范围错误。我最初尝试在 c# 中做这个项目,但发现缺少 api,所以我切换到 python,但我很不擅长。

错误出在这段生成数据库的代码中。

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")

它永远不会到达数据转储部分,因为它遇到了这个错误:

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

该文件夹中大约有 2600 张照片,但在发生此错误之前只处理了几百张照片。

有什么想法吗?

最后发现解决这个问题最简单的方法是添加异常处理。数据库的创建没有任何问题,添加到代码中。

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