学习Python3 Dlib19.7进行人脸面部识别

时间:2019-04-13
本文章向大家介绍学习Python3 Dlib19.7进行人脸面部识别,主要包括学习Python3 Dlib19.7进行人脸面部识别使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

0.引言

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

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

1.简介

python:  3.6.3

dlib:    19.7

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

dets = dlib.get_frontal_face_detector(img)

利用dlib的68点特征预测器,进行人脸 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

2.py文件功能介绍

face_detector.py :

识别出图片文件中一张或多张人脸,并用矩形框框出标识出人脸;

link: http://dlib.net/cnn_face_detector.py.html

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

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

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 and boost-python installed. On Ubuntu, this can be done easily by
#  running the command:
#    sudo apt-get install libboost-python-dev 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.py
import dlib
from skimage import io
# 使用特征提取器frontal_face_detector
detector = dlib.get_frontal_face_detector()
# path是图片所在路径
path = "F:/code/python/P_dlib_face/pic/"
img = io.imread(path+"1.jpg")
# 特征提取器的实例化
dets = detector(img)
print("人脸数:", len(dets))
# 输出人脸矩形的四个坐标点
for i, d in enumerate(dets):
  print("第", i, "个人脸d的坐标:",
     "left:", d.left(),
     "right:", d.right(),
     "top:", d.top(),
     "bottom:", d.bottom())
# 绘制图片
win = dlib.image_window()
# 清除覆盖
#win.clear_overlay()
win.set_image(img)
# 将生成的矩阵覆盖上
win.add_overlay(dets)
# 保持图像
dlib.hit_enter_to_continue()

对test.jpg进行人脸检测: 

结果:

图片窗口结果:

输出结果:   

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

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

2.2 face_landmark_detection.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 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 and boost-python installed. On Ubuntu, this can be done easily by
#  running the command:
#    sudo apt-get install libboost-python-dev 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()

修改:

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

import dlib
from skimage import io
# 使用特征提取器frontal_face_detector
detector = dlib.get_frontal_face_detector()
# dlib的68点模型
path_pre = "F:/code/python/P_dlib_face/"
predictor = dlib.shape_predictor(path_pre+"shape_predictor_68_face_landmarks.dat")
# 图片所在路径
path_pic = "F:/code/python/P_dlib_face/pic/"
img = io.imread(path_pic+"1.jpg")
# 生成dlib的图像窗口
win = dlib.image_window()
win.clear_overlay()
win.set_image(img)
# 特征提取器的实例化
dets = detector(img, 1)
print("人脸数:", len(dets))
for k, d in enumerate(dets):
    print("第", k, "个人脸d的坐标:",
       "left:", d.left(),
       "right:", d.right(),
       "top:", d.top(),
       "bottom:", d.bottom())
    # 利用预测器预测
    shape = predictor(img, d)
    # 绘制面部轮廓
    win.add_overlay(shape)
# 绘制矩阵轮廓
win.add_overlay(dets)
# 保持图像
dlib.hit_enter_to_continue()

结果:

人脸数: 1
第 0 个人脸d的坐标: left: 79 right: 154 top: 47 bottom: 121

图片窗口结果:

蓝色的是绘制的  win.add_overlay(shape)
红色的是绘制的  win.add_overlay(dets)

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

官网例程中是利用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:

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

cmd input:

python test1.py

cmd output:

test1.py

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

test2.py:

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

cmd input:

python test2.py what is your name

cmd output: 

what

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

test3.py:

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

cmd input: 

python test3.py what is your name

cmd output: 

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

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

test4.py:

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

cmd input:

python test4.py what is your name

cmd output:

"is"