东方耀AI技术分享
标题:
19、Dlib中人脸配准有两种方式一种是使用 get_face_chip()方法
[打印本页]
作者:
东方耀
时间:
2020-3-24 17:15
标题:
19、Dlib中人脸配准有两种方式一种是使用 get_face_chip()方法
19、Dlib中人脸配准有两种方式一种是使用 get_face_chip()方法
# -*- coding: utf-8 -*-
__author__ = u'东方耀 微信:dfy_88888'
__date__ = '2020/3/24 下午4:28'
__product__ = 'PyCharm'
__filename__ = 'dfy_demo03'
import cv2
import dlib
'''
人脸配准
人脸配准是很多其他功能的前提,比如我们最近做的一个脸型预测功能,就必须对人脸进行配准之后才能得到更好的准确率,
否则脸在水平平面和垂直水平平面进行旋转都会影响准确率的计算。
Dlib中人脸配准有两种方式一种是使用 get_face_chip()方法,使用5个关键点模型来进行配准,
这种方法Dlib已经提供了完整的接口,
另一种是自己使用68点关键点模型,根据关键点信息求解变换矩阵,然后把变换矩阵应用到整个图像上。
先看get_face_chip()方法:
先检测人脸
用得到的人脸位置信息来预测关键点
再使用get_face_chips方法返回配准好的人脸
这个get_face_chip()方法内部是怎么实现的,我现在还不知道
'''
img_path = '/home/dfy888/DataSets/64_CASIA-FaceV5/images_test/000/000_0.png'
predictor_path = './models/shape_predictor_5_face_landmarks.dat'
# 定义人脸关键点检测器
sp = dlib.shape_predictor(predictor_path)
hog_detector = dlib.get_frontal_face_detector()
cnn_detector = dlib.cnn_face_detection_model_v1('./models/mmod_human_face_detector.dat')
img_data = cv2.imread(img_path)
cv2.imshow('orig', img_data)
cv2.waitKey(0)
rgb_img2 = img_data[:, :, ::-1]
def align_face(detector, five_landmarks_predictor, rgb_img, height=512, width=0):
# gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2GRAY)
faceRects = detector(rgb_img, 0)
print('检测出%d个人脸' % len(faceRects))
faces = dlib.full_object_detections()
faces.append(five_landmarks_predictor(rgb_img, faceRects[0].rect))
# for faceRect in faceRects:
# faces.append(five_landmarks_predictor(rgb_img, faceRect.rect))
if len(faces) != 0:
face = dlib.get_face_chip(rgb_img, faces[0], size=139, padding=0.25)
print(face.shape)
cv2.imshow("aligned_face", face[:, :, ::-1])
align_face(cnn_detector, sp, rgb_img2)
cv2.waitKey(0)
复制代码
作者:
东方耀
时间:
2020-3-24 17:21
成功跑起来的例子:
img_data = cv2.imread('/home/dfy888/DataSets/64_CASIA-FaceV5/images_test/000/000_0.png')
# current word dict
print(os.getcwd())
# 定义人脸检测器
# detector = dlib.get_frontal_face_detector()
cnn_face_detector = dlib.cnn_face_detection_model_v1('./models/mmod_human_face_detector.dat')
predictor_path = './models/shape_predictor_5_face_landmarks.dat'
# 定义人脸关键点检测器
sp = dlib.shape_predictor(predictor_path)
# OpenCV + Dlib对人脸位置进行校准 查找脸部位置
fo = dlib.full_object_detections()
# img_path = '/home/dfy888/DataSets/CelebA/img_align_celeba/100205.jpg'
# img_path = './testimgs/img01.png'
# # img_path = '/home/dfy888/DataSets/VOCdevkit_widerface_dfy/VOC2020/JPEGImages/9--Press_Conference_9_Press_Conference_Press_Conference_9_91.jpg'
# img_data = cv2.imread(img_path)
print(img_data.shape)
img = img_data[:, :, ::-1]
# 1、人脸检测:使用 Dlib 中预先训练的模型检测面部
dets = cnn_face_detector(img, 0)
# dets = detector(img, 1)
num_faces = len(dets)
print('检测出%d个人脸' % num_faces)
if num_faces == 1:
# 获取人脸的坐标 注意img与img_data的区别
x1 = dets[0].rect.left()
y1 = dets[0].rect.top()
x2 = dets[0].rect.right()
y2 = dets[0].rect.bottom()
cv2.rectangle(img_data, (x1, y1), (x2, y2), (0, 0, 255), 2)
landmarks = np.matrix([[p.x, p.y] for p in sp(img, dets[0].rect).parts()])
print('每个人脸的关键点个数:', len(landmarks))
for point in landmarks:
pos = (point[0, 0], point[0, 1])
print('pos-', pos)
cv2.circle(img_data, pos, 2, color=(0, 255, 255), thickness=3)
# 下面开始clip 查找脸部位置
fo.append(sp(img, dets[0].rect))
image = dlib.get_face_chip(img, fo[0], size=139)
fo.pop()
image = image[:, :, ::-1]
cv2.imshow('chip shape:%s' % str(image.shape), image)
cv2.waitKey(0)
pass
cv2.imshow('origianl+face', img_data)
cv2.waitKey(0)
复制代码
作者:
lyh
时间:
2020-3-27 16:03
谢谢楼主分享代码
作者:
栀子花
时间:
2020-4-5 00:14
感谢楼主的分享
作者:
terry122222
时间:
2021-5-24 11:09
感谢楼主的分享
欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/)
Powered by Discuz! X3.4