2017-10-12 9 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

ブリリアント!ありがとう、それは完全に問題を解決しました!ここで言及したように、pt#16 - pt#17も同様に凹面コーナーのように見え、fillConvexPoly()によって十分に許容されました。それがなぜそのような考えですか? – Squiggles

+0

dlibを使用してポイントをフィッティングする際に、常にいくつかのエラーが発生します。ポイント#15-16-17の場合、それらはほとんど直線上にある。たとえわずかなエラーであっても、左側の点16を押して、それを凹状のコーナーにすることができます。 これを修正するには、シェイプ内のポイントのサブセットを見つける必要がありますが、cv2.convexHull(shape)では見つからないことがあります。そして、これらのポイントをループして残りのエリアをカバーします。 –

関連する問題