大家好,最近我正在研究人脸识别考勤系统,我正在使用人脸识别库来做到这一点,我需要对图像进行编码,所以我使用 for 循环遍历图像并对其进行编码,但我遇到了错误上面写着“encode = face_recognition.face_encodings(img)[0] IndexError: list index out of range”我什至看到有人这样做,它工作得很好,但它对我不起作用。我正在使用 python 3.7.6 这是完整的代码:
import cv2
import face_recognition
import pickle
import os
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from firebase_admin import storage
cred = credentials.Certificate("Service acc key data base.json")
firebase_admin.initialize_app(cred, {
'databaseURL': "Don't mind this",
'storageBucket': "Don't mind this"
})
# Importing student images
folderPath = 'Images'
pathList = os.listdir(folderPath)
print(pathList)
imgList = []
studentIds = []
for path in pathList:
imgList.append(cv2.imread(os.path.join(folderPath, path)))
studentIds.append(os.path.splitext(path)[0])
fileName = f'{folderPath}/{path}'
bucket = storage.bucket()
blob = bucket.blob(fileName)
blob.upload_from_filename(fileName)
# print(path)
# print(os.path.splitext(path)[0])
print(studentIds)
def findEncodings(imagesList):
encodeList = []
for img in imagesList:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
return encodeList
print("Encoding Started ...")
encodeListKnown = findEncodings(imgList)
print(encodeListKnown)
encodeListKnownWithIds = [encodeListKnown, studentIds]
print("Encoding Complete")
file = open("EncodeFile.p", 'wb')
pickle.dump(encodeListKnownWithIds, file)
file.close()
print("File Saved")
我已经尝试了我所知道的一切
当
face_recognition.face_encodings(img)
没有找到任何面时,它返回一个空列表 []
所以 [0]
给你索引错误。您必须首先检查图像中是否存在面部。
encode = face_recognition.face_encodings(img)
if len(encodings) > 0:
# Append encode to encodList
encodeList.append(encode)
locations = face_recognition.face_locations(img)
if len(locations) == 0:
# no images found
continue
有关更多信息,您可以查看list index out of range error in facial recognition