失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 会议中人脸面部轮廓图像提取(三)——Dlib库人脸面部轮廓图像特征提取

会议中人脸面部轮廓图像提取(三)——Dlib库人脸面部轮廓图像特征提取

时间:2023-02-22 10:46:04

相关推荐

会议中人脸面部轮廓图像提取(三)——Dlib库人脸面部轮廓图像特征提取

前言:所使用图片并无盈利等目的,如有侵犯他人肖像权请联系删除。

当当当当,第三期来廖!接上一期在线会议中人脸面部轮廓图像提取(二)——HOG人脸面部轮廓图像特征提取,介绍完HOG特征提取我们继续学习Dlib库提取特征叭!

1、人脸轮廓图像提取原理

在我们检测到人脸区域之后,接下来要研究的问题是获取到不同的脸部的特征,以区分不同人脸,即人脸特征检测(facial feature detection)。它也被称为人脸特征点检测(facial landmark detection)。

人脸特征点通常会标识出脸部的下列数个区域:

右眼眉毛(Right eyebrow)左眼眉毛(Left eyebrow)右眼(Right eye)左眼(Left eye)嘴巴(Mouth)鼻子(Nose)下巴(Jaw)

检测人脸特征点,分为如下两步:

第一步:定位图像中人脸区域;这一步我们可以使用之前学到过的 OpenCV中Harr检测器 或者 Dlib中HOG加SVM的检测器。

第一步:在人脸区域内检测出人脸关键特征;

对于第二步,我们使用Dlib中的特征点检测。

Dlib中使用的人脸特征检测的原理来自,由Vahid KazemiJosephine Sullivan在论文 《One Millisecond Face Alignment with an Ensemble of Regression Trees》中提出的人脸特征点评估的方法。

论文中方法的主要思想是:

使用级联回归树(ensemble of regression trees,

ERT),即使用级联回归因子基于梯度提高学习的回归树方法。该方法首先需要使用一系列标定好的人脸图片作为训练集,然后会生成一个模型。使机器学习模型能够找出任何脸上的这些特征点。

简要的分为以下三步:

定义一张脸上的68个具体的特征点(landmarks);标记面部特征点,获得带标记的训练数据。需要手动标记图像上的面部特征点,标签指定围绕每个面部结构的区域;给定训练数据,训练回归树的集合,直接从像素强度本身估计面部界标位置,得到模型。

2、模型算法实现

Dlib提供两种人脸检测模型:

shape_predictor_5_face_landmarks.dat:

检测5个人脸特征关键点,即双眼的眼头及眼尾以及鼻头这五个位置。因为只检测五个点,所以执行速度很快。如图:

5点人脸特征图shape_predictor_68_face_landmarks.dat:

检测68个人脸特征关键点。如图:

68点人脸特征图

3、具体实践步骤

利用Dlib的正向人脸检测器 get_frontal_face_detector(),进行人脸检测,提取人脸外部矩形框:

detector = dlib.get_frontal_face_detector() faces = detector(image, 1)

返回的faces为识别出的脸部数组。

利用训练好的人脸68点特征检测器,进行人脸面部轮廓特征提取:

predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") shape = predictor(image, face)

源码示例与解析:

import cv2import dlib# 读取图片img_path = "C.jpg"img = cv2.imread(img_path)n = 2img = cv2.resize(img, (0, 0), fx=1/n, fy=1/n, interpolation=cv2.INTER_NEAREST)# 转换为灰阶图片gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 正向人脸检测器将图像detector = dlib.get_frontal_face_detector()# 使用训练好的68个特征点模型predictor_path = "shape_predictor_68_face_landmarks.dat"predictor = dlib.shape_predictor(predictor_path)# 使用检测器来检测图像中的人脸faces = detector(gray, 1)# 打印结果print("人脸数: ", len(faces))for i, face in enumerate(faces):print("第", i+1, "个人脸的矩形框坐标:\n","left:", face.left(), "right:", face.right(), "top:", face.top(), "bottom:", face.bottom())# 获取人脸特征点shape = predictor(img, face)print("第", i+1, '个人脸特征点:')print(shape.parts())

结果:

人脸数: 1 第 1 个人脸的矩形框坐标: left: 82 right: 349 top: 142 bottom: 409 第 1 个人脸特征点: points[(94, 238), (98, 271), (105, 303), (113, 334), (125, 362), (144, 385), (168, 405), (195, 420), (225, 426), (256, 421), (284, 406), (308, 386), (328, 362), (340, 333), (348, 302), (354, 271), (360, 238), (115, 206), (130, 184), (154, 172), (182, 170), (208, 174), (251, 174), (277, 169), (304, 171), (327, 182), (341, 203), (229, 207), (229, 220), (229, 234), (228, 248), (204, 275), (216, 277), (228, 279), (240, 278), (251, 276), (147, 221), (162, 212), (179, 210), (194, 218), (179, 222), (162, 224), (263, 218), (278, 209), (295, 211), (308, 220), (295, 223), (278, 222), (187, 332), (202, 315), (219, 306), (228, 309), (238, 307), (255, 319), (271, 335), (255, 350), (239, 356), (227, 357), (217, 355), (202, 348), (197, 332), (218, 321), (228, 321), (238, 321), (262, 334), (238, 338), (228, 339), (218, 337)]

Dlib 绘制人脸轮廓图

在识别出人脸特征点之后,绘制对应的特征点能可视化特征效果。Dlib本身提供了绘制特征点的方法,主要分为以下几步:

(1)使用image_window()新建图像窗口:

win = dlib.image_window()

(2)指定窗口图片:

win.clear_overlay() win.set_image(img)

(3)绘制面部轮廓:

# 使用predictor来计算面部轮廓 shape = predictor(img, faces[i]) # 绘制面部轮廓 win.add_overlay(shape)# 绘制面部轮廓 win.add_overlay(shape)

(4) 绘制人脸区域矩阵:

# 绘制矩阵轮廓 win.add_overlay(faces)

经过以上这些步骤后,可成功计算出人脸面部特征并画出轮廓图。

运行结果:

人脸数: 1第 1 个人脸的矩形框坐标: left: 97 right: 283 top: 118 bottom: 304Hit enter to continue

可能是因为肤色和清晰度问题、面部姿势会稍微有点小误差:

人脸数: 5第 1 个人脸的矩形框坐标: left: 833 right: 895 top: 135 bottom: 198第 2 个人脸的矩形框坐标: left: 135 right: 197 top: 149 bottom: 211第 3 个人脸的矩形框坐标: left: 487 right: 550 top: 73 bottom: 135第 4 个人脸的矩形框坐标: left: 229 right: 303 top: 96 bottom: 171第 5 个人脸的矩形框坐标: left: 626 right: 688 top: 87 bottom: 149Hit enter to continue

人脸数: 1第 1 个人脸的矩形框坐标: left: 262 right: 448 top: 98 bottom: 284Hit enter to continue

源码与解析:

import dlibimport cv2# 使用 Dlib 的正面人脸检测器 frontal_face_detectordetector = dlib.get_frontal_face_detector()# Dlib 的 68点模型predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")# 读取图片img = cv2.imread("R.jpg")n = 1#缩放img = cv2.resize(img, (0, 0), fx=1/n, fy=1/n, interpolation=cv2.INTER_NEAREST)# 生成 Dlib 的图像窗口win = dlib.image_window()win.set_image(img)# 使用 detector 检测器来检测图像中的人脸faces = detector(img, 1)print("人脸数:", len(faces))for i, d in enumerate(faces):print("第", i+1, "个人脸的矩形框坐标:","left:", d.left(), "right:", d.right(), "top:", d.top(), "bottom:", d.bottom())# 使用predictor来计算面部轮廓shape = predictor(img, faces[i])# 绘制面部轮廓win.add_overlay(shape)# 绘制矩阵轮廓win.add_overlay(faces)dlib.hit_enter_to_continue()

一些学习到的有趣的转化:

RGB转HSV:

源码与解释:

#encoding:utf-8import numpy as npimport cv2image = cv2.imread("R.jpg")n = 1image = cv2.resize(image, (0, 0), fx=1/n, fy=1/n, interpolation=cv2.INTER_NEAREST)cv2.imshow("Original",image)# cv2.waitKey(0)#HSV空间hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)cv2.imshow("HSV",hsv)cv2.waitKey(0)

RGB转LAB:

源码与解释:

#encoding:utf-8import numpy as npimport cv2image = cv2.imread("R.jpg")n = 1image = cv2.resize(image, (0, 0), fx=1/n, fy=1/n, interpolation=cv2.INTER_NEAREST)cv2.imshow("Original",image)# cv2.waitKey(0)#lab空间lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)cv2.imshow("L*A*B*", lab)cv2.waitKey(0)

4、最后

以上有一些知识还是挺有趣的,大家有兴趣的话可以去搜索学习一下奥。所使用的素材与源码等之后会同一整理打包上传。

创作不易,感谢您的三连嘻嘻,下期更加精彩,敬请期待窝!

如果觉得《会议中人脸面部轮廓图像提取(三)——Dlib库人脸面部轮廓图像特征提取》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。