Python 3 利用 Dlib 19.7 进行人脸检测

0.引言

  自己在下载 Dlib 官网给的 example 代码时,一开始不知道怎么使用,在一番摸索之后弄明白怎么使用了;

  现分享下 face_detector.py 和 face_landmark_detection.py 这两个 官网例程 的使用方法;

  可以实现对 单张图片中的单个/多个人脸的面部定位 脸部特征点标定 

 

  (关于 Windows下 Python  Dlib 库的安装,建议在 Anaconda 集成环境下安装,安装过程中如有问题,可以参考我的另一篇博客:http://www.cnblogs.com/AdaminXie/p/9032224.html

 

1.简介

Python:  3.6.3

Dlib:    19.7

实现人脸检测 / Face Detection;

利用 Dlib 的特征提取器,进行人脸 矩形框 的提取:  

1 dets = dlib.get_frontal_face_detector(img)

 

利用 Dlib 的68点特征预测器,进行人脸 面部轮廓特征 提取: 

1 predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
2 shape = predictor(img, dets[0])

  

  结果:

 

     (a) face_detector.py  (b) face_landmark_detection.py

    图1 工程结果示例

 

2. 源码介绍

   根据官网的介绍:
    "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."

  在 face_detector.py的检测人脸基础上,检测出人脸部的具体特征部位:下巴轮廓、眉毛、眼睛、嘴巴,同样用标记标识出面部特征;

     

2.1. face_detector.py 

  官网给的 face detector.py :

 1 #!/usr/bin/python
 2 # The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
 3 #
 4 #   This example program shows how to find frontal human faces in an image.  In
 5 #   particular, it shows how you can take a list of images from the command
 6 #   line and display each on the screen with red boxes overlaid on each human
 7 #   face.
 8 #
 9 #   The examples/faces folder contains some jpg images of people.  You can run
10 #   this program on them and see the detections by executing the
11 #   following command:
12 #       ./face_detector.py ../examples/faces/*.jpg
13 #
14 #   This face detector is made using the now classic Histogram of Oriented
15 #   Gradients (HOG) feature combined with a linear classifier, an image
16 #   pyramid, and sliding window detection scheme.  This type of object detector
17 #   is fairly general and capable of detecting many types of semi-rigid objects
18 #   in addition to human faces.  Therefore, if you are interested in making
19 #   your own object detectors then read the train_object_detector.py example
20 #   program.  
21 #
22 #
23 # COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
24 #   You can install dlib using the command:
25 #       pip install dlib
26 #
27 #   Alternatively, if you want to compile dlib yourself then go into the dlib
28 #   root folder and run:
29 #       python setup.py install
30 #   or
31 #       python setup.py install --yes USE_AVX_INSTRUCTIONS
32 #   if you have a CPU that supports AVX instructions, since this makes some
33 #   things run faster.  
34 #
35 #   Compiling dlib should work on any operating system so long as you have
36 #   CMake installed.  On Ubuntu, this can be done easily by running the
37 #   command:
38 #       sudo apt-get install cmake
39 #
40 #   Also note that this example requires scikit-image which can be installed
41 #   via the command:
42 #       pip install scikit-image
43 #   Or downloaded from http://scikit-image.org/download.html. 
44 
45 import sys
46 
47 import dlib
48 from skimage import io
49 
50 
51 detector = dlib.get_frontal_face_detector()
52 win = dlib.image_window()
53 
54 for f in sys.argv[1:]:
55     print("Processing file: {}".format(f))
56     img = io.imread(f)
57     # The 1 in the second argument indicates that we should upsample the image
58     # 1 time.  This will make everything bigger and allow us to detect more
59     # faces.
60     dets = detector(img, 1)
61     print("Number of faces detected: {}".format(len(dets)))
62     for i, d in enumerate(dets):
63         print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
64             i, d.left(), d.top(), d.right(), d.bottom()))
65 
66     win.clear_overlay()
67     win.set_image(img)
68     win.add_overlay(dets)
69     dlib.hit_enter_to_continue()
70 
71 
72 # Finally, if you really want to you can ask the detector to tell you the score
73 # for each detection.  The score is bigger for more confident detections.
74 # The third argument to run is an optional adjustment to the detection threshold,
75 # where a negative value will return more detections and a positive value fewer.
76 # Also, the idx tells you which of the face sub-detectors matched.  This can be
77 # used to broadly identify faces in different orientations.
78 if (len(sys.argv[1:]) > 0):
79     img = io.imread(sys.argv[1])
80     dets, scores, idx = detector.run(img, 1, -1)
81     for i, d in enumerate(dets):
82         print("Detection {}, score: {}, face_type:{}".format(
83             d, scores[i], idx[i]))

 

为了方便理解,修改增加注释之后的 face_detector.py

 1 # created at 2017-11-27
 2 # updated at 2018-05-14
 3 # by TimeStamp
 4 # cnblogs: http://www.cnblogs.com/AdaminXie/
 5 
 6 import dlib
 7 from skimage import io
 8 
 9 # 使用Dlib的正面人脸检测器frontal_face_detector
10 detector = dlib.get_frontal_face_detector()
11 
12 # 图片所在路径
13 path_pic = "F:/code/python/P_dlib_face/pic/"
14 img = io.imread(path_pic+"img_faces_2.jpg")
15 
16 # 生成dlib的图像窗口
17 win = dlib.image_window()
18 win.set_image(img)
19 
20 # 使用detector检测器来检测图像中的人脸
21 dets = detector(img, 1)
22 print("人脸数:", len(dets))
23 
24 for i, d in enumerate(dets):
25     print("", i+1, "个人脸的矩形框坐标:",
26           "left:", d.left(), "right:", d.right(), "top:", d.top(), "bottom:", d.bottom())
27 
28 
29 # 绘制矩阵轮廓
30 win.add_overlay(dets)
31 
32 # 保持图像
33 dlib.hit_enter_to_continue()

 

 

 

    图2 参数d.top(), d.right(), d.left(), d.bottom() 位置坐标说明

 

  path就是你图像文件存放的位置,path+img 就是一个图像文件完整的访问路径;

  比如:

    path="F:/photos/"

    img="test_1.jpg" 

结果

  生成的图片窗口结果:

图3 face_detector.py 的输出结果(单张人脸)

     

  输出结果:   

1 人脸数: 1
2 第 0 个人脸: left: 79 right: 154 top: 47 bottom: 121
3 Hit enter to continue

  

 对于多个人脸的检测结果:

图4 face_detector.py 的输出结果(多张人脸)

 

2.2 face_landmark_detection.py

  官网给的  face_landmark_detection.py

(link: http://dlib.net/face_landmark_detection.py.html)

  1 #!/usr/bin/python
  2 # The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
  3 #
  4 #   This example program shows how to find frontal human faces in an image and
  5 #   estimate their pose.  The pose takes the form of 68 landmarks.  These are
  6 #   points on the face such as the corners of the mouth, along the eyebrows, on
  7 #   the eyes, and so forth.
  8 #
  9 #   The face detector we use is made using the classic Histogram of Oriented
 10 #   Gradients (HOG) feature combined with a linear classifier, an image pyramid,
 11 #   and sliding window detection scheme.  The pose estimator was created by
 12 #   using dlib's implementation of the paper:
 13 #      One Millisecond Face Alignment with an Ensemble of Regression Trees by
 14 #      Vahid Kazemi and Josephine Sullivan, CVPR 2014
 15 #   and was trained on the iBUG 300-W face landmark dataset (see
 16 #   https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/):  
 17 #      C. Sagonas, E. Antonakos, G, Tzimiropoulos, S. Zafeiriou, M. Pantic. 
 18 #      300 faces In-the-wild challenge: Database and results. 
 19 #      Image and Vision Computing (IMAVIS), Special Issue on Facial Landmark Localisation "In-The-Wild". 2016.
 20 #   You can get the trained model file from:
 21 #   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2.
 22 #   Note that the license for the iBUG 300-W dataset excludes commercial use.
 23 #   So you should contact Imperial College London to find out if it's OK for
 24 #   you to use this model file in a commercial product.
 25 #
 26 #
 27 #   Also, note that you can train your own models using dlib's machine learning
 28 #   tools. See train_shape_predictor.py to see an example.
 29 #
 30 #
 31 # COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
 32 #   You can install dlib using the command:
 33 #       pip install dlib
 34 #
 35 #   Alternatively, if you want to compile dlib yourself then go into the dlib
 36 #   root folder and run:
 37 #       python setup.py install
 38 #   or
 39 #       python setup.py install --yes USE_AVX_INSTRUCTIONS
 40 #   if you have a CPU that supports AVX instructions, since this makes some
 41 #   things run faster.  
 42 #
 43 #   Compiling dlib should work on any operating system so long as you have
 44 #   CMake installed.  On Ubuntu, this can be done easily by running the
 45 #   command:
 46 #       sudo apt-get install cmake
 47 #
 48 #   Also note that this example requires scikit-image which can be installed
 49 #   via the command:
 50 #       pip install scikit-image
 51 #   Or downloaded from http://scikit-image.org/download.html. 
 52 
 53 import sys
 54 import os
 55 import dlib
 56 import glob
 57 from skimage import io
 58 
 59 if len(sys.argv) != 3:
 60     print(
 61         "Give the path to the trained shape predictor model as the first "
 62         "argument and then the directory containing the facial images.\n"
 63         "For example, if you are in the python_examples folder then "
 64         "execute this program by running:\n"
 65         "    ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat ../examples/faces\n"
 66         "You can download a trained facial shape predictor from:\n"
 67         "    http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2")
 68     exit()
 69 
 70 predictor_path = sys.argv[1]
 71 faces_folder_path = sys.argv[2]
 72 
 73 detector = dlib.get_frontal_face_detector()
 74 predictor = dlib.shape_predictor(predictor_path)
 75 win = dlib.image_window()
 76 
 77 for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
 78     print("Processing file: {}".format(f))
 79     img = io.imread(f)
 80 
 81     win.clear_overlay()
 82     win.set_image(img)
 83 
 84     # Ask the detector to find the bounding boxes of each face. The 1 in the
 85     # second argument indicates that we should upsample the image 1 time. This
 86     # will make everything bigger and allow us to detect more faces.
 87     dets = detector(img, 1)
 88     print("Number of faces detected: {}".format(len(dets)))
 89     for k, d in enumerate(dets):
 90         print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
 91             k, d.left(), d.top(), d.right(), d.bottom()))
 92         # Get the landmarks/parts for the face in box d.
 93         shape = predictor(img, d)
 94         print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
 95                                                   shape.part(1)))
 96         # Draw the face landmarks on the screen.
 97         win.add_overlay(shape)
 98 
 99     win.add_overlay(dets)
100     dlib.hit_enter_to_continue()

 

 修改之后:

绘制两个overlay,矩阵框 和 面部特征框 

 1 # created at 2017-11-27
 2 # updated at 2018-05-14
 3 # by TimeStamp
 4 # cnblogs: http://www.cnblogs.com/AdaminXie/
 5 
 6 import dlib
 7 from skimage import io
 8 
 9 # 使用Dlib的正面人脸检测器frontal_face_detector
10 detector = dlib.get_frontal_face_detector()
11 
12 # dlib的68点模型
13 path_68points_model = "F:/code/python/P_dlib_face/"
14 predictor = dlib.shape_predictor(path_68points_model+"shape_predictor_68_face_landmarks.dat")
15 
16 # 图片所在路径
17 path_pic = "F:/code/python/P_dlib_face/pic/"
18 img = io.imread(path_pic+"test_faces_1.jpg")
19 
20 # 生成dlib的图像窗口
21 win = dlib.image_window()
22 win.set_image(img)
23 
24 # 使用detector检测器来检测图像中的人脸
25 dets = detector(img, 1)
26 print("人脸数:", len(dets))
27 
28 for i, d in enumerate(dets):
29     print("", i+1, "个人脸的矩形框坐标:",
30           "left:", d.left(), "right:", d.right(), "top:", d.top(), "bottom:", d.bottom())
31 
32     # 使用predictor来计算面部轮廓
33     shape = predictor(img, dets[i])
34     # 绘制面部轮廓
35     win.add_overlay(shape)
36 
37 # 绘制矩阵轮廓
38 win.add_overlay(dets)
39 
40 # 保持图像
41 dlib.hit_enter_to_continue()

 

 输出结果:

人脸数: 11 个人脸的矩形框坐标: left: 63 right: 384 top: 206 bottom: 527
Hit enter to continue

 

  红色的是绘制的矩形框  win.add_overlay(dets)

 蓝色的是绘制的面部轮廓  win.add_overlay(shape)

图5 face_landmark_detection.py 的输出结果(单张人脸)

 

  对于多张人脸的检测结果:

  图6 face_landmark_detection.py 的输出结果(多张人脸)

  

 

 

 

(官网例程中是利用 sys.argv[] 读取命令行输入,如果对于 sys.argv[] 有疑惑,可以参照下面的总结,不过还是建议,直接在代码中指定图像路径,不要在命令行下操作) 

* 关于sys.argv[]的使用:

  ( 如果对于代码中 sys.argv[] 的使用不了解可以参考这里 )

  用来获取cmd命令行参数,例如 获取cmd命令输入“python test.py XXXXX” 的XXXXX参数,可以用于cmd下读取用户输入的文件路径;

  如果不明白可以在python代码内直接 img = imread("F:/*****/test.jpg") 代替 img = imread(sys.argv[1]) 读取图片;

 

   用代码实例来帮助理解:

  1.(sys.argv[0],指的是代码文件本身在的路径)

   test1.py:

1  import sys
2  a=sys.argv[0]
3  print(a)

 

cmd input:

 python test1.py

 

  cmd output:

test1.py

 

  2.(sys.argv[1],cmd输入获取的参数字符串中,第一个字符)

   test2.py:

1 import sys
2 a=sys.argv[1]
3 print(a) 

 

 cmd input:

python test2.py what is your name

 

 cmd output: 

what

 

3.(sys.argv[1:],cmd输入获取的参数字符串中,从第一个字符开始到结束

 test3.py:

1 import sys
2 a=sys.argv[1:]
3 print(a) 

 

   cmd input: 

python test3.py what is your name

 

 cmd output: 

[“what”,“is”,“your”,“name”]

 

3.(sys.argv[2],cmd输入获取的参数字符串中,第二个字符)

 test4.py:

1 import sys
2 a=sys.argv[2]
3 print(a) 

 

 cmd input:

python test4.py what is your name  

 

  cmd output:

"is"

 

 

# 请尊重他人劳动成果,转载或者使用源码请注明出处:http://www.cnblogs.com/AdaminXie

# 如有问题请留言或者联系邮箱 coneypo@foxmail.com

posted @ 2017-11-27 20:20 coneypo 阅读(...) 评论(...) 编辑 收藏