赞
踩
目录
Facial landmarks with dlib, OpenCV, and Python
1.什么是 面部标志 ?(What are facial landmarks?)
下面我们 dlib和OpenCV来检测图像中的面部标志(Facial landmarks):
面部标志用来定位和便是人脸的显著区域,例如:
面部标注点已成功应用于人脸对齐、头部姿态估计、换脸、眨眼检测等领域。
这篇博客文章中,将重点关注面部标志的基础知识,包括:
这篇博文的第一部分将讨论面部标志以及为什么它们被用于计算机视觉应用。
在这里,我将演示如何使用dlib、OpenCV和Python来检测和提取面部标志。
顺便提一嘴 python3.7 的dilb 安装:
链接: https://pan.baidu.com/s/1MKqW7WH2XP-J8MOLeq3cDA 提取码: rfh8
cd到下载文件同一个目录,然后:
pip install dlib-19.17.99-cp37-cp37m-win_amd64.whl
我用了这个,成功了
最后,我们将看看将面部标志检测应用于图像的一些结果。
Fig 1 :面部标志用于对图像中关键的人脸属性进行标注和识别
检测面部标志是形状预测问题的一个子集。给定输入图像(通常是指定感兴趣对象的感兴趣区域),形状预测器试图沿着形状定位感兴趣的关键点。 在面部标志的上下文(context of facial landmarks)中,我们的目标是使用形状预测方法检测面部的重要面部结构。
因此,面部标志检查分为两步:
步骤1:定位图片中的脸。
步骤2:检测人脸ROI(Region interset)中的关键面部结构。
人脸检测(步骤#1):
可以通过多种方式实现。 我们可以使用OpenCV, 或者我们甚至可以使用基于深度学习的算法进行人脸定位。用于检测图像中的人脸的实际算法都无关紧要。相反,重要的是通过某种方法我们获得了人脸边界框(即,图像中人脸的(x,y)坐标)。
给定面部区域,我们可以应用步骤#2:
检测面部区域中的关键面部结构。 有各种各样的面部标志检测器,但是所有方法本质上都试图定位和标记以下面部区域:
dlib库中包含的面部标志检测器是Kazemi and Sullivan(2014年)发表的论文"One Millisecond Face Alignment with an Ensemble of Regression Trees"
该方法首先使用:
图像上标记的面部标志的训练集。这些图像是手动标记的,指定每个面部结构周围区域的特定(x,y)坐标。 更具体地说,先验是输入像素对之间距离的概率。
给定该训练数据,训练回归树的集合以直接从像素强度本身估计面部标志点位置(即,没有“特征提取”发生)。 最终结果是一个面部标志检测器,可用于实时检测具有高质量预测的面部标志。 关于这种技术的更多信息和细节,请务必阅读该论文以及官方的dlib文档。
dlib库中预先训练的面部标志检测器用于估计映射到面部面部结构的 68 个(x,y)坐标的位置。
68个坐标的索引如下图所示:
Fig 2 :从iBUG 300-W数据集可视化68个面部地标坐标
这些注释是dlib面部标志预测器所训练的68点iBUG 300-W数据集的一部分。
值得注意的是,还存在其他类型的面部标志检测器,包括可以在HELEN数据集上训练的194点模型。
无论使用哪个数据集,都可以利用同一个dlib框架在输入的训练数据上训练一个形状预测器。如果您想训练面部标志检测器或您自己的自定义形状预测器,这是很有用的。
在这篇博文的剩余部分,将演示如何在图像中检测这些面部标志。
代码地址:https://github.com/iAuAi/Learning_demo
- # -*- coding: utf-8 -*-
-
-
- import dlib
- import cv2
-
-
- #源程序是用sys.argv从命令行参数去获取训练模型,精简版我直接把路径写在程序中了
- predictor_path = "D:/pregram/spider_test/code/face_model/data/data_dlib/shape_predictor_68_face_landmarks.dat"
-
- faces_path = "D:/pregram/spider_test/code/face_model/data/data_faces/face_1.jpeg"
- #与人脸检测相同,使用dlib自带的frontal_face_detector作为人脸检测器
- detector = dlib.get_frontal_face_detector()
-
- #使用官方提供的模型构建特征提取器
- predictor = dlib.shape_predictor(predictor_path)
- #读取图片
- img = cv2.imread(faces_path)
-
- #与人脸检测程序相同,使用detector进行人脸检测 dets为返回的结果
- dets = detector(img, 1)
- #使用enumerate 函数遍历序列中的元素以及它们的下标
- #下标k即为人脸序号
- #left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
- #top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
- for k, d in enumerate(dets):
- print("dets{}".format(d))
- print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
- k, d.left(), d.top(), d.right(), d.bottom()))
-
- #使用predictor进行人脸关键点识别 shape为返回的结果
- shape = predictor(img, d)
- #获取第一个和第二个点的坐标(相对于图片而不是框出来的人脸)
- print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
-
- #绘制特征点
- for index, pt in enumerate(shape.parts()):
- print('Part {}: {}'.format(index, pt))
- pt_pos = (pt.x, pt.y)
- cv2.circle(img, pt_pos, 2, (255, 0, 0), 1)
-
-
-
- cv2.imshow('test2', img)
- k = cv2.waitKey(0)
- cv2.destroyAllWindows()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
output:
dets[(103, 44) (211, 152)]
Detection 0: Left: 103 Top: 44 Right: 211 Bottom: 152
Part 0: (108, 82), Part 1: (109, 93) ...
Part 0: (108, 82)
Part 1: (109, 93)
Part 2: (111, 105)
Part 3: (113, 116)
Part 4: (118, 126)
Part 5: (125, 135)
Part 6: (136, 142)
Part 7: (147, 146)
Part 8: (160, 147)
Part 9: (172, 145)
Part 10: (182, 139)
Part 11: (190, 131)
Part 12: (196, 121)
Part 13: (198, 111)
Part 14: (200, 99)
Part 15: (200, 88)
Part 16: (200, 77)
Part 17: (120, 66)
Part 18: (126, 61)
Part 19: (134, 59)
Part 20: (142, 61)
Part 21: (150, 65)
Part 22: (162, 65)
Part 23: (169, 61)
Part 24: (177, 59)
Part 25: (184, 61)
Part 26: (189, 66)
Part 27: (156, 75)
Part 28: (157, 82)
Part 29: (157, 89)
Part 30: (158, 97)
Part 31: (149, 105)
Part 32: (154, 106)
Part 33: (158, 106)
Part 34: (162, 105)
Part 35: (166, 105)
Part 36: (129, 79)
Part 37: (133, 76)
Part 38: (139, 76)
Part 39: (144, 79)
Part 40: (139, 80)
Part 41: (133, 80)
Part 42: (168, 79)
Part 43: (172, 76)
Part 44: (178, 75)
Part 45: (182, 77)
Part 46: (178, 79)
Part 47: (173, 80)
Part 48: (142, 120)
Part 49: (148, 116)
Part 50: (154, 113)
Part 51: (158, 114)
Part 52: (162, 112)
Part 53: (168, 115)
Part 54: (173, 119)
Part 55: (168, 123)
Part 56: (163, 124)
Part 57: (159, 125)
Part 58: (155, 125)
Part 59: (149, 124)
Part 60: (144, 120)
Part 61: (155, 119)
Part 62: (159, 119)
Part 63: (163, 118)
Part 64: (171, 119)
Part 65: (163, 118)
Part 66: (159, 119)
Part 67: (155, 119)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。