Face_recognition error: ValueError: list.remove(x): x not in list
Question:
So i have made a python program which marks down attendance in an excel sheet using webcam. I am making "names" array for names i recognise as students to verify through webcam, and then if they get verified, their name will be removed from the name list array and will be added to the excel sheet. I am using list.remove() function to do so but when ever i try to recognise a face it works but throws the error thus, resulting in crashing of the python program: ValueError: list.remove(x): x not in list..
Here is the code i am using:
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 = []
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]
face_names.append(name)
if name in known_faces_names:
students.remove(name)
print(students)
time = 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()
What should i do to remove the error?
Answers:
This error usually occurs when removing list contents while looping over it.
I reckon here the cause of it is that you used copy
to duplicate the array, which is a shallow copy: https://docs.python.org/3/library/copy.html – rather than duplicating memory it creates references to the original objects. So when you removed a name from students
, the same name was also deleted from the known_faces_names
list.
I think the solution here is to just use students = known_faces_names.deepcopy()
, although it is good practice not to remove elements from a list while looping, you should rather create a new list and insert into it, which is safer and easier to debug.
So i have made a python program which marks down attendance in an excel sheet using webcam. I am making "names" array for names i recognise as students to verify through webcam, and then if they get verified, their name will be removed from the name list array and will be added to the excel sheet. I am using list.remove() function to do so but when ever i try to recognise a face it works but throws the error thus, resulting in crashing of the python program: ValueError: list.remove(x): x not in list..
Here is the code i am using:
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 = []
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]
face_names.append(name)
if name in known_faces_names:
students.remove(name)
print(students)
time = 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()
What should i do to remove the error?
This error usually occurs when removing list contents while looping over it.
I reckon here the cause of it is that you used copy
to duplicate the array, which is a shallow copy: https://docs.python.org/3/library/copy.html – rather than duplicating memory it creates references to the original objects. So when you removed a name from students
, the same name was also deleted from the known_faces_names
list.
I think the solution here is to just use students = known_faces_names.deepcopy()
, although it is good practice not to remove elements from a list while looping, you should rather create a new list and insert into it, which is safer and easier to debug.