![](/img/trans.png)
[英]Working on a face recog project(using face_recognition) but when i am running the program to get face encodings it is showing list index out of range
[英]Why am I getting a list index out of range error while using face_recognition library in python?
我正在用 python 编写一个程序,它输入一个人的图像,将它与目录中的图像进行比较,然后将那些 face_recognition 显示为真实的图像复制到另一个目录中。 当只有 10 张图像要比较时,它似乎工作正常,但是当我将图像与目录中几乎等于 1000 的图像进行比较时,我得到列表索引超出范围错误。 为什么会这样? 代码如下
import face_recognition
import os
from shutil import copy
person = input("Enter the photo location of person to be found (eg. users/rishabh/my_photo.jpg) : ")
photos = input("Enter the photos folder location (eg. users/photo_folder) : ")
dest = input("Enter the location of folder to copy items into (eg. users/destination_folder) : ")
for filename in os.listdir(photos):
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png") or filename.endswith(".JPG"):
print(filename)
files = photos + '/' + filename
known_image = face_recognition.load_image_file(person)
unknown_image = face_recognition.load_image_file(files)
biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
if(results[0]==True):
src = photos+'/'+str(filename)
copy(src, dest)
图像(没有人脸的图像)可能没有任何人脸编码。
在这种情况下,您将在执行face_recognition.face_encodings
得到空列表。
对空列表进行索引将引发异常 (a=[]; a[0])。
因此,我在您的代码中添加了一行,用于检查列表是否具有值。
尝试运行以下代码并检查
import face_recognition
import os
from shutil import copy
person = input("Enter the photo location of person to be found (eg. users/rishabh/my_photo.jpg) : ")
photos = input("Enter the photos folder location (eg. users/photo_folder) : ")
dest = input("Enter the location of folder to copy items into (eg. users/destination_folder) : ")
known_image = face_recognition.load_image_file(person)
biden_encoding = face_recognition.face_encodings(known_image)
for filename in os.listdir(photos):
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png") or filename.endswith(".JPG"):
print(filename)
f = os.path.join(photos, filename)
unknown_image = face_recognition.load_image_file(f)
unknown_encoding = face_recognition.face_encodings(unknown_image)
if not len(unknown_encoding):
print(filename, "can't be encoded")
continue
results = face_recognition.compare_faces(biden_encoding, unknown_encoding[0])
if(results[0]==True):
copy(f, dest)
我也有同样的错误。
我通过使用以下方法找出了导致问题的图像:
print(len(face_recognition.face_encodings(unknown_image))
上面的命令返回图像中检测到的人脸数量。
以下命令识别列表中的第一个人脸编码:
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
如果没有检测到人脸,就会有任何空列表,即 (len(face_encodings(image)=0) 并抛出错误“列表索引超出范围”)。
此错误是因为序列脚本超出范围。 考虑以下:
my_list = []
mylist[0]
将导致以下错误:
----> 1 my_list[0]
IndexError: list index out of range
由于在给定的序列范围内没有找到元素,因此会引发错误
所以以下将起作用
my_list = [1,2]
my_list[0]
my_list[1]
但是又
my_list[2] will raise same error
似乎以下是返回空列表/元组:
face_recognition.face_encodings(unknown_image)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.