【计算机视觉】使用python dlib做人脸68点对齐

使用python dlib做人脸68点对齐,读取文件夹内image,输出到同目录下同名pts文件
不多说

import os
import dlib
from skimage import io

def landmark_helper(data_dir):

    predictor_path = "model/shape_predictor_68_face_landmarks.dat"

    face_paths= os.listdir(data_dir)

    for path in face_paths:
        face_path = data_dir + path
        '''加载人脸检测器、加载官方提供的模型构建特征提取器'''
        detector = dlib.get_frontal_face_detector()
        predictor = dlib.shape_predictor(predictor_path)

        img = io.imread(face_path)

        dets = detector(img, 1)

        if (len(dets) != 1):
            print('Illegal facial image : ' + face_path + ', detected ' + len(dets) + 'face(s), need 1 !')

        pts_path = face_path.split('.')[0] + '.pts'

        with open(pts_path, mode='w', encoding='utf-8') as file_obj:
            file_obj.write('version: 1\n')
            file_obj.write('n_points:  68\n')
            file_obj.write('{\n')

            for k, d in enumerate(dets):
                shape = predictor(img, d) 
                for p in shape.parts():
                    file_obj.write(str(p.x) + ' ' + str(p.y) + '\n')

            file_obj.write('}\n')

shape_predictor_68_face_landmarks.dat很多地方可以下载,这里贴一个可以下载的gitHub地址

你可能感兴趣的:(计算机视觉,python,计算机视觉,开发语言)