You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just wanted to share a code that might be helpfull for others - a face similiarity checker, but for multiple images. Not sure how to request pushing this to examples dir, so let me paste it here and someone might add it.
This code takes 2 directories, one with images of person and one with images that we want to test, do the similiarity check across all of them and save results to results.csv together with average distance and true/falce similiarity statement.
(FYI - I am not a dev, so it may be not 100% efficient, but it works)
#!/usr/bin/pythonimportface_recognitionimportosimportcsv#Often instead of just checking if two faces match or not (True or False), it's helpful to see how similar they are.#You can do that by using the face_distance function.#The model was trained in a way that faces with a distance of 0.6 or less should be a match. But if you want to#be more strict, you can look for a smaller face distance. For example, using a 0.55 cutoff would reduce false#positive matches at the risk of more false negatives.#Note: This isn't exactly the same as a "percent match". The scale isn't linear. But you can assume that images with a#smaller distance are more similar to each other than ones with a larger distance.known_images=os.listdir("./known_face/")
images_for_checking=os.listdir("./check/")
known_encodings= []
checks_encodings= []
face_distances= []
image_good_names= []
image_bad_names= []
csv_header= ['x']
csv_data= []
row_data= []
avg_distance= []
defaverage(lst):
returnsum(lst) /len(lst)
#get all good images and do the encodingforfilenameinknown_images:
ifos.path.isfile("./known_face/"+filename):
#Load some images to compare againstknown_image=face_recognition.load_image_file("./known_face/"+filename)
#Get the face encodings for the known imagesface_encoding=face_recognition.face_encodings(known_image)[0]
known_encodings.append(face_encoding)
image_good_names.append(filename)
csv_header.append(filename)
#get images for checking and do the encodingforfilenameinimages_for_checking:
ifos.path.isfile("./check/"+filename):
#Load some images to compare againstcheck_image=face_recognition.load_image_file("./check/"+filename)
#Get the face encodings for the known imagesface_for_check_encoding=face_recognition.face_encodings(check_image)[0]
checks_encodings.append(face_for_check_encoding)
image_bad_names.append(filename)
#write base csv filecsv_header.append("avg")
csv_header.append("Similar?")
forindex, imageinenumerate(checks_encodings):
face_distances=face_recognition.face_distance(known_encodings, image)
fori, face_distanceinenumerate(face_distances):
ifimage_bad_names[index] inrow_data:
row_data.append(round(face_distance, 2))
avg_distance.append(round(face_distance, 2))
else:
row_data= [image_bad_names[index], round(face_distance, 2)]
avg_distance= [round(face_distance, 2)]
if ( average(avg_distance) <0.5):
similiar=Trueelse:
similiar=Falserow_data.append(str(average(avg_distance)).replace(".", ","))
row_data.append(str(similiar))
csv_data.append(row_data)
withopen('results.csv', 'w', encoding='UTF8', newline='') asf:
writer=csv.writer(f)
#write the headerwriter.writerow(csv_header)
#write multiple rowswriter.writerows(csv_data)
print("end")
The text was updated successfully, but these errors were encountered:
Hey,
I just wanted to share a code that might be helpfull for others - a face similiarity checker, but for multiple images. Not sure how to request pushing this to examples dir, so let me paste it here and someone might add it.
This code takes 2 directories, one with images of person and one with images that we want to test, do the similiarity check across all of them and save results to results.csv together with average distance and true/falce similiarity statement.
(FYI - I am not a dev, so it may be not 100% efficient, but it works)
The text was updated successfully, but these errors were encountered: