解决Face_recognition中ValueError: list.remove(x): x not in list错误
解决人脸考勤程序
ValueError: list.remove(x): x not in list错误 错误原因
程序在持续运行的while循环中,每帧都会检测人脸并尝试移除students列表中的已识别学生。当某个学生被识别一次后,已从students列表中移除,但后续帧再次识别到该学生时,students.remove(name)会因目标名字不在列表中抛出错误。此外,程序启动时获取的now变量会导致所有考勤记录的时间都是程序启动时间,而非实际识别时间。
修复方案
1. 仅处理未考勤的学生
将判断条件从检查name in known_faces_names改为检查name in students,确保只有待考勤的学生才会被执行移除和写入操作:
if name in students: # 替换原有的if name in known_faces_names: students.remove(name) print(students) time = datetime.now().strftime("%H-%M-%S") lnwriter.writerow([name, time])
2. 实时获取考勤时间
把时间获取逻辑移到循环内部,每次识别时获取当前时间,避免所有记录时间一致:
time = datetime.now().strftime("%H-%M-%S")
3. 可选:避免单帧重复识别
如果一帧中可能多次识别到同一个人,可使用集合临时存储当前帧识别到的名字,去重后再处理:
current_frame_names = set() for face_encoding in face_encodings: # 原有识别逻辑... if matches[best_match_index]: name = known_faces_names[best_match_index] current_frame_names.add(name) face_names.append(name) # 统一处理当前帧的已识别学生 for name in current_frame_names: if name in students: students.remove(name) print(students) time = datetime.now().strftime("%H-%M-%S") lnwriter.writerow([name, time])
修改后的完整代码
import face_recognition import cv2 import numpy as np import csv import os from datetime import datetime video_capture = cv2.VideoCapture(0) tata_image = face_recognition.load_image_file("./faces/index4.jpg") tata_encoding = face_recognition.face_encodings(tata_image)[0] einstein_image = face_recognition.load_image_file("./faces/index2.jpg") einstein_encoding = face_recognition.face_encodings(einstein_image)[0] tesla_image = face_recognition.load_image_file("./faces/index.jpg") tesla_encoding = face_recognition.face_encodings(tesla_image)[0] eefa_image = face_recognition.load_image_file("./faces/eefa.jpg") eefa_encoding = face_recognition.face_encodings(eefa_image)[0] teresa_image = face_recognition.load_image_file("./faces/index3.jpg") teresa_encoding = face_recognition.face_encodings(teresa_image)[0] known_face_encoding = [ tesla_encoding, tata_encoding, teresa_encoding, eefa_encoding, einstein_encoding ] known_faces_names = [ "Nikola Tesla", "Ratan Tata", "Mother Teresa", "Eefa Khadeeja Abidi", "Albert Einstein" ] students = known_faces_names.copy() face_location = [] face_encodings = [] face_names = [] s=True now = datetime.now() date = now.strftime("%Y-%m-%d") f = open(date+'.csv','w+',newline='') lnwriter = csv.writer(f) while True: _,frame = video_capture.read() small_frame = cv2.resize(frame,(0,0),fx=0.25,fy=0.25) rgb_small_frame = small_frame[:,:,::-1] if s: face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame,face_locations) face_names = [] current_frame_names = set() for face_encoding in face_encodings: matches = face_recognition.compare_faces(known_face_encoding,face_encoding) name = "" face_distance = face_recognition.face_distance(known_face_encoding,face_encoding) best_match_index = np.argmin(face_distance) if matches[best_match_index]: name = known_faces_names[best_match_index] current_frame_names.add(name) face_names.append(name) # 处理当前帧的考勤记录 for name in current_frame_names: if name in students: students.remove(name) print(students) time = datetime.now().strftime("%H-%M-%S") lnwriter.writerow([name,time]) cv2.imshow("attendance system",frame) if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows() f.close()
内容的提问来源于stack exchange,提问作者ZainCoder
相关产品推荐
相关产品推荐

