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

How to Compare two faces? #11

Closed
Adeel-Intizar opened this issue Feb 6, 2021 · 4 comments
Closed

How to Compare two faces? #11

Adeel-Intizar opened this issue Feb 6, 2021 · 4 comments

Comments

@Adeel-Intizar
Copy link

Adeel-Intizar commented Feb 6, 2021

Hello, Thank you for your great work
Is there any way to compare two detected faces and return a percentage of similarity?
In other words, is there any way to get face encodings ?

@sajjjadayobi
Copy link
Owner

yes there is, you can look at the following function and find out your two image distance with print

diff = embs.unsqueeze(-1) - target_embs.transpose(1, 0).unsqueeze(0)
dist = torch.sum(torch.pow(diff, 2), dim=1)
print(dist) # it returns the distance between new face with all faces in your bank

it's in facelib/InsightFace/models/Learner.py
or you can write your own function in Learner Class
like the following

def compare2faces(self, conf, faces, tta=False)
        """faces : list of PIL Image (your faces for comparing) """
        faces = faces_preprocessing(faces, conf.device)
        if tta:
            faces_emb = self.model(faces)
            hflip_emb = self.model(faces.flip(-1))  # image horizontal flip
            embs = l2_norm((faces_emb + hflip_emb)/2)  # take mean
        else:
            embs = self.model(faces)

        diff = embs[0] - embs[1]
        dist = torch.sum(torch.pow(diff, 2), dim=1)
        return dist

and run it like this:

from facelib import FaceRecognizer, get_config
recognizer = FaceRecognizer(get_config())
recognizer.model.eval()
img1 = PIL.Image.open('')
img2 = PIL.Image.open('')
recognizer.compare2faces(self.conf, faces=[img1, img2], tta=self.tta)

@sajjjadayobi
Copy link
Owner

and you can also see the embeddings at (embs in the infer function)
if you add any new features to this project let me know

@Adeel-Intizar
Copy link
Author

sure I will let you know, Thank you

@bistcuite
Copy link

I tried to run this code but i got this error :
Capture

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

3 participants