该项目基于dlib模块
提供的人脸检测器
以及关键点定位工具
完成。首先通过检测器在图像中定位人脸的位置,然后通过关键点定位工具提取脸部关键点坐标,最后绘制脸部特征点。
根据特征点对各个待检测区域进行划分
import sys
import os
import dlib
import glob
# from skimage import io
import errno
import numpy as np
import cv2
import argparse
#关键点提取
def main():
args = parse_arguments()
srcimg = cv2.imread(args.imgpath)
predictor_path = 'shape_predictor_81_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
faceMarks=[]
mouthMarks=[]
noseMarks=[]
dets = detector(srcimg)
for k, d in enumerate(dets):
shape = predictor(srcimg, d)
landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
faceCouter = [75, 70, 80, 74, 22, 42, 47, 46, 16, 15, 13, 12, 11, 5, 4, 3, 0, 40, 39, 21, 20]
mouthCouter=[48,49,50,51,52,53,54,55,56,57,58,59,48]
noseCouter = [27, 31, 35]
for id1 in faceCouter:
faceMarks.append(landmarks[id1])
for id2 in mouthCouter:
mouthMarks.append(landmarks[id2])
for id3 in noseCouter:
noseMarks.append(landmarks[id3])
mask = np.zeros(srcimg.shape, np.uint8)
maskf = cv2.polylines(mask, [np.array(faceMarks, dtype=np.int32)], True, (255, 255, 255), 2) # 画多边形
maskface = cv2.fillPoly(maskf.copy(), [np.array(faceMarks, dtype=np.int32)], (255, 255, 255)) # 将多边形区域填充白色
maskface2 = cv2.fillPoly(maskface.copy(),