Hy guys based on then last question that I asked in which I haven't gotten a correct answer.
I want to write a program with face_recognition library. Its purpose is to save an image clip of a new face to a folder, updates the folder so as not to save the same face twice. I want the program to save an image to folder whenever it encounters a new face in the input video. But for now it doesn't seem to work. Sometimes it saves the whole clip of the same face to the folder which is not what I want. Can anyone help me with this code. I modified this code from a tutorial from #Murtaza I saw on youtube.
I still get this error
Traceback (most recent call last):
File "C:/Users/CHIJINDU/AppData/Roaming/JetBrains/PyCharmEdu2020.1/scratches/KRecUnknownFace2.py", line 26, in <module>
encodelistKnown = find_encodings(images)
File "C:/Users/CHIJINDU/AppData/Roaming/JetBrains/PyCharmEdu2020.1/scratches/KRecUnknownFace2.py", line 21, in find_encodings
encode = face_recognition.face_encodings(img)[0]
IndexError: list index out of range
This is the improved code below.
import face_recognition
import cv2
import os
import numpy as np
path = r"C:\Users\CHIJINDU\Desktop\KArtIntel"
images = []
class_names= []
myList = os.listdir(path)
for cl in myList:
curImg= cv2.imread(f'{path}\{cl}')
images.append(curImg)
class_names.append(os.path.splitext(cl)[0])
print(class_names)
def find_encodings(images):
encodelist = []
for img in images:
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
#encode= face_recognition.face_encodings(img)[0]
encodelist.append(encode)
return encodelist
encodelistKnown = find_encodings(images)
print(len(encodelistKnown))
cap = cv2.VideoCapture(r"C:\Users\CHIJINDU\Desktop\Elastic.mp4")
while True:
success, img = cap.read()
imgS = cv2.resize(img, (0,0), None, 0.25,0.25)
imgS =cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
faceCurFrame= face_recognition.face_locations(imgS)
encodesCurFrame= face_recognition.face_encodings(imgS, faceCurFrame)
for encodeFace, faceloc in zip(encodesCurFrame, faceCurFrame):
matches = face_recognition.compare_faces(encodelistKnown,encodeFace)
faceDis = face_recognition.face_distance(encodelistKnown,encodeFace)
matchIndex= np.argmin(faceDis)
i=0
if encodesCurFrame not in encodelistKnown:
newImg= cv2.imwrite(r'C:\Users\CHIJINDU\Desktop\KArtIntel\KUDOS-J14{index}.jpg'.format(index=i), img)
images.append((newImg))
#fps = int(video_capture.get(cv2.CAP_PROP_FPS))
#print(fps)
i+=1
# Display the resulting image
cv2.imshow('Video', img)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
cap.release()
cv2.destroyAllWindows()
Random Davis is right on....i changed the portion of code to include failover and ti worked. try also with the attached file to see the different behavior with a very easy to find face