这几天在做一个基于AU进行人脸识别的实验,在这过程中,需要提取AU指定部位。但是我又不想自己再训练一个网络,于是,就使用dlib和opencv提供的人脸关键点进行试验。
具体思路就是通过人脸关键点的坐标进行定位,并提取指定部分,这里就以提取额头部分为例:
- #coding=utf-8
- import numpy as np
- import cv2
- import dlib
- from scipy.spatial import distance
- import os
- from imutils import face_utils
-
- base_path="D:/CK_Emotion_DD/"
- source_path=base_path+'cohn-kanade/'
- to_path=base_path+'forehead/'
-
- test_path=source_path+'/S010/001/S010_001_01594215.png'
-
- shape_detector_path="D:/blink_detect/model/shape_predictor_68_face_landmarks.dat"
-
- left_eye_up_point1=38-1
- left_eye_up_point2=39-1
- right_eye_up_point1=44-1
- right_eye_up_point2=45-1
-
- detector = dlib.get_frontal_face_detector()# 人脸检测器
- predictor = dlib.shape_predictor(shape_detector_path)# 人脸特征点检测器
-
- def catch_forehead(image_path,save_path):
- image = cv2.imread(image_path)
- cv2.imshow("1",image)
- cv2.waitKey()
- dets = detector(image,1)
- sp=image.shape
- h=sp[0]
- w=sp[1]
- print(h)
- for k, d in enumerate(dets):
- print("dets{}".format(d))
- print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
- k, d.left(), d.top(), d.right(), d.bottom()))
- shape = predictor(image, d)
- #print(shape.part(0).x,shape.part(0).y)
- l1=shape.part(left_eye_up_point1)
- l2=shape.part(left_eye_up_point2)
- r1=shape.part(right_eye_up_point1)
- r2=shape.part(right_eye_up_point2)
- y=min(l1.y,l2.y,r1.y,r2.y)
- print(y,h-d.top(),d.left(),w-d.right())
- forehead=image[d.top():y,d.left():d.right()]
- cv2.imshow('1',forehead)
- cv2.waitKey()
- cv2.imwrite(save_path,forehead)
-
-
- catch_forehead(test_path,"d:/1.png")
-
提取效果如下