Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Face similiarity for multiple images #1544

Open
budzyn0 opened this issue Dec 10, 2023 · 0 comments
Open

Face similiarity for multiple images #1544

budzyn0 opened this issue Dec 10, 2023 · 0 comments

Comments

@budzyn0
Copy link

budzyn0 commented Dec 10, 2023

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)

#!/usr/bin/python

import face_recognition

import os

import csv

#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 = []

def average(lst): 
    return sum(lst) / len(lst) 
    


#get all good images and do the encoding

for filename in known_images:
    if os.path.isfile("./known_face/"+filename):
        
        #Load some images to compare against
        known_image = face_recognition.load_image_file("./known_face/"+filename)

        #Get the face encodings for the known images
        face_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 encoding
      
for filename in images_for_checking:
    if os.path.isfile("./check/"+filename):
        
        #Load some images to compare against
        check_image = face_recognition.load_image_file("./check/"+filename)

        #Get the face encodings for the known images
        face_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 file

csv_header.append("avg")
csv_header.append("Similar?")





for index, image in enumerate(checks_encodings):
    face_distances = face_recognition.face_distance(known_encodings, image)
    
    
    for i, face_distance in enumerate(face_distances):
        
        if image_bad_names[index] in row_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 = True
    else:
        similiar = False
    
    row_data.append(str(average(avg_distance)).replace(".", ","))
    row_data.append(str(similiar))
    csv_data.append(row_data)

    
    


with open('results.csv', 'w', encoding='UTF8', newline='') as f:
    writer = csv.writer(f)

    #write the header
    writer.writerow(csv_header)

    #write multiple rows
    writer.writerows(csv_data)

print("end")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant