python dlib学习(七):人脸特征点对齐

来源:互联网 发布:乐福总决赛第七场数据 编辑:程序博客网 时间:2024/05/16 14:20

前言

前面的博客介绍过人脸特征点标定:python dlib学习(二):人脸特征点标定。这次试着使用这些人脸特征点来对人脸进行对齐。
完整工程链接附在文章最后。

程序

上代码,程序中使用了python-opencv,事先要配置好环境。
我们在程序中会导入识别人脸特征点的模型,官方例程给出的模型的链接:
http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2(5个特征点)
我使用的跟前面博客(python dlib学习(二):人脸特征点标定)中的一样,是检测人脸68个特征点的模型。
下载链接:http://pan.baidu.com/s/1i46vPu1。

程序中已有注释,不做赘述。

# coding: utf-8import cv2import dlibimport sysimport numpy as npimport os# 获取当前路径current_path = os.getcwd()# 指定你存放的模型的路径,我使用的是检测68个特征点的那个模型,# predicter_path = current_path + '/model/shape_predictor_5_face_landmarks.dat'# 检测人脸特征点的模型放在当前文件夹中predicter_path = current_path + '/model/shape_predictor_68_face_landmarks.dat'face_file_path = current_path + '/faces/inesta.jpg'# 要使用的图片,图片放在当前文件夹中print predicter_pathprint face_file_path# 导入人脸检测模型detector = dlib.get_frontal_face_detector()# 导入检测人脸特征点的模型sp = dlib.shape_predictor(predicter_path)# 读入图片bgr_img = cv2.imread(face_file_path)if bgr_img is None:    print("Sorry, we could not load '{}' as an image".format(face_file_path))    exit()# opencv的颜色空间是BGR,需要转为RGB才能用在dlib中rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)# 检测图片中的人脸dets = detector(rgb_img, 1)# 检测到的人脸数量num_faces = len(dets)if num_faces == 0:    print("Sorry, there were no faces found in '{}'".format(face_file_path))    exit()# 识别人脸特征点,并保存下来faces = dlib.full_object_detections()for det in dets:    faces.append(sp(rgb_img, det))# 人脸对齐images = dlib.get_face_chips(rgb_img, faces, size=320)# 显示计数,按照这个计数创建窗口image_cnt = 0# 显示对齐结果for image in images:    image_cnt += 1    cv_rgb_image = np.array(image).astype(np.uint8)# 先转换为numpy数组    cv_bgr_image = cv2.cvtColor(cv_rgb_image, cv2.COLOR_RGB2BGR)# opencv下颜色空间为bgr,所以从rgb转换为bgr    cv2.imshow('%s'%(image_cnt), cv_bgr_image)cv2.waitKey(0)cv2.destroyAllWindows()

运行结果

这里写图片描述

原图:

这里写图片描述

完整工程下载链接:
http://download.csdn.net/download/hongbin_xu/10115082

阅读全文
1 0
原创粉丝点击