gpt4 book ai didi

python - IndexError:列表索引超出范围,face_recognition

转载 作者:行者123 更新时间:2023-12-02 16:35:56 25 4
gpt4 key购买 nike

我正在同时使用开放式简历和人脸识别,但是下面这行代码:

biden_encoding = face_recognition.face_encodings(known_image)[0]

给我以下错误:
IndexError: list index out of range

我已经阅读了此错误,并且大多数结论认为这意味着face_recognition没有检测到帧中的任何面孔。 但是,打开的cv正在检测同一 同一帧内的人脸,因此我不确定face_recognition是否确实未检测到任何人脸,或者我是否由于其他原因收到了IndexError?

获取问题背景所需的所有代码:
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

最佳答案

这意味着dlib人脸检测器无法检测到您传入的图像中的人脸。您可以添加try异常块,如下所示:

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
有关更多信息,请参见: https://github.com/ageitgey/face_recognition/issues/100#issuecomment-307590846

关于python - IndexError:列表索引超出范围,face_recognition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59919993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com