如何使用 DeepFace.detectFace() 实际检测图像中的人脸?
How to use DeepFace.detectFace() to actually detect a face in an image?
我正在尝试识别和计算装满图片的文件夹中每张图片中的人脸数量,我正在使用 Deepface 来完成这项工作。我只在网上找到一个函数 DeepFace.detectFace()
的引用,据说它可以识别面孔,但我在正确实施框架时遇到了一些问题,除了这个函数之外,我没有找到该函数的任何其他引用: Face Alignment for Face Recognition in Python within OpenCV
输入文件仅为“jpg”和“jpeg”文件。
我的代码如下:
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)
我收到以下错误:
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')
您输入的是文件名。你应该给出图像数组。
import cv2
img = cv2.imread(os.path.join(path, file))
image_face = DeepFace.detectFace(img)
输出:
将准确的图像路径传递给 detectFace 函数效果很好,而不是通过 opencv 读取它。
#!pip install deepface
from deepface import DeepFace
img = DeepFace.detectFace("img1.jpg")
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)
启动了一个计数变量来统计发现人脸的图像的数量。根据 deepface 包,人脸可以是男人或女人。
deepface 设计用于处理单个图像。即使图像有多个面孔,它也会忽略它们并使用第一个。
我推荐你使用retina-face检测人脸。
#!pip install retina-face
from retinaface import RetinaFace
resp = RetinaFace.detect_faces("img1.jpg")
num_of_faces = len(list(resp.keys()))
我正在尝试识别和计算装满图片的文件夹中每张图片中的人脸数量,我正在使用 Deepface 来完成这项工作。我只在网上找到一个函数 DeepFace.detectFace()
的引用,据说它可以识别面孔,但我在正确实施框架时遇到了一些问题,除了这个函数之外,我没有找到该函数的任何其他引用: Face Alignment for Face Recognition in Python within OpenCV
输入文件仅为“jpg”和“jpeg”文件。
我的代码如下:
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)
我收到以下错误:
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')
您输入的是文件名。你应该给出图像数组。
import cv2
img = cv2.imread(os.path.join(path, file))
image_face = DeepFace.detectFace(img)
输出:
将准确的图像路径传递给 detectFace 函数效果很好,而不是通过 opencv 读取它。
#!pip install deepface
from deepface import DeepFace
img = DeepFace.detectFace("img1.jpg")
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)
启动了一个计数变量来统计发现人脸的图像的数量。根据 deepface 包,人脸可以是男人或女人。
deepface 设计用于处理单个图像。即使图像有多个面孔,它也会忽略它们并使用第一个。
我推荐你使用retina-face检测人脸。
#!pip install retina-face
from retinaface import RetinaFace
resp = RetinaFace.detect_faces("img1.jpg")
num_of_faces = len(list(resp.keys()))