Python 索引器错误:尝试使用face_Recognition自动识别人脸时,列表索引超出范围
我试图将未知人脸保存在视频中,并根据它们出现的第一帧识别它们。例如,如果未知人脸出现在第14帧上,则应将其识别为“新14”,但当出现新人脸时,我不断收到错误“Indexer:list index out range”(索引器:列表索引超出范围)。 这是我的代码和回溯Python 索引器错误:尝试使用face_Recognition自动识别人脸时,列表索引超出范围,python,opencv,face-recognition,Python,Opencv,Face Recognition,我试图将未知人脸保存在视频中,并根据它们出现的第一帧识别它们。例如,如果未知人脸出现在第14帧上,则应将其识别为“新14”,但当出现新人脸时,我不断收到错误“Indexer:list index out range”(索引器:列表索引超出范围)。 这是我的代码和回溯 import face_recognition import cv2 input_movie = cv2.VideoCapture("video.mp4") length = int(input_movie.get(cv2.CAP
import face_recognition
import cv2
input_movie = cv2.VideoCapture("video.mp4")
length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))
# Create an output movie file (make sure resolution/frame rate matches input video!)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_movie = cv2.VideoWriter('output.avi', fourcc, 29.97, (640, 360))
newimage = face_recognition.load_image_file("anchor.png")
new_face_encoding = face_recognition.face_encodings(newimage)[0]
known_faces = [
new_face_encoding,
]
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
frame_number = 0
def recog(frame_number, known_faces, face_names):
toenc = []
torec = face_recognition.load_image_file(r"New\Unknown%s.jpg" %str(frame_number))
#if not len(torec):
# print("cannot find image")
#torec = face_recognition.load_image_file(r"New\Unknown%s.jpg" %str(frame_number))
toenc.append((face_recognition.face_encodings(torec))[0])
if not len(toenc):
print("can't be encoded")
known_faces.append(toenc.pop())
face_names.append("new %s" %str(frame_number))
# Load some sample pictures and learn how to recognize them.
while True:
# Grab a single frame of video
ret, frame = input_movie.read()
frame_number += 1
# Quit when the input video file ends
if not ret:
break
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
#face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces(known_faces, face_encoding)
# If you had more than 2 faces, you could make this logic a lot prettier
# but I kept it simple for the demo
name = "Unknown"
face_names.append(name)
# Label the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
if not name:
continue
# Draw a box around the face
unface = cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
if name == "Unknown":
res = frame[top:bottom, left:right]
cv2.imwrite(r"New\Unknown%s.jpg" %str(frame_number), res)
recog(frame_number, known_faces, face_names)
cv2.rectangle(frame, (left, bottom - 25), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)
# Write the resulting image to the output video file
print("Processing frame {} / {}".format(frame_number, length))
#output_movie.write(frame)
cv2.imshow("frame", frame)
if( cv2.waitKey(27) & 0xFF == ord('q')):
break
# All done!
input_movie.release()
cv2.destroyAllWindows()
输出
In [1]: runfile('D:/project_new/facerec_from_video_file.py', wdir='D:/project_new')
Processing frame 1 / 3291
Processing frame 2 / 3291
Processing frame 3 / 3291
Processing frame 4 / 3291
Processing frame 5 / 3291
Processing frame 6 / 3291
Processing frame 7 / 3291
Processing frame 8 / 3291
Processing frame 9 / 3291
Processing frame 10 / 3291
Processing frame 11 / 3291
Processing frame 12 / 3291
Traceback (most recent call last):
File "<ipython-input-1-4b2c69ca71f8>", line 1, in <module>
runfile('D:/project_new/facerec_from_video_file.py', wdir='D:/project_new')
File "C:\Users\saber\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\saber\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/project_new/facerec_from_video_file.py", line 81, in <module>
recog(frame_number, known_faces, face_names)
File "D:/project_new/facerec_from_video_file.py", line 35, in recog
toenc.append((face_recognition.face_encodings(torec))[0])
IndexError: list index out of range
[1]中的runfile('D:/project\u new/facerec\u from\u video\u file.py',wdir='D:/project\u new')
处理帧1/3291
处理帧2/3291
处理帧3/3291
处理帧4/3291
处理帧5/3291
处理帧6/3291
处理帧7/3291
处理帧8/3291
处理帧9/3291
处理帧10/3291
处理帧11/3291
处理帧12/3291
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
runfile('D:/project\u new/facerec\u from\u video\u file.py',wdir='D:/project\u new')
文件“C:\Users\saber\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第827行,在runfile中
execfile(文件名、命名空间)
文件“C:\Users\saber\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第110行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“D:/project\u new/facerec\u from\u video\u File.py”,第81行,in
记录(帧编号、已知面、面名称)
文件“D:/project\u new/facerec\u from\u video\u File.py”,第35行,记录
toenc.append((人脸识别.人脸编码(torec))[0])
索引器:列表索引超出范围
我不能百分之百肯定这反映了您的情况,但我对人脸识别库的经验是,它不能总是对某些人脸进行编码。这通常发生在侧面,或者如果他们的视角太扭曲,或者即使有太多的头发覆盖在脸上
就我个人而言
我们可能需要看看您在哪里定义face\u recognition.face\u encodings()
。我很好奇它的回报是什么。这是人脸识别库中的一个预定义方法。嗯……请查看您的文档,了解人脸识别。人脸编码()
。我想知道如果没有匹配项,它是否会返回一个空集合,因为您的代码似乎假设返回的集合中有项。
encodings = face_recognition.face_encodings(known_image)
if len(encodings) > 0:
biden_encoding = encodings[0]
else:
print("No faces found in the image!")
quit()