我正在尝试使用dlib识别的面部标志来裁剪脸部。正确的眉毛造成了问题 - 作物平整而不是跟随眉毛弧。
我在这里做错了什么?
from imutils import face_utils
import imutils
import numpy as np
import collections
import dlib
import cv2
def face_remap(shape):
remapped_image = shape.copy()
# left eye brow
remapped_image[17] = shape[26]
remapped_image[18] = shape[25]
remapped_image[19] = shape[24]
remapped_image[20] = shape[23]
remapped_image[21] = shape[22]
# right eye brow
remapped_image[22] = shape[21]
remapped_image[23] = shape[20]
remapped_image[24] = shape[19]
remapped_image[25] = shape[18]
remapped_image[26] = shape[17]
# neatening
remapped_image[27] = shape[0]
return remapped_image
"""
MAIN CODE STARTS HERE
"""
# load the input image, resize it, and convert it to grayscale
image = cv2.imread("images/faceCM1.jpg")
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
out_face = np.zeros_like(image)
# initialize dlib's face detector (HOG-based) and then create the facial landmark predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(SHAPE_PREDICTOR)
# detect faces in the grayscale image
rects = detector(gray, 1)
# loop over the face detections
for (i, rect) in enumerate(rects):
"""
Determine the facial landmarks for the face region, then convert the facial landmark (x, y)-coordinates to a NumPy array
"""
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
#initialize mask array
remapped_shape = np.zeros_like(shape)
feature_mask = np.zeros((image.shape[0], image.shape[1]))
# we extract the face
remapped_shape = face_remap(shape)
cv2.fillConvexPoly(feature_mask, remapped_shape[0:27], 1)
feature_mask = feature_mask.astype(np.bool)
out_face[feature_mask] = image[feature_mask]
cv2.imshow("mask_inv", out_face)
cv2.imwrite("out_face.png", out_face)
答案 0 :(得分:3)
因为你提供的脸部形状不凸。 fillConvexPoly仅适用于凸形,在这种情况下有一个凹角(在#27点),因此结果搞砸了。
要解决此问题,请将功能修改为
def face_remap(shape):
remapped_image = cv2.convexHull(shape)
return remapped_image
现在你可以写一些代码去除额头上的三角形部分(如果你想这样)
答案 1 :(得分:0)
使用由68个地标构成的凸包不能完全实现所需的输出,因此我使用以下方法使用scikit-image
而不是OpenCV来解决此问题
1。加载图像并预测68个地标
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
img = dlib.load_rgb_image('mean.jpg')
rect = detector(img)[0]
sp = predictor(img, rect)
landmarks = np.array([[p.x, p.y] for p in sp.parts()])
2。选择代表面部形状的地标
(我不得不颠倒眉毛界标的顺序,因为68 landmarks并没有被要求描述脸部轮廓)
outline = landmarks[[*range(17), *range(26,16,-1)]]
3。使用scikit-image
Y, X = skimage.draw.polygon(outline[:,1], outline[:,0])
4。用零创建画布,并使用多边形作为原始图像的遮罩
cropped_img = np.zeros(img.shape, dtype=np.uint8)
cropped_img[Y, X] = img[Y, X]
出于完整性考虑,如果仍首选此选项,我在下面提供使用scipy.spatial.ConvexHull的解决方案
vertices = ConvexHull(landmarks).vertices
Y, X = skimage.draw.polygon(landmarks[vertices, 1], landmarks[vertices, 0])
cropped_img = np.zeros(img.shape, dtype=np.uint8)
cropped_img[Y, X] = img[Y, X]