Dlib人脸特征提取

使用dlib提取人脸特征需要以下几个步骤。

1.使用人脸检测器检测人脸来获得人脸框。可采用任何人脸检测方法,但需要符合dlib所要求的格式(本文采用之前使用的MTCNN方法)。

face_detector = dlib.get_frontal_face_detector()
boxs = face_detector(img,1)

2.定义dlib的特征点预测器shape_predector。dlib提供了两种特征点的预测器,即shape_predector_5_face_landmarks.dat和shape_predector_68_face_landmarks.dat,分别能够得到人脸的5个和68个特征点。下载链接:http://dlib.net/files/。

shape_predictor_5_face_landmarks = "./shape_predictor_5_face_landmarks.dat"
shape_predictor_68_face_landmarks = "./shape_predictor_68_face_landmarks.dat"

3.将原始图像和检测到的每个人脸框传给预测器,得到每个人脸的多个关键点,需要注意的是在没有使用dlib人脸检测方法时传给dlib的人脸框rect需符合dlib的要求。

shape_5 = face_5_landmarks(img, rects)
shape_68 = face_68_landmarks(img, rects)

4.使用官方训练好的模型文件dlib_face_recognition_resnet_model_v1.dat,定义dlib的人脸识别模型。

face_rec_model_path = "./dlib_face_recognition_resnet_model_v1.dat"
facerec = dlib.face_recognition_model_v1(face_rec_model_path)

5.调用dlib人脸识别模型的facerec.comput_face_descriptor方法,传入原始图像和每张人脸的关键点位置,计算人脸特征。

face_descriptor_5 = facerec.compute_face_descriptor(img, shape_5)
face_descriptor_68 = facerec.compute_face_descriptor(img, shape_68)

经过以上这些步骤后,可成功计算出人脸特征。

 

代码实现

from scipy import misc
import numpy as np
import os
import cv2
import dlib
# 引入上一节定义的MTCNN人脸检测的函数mtcnn_findFace
from face_detect_main import mtcnn_findFace

# 特征点预测器文件和dlib人脸识别模型文件
shape_predictor_68_face_landmarks = "D:/GitHub/Face_Recognizer-master/shape_predictor_68_face_landmarks.dat"
shape_predictor_5_face_landmarks = "D:/GitHub/Face_Recognizer-master/shape_predictor_5_face_landmarks.dat"
face_rec_model_path = "D:/GitHub/Face_Recognizer-master/dlib_face_recognition_resnet_model_v1.dat"

# 转换人脸框为dlib的rectangle对象
def convert_to_rect(rect):
    return dlib.rectangle(rect[0], rect[1], rect[2], rect[3])


def feature_extraction_single(img, rect, mode="68"):

    feature = []

    show_img = img.copy()

    # 定义特征点预测器
    if mode == "68":
        sp = dlib.shape_predictor(shape_predictor_68_face_landmarks)
    else:
        sp = dlib.shape_predictor(shape_predictor_5_face_landmarks)

    # 定义dlib人脸识别模型
    facerec = dlib.face_recognition_model_v1(face_rec_model_path)

    # 计算每张人脸的特征点
    shape = sp(img, convert_to_rect(rect))

    # 把特征点画出来,检验程序是否正确
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        cv2.circle(show_img, pt_pos, 2, (0, 255, 0), 1)

    # 将原图像和特征点传入人脸识别模型,得到人脸特征
    face_descriptor = facerec.compute_face_descriptor(img, shape)
    feature.append(face_descriptor)

    feature = np.array(feature)
    # 结果保存到本地文件
    misc.imsave("D:/GitHub/Face_Recognizer-master/dlib_feature_extraction_{}.4.7.png".format(mode), show_img)

    # 返回人脸特征
    return feature


# 提取人脸特征
def feature_extraction(img, rects, mode="68"):

    feature = []

    if len(rects) > 0:

        show_img = img.copy()

        # 定义特征点预测器
        if mode == "68":
            sp = dlib.shape_predictor(shape_predictor_68_face_landmarks)
        else:
            sp = dlib.shape_predictor(shape_predictor_5_face_landmarks)

        # 定义dlib人脸识别模型
        facerec = dlib.face_recognition_model_v1(face_rec_model_path)
        for rect in rects:
            # 计算每张人脸的特征点
            shape = sp(img, convert_to_rect(rect))

            # 把特征点画出来,检验程序是否正确
            for pt in shape.parts():
                pt_pos = (pt.x, pt.y)
                cv2.circle(show_img, pt_pos, 2, (0, 255, 0), 1)

            # 将原图像和特征点传入人脸识别模型,得到人脸特征
            face_descriptor = facerec.compute_face_descriptor(img, shape)
            feature.append(face_descriptor)

        feature = np.array(feature)
        # 结果保存到本地文件
        misc.imsave("D:/GitHub/Face_Recognizer-master/dlib_feature_extraction_{}.4.7.png".format(mode), show_img)

    # 返回人脸特征
    return feature

if __name__ == '__main__':
    # 先通过MTCNN取得人脸框位置
    image_path = "D:/GitHub/Face_Recognizer-master/0.jpg"
    img = misc.imread(os.path.expanduser(image_path), mode='RGB')
    images, rects = mtcnn_findFace(img)

    img = misc.imread(os.path.expanduser(image_path), mode='RGB')
    feature_extraction(img, rects, "68")

    img = misc.imread(os.path.expanduser(image_path), mode='RGB')
    feature_extraction(img, rects, "5")

结果

                                        Dlib人脸特征提取_第1张图片                                              Dlib人脸特征提取_第2张图片

                                                       5特征点                                                                              68特征点

 

你可能感兴趣的:(人脸检测,特征提取,人脸识别,python)