Python中的人脸对齐与DNN人脸检测器

Posted

技术标签:

【中文标题】Python中的人脸对齐与DNN人脸检测器【英文标题】:Face alignment in Python with DNN face detector 【发布时间】:2020-12-15 08:34:36 【问题描述】:

我正在尝试在 python 中进行人脸对齐代码。我正在关注此article,但在本文中使用了人脸检测 dlib。以下是原代码:

from imutils.face_utils import FaceAligner
from imutils.face_utils import rect_to_bb
import argparse
import imutils
import dlib
import cv2

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
fa = FaceAligner(predictor, desiredFaceWidth=256)

image = cv2.imread('images\\1.jpg')

image = imutils.resize(image, width=300)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

rects = detector(gray, 2)

for rect in rects:
    (x, y, w, h) = rect_to_bb(rect)
    faceOrig = imutils.resize(image[y:y + h, x:x + w], width=256)

    faceAligned = fa.align(image, gray, rect)  # Here we get the aligned face

我有一些人脸图像没有被 dlib 人脸检测器检测到。所以我正在修改上面的代码并使用 DNN 人脸检测。以下是我的代码:

from imutils.face_utils import FaceAligner
from imutils.face_utils import rect_to_bb
import argparse
import imutils
import dlib
import cv2

protoPath = "deploy.prototxt"
modelPath = "res10_300x300_ssd_iter_140000.caffemodel"
detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

fa = FaceAligner(predictor, desiredFaceWidth=256)

image = cv2.imread('images\\1.jpg')
image = imutils.resize(image, width=300)

(h, w) = image.shape[:2]
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
imageBlob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False)

detector.setInput(imageBlob)
detections = detector.forward()

for i in range(0, detections.shape[2]):
    confidence = detections[0, 0, i, 2]

    if confidence > 0.5:
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")
    
        face = image[startY:endY, startX:endX]
        gray = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
        r = dlib.rectangle(int(startX), int(startY), int(endX), int(endY))
        
        faceAligned = fa.align(face, gray, r)

但是在上面的代码中faceAligned 是全零,因此是一个空白图像。我不确定我做错了什么。谁能指出错误并帮助我解决问题。请帮忙。谢谢

【问题讨论】:

【参考方案1】:

我建议您在 deepface 中执行此操作。它封装了opencv、ssd、dlib和mtcnn来检测和对齐人脸。

detectFace 函数分别在后台应用检测和对齐。

#!pip install deepface
from deepface import DeepFace
backends = ['opencv', 'ssd', 'dlib', 'mtcnn']
DeepFace.detectFace("img.jpg", detector_backend = backends[2])

此外,您可以手动应用检测和对齐。

from deepface.commons import functions
img = functions.load_image("img.jpg")
backends = ['opencv', 'ssd', 'dlib', 'mtcnn']

detected_face = functions.detect_face(img = img, detector_backend = backends[2])
plt.imshow(detected_face)

aligned_face = functions.align_face(img = img, detector_backend = backends[2])
plt.imshow(aligned_face)

processed_img = functions.detect_face(img = aligned_face, detector_backend = backends[2])
plt.imshow(processed_img)

如果您打算应用人脸识别,它也会在后台处理这些预处理步骤。

from deepface import DeepFace
DeepFace.verify("img1.jpg", "img2.jpg", detector_backend = 'dlib')

【讨论】:

【参考方案2】:

您将人脸和灰色裁剪图像传递给 fa.align(face, gray, r),正如您在第一个代码中显示的那样,此参数必须是完整图像和矩形。这是完整的示例:

import numpy as np
import imutils
import dlib
import cv2

from imutils.face_utils import FaceAligner

protoPath = "path/to/deploy.prototxt.txt"
modelPath = "path/to/res10_300x300_ssd_iter_140000.caffemodel"
detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath)
predictor = dlib.shape_predictor("path/to/shape_predictor_68_face_landmarks.dat")

fa = FaceAligner(predictor, desiredFaceWidth=256)

image = cv2.imread('path/to/image.jpg')
image = imutils.resize(image, width=300)
cv2.imshow("image", image)
(h, w) = image.shape[:2]

rgb = image.copy()
grey = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
imageBlob = cv2.dnn.blobFromImage(rgb, 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False)

detector.setInput(imageBlob)
detections = detector.forward()

for i in range(0, detections.shape[2]):
    confidence = detections[0, 0, i, 2]

    if confidence > 0.5:
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")

        face = image[startY:endY, startX:endX]

        r = dlib.rectangle(int(startX), int(startY), int(endX), int(endY))
        faceAligned = fa.align(rgb, grey, r)
        cv2.imshow("FACE ALIGNED :d".format(i), faceAligned)

k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()

祝你好运。

【讨论】:

以上是关于Python中的人脸对齐与DNN人脸检测器的主要内容,如果未能解决你的问题,请参考以下文章

通过 Python 进行人脸检测程序中的 CV2 错误

OpenCV基于残差网络实现人脸检测

OpenCV基于残差网络实现人脸检测

人脸检测实战终极:使用 OpenCV 和 Python 进行人脸对齐

计算机视觉OpenCv学习系列:第十部分实时人脸检测

基于yolo的人脸检测与人脸对齐