Dlib系列-人脸检测
0.引言
在之前的文章中,介绍了基于OpenCV的人脸检测方法,可以对比两篇文章来查看实现效果。
Dlib是基于HOG,也就是方向梯度直方图实现的人脸检测。
1.简介
开发环境
python 3.6
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])
程序执行结果
2.源码介绍
两个源码Face detect 都是基于HOG/方向梯度直方图实现的,关于这方面的理论知识,可以自行百度查找。
face_detector.py:官网的介绍如下
This example program shows howtofind frontal human facesinan image. / 这个例子程序告诉我们如何找出图像中的正向人脸;
Inparticular, it shows how you cantakea listofimagesfromthe command line / 从命令行读取一系列图像文件,
anddisplayeachonthe screenwithred boxes overlaidoneachhuman face. / 然后在每张人脸上面用红色框标识出来。
./face_detector.py ../examples/faces/*.jpg
face_Landmark_detection.py:官网的介绍如下
This exampleprogramshows howtofind frontal human facesinan imageandestimate their pose. / 这个例子程序向我们展示如何图像中的正向人脸并且估计出他们的相貌;
The pose takes the formof68landmarks. These are pointsonthe face suchasthe cornersofthe mouth, along the eyebrows,onthe eyes,andso forth. / 这些面部特征由68点特征构成,这些特征点向我们展示了人脸部的嘴部,眼部等等;
2.1 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.
importsys
importdlib
fromskimageimportio
detector = dlib.get_frontal_face_detector()
win = dlib.image_window()
forfinsys.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)))
fori, dinenumerate(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)
fori, dinenumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i]))
```
face_dector代码处理过程如下:
为了方便理解,修改增加注释之后的face_detector_v1.py
importdlib
fromskimageimportio
# 使用 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))
fori, dinenumerate(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()
结果输出如下:
人脸数 / facesinall:3
第1个人脸的矩形框坐标:left:353right:389top:74bottom:110
第2个人脸的矩形框坐标:left:225right:261top:54bottom:90
第3个人脸的矩形框坐标:left:93right:129top:70bottom:106
Hit entertocontinue
但是我们进行图像处理的时候,经常是用opencv进行处理的,所以我们修改代码如下,使用 OpenCv 的对象( 矩形框用 cv2.rectangle() 函数绘制 )
face_detector_v2_use_opencv.py :
# -*- coding: utf-8 -*-
"""
Created on 2019/2/12 14:22
@Author: Johnson
@Email:[email protected]
@File: demo.py
"""
importdlib
importcv2
# 使用 Dlib 的正面人脸检测器 frontal_face_detector
detector = dlib.get_frontal_face_detector()
# 图片所在路径
# read image
img = cv2.imread("C:\\Users\\johnson.zhong\\Documents\\GitHub\\AI\\timg.jpg")
# 使用 detector 检测器来检测图像中的人脸
# use detector of Dlib to detector faces
faces = detector(img,1)
print("人脸数 / Faces in all: ", len(faces))
# Traversal every face
fori, dinenumerate(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
#!/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.
importsys
importos
importdlib
importglob
fromskimageimportio
iflen(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()
forfinglob.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)))
fork, dinenumerate(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()
代码处理流程
会绘制两个 overlay,人脸外接矩阵框 和 面部特征框 ;
红色的是绘制的 人脸矩形框 win.add_overlay(dets)
蓝色的是绘制的 人脸面部轮廓 win.add_overlay(shape)
我们不用命令行读取,简化代码,直接在代码内指定图像文件地址:
face_landmark_detection_v1.py:
# -*- coding: utf-8 -*-
"""
Created on 2019/2/12 14:22
@Author: Johnson
@Email:[email protected]
@File: demo.py
"""
importdlib
fromskimageimportio
# 使用 Dlib 的正面人脸检测器 frontal_face_detector
detector = dlib.get_frontal_face_detector()
# Dlib 的 68点模型
predictor = dlib.shape_predictor("D:\\BaiduNetdiskDownload\\shape_predictor_68_face_landmarks.dat")
# 图片所在路径
img = io.imread("C:\\Users\\johnson.zhong\\Documents\\GitHub\\AI\\timg.jpg")
# 生成 Dlib 的图像窗口
win = dlib.image_window()
win.set_image(img)
# 使用 detector 检测器来检测图像中的人脸
faces = detector(img,1)
print("人脸数:", len(faces))
fori, dinenumerate(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()
结果输出如下:
人脸数:3
第1个人脸的矩形框坐标:left:353right:389top:74bottom:110
第2个人脸的矩形框坐标:left:225right:261top:54bottom:90
第3个人脸的矩形框坐标:left:93right:129top:70bottom:106
参考链接
1:https://www.cnblogs.com/AdaminXie/p/7905888.html
2:https://blog.csdn.net/hongbin_xu/article/details/78347484
3: