2017-10-12 3 views
1

dlib로 식별 된 얼굴 표식을 사용하여 얼굴을 잘라내려고합니다. 오른쪽 눈썹이 문제를 일으키고 있습니다 - 작물이 눈썹 호를 따르기보다는 넘어져 간다.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) 

sample image of cropped face showing the issue

+0

임하지가 탐지 만 가정 당신이 뭔가 잘못을하고 완전히 있는지,없는 사람들을 이마에 삼각형 섹션을 제거하기 위해 좀 더 많은 코드를 작성 할 수 있습니다 전철기? [출처] (http://www.codesofinterest.com/2017/04/extracting-individual-facial-features-dlib.html) – GPPK

답변

0

그것의 당신이 제공하는 얼굴 모양이 볼록하지 않기 때문에. fillConvexPoly는 볼록한 모양에서만 완벽하게 작동합니다.이 경우 오목한 코너가 있습니다 (지점 # 27에서) 결과가 엉망입니다.

는이 문제를 해결 당신과 같은 결과를 줄 것이다

def face_remap(shape): 
    remapped_image = cv2.convexHull(shape) 
    return remapped_image 

이 같은 기능을 수정합니다. enter image description here

이제 당신은 (당신이 그런 식으로하려는 경우)

+0

Brilliant! 고맙습니다, 그 문제를 완벽하게 해결했습니다! 이제는 pt # 16 - pt # 17도 오목한 모서리처럼 보이며 fillConvexPoly()에서 잘 견뎌 냈습니다. 그게 왜 그런가? – Squiggles

+0

dlib를 사용하여 점을 맞추는 동안 항상 약간의 오류가 발생합니다. 포인트 # 15-16-17의 경우 대부분 직선입니다. 약간의 오류조차도 왼쪽에 점 # 16을 밀어 넣으면 오목한 모서리가됩니다. 이 문제를 해결하려면 모양이 아닌 cv2.convexHull (모양)의 점 집합을 찾아야합니다. 그리고 그들은 그 지역을 둘러 싸서 나머지 지역을 덮습니다. –