首页 > 解决方案 > 当我将 [0] 添加到 face_encodings 时,如何修复 IndexError:列表索引超出范围?

问题描述

我正在尝试学习人脸识别,并在 google colab 和 jupyter notebook 上都尝试过,当我编写添加索引 [0] 的写入方式时,我得到一个 IndexError: list index out of range 但它没有给出错误时我省略了 [0],在这种情况下,当我尝试比较人脸时会出现另一个错误。这是我的错误的样子

() 中的 IndexError Traceback(最近一次调用最后一次)

  2 for file in os.listdir(known_dir):

  3   img = read_img(known_dir + '/' + file)

----> 4 img_enc = face_recognition.face_encodings(img)[0]

  5   known_encodings.append(img_enc)

  6   known_names.append(file.split('.')[0])

IndexError:列表索引超出范围

这是我的代码

known_dir = '/content/drive/My Drive/Colab Notebooks/behic_faces'
for file in os.listdir(known_dir):
    img = read_img(known_dir + '/' + file)
    img_enc = face_recognition.face_encodings(img)[0]
    known_encodings.append(img_enc)
    known_names.append(file.split('.')[0])

标签: pythonartificial-intelligenceface-recognition

解决方案


推荐阅读