dlib人脸配准有两种方式。

  1. 一种是使用 get_face_chip()方法,使用5个关键点模型来进行配准,这种方法Dlib已经提供了完整的接口
  2. 另一种是自己使用68点关键点模型,根据关键点信息求解变换矩阵,然后把变换矩阵应用到整个图像上。

(1)使用 get_face_chip()方法实验结果:

import cv2
import dlib
import matplotlib.pyplot as plt# 获取图片
image = cv2.imread('33.jpg')image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)# 使用特征提取器get_frontal_face_detector
detector = dlib.get_frontal_face_detector()dets = detector(image_gray, 1)for det in dets:# 将框画在原图上# cv2.rectangle  参数1:图片, 参数2:左上角坐标, 参数2:左上角坐标, 参数3:右下角坐标, 参数4:颜色(R,G,B), 参数2:粗细my_img = cv2.rectangle(image, (det.left(), det.top()), (det.right(), det.bottom()), (0, 255, 0), 2)cv2.imshow("image", image)
cv2.waitKey(0)# 人脸检测器
predictor = dlib.shape_predictor(r'./shape_predictor_68_face_landmarks.dat')
#
for det in dets:shape = predictor(image, det)# 将关键点绘制到人脸上for i in range(68):cv2.putText(image, str(i), (shape.part(i).x, shape.part(i).y), cv2.FONT_HERSHEY_DUPLEX, 0.1, (0, 255,0 ), 1, cv2.LINE_AA)cv2.circle(image, (shape.part(i).x, shape.part(i).y), 1, (0, 0, 255))cv2.imshow("rotated", image)
cv2.waitKey(0)# 人脸对齐
image = dlib.get_face_chip(image, shape, size = 150)
cv2.imshow("68landmarks", image)
cv2.waitKey(0)

原理参考:博文1 博文2

(2)使用68点关键点模型方法如下:
人脸对齐思路:

  • 分别计算左、右眼中心坐标
  • 计算左右眼中心坐标连线与水平方向的夹角θ
  • 计算左右两眼整体中心坐标
  • 以左右两眼整体中心坐标为基点,将图片array逆时针旋转θ

实验结果如下:

import cv2
import dlib
import numpy as npclass Face_Align(object):def __init__(self, shape_predictor_path):self.detector = dlib.get_frontal_face_detector()self.predictor = dlib.shape_predictor(shape_predictor_path)self.LEFT_EYE_INDICES = [36, 37, 38, 39, 40, 41]self.RIGHT_EYE_INDICES = [42, 43, 44, 45, 46, 47]def rect_to_tuple(self, rect):left = rect.left()right = rect.right()top = rect.top()bottom = rect.bottom()return left, top, right, bottomdef extract_eye(self, shape, eye_indices):points = map(lambda i: shape.part(i), eye_indices)return list(points)def extract_eye_center(self, shape, eye_indices):points = self.extract_eye(shape, eye_indices)xs = map(lambda p: p.x, points)ys = map(lambda p: p.y, points)return sum(xs) // 6, sum(ys) // 6def extract_left_eye_center(self, shape):return self.extract_eye_center(shape, self.LEFT_EYE_INDICES)def extract_right_eye_center(self, shape):return self.extract_eye_center(shape, self.RIGHT_EYE_INDICES)def angle_between_2_points(self, p1, p2):x1, y1 = p1x2, y2 = p2tan = (y2 - y1) / (x2 - x1)print("旋转角度:",np.degrees(np.arctan(tan)))return np.degrees(np.arctan(tan))def get_rotation_matrix(self, p1, p2):angle = self.angle_between_2_points(p1, p2)x1, y1 = p1x2, y2 = p2xc = (x1 + x2) // 2yc = (y1 + y2) // 2M = cv2.getRotationMatrix2D((xc, yc), angle, 1)print("旋转矩阵:",M)return Mdef crop_image(self, image, det):left, top, right, bottom = self.rect_to_tuple(det)return image[top:bottom, left:right]def __call__(self, image=None, image_path=None, save_path=None, only_one=True):'''Face alignment, can select input image variable or image path, when inputimage format that return alignment face image crop or image path as inputwill return None but save image to the save path.:image: Face image input:image_path: if image is None than can input image:save_path: path to save image:detector: detector = dlib.get_frontal_face_detector():predictor: predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")'''if image is not None:# convert BGR format to Grayimage_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)elif image_path is not None:image_gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)image = cv2.imread(image_path)height, width = image.shape[:2]print("原图形状:",image.shape)  # 获取图像的形状,返回值是一个包含行数、列数、通道数的元组。# Dector facedets = self.detector(image_gray, 1)# i donate the i_th face detected in imagecrop_images = []for i, det in enumerate(dets):shape = self.predictor(image_gray, det)left_eye = self.extract_left_eye_center(shape)right_eye = self.extract_right_eye_center(shape)M = self.get_rotation_matrix(left_eye, right_eye)rotated = cv2.warpAffine(image, M, (width, height), flags=cv2.INTER_CUBIC)cv2.imshow("xuanzhuan", rotated)cv2.waitKey(0)cropped = self.crop_image(rotated, det)if only_one == True:if save_path is not None:cv2.imwrite(save_path, cropped)return croppedelse:crop_images.append(cropped)return crop_images
if __name__ == "__main__":image = cv2.imread('33.jpg')cv2.imshow("image", image)cv2.waitKey(0)align = Face_Align("./shape_predictor_68_face_landmarks.dat")align = align(image,image_path=None,save_path="test.jpg",only_one=True)print(align.shape)cv2.imshow("align", align)cv2.waitKey(0)

原理参考:博文

dlib人脸配准(人脸对齐)相关推荐

  1. MTCNN人脸检测与对齐和FaceNet人脸识别

    一 MTCNN算法结构 MTCNN算法是一种基于深度学习的人脸检测和人脸对齐方法,它可以同时完成人脸检测和人脸对齐的任务,相比于传统的算法,它的性能更好,检测速度更快. MTCNN算法包含三个子网络: ...

  2. MTCNN-将多任务级联卷积神经网络用于人脸检测和对齐

    论文链接:  https://kpzhang93.github.io/MTCNN_face_detection_alignment/,本文是根据自己的理解翻译的如有错误,还请见谅,评论提出,立马修 ...

  3. python表情识别程序_Python+Dlib+Opencv实现人脸采集并表情判别功能的代码

    一.dlib以及opencv-python库安装 介于我使用的是jupyter notebook,所以在安装dlib和opencv-python时是在 这个命令行安装的 dlib安装方法: 1.若可以 ...

  4. python中dlib库_python 基于dlib库的人脸检测的实现

    本周暂时比较清闲,可以保持每日一更的速度. 国外身份证项目新增需求,检测出身份证正面的人脸.最开始考虑mobilenet-ssd,经同事提醒,有现成的人脸库dlib,那就用传统方法尝试一下. dlib ...

  5. Centerface:开源实用的边缘设备无锚人脸检测与对齐算法

    点击我爱计算机视觉标星,更快获取CVML新技术 昨天Github上 ywlife 开源了 CenterFace:实用的边缘设备无锚人脸检测与对齐算法,引起了很多52CV群友的关注,开源不到一天,已经1 ...

  6. 论文笔记--跨模态间的人脸与人名对齐方法研究-2012

    论文信息: 硕士论文-跨模态间的人脸与人名对齐方法研究-2012-复旦大学-吴伟 文末附人脸标注论文下载地址 文章目录 摘要 技术 框架图 人名重要性评估 人名检测 句法分析获得人名重要性&语 ...

  7. tensorflow精进之路(二十七)——人脸识别(中)(MTCNN人脸检查和人脸对齐+FaceNet模型)

    1.概述 上一讲,我们讲了人脸识别的基本原理,这一讲,我们用tensorflow来实现它. 2.下载LFW人脸数据集 2.1.LFW数据集简介 LFW人脸数据集主要用来研究非受限情况下的人脸识别问题, ...

  8. dlib php,图片人脸检测——Dlib版(四)

    上几篇给大家讲了OpenCV的图片人脸检测,而本文给大家带来的是比OpenCV更加精准的图片人脸检测Dlib库. 往期目录 dlib与OpenCV对比 识别精准度:Dlib >= OpenCV ...

  9. Dlib模型实现人脸识别

    文章目录 一.安装dlib库 二.利用dlib实现人脸68个关键点检测并标注 三.人脸特征提取 四.人脸识别 参考链接 环境说明: python3.6+spyder 第三方库的说明 skimage,p ...

  10. 【人脸识别】基于dlib库实现人脸特征值提取

    一.Dlib库介绍与安装 1. Dlib库简介 Dlib库是一个机器学习的开源库,包含了机器学习的很多算法,使用起来很方便,直接包含头文件即可,并且不依赖于其他库(自带图像编解码库源码).Dlib可以 ...

最新文章

  1. 打开DataGrip显示Load Data Ctrl+F5解决方案
  2. 技术人员职业生涯的红色警报
  3. Okhttp 插入缓存拦截器 解析
  4. 1 课外笔记之网页动画——jsp系列问题
  5. 5、Spring Cloud-声明式调用 Feign(下)
  6. centos6重启网络命令_Docker常用命令详解
  7. jq实现跟随鼠标点击移动的下划线效果
  8. 读《Web全栈工程师的自我修养》
  9. 福州发布《关于运用大数据加强对市场主体服务和监管实施方案》
  10. MySQL之环境变量配置
  11. 阿里云RDS Serverless测评
  12. Vue+ElementUI后台管理系统模板推荐
  13. 给机器人罗宾写一封英语回信_人教版pep小学英语三年级起点六上单词和习惯用语朗读mp3+文本Unit1...
  14. 【论文分享】GeoGAN:从卫星图像中生成地图的标准层
  15. QT学习11:clicked和toggled信号
  16. Spring-Cloud-Gateway之代码注入漏洞及解决
  17. 今天不谈技术,说说一些常用的软件~By 逆天
  18. git设置用户名和邮箱地址
  19. jQuery多余文字折叠效果
  20. 功利主义穆勒思维导图_边沁与穆勒的功利主义思想之比较

热门文章

  1. 浅谈Java垃圾回收
  2. 视频教程-手把手教你掌握区块链技术视频教程-区块链
  3. 计算机网络七层结构与功能,网络七层结构介绍
  4. 第一章 计算机网络概述(计算机网络韩立刚)
  5. 韩立刚《计算机网络》| 第5章 传输层
  6. 浅谈 Android Dex 文件
  7. 模电、数电、电力电子、开关电源基础知识总结
  8. office在线编辑 html,Office在线编辑功能实现.doc
  9. 电脑和开发板如何串口连接
  10. matlab-m文件常用积分函数-ode45含有时变参数用法/菜鸟理解4