如何首先对齐,然后提取 - 使用 RetinaFace 进行人脸对齐,没有黑色区域

问题描述 投票:0回答:1

Extracted Image sample 我正在尝试使用 RetinaFace 进行面部检测、面部对齐、面部提取和文件裁剪,以应对头部倾斜或处于垂直以外的其他位置的情况。

我有代码:

import matplotlib.pyplot as plt
img_path = "/content/drive/img/0036.jpg"
faces = RetinaFace.extract_faces(img_path, align = True)
for face in faces:
  plt.imshow(face)
  plt.show()

那么,如何先进行对齐,然后再进行提取,最后进行裁剪?我想避免在角落里出现那些奇怪的黑色区域(如附图所示),它们只代表可能重要的丢失数据区域?

我尝试过 MTCNN 和 Dlib,但它对我不起作用,我遇到了很多错误,我发现 RetinaFace 的人脸对齐和人脸提取非常简单,只需要一行代码。唯一的问题是没有通过添加黑色区域以正确的顺序来避免数据丢失。 另外,应该有一种我还没有发现的方法,使检测到的脸部的边界框与视线对齐,而不是与图像/照片边缘(垂直)对齐。

alignment extract detection face-detection
1个回答
0
投票

您仍然可以使用核心 RetinaFace 功能来做到这一点。您基本上需要首先对齐,然后检测面部区域。

import cv2
import matplotlib.pyplot as plt
from retinaface import RetinaFace
from retinaface.commons import postprocess

# define the target path
img_path = "img11.jpg"

# find landmarks with retinaface ()
result = RetinaFace.detect_faces(img_path)
landmarks = result["face_1"]["landmarks"]
left_eye = landmarks["left_eye"]
right_eye = landmarks["right_eye"]
nose = landmarks["nose"]

# align the original image with respect to the eye coordinates
img = cv2.imread(img_path)
img_aligned = postprocess.alignment_procedure(img, right_eye, left_eye, nose)
img_aligned = img_aligned[:,:,::-1]
# plt.imshow(img_aligned)
# plt.show()

# detect facial area on the alreday aligned image
faces = RetinaFace.extract_faces(img_aligned)
plt.imshow(faces[0][:,:,::-1])
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.