'How to use DeepFace.detectFace() to actually detect a face in an image?
I am trying to identify and count the number of human faces in a every picture of a folder full of pictures, I am using Deepface to get the job done. I have only found one reference to the function DeepFace.detectFace()
on the web, that supposedly identifies the faces, but I am having some issues implementing the framework properly and I haven't found any other reference of this function other than this one: Face Alignment for Face Recognition in Python within OpenCV
The input files are "jpg" and "jpeg" files only.
My code is the following:
from deepface import DeepFace
import os
import os.path
import numpy as np
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
imgs = []
path = os.path.join(BASE_DIR, 'images')
valid_images = [".jpg", ".gif", ".png", ".tga", ".jpeg"]
# image = Image.open(os.path.join(path, file))
for file in os.listdir(path):
print(file)
ext = os.path.splitext(file)[1]
if ext.lower() not in valid_images:
continue
print(ext)
image_face = DeepFace.detectFace(file)
print(image_face)
And I get the following error:
2020-08-17 22:33:34.148103: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2020-08-17 22:33:34.148135: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
/home/user/.local/share/virtualenvs/image_cleaning-iUI7F59N/lib/python3.7/site-packages/pandas/compat/__init__.py:120: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)
150_save_2020-08-17-21:44:28_9245.png
.png
Traceback (most recent call last):
File "face_detection.py", line 18, in <module>
image_face = DeepFace.detectFace(file)
File "/home/user/.local/share/virtualenvs/image_cleaning-iUI7F59N/lib/python3.7/site-packages/deepface/DeepFace.py", line 513, in detectFace
img = functions.detectFace(img_path)[0] #detectFace returns (1, 224, 224, 3)
File "/home/user/.local/share/virtualenvs/image_cleaning-iUI7F59N/lib/python3.7/site-packages/deepface/commons/functions.py", line 200, in detectFace
raise ValueError("Confirm that ",img," exists")
ValueError: ('Confirm that ', '150_save_2020-08-17-21:44:28_9245.png', ' exists')
Solution 1:[1]
You are giving a file name as the input. You should be giving the image array.
import cv2
img = cv2.imread(os.path.join(path, file))
image_face = DeepFace.detectFace(img)
Output:
Solution 2:[2]
Passing exact image path to detectFace function works well instead of reading it via opencv.
#!pip install deepface
from deepface import DeepFace
img = DeepFace.detectFace("img1.jpg")
Solution 3:[3]
from deepface import DeepFace
from tqdm import tqdm
import cv2
import os
dire = r"path of the folder"
count = 0
for img in tqdm(os.listdir(dire)):
path = dire+'/'+img
try:
img = cv2.imread(path)
result = DeepFace.analyze(img, actions= ['gender'])
if result['gender'] == "Man" or result['gender'] == "Woman":
count += 1
except ValueError:
pass
print(count)
Initiated a count variable to count the no of images in which the human face is found. The human faces can be either man or woman as per the deepface package.
Solution 4:[4]
deepface is desgined to work with single image. even if an image with multiple faces, it will ignore them and work with the first one.
I recommend you to use retina-face to detect faces.
#!pip install retina-face
from retinaface import RetinaFace
resp = RetinaFace.detect_faces("img1.jpg")
num_of_faces = len(list(resp.keys()))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ahx |
Solution 2 | johncasey |
Solution 3 | NELSON JOSEPH |
Solution 4 | johncasey |