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')
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: