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 源碼:
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 代碼處理過程如下:
圖 2 face_detector 代碼處理過程
為了方便理解,修改增加注釋之后的 face_detector_v1.py
1 # created at 2017-11-27
2 # updated at 2018-09-06
3
4 # Author: coneypo
5 # Dlib: http://dlib.net/
6 # Blog: http://www.cnblogs.com/AdaminXie/
7 # Github: https://github.com/coneypo/Dlib_examples
8
9 import dlib 10 from skimage import io 11
12 # 使用 Dlib 的正面人臉檢測器 frontal_face_detector
13 detector = dlib.get_frontal_face_detector() 14
15 # 圖片所在路徑
16 img = io.imread("../imgs/faces_2.jpeg") 17
18 # 生成 Dlib 的圖像窗口
19 win = dlib.image_window() 20 win.set_image(img) 21
22 # 使用detector檢測器來檢測圖像中的人臉
23 faces = detector(img, 1) 24 print(type(faces[0]), '\n') 25
26 print("人臉數 / faces in all:", len(faces)) 27
28 for i, d in enumerate(faces): 29 print("第", i+1, "個人臉的矩形框坐標:", 30 "left:", d.left(), '\t', "right:", d.right(), '\t', "top:", d.top(),'\t', "bottom:", d.bottom()) 31
32 # 繪制矩陣輪廓
33 win.add_overlay(faces) 34
35 # 保持圖像
36 dlib.hit_enter_to_continue()

圖 3 參數 d.top(), d.right(), d.left(), d.bottom() 位置坐標說明
結果:
生成的圖片窗口結果:
圖 4 face_detector.py 的輸出結果(單張人臉)
輸出結果:
人臉數 / faces in all: 1 第 1 個人臉的矩形框坐標: left: 103 right: 211 top: 44 bottom: 152 Hit enter to continue
對於多個人臉的檢測結果:
圖 5 face_detector.py 的輸出結果(多張人臉)
但是我們進行圖像處理的時候,經常是用 OpenCv 進行處理,所以我們在下面代碼中,使用 OpenCv 的對象( 矩形框用 cv2.rectangle() 函數繪制 ):
face_detector_v2_use_opencv.py :
1 # created at 2017-11-27
2 # updated at 2018-09-06
3
4 # Author: coneypo
5 # Dlib: http://dlib.net/
6 # Blog: http://www.cnblogs.com/AdaminXie/
7 # Github: https://github.com/coneypo/Dlib_examples
8
9 # create object of OpenCv
10 # use OpenCv to read and show images
11
12 import dlib 13 import cv2 14
15 # 使用 Dlib 的正面人臉檢測器 frontal_face_detector
16 detector = dlib.get_frontal_face_detector() 17
18 # 圖片所在路徑
19 # read image
20 img = cv2.imread("imgs/faces_2.jpeg") 21
22 # 使用 detector 檢測器來檢測圖像中的人臉
23 # use detector of Dlib to detector faces
24 faces = detector(img, 1) 25 print("人臉數 / Faces in all: ", len(faces)) 26
27 # Traversal every face
28 for i, d in enumerate(faces): 29 print("第", i+1, "個人臉的矩形框坐標:", 30 "left:", d.left(), "right:", d.right(), "top:", d.top(), "bottom:", d.bottom()) 31 cv2.rectangle(img, tuple([d.left(), d.top()]), tuple([d.right(), d.bottom()]), (0, 255, 255), 2) 32
33 cv2.namedWindow("img", 2) 34 cv2.imshow("img", img) 35 cv2.waitKey(0)
2.2 face_landmark_detection.py
官網 face_landmark_detection.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 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()
代碼處理流程:
圖 6 face_landmark_detection 處理流程
會繪制兩個 overlay,人臉外接矩陣框 和 面部特征框 ;
紅色的是繪制的 人臉矩形框 win.add_overlay(dets)
藍色的是繪制的 人臉面部輪廓 win.add_overlay(shape)
我們不用命令行讀取,簡化代碼,直接在代碼內指定圖像文件地址:
face_landmark_detection_v1.py:
1 # created at 2017-11-27
2 # updated at 2018-09-06
3
4 # Author: coneypo
5 # Dlib: http://dlib.net/
6 # Blog: http://www.cnblogs.com/AdaminXie/
7 # Github: https://github.com/coneypo/Dlib_examples
8
9 import dlib 10 from skimage import io 11
12 # 使用 Dlib 的正面人臉檢測器 frontal_face_detector
13 detector = dlib.get_frontal_face_detector() 14
15 # Dlib 的 68點模型
16 predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") 17
18 # 圖片所在路徑
19 img = io.imread("imgs/faces_2.jpeg") 20
21 # 生成 Dlib 的圖像窗口
22 win = dlib.image_window() 23 win.set_image(img) 24
25 # 使用 detector 檢測器來檢測圖像中的人臉
26 faces = detector(img, 1) 27 print("人臉數:", len(faces)) 28
29 for i, d in enumerate(faces): 30 print("第", i+1, "個人臉的矩形框坐標:", 31 "left:", d.left(), "right:", d.right(), "top:", d.top(), "bottom:", d.bottom()) 32
33 # 使用predictor來計算面部輪廓
34 shape = predictor(img, faces[i]) 35 # 繪制面部輪廓
36 win.add_overlay(shape) 37
38 # 繪制矩陣輪廓
39 win.add_overlay(faces) 40
41 # 保持圖像
42 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