Python 3 利用 Dlib 19.7 进行人脸检测
0. 引言 / Overview
介绍 Dlib 中基于 HOG,Histogram of Oriented Gradients / 方向梯度直方图 实现 Face Detect / 人脸检测 的两个 Examples / 例程 :
1. face_detector.py: 单张图片中的单个/多个人脸的面部定位 ;
2. face_landmark_detection.py: 单张图片的脸部特征点标定 ;
如果在 Windows下开发,在 Python 中安装 Dlib 有问题,可以参考我的另一篇博客:http://www.cnblogs.com/AdaminXie/p/9032224.html
1. 简介 / Breif introduction of codes
开发环境:
Python: 3.6.3
Dlib: 19.7
face_detector.py, 利用 Dlib 的 get_frontal_face_detector / 正向人脸检测器,进行人脸检测,提取人脸外部矩形框 :
detector = dlib.get_frontal_face_detector()
face_landmark_detection.py, 利用训练好的 shape_predictor("shape_predictor_68_face_landmarks.dat") / 人脸 68 点特征检测器,进行人脸面部轮廓特征提取:
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
shape = predictor(img, dets[0])
程序执行结果:
(a) face_detector.py (b) face_landmark_detection.py
图 1 代码实现结果示例
2. 源码介绍 / Source code introduction
两个源码的 Face detect / 人脸检测 都是基于 HOG / 方向梯度直方图 实现的 ( 关于 HOG,可以看我另一篇博客的介绍:https://www.cnblogs.com/AdaminXie/p/9884096.html )
This face detector is made using the now classic Histogram of Oriented Gradients (HOG) feature combined with a linear classifier, / 这种人脸检测器是由传统的 HOG / 方向梯度直方图 加上 线性分类器,
an image pyramid, and sliding window detection scheme. / 图像金字塔,和一个 滑动窗口检测。
This type of object detector is fairly general and capable of detecting many types of semi-rigid objects / 这种目标检测器适合检测很多种物体。
This example program shows how to find frontal human faces in an image. / 这个例子程序告诉我们如何找出图像中的正向人脸;
In particular, it shows how you can take a list of images from the command line / 从命令行读取一系列图像文件,
and display each on the screen with red boxes overlaid on each human face. / 然后在每张人脸上面用红色框标识出来。
This example program shows how to find frontal human faces in an image and estimate their pose. / 这个例子程序向我们展示如何图像中的正向人脸并且估计出他们的相貌;
The pose takes the form of 68 landmarks. These are points on the face such as the corners of the mouth, along the eyebrows, on the eyes, and so forth. / 这些面部特征由 68 点特征构成,这些特征点向我们展示了人脸部的嘴部,眼部等等;
2.1. face_detector.py
原始的 face detector.py 源码:
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
# This example program shows how to find frontal human faces in an image. In
# particular, it shows how you can take a list of images from the command
# line and display each on the screen with red boxes overlaid on each human
# face.
#
# The examples/faces folder contains some jpg images of people. You can run
# this program on them and see the detections by executing the
# following command:
# ./face_detector.py ../examples/faces/*.jpg
#
# This face detector is made using the now classic Histogram of Oriented
# Gradients (HOG) feature combined with a linear classifier, an image
# pyramid, and sliding window detection scheme. This type of object detector
# is fairly general and capable of detecting many types of semi-rigid objects
# in addition to human faces. Therefore, if you are interested in making
# your own object detectors then read the train_object_detector.py example
# program.
#
#
# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
# You can install dlib using the command:
# pip install dlib
#
# Alternatively, if you want to compile dlib yourself then go into the dlib
# root folder and run:
# python setup.py install
# or
# python setup.py install --yes USE_AVX_INSTRUCTIONS
# if you have a CPU that supports AVX instructions, since this makes some
# things run faster.
#
# Compiling dlib should work on any operating system so long as you have
# CMake installed. On Ubuntu, this can be done easily by running the
# command:
# sudo apt-get install cmake
#
# Also note that this example requires scikit-image which can be installed
# via the command:
# pip install scikit-image
# Or downloaded from http://scikit-image.org/download.html. import sys import dlib
from skimage import io detector = dlib.get_frontal_face_detector()
win = dlib.image_window() for f in sys.argv[1:]:
print("Processing file: {}".format(f))
img = io.imread(f)
# The 1 in the second argument indicates that we should upsample the image
# 1 time. This will make everything bigger and allow us to detect more
# faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for i, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
i, d.left(), d.top(), d.right(), d.bottom())) win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
dlib.hit_enter_to_continue() # Finally, if you really want to you can ask the detector to tell you the score
# for each detection. The score is bigger for more confident detections.
# The third argument to run is an optional adjustment to the detection threshold,
# where a negative value will return more detections and a positive value fewer.
# Also, the idx tells you which of the face sub-detectors matched. This can be
# used to broadly identify faces in different orientations.
if (len(sys.argv[1:]) > 0):
img = io.imread(sys.argv[1])
dets, scores, idx = detector.run(img, 1, -1)
for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i]))
face_detector 代码处理过程如下:
图 2 face_detector 代码处理过程
为了方便理解,修改增加注释之后的 face_detector_v1.py
# created at 2017-11-27
# updated at 2018-09-06 # Author: coneypo
# Dlib: http://dlib.net/
# Blog: http://www.cnblogs.com/AdaminXie/
# Github: https://github.com/coneypo/Dlib_examples import dlib
from skimage import io # 使用 Dlib 的正面人脸检测器 frontal_face_detector
detector = dlib.get_frontal_face_detector() # 图片所在路径
img = io.imread("../imgs/faces_2.jpeg") # 生成 Dlib 的图像窗口
win = dlib.image_window()
win.set_image(img) # 使用detector检测器来检测图像中的人脸
faces = detector(img, 1)
print(type(faces[0]), '\n') print("人脸数 / faces in all:", len(faces)) for i, d in enumerate(faces):
print("第", i+1, "个人脸的矩形框坐标:",
"left:", d.left(), '\t', "right:", d.right(), '\t', "top:", d.top(),'\t', "bottom:", d.bottom()) # 绘制矩阵轮廓
win.add_overlay(faces) # 保持图像
dlib.hit_enter_to_continue()
图 3 参数 d.top(), d.right(), d.left(), d.bottom() 位置坐标说明
结果:
生成的图片窗口结果:
图 4 face_detector.py 的输出结果(单张人脸)
输出结果:
人脸数 / faces in all:
第 个人脸的矩形框坐标: left: right: top: bottom:
Hit enter to continue
对于多个人脸的检测结果:
图 5 face_detector.py 的输出结果(多张人脸)
但是我们进行图像处理的时候,经常是用 OpenCv 进行处理,所以我们在下面代码中,使用 OpenCv 的对象( 矩形框用 cv2.rectangle() 函数绘制 ):
face_detector_v2_use_opencv.py :
# created at 2017-11-27
# updated at 2018-09-06 # Author: coneypo
# Dlib: http://dlib.net/
# Blog: http://www.cnblogs.com/AdaminXie/
# Github: https://github.com/coneypo/Dlib_examples # create object of OpenCv
# use OpenCv to read and show images import dlib
import cv2 # 使用 Dlib 的正面人脸检测器 frontal_face_detector
detector = dlib.get_frontal_face_detector() # 图片所在路径
# read image
img = cv2.imread("imgs/faces_2.jpeg") # 使用 detector 检测器来检测图像中的人脸
# use detector of Dlib to detector faces
faces = detector(img, 1)
print("人脸数 / Faces in all: ", len(faces)) # Traversal every face
for i, d in enumerate(faces):
print("第", i+1, "个人脸的矩形框坐标:",
"left:", d.left(), "right:", d.right(), "top:", d.top(), "bottom:", d.bottom())
cv2.rectangle(img, tuple([d.left(), d.top()]), tuple([d.right(), d.bottom()]), (0, 255, 255), 2) cv2.namedWindow("img", 2)
cv2.imshow("img", img)
cv2.waitKey(0)
2.2 face_landmark_detection.py
官网 face_landmark_detection.py 源码:
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
# This example program shows how to find frontal human faces in an image and
# estimate their pose. The pose takes the form of 68 landmarks. These are
# points on the face such as the corners of the mouth, along the eyebrows, on
# the eyes, and so forth.
#
# The face detector we use is made using the classic Histogram of Oriented
# Gradients (HOG) feature combined with a linear classifier, an image pyramid,
# and sliding window detection scheme. The pose estimator was created by
# using dlib's implementation of the paper:
# One Millisecond Face Alignment with an Ensemble of Regression Trees by
# Vahid Kazemi and Josephine Sullivan, CVPR 2014
# and was trained on the iBUG 300-W face landmark dataset (see
# https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/):
# C. Sagonas, E. Antonakos, G, Tzimiropoulos, S. Zafeiriou, M. Pantic.
# 300 faces In-the-wild challenge: Database and results.
# Image and Vision Computing (IMAVIS), Special Issue on Facial Landmark Localisation "In-The-Wild". 2016.
# You can get the trained model file from:
# http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2.
# Note that the license for the iBUG 300-W dataset excludes commercial use.
# So you should contact Imperial College London to find out if it's OK for
# you to use this model file in a commercial product.
#
#
# Also, note that you can train your own models using dlib's machine learning
# tools. See train_shape_predictor.py to see an example.
#
#
# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
# You can install dlib using the command:
# pip install dlib
#
# Alternatively, if you want to compile dlib yourself then go into the dlib
# root folder and run:
# python setup.py install
# or
# python setup.py install --yes USE_AVX_INSTRUCTIONS
# if you have a CPU that supports AVX instructions, since this makes some
# things run faster.
#
# Compiling dlib should work on any operating system so long as you have
# CMake installed. On Ubuntu, this can be done easily by running the
# command:
# sudo apt-get install cmake
#
# Also note that this example requires scikit-image which can be installed
# via the command:
# pip install scikit-image
# Or downloaded from http://scikit-image.org/download.html. import sys
import os
import dlib
import glob
from skimage import io if len(sys.argv) != 3:
print(
"Give the path to the trained shape predictor model as the first "
"argument and then the directory containing the facial images.\n"
"For example, if you are in the python_examples folder then "
"execute this program by running:\n"
" ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat ../examples/faces\n"
"You can download a trained facial shape predictor from:\n"
" http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2")
exit() predictor_path = sys.argv[1]
faces_folder_path = sys.argv[2] detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window() for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f) win.clear_overlay()
win.set_image(img) # Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for k, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))
# Get the landmarks/parts for the face in box d.
shape = predictor(img, d)
print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
shape.part(1)))
# Draw the face landmarks on the screen.
win.add_overlay(shape) win.add_overlay(dets)
dlib.hit_enter_to_continue()
代码处理流程:
图 6 face_landmark_detection 处理流程
会绘制两个 overlay,人脸外接矩阵框 和 面部特征框 ;
红色的是绘制的 人脸矩形框 win.add_overlay(dets)
蓝色的是绘制的 人脸面部轮廓 win.add_overlay(shape)
我们不用命令行读取,简化代码,直接在代码内指定图像文件地址:
face_landmark_detection_v1.py:
# created at 2017-11-27
# updated at 2018-09-06 # Author: coneypo
# Dlib: http://dlib.net/
# Blog: http://www.cnblogs.com/AdaminXie/
# Github: https://github.com/coneypo/Dlib_examples import dlib
from skimage import io # 使用 Dlib 的正面人脸检测器 frontal_face_detector
detector = dlib.get_frontal_face_detector() # Dlib 的 68点模型
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 图片所在路径
img = io.imread("imgs/faces_2.jpeg") # 生成 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()
输出结果:
人脸数: 1
第 1 个人脸的矩形框坐标: left: 63 right: 384 top: 206 bottom: 527
Hit enter to continue
图 7 face_landmark_detection.py 的输出结果(多张人脸)
因为面部轮廓 shape 在 OpenvCv 中不太好转换 ,所以这里不给出 OpenCv 对象的实现;
Appendix: 关于 sys.argv[] 的使用:
官网例程中是利用 sys.argv[] 读取命令行输入,如果对于 sys.argv[] 有疑惑,可以参照下面的总结;
sys.argv[] 用来获取命令交互模式下,尾随的参数;
例如在 cmd 的 console 模式下,输入
>>> python test.py parameter_1
就可以利用 sys.argv[] 拿到 parameter_1 的值;
建议还是直接在源码指定好图片等参数路径;
下面用代码实例来帮助理解:
在 Console( Windows 下是 Command Prompt,命令提示符 )输入以下命令:
python test.py what is your name
test.py 内容:
import sys print(sys.argv[0])
# test.py print(sys.argv[1])
# what print(sys.argv[2])
# is print(sys.argv[1:])
# [“what”,“is”,“your”,“name”]
# 如果对您有帮助,欢迎在 GitHub 上 Star 支持我 : https://github.com/coneypo/Dlib_examples
# 请尊重他人劳动成果,转载或者使用源码请注明出处 : http://www.cnblogs.com/AdaminXie
# 如有问题请留言或者联系邮箱 : coneypo@foxmail.com
Python 3 利用 Dlib 19.7 进行人脸检测的更多相关文章
- Python 3 利用 Dlib 19.7 实现人脸识别和剪切
0.引言 利用python开发,借助Dlib库进行人脸识别,然后将检测到的人脸剪切下来,依次排序显示在新的图像上: 实现的效果如下图所示,将图1原图中的6张人脸检测出来,然后剪切下来,在图像窗口中依次 ...
- Python 3 利用 Dlib 实现摄像头实时人脸检测和平铺显示
1. 引言 在某些场景下,我们不仅需要进行实时人脸检测追踪,还要进行再加工:这里进行摄像头实时人脸检测,并对于实时检测的人脸进行初步提取: 单个/多个人脸检测,并依次在摄像头窗口,实时平铺显示检测到的 ...
- Python 3 利用 Dlib 19.7 实现摄像头人脸识别
0.引言 利用python开发,借助Dlib库捕获摄像头中的人脸,提取人脸特征,通过计算欧氏距离来和预存的人脸特征进行对比,达到人脸识别的目的: 可以自动从摄像头中抠取人脸图片存储到本地: 根据抠取的 ...
- Python 3 利用 Dlib 19.7 和 sklearn机器学习模型 实现人脸微笑检测
0.引言 利用机器学习的方法训练微笑检测模型,给一张人脸照片,判断是否微笑: 使用的数据集中69张没笑脸,65张有笑脸,训练结果识别精度在95%附近: 效果: 图1 示例效果 工程利用pytho ...
- Python学习--使用dlib、opencv进行人脸检测标注
参考自https://www.pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/ 在原有基础上有一部分的修改(image ...
- Python 3 利用 Dlib 实现人脸检测和剪切
0. 引言 利用 Python 开发,借助 Dlib 库进行人脸检测 / face detection 和剪切: 1. crop_faces_show.py : 将检测到的人脸剪切下来,依次排序平 ...
- Python 3 利用 Dlib 实现摄像头人脸检测特征点标定
0. 引言 利用 Python 开发,借助 Dlib 库捕获摄像头中的人脸,进行实时人脸 68 个特征点标定: 支持多张人脸: 有截图功能: 图 1 工程效果示例( gif ) 图 2 工程效果示例( ...
- Python 3.6.3 利用Dlib 19.7库进行人脸识别
0.引言 自己在下载dlib官网给的example代码时,一开始不知道怎么使用,在一番摸索之后弄明白怎么使用了: 现分享下 face_detector.py 和 face_landmark_detec ...
- Python 3.6.3 利用 Dlib 19.7 和 opencv 实现人脸68点定位 进行人脸识别
0.引言 介绍利用Dlib官方给的人脸识别预测器"shape_predictor_68_face_landmarks.dat"进行68点标定,利用OpenCv进行图像化处理,在人脸 ...
随机推荐
- 使用jenkins管理uirecorder录制的任务
在uirecorder官网(http://uirecorder.com/)上,对jenkins的配置只有简单的几句话: How to dock Jenkins? Add commands source ...
- Fluent Terminal
特性: PowerShell,CMD,WSL或自定义shell的终端 支持选项卡和多个窗口 主题和外观配置 导入/导出主题 导入iTerm主题 全屏模式 可编辑的键绑定 搜索功能 配置shell配置文 ...
- 20155314 2016-2017-2 《Java程序设计》实验四 Android程序设计
20155314 2016-2017-2 <Java程序设计>实验四 Android程序设计 实验任务 基于Android Studio开发简单的Android应用并部署测试 了解Andr ...
- virtualbox+vagrant学习-2(command cli)-19-vagrant box命令
Status 格式: vagrant status [name|id] options只有 -h, --help 这将告诉你vagrant正在管理的机器的状态. 很容易就会忘记你的vagrant机器是 ...
- 阿里开源项目之Ant Design Pro
本篇文章主要包含的内容有三个方面. 第一.Ant Design Pro简介; 第二.Ant Design Pro能做什么; 第三.初步使用; 我相信通过这三个方面的讲解能让你大概知道Ant Desig ...
- Lambda表达式学习(2)
在. net3. 5里面 , 委托的定义和实现被大大的简化了!使用关键字Func或Action就可以定义一个委托 , 使用拉姆达表达式就可以实现一个具体的委托. Func关键字是用来定义一个有返回值的 ...
- Grid Selenium
python selenium-9 grid模式 grid是进行分布式测试的工具,由一个hub主节点和若干个node代理节点组成 1.下载Selenium Standalone Server 下载地址 ...
- 【题解】洛谷P4158 [SCOI2009] 粉刷匠(DP)
次元传送门:洛谷P4158 思路 f[i][j][k][0/1]表示在坐标为(i,j)的格子 已经涂了k次 (0是此格子涂错 1是此格子涂对)涂对的格子数 显然的是 每次换行都要增加一次次数 那么当j ...
- C++程序设计--实验二
第二次实验主要内容是函数重载,快速排序及其模板实现,简单的user类实现. 实验结论: 一.函数重载编程练习 /*编写重载函数add(),实现对int型,double型和Complex型数据的加法.在 ...
- BCNF范式及其分解方法(对一次Lab作业的总结)
BCNF是比第三范式更严格一个范式.它要求关系模型中所有的属性(包括主属性和非主属性)都不传递依赖于任何候选关键字.也就是说,当关系型表中功能上互相依赖的那些列的每一列都是一个候选关键字时候,该满足B ...