How do you compare two faces in Python?

I went through Pyimagesearch face Recognition tutorial, but my application need to compare two faces only, I have embedding of two faces, how to compare them using opencv ? about the trained model which is use to extract embedding from face is mentioned in link, I want to know that what methods I should try to compare two face embedding.

(Note: I am new to this field)

How do you compare two faces in Python?

Sociopath

12.6k18 gold badges43 silver badges71 bronze badges

asked Nov 13, 2019 at 13:30

3

First of all your case is similar to given tutorial, instead of multiple images you have single image that you need to compare with test image,

So you don't really need training step here.

You can do

# read 1st image and store encodings
image = cv2.imread(args["image"])
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

boxes = face_recognition.face_locations(rgb, model=args["detection_method"])
encodings1 = face_recognition.face_encodings(rgb, boxes)

# read 2nd image and store encodings
image = cv2.imread(args["image"])
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

boxes = face_recognition.face_locations(rgb, model=args["detection_method"])
encodings2 = face_recognition.face_encodings(rgb, boxes)


# now you can compare two encodings
# optionally you can pass threshold, by default it is 0.6
matches = face_recognition.compare_faces(encoding1, encoding2)

matches will give you True or False based on your images

answered Nov 14, 2019 at 5:16

How do you compare two faces in Python?

SociopathSociopath

12.6k18 gold badges43 silver badges71 bronze badges

2

Based on the article you mentioned, you can actually compare if two faces are the same using only the face_recognition library.

You can use the compare faces to determine if two pictures have the same face

import face_recognition
known_image = face_recognition.load_image_file("biden.jpg")
unknown_image = face_recognition.load_image_file("unknown.jpg")

biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([biden_encoding], unknown_encoding)

answered May 5, 2021 at 3:13

How do you compare two faces in Python?

Project description

face-comparison

AI Face comparison using FaceNet, compare two photos and see if they are the same person.

Installation

pip install face-compare

Usage

Use compare_faces.py to compare two images of people to see if they are the same person.

compare_faces.py --image-one /path/to/image_one.png --image-two /path/to/image_two.png

Optionally output the cropped image output to a directory (useful for inspecting input to AI model)

compare_faces.py --image-one /path/to/image_one.png --image-two /path/to/image_two.png -s /path/to/outputs/

Steps Involved

  1. A cascade classifier is used to detect the face within the input images.
  2. The bounding box of this segmentation is then used to crop the images, and fed into the AI model.
  3. The FaceNet model then calculates the image embeddings for the two cropped images.
  4. Finally the second embedding is subtracted from the first, and the Euclidean norm of that vector is calculated.
  5. A threshold of 0.7 is used to determine whether they are the same person or not.

Known Issues

CPU Only runtime issue

If you are trying to run the module without a suitable GPU, you may run into the following error message:

tensorflow.python.framework.errors_impl.InvalidArgumentError:  Default MaxPoolingOp only supports NHWC on device type CPU

To fix this issue with Intel CPU architecture, you can install the TensorFlow Intel Optimization package via

pip install intel-tensorflow

References

This module uses the AI model FaceNet, which can be found here, and the journal article here.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

How does compare a face work?

Compare-a-Face uses facial recognition technology to analyze a picture of you, along with old family photos. It gives each pairing a resemblance percentage and ranks relatives' faces in order of resemblance.

How do I compare faces in photos?

Description.
Upload photos from your phone's camera roll..
Witness the app automatically detect each face..
Select two faces to see how similar they are..
Watch as Face/Face reveals your similarity score..
Compare similarities between different people..

What is DLIB Get_frontal_face_detector ()?

You can use dlib. get_frontal_face_detector() to create a frontal face detector, which is based on Histogram of Oriented Gradients (HOG) features and a linear classifier in a sliding window detection approach.

How do you unlock faces in Python?

First, create a python file face_detection.py and paste the below code:.
Imports: import cv2. import os. ... .
Initialize the classifier: cascPath=os. path. ... .
Apply faceCascade on webcam frames: video_capture = cv2. VideoCapture(0) ... .
Release the capture frames: video_capture. release() ... .
Now, run the project file using:.