user206
user206

Reputation: 51

Face comparison in group photos

I want to compare two photos. The first has the face of one individual. The second is a group photo with many faces. I want to see if the individual from the first photo appears in the second photo.

I have tried to do this with the deepface and face_recognition libraries in python, by pulling faces one by one from the group photo and comparing them to the original photo.

face_locations = face_recognition.face_locations(img2_loaded)

        for face in face_locations:
            top, right, bottom, left = face
            face_img = img2_loaded[top:bottom, left:right]
           
            face_recognition.compare_faces(img1_loaded, face_img)

This results in an error about operands cannot be broadcast together with shapes (3088,2316,3) (90,89,3). I also get this same error when I take the faces I pulled out from the group photo, save them using PIL and then try passing them into deepface. Can anyone recommend any alternative ways to achieve this functionality? Or fix my current attempt? Thank you so much!

Upvotes: 2

Views: 2420

Answers (1)

sefiks
sefiks

Reputation: 1630

deepface is designed to compare two faces but you can still compare one to many face recognition.

You have two pictures. One has just a single face photo. I call this img1.jpg. And second has many faces. I call this img2.jpg.

You can firstly detect faces in img2.jpg with OpenCV.

import cv2
img2 = cv2.imread("img2.jpg")
face_detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
faces = face_detector.detectMultiScale(img2, 1.3, 5)

detected_faces = []
for face in faces:
    x,y,w,h = face
    detected_face = img2[int(y):int(y+h), int(x):int(x+w)]
    detected_faces.append(detected_face)

Then, you need to compare each item of faces variable with img1.jpg.

img1 = cv2.imread("img1.jpg")
targets = face_detector.detectMultiScale(img1, 1.3, 5)
x,y,w,h = targets[0] #this has just a single face
target = img1[int(y):int(y+h), int(x):int(x+w)]

for face in detected_faces:
    #compare face and target in each iteration
    compare(face, target)

We should design compare function

from deepface import DeepFace

def compare(img1, img2):
    resp = DeepFace.verify(img1, img2)
    print(resp["verified"])

So, you can adapt deepface for your case like that.

Upvotes: 3

Related Questions