Open cv compare two face embeddings

10.4k views Asked by At

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)

2

There are 2 answers

0
Daniel Renteria On BEST ANSWER

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)
2
Sociopath On

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