VISIONEST
VISIONEST

Reputation: 11

how to enforce deepface to generate embeddings for multiple faces in single image( assuming there are more than one face in given image)

I tried adding the following code, but I end up with the error from represent function saying :

TypeError: stat: path should be string, bytes, os.PathLike or integer, not dict

I understand that the first argument to represent function should be image path but I am supplying the output of MTCNN detection, which is metadata. I am unable to figure out how I can enforce it to get multiple embedding when there is more than one face in a given image

from mtcnn import MTCNN
import cv2

#pass1
img = cv2.cvtColor(cv2.imread("all_faces.jpeg"), cv2.COLOR_BGR2RGB)
detector = MTCNN()

faces=detector.detect_faces(img)
    
#pass2
embeddings = []
for face in faces:
   embedding = DeepFace.represent(face, model_name = 'Facenet', enforce_detection = False)
   embeddings.append(embedding)

Upvotes: 1

Views: 948

Answers (1)

sefiks
sefiks

Reputation: 1650

Your face object stores the bounding box and confidence scores.

face:  {'box': [412, 161, 593, 853], 'confidence': 0.9996218681335449, 'keypoints': {'left_eye': (579, 518), 'right_eye': (861, 518), 'nose': (735, 681), 'mouth_left': (575, 790), 'mouth_right': (883, 773)}}

You need to extract the faces one by one with the bounding box information. To sum up, you will find the detected face with face["box"] and then pass the detected face to deepface library as shown below.

from mtcnn import MTCNN
from deepface import DeepFace
import cv2

#pass1
img = cv2.cvtColor(cv2.imread("deepface/tests/dataset/img1.jpg"), cv2.COLOR_BGR2RGB)
detector = MTCNN()

faces=detector.detect_faces(img)

#pass2
embeddings = []
for face in faces:
   x, y, w, h = face["box"]
   detected_face = img[int(y):int(y+h), int(x):int(x+w)]

   embedding = DeepFace.represent(detected_face, model_name = 'Facenet', enforce_detection = False)
   embeddings.append(embedding)

Upvotes: 1

Related Questions