Search code examples
numpyface-recognitiondeepface

How to feed DeepFace.find() the faces extracted by DeepFace.extract_faces()?


I'm creating a face recognition script that process a list of photos and check if the face has a match in the database.

If Yes, add the cropped face to its matched folder in the database.

If No, add the cropped face to new folder called NewFace_01.

My question is: How to feed DeepFace.find() the faces extracted by DeepFace.extract_faces() ? as when I run DeepFace.find() with image path it works successfully, but I couldn't figure a way to give it the faces extracted by DeepFace.extract_faces()

Any ideas ?

When I try:

extracted_face = DeepFace.extract_faces(img_path=img_path)
res = DeepFace.find(img_path=extracted_face['face'], db_path=db_path)

It gives me this error:

ValueError: Face could not be detected in numpy array. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.

I was expecting it to read the extracted face and find a matches of it in the Database as when I feed it image_path

The photo is definitely has a face. but I think i'm giving it a different data type or i have to convert the extracted_face to numpy array or something !??!


Solution

  • Exception message is clear. You need to set enforce_detection argument to false while calling find. Besides, you don't have to call extract_faces and find respectively. Find handles this already.

    import cv2
    
    dfs = DeepFace.find(
       img_path=img_path,
       db_path=db_path,
       enforce_detection=False
    )
    
    for df in dfs:
       # df is a pandas dataframe
       for index, instance in df.iterrows():
          source_path = instance["identity"]
          source_img = cv2.imread(source_path)
    
          # extract facial area of the source image
          x = instance["target_x"]
          y = instance["target_y"]
          w = instance["target_w"]
          h = instance["target_h"]
          source_img = source_img [y:y+h, x:x+w]