Python利用Dlib库进行人脸识别

  0.引言

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

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

  1.开发环境

  python:  3.6.3

  dlib:    19.7

2.py文件功能介绍

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

  face_landmark_detection.py :  在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 and boost-python installed.  On Ubuntu, this can be done easily by
37 #   running the command:
38 #       sudo apt-get install libboost-python-dev 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 import sys
 2
 3 import dlib
 4 from skimage import io
 5
 6 #使用dlib自带的frontal_face_detector()函数作为特征提取器
 7 detector = dlib.get_frontal_face_detector()
 8
 9 #使用dlib的图片窗口
10 win = dlib.image_window()
11
12 #sys.argv用来获取命令行参数,[0]表示代码本身文件路径,参数1开始向后依次是获取图片路径
13 for f in sys.argv[1:]:
14     #输出目前处理的图片地址
15     print("Processing file: {}".format(f))
16     #使用skimage的io读取图片
17     img = io.imread(f)
18      #使用detector进行人脸检测,dets为人脸个数
19     dets = detector(img, 1)
20     print("Number of faces detected: {}".format(len(dets)))
21     for i, d in enumerate(dets):
22         print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
23         i, d.left(), d.top(), d.right(), d.bottom()))
24
25     #绘制图片
26     win.clear_overlay()
27     win.set_image(img)
28     win.add_overlay(dets)
29     dlib.hit_enter_to_continue()
30
31
32 # Finally, if you really want to you can ask the detector to tell you the score
33 # for each detection.  The score is bigger for more confident detections.
34 # The third argument to run is an optional adjustment to the detection threshold,
35 # where a negative value will return more detections and a positive value fewer.
36 # Also, the idx tells you which of the face sub-detectors matched.  This can be
37 # used to broadly identify faces in different orientations.
38 if (len(sys.argv[1:]) > 0):
39     img = io.imread(sys.argv[1])
40     dets, scores, idx = detector.run(img, 1, -1)
41     for i, d in enumerate(dets):
42         print("Detection {}, score: {}, face_type:{}".format(d, scores[i], idx[i]))

打开cmd命令提示符,cd到face_detector.py所在目录,然后输入

python face_detector.py test.jpg

对test.jpg进行人脸检测,test.jpg需要和py文件放在同一目录下;  

结果

  图片窗口结果:

    cmd输出结果: 

1 F:\code\******\face_test>python face_detector.py test.jpg
2 Processing file: test.jpg
3 Number of faces detected: 1
4 Detection 0: Left: 79 Top: 47 Right: 154 Bottom: 121
5 Hit enter to continue
6 Detection [(79, 47) (154, 121)], score: 2.5174034275544996, face_type:0.0

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

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

修改:

 1 import sys
 2 import os
 3 import dlib
 4 import glob
 5 from skimage import io
 6
 7 detector = dlib.get_frontal_face_detector()
 8
 9 #使用预测器,此处为预测器的路径,预测器在下载的example文件夹里面,  *****修改此处****
10 predictor = dlib.shape_predictor("F:/code/******/shape_predictor_68_face_landmarks.dat")
11
12 #使用dlib的图片窗口
13 win = dlib.image_window()
14
15 #1.读取图片test2.jpg的路径,    ******修改此处*****
16 img = io.imread("F:/code/*****/test2.jpg")
17
18 #2.或者还是利用cmd参数输入读取路径:
19 #img=io.imread(sys.argv[1])
20
21 win.clear_overlay()
22 win.set_image(img)
23
24 dets = detector(img, 1)
25 print("Number of faces detected: {}".format(len(dets)))
26
27 for k, d in enumerate(dets):
28         print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
29             k, d.left(), d.top(), d.right(), d.bottom()))
30         # Get the landmarks/parts for the face in box d.
31         shape = predictor(img, d)
32         print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
33                                                   shape.part(1)))
34         # Draw the face landmarks on the screen.
35         win.add_overlay(shape)
36
37 win.add_overlay(dets)
38 dlib.hit_enter_to_continue()

结果

cmd输出:

  可以看到Number of faces detected:1,即检测到人脸数为1

1 F:\code\python\test2017110601\python_examples\face_test>python face_landmark_detection.py
2 Number of faces detected: 1
3 Detection 0: Left: 200 Top: 142 Right: 468 Bottom: 409
4 Part 0: (195, 233), Part 1: (197, 267) ...
5 Hit enter to continue

图片窗口结果:

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

  

* 关于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"
时间: 2024-10-08 19:10:02

Python利用Dlib库进行人脸识别的相关文章

python中使用Opencv进行人脸识别

上一节讲到人脸检测,现在讲一下人脸识别.具体是通过程序采集图像并进行训练,并且基于这些训练的图像对人脸进行动态识别. 人脸识别前所需要的人脸库可以通过两种方式获得:1.自己从视频获取图像   2.从人脸数据库免费获得可用人脸图像,如ORL人脸库(包含40个人每人10张人脸,总共400张人脸),ORL人脸库中的每一张图像大小为92x112.若要对这些样本进行人脸识别必须要在包含人脸的样本图像上进行人脸识别.这里提供自己准备图像识别出自己的方法. 1.采集人脸信息:通过摄像头采集人脸信息,10张以上

Python丨调用百度的人脸识别api给你的颜值打个分

需要用到的工具 百度的人脸识别api Flask PIL requests 主要思路 利用的百度的人脸识别库,然后自己做了一个简单的图片上传和图片处理以及信息提取加工. 官网给的方法相对比较繁琐,我使用request改写了一下如下(注意把url里面的Key换成你申请的). 首先,我们不采用图片url的方式,我们直接使用对图片进行编码的形式进行处理.编码的过程如下: 请求的参数构造如下: 其中image是我们上面编码过的结果,imageType是,face_field是我们想要它返回给我们的内容,

python利用selenium库识别点触验证码

利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路) 一.超级鹰注册:超级鹰入口 1.首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分,基本上就够学习使用了.如果想一直用可以用,可以充值,不是很贵. 2.下载超级鹰的python库代码.代码 3.然后有测试案例,自己可以试着跑一跑代码. 二.使用selenium库来识别点触式验证码: 1.首先是找一个使用点触式二维码的网站:(这个真的是比较难找了,由于静谧大大书上的网站被封了,我找

python下PCA算法与人脸识别

关于这部分主要是想在python下试验一下主成分分析(PCA)算法以及简单的人脸识别.曾经详述过matlab下的PCA以及SVM算法进行人脸识别技术,参考如下: 主成分分析法-简单人脸识别(一) 主成分分析-简单人脸识别(二) PCA实验人脸库-人脸识别(四) PCA+支持向量机-人脸识别(五) 主成分分析(PCA)算法主要是对高维数据进行降维,最大限度的找到数据间的相互关系,在机器学习.数据挖掘上很有用.在机器学习领域算法众多,贴一个: 大神博客索引 关于PCA的核心思想与原理介绍上述已经给出

【人工智能】用Python实现一个简单的人脸识别,原来我和这个明星如此相似

近几年来,兴起了一股人工智能热潮,让人们见到了AI的能力和强大,比如图像识别,语音识别,机器翻译,无人驾驶等等.总体来说,AI的门槛还是比较高,不仅要学会使用框架实现,更重要的是,需要有一定的数学基础,如线性代数,矩阵,微积分等. 幸庆的是,国内外许多大神都已经给我们造好"轮子",我们可以直接来使用某些模型.今天就和大家交流下如何实现一个简易版的人脸对比,非常有趣!我们都知道Python容易学,但是就是不知道如何去学,去×××资料,机器学习,人工智能,深度学习,都在这学习,小编推荐一个

python 利用PIL库进行更改图片大小的操作

python 是可以利用PIL库进行更改图片大小的操作的,当然一般情况下是不需要的,但是在一些特殊的利用场合,是需要改变图片的灰度或是大小等的操作的,其实用python更改图片的大小还是蛮简单的,只需要几行代码,有一点可能刚入门的小伙伴们可能不知道PIL库,PIL是一个库的简写,他的真名叫做pillow,因此,需要pip install pillow 用anaconda的话是conda install pillow千万不要pip/conda install PIL咯,下面贴出代码,希望对一些小伙

利用Emgu.CV实现人脸识别详解 (C#)--附源码

 本文由:姚磊岳([email protected])于2016-6-9撰,转载请保留作者信息. 最近需要实现一个基于生理特征的门禁准入系统,其中一个环节就是要用到人脸识别.虽说人脸识别的算法已经非常成熟,网络上也有很多专业的介绍,但从零做起工作量还是太大,所以想找一些现成的控件.上网一搜,还真发现了不少:如果是基于移动端的android开发,科大讯飞的人脸识别插件应该是个不错的选择:百度也提供人脸识别的插件(没深入看,不知道要不要收费).对比再三,发现如果是要做C/S程序,精度又不是要求非

【Python】使用Face++的人脸识别detect API进行本地图片情绪识别并存入excel

准备工作 首先,需要在Face++的主页注册一个账号,在控制台去获取API Key和API Secret. 然后在本地文件夹准备好要进行情绪识别的图片/相片. 代码 介绍下所使用的第三方库 --urllib2是使用各种协议完成打开url的一个库 --time是对时间进行处理的一个库,以下代码中其实就使用了sleep()和localtime()两个函数,sleep()是用来让程序暂停几秒的,localtime()是格式化时间戳为本地的时间 --xlwt是对excel进行写入操作的一个库 --os是

python利用PIL库使图片高斯模糊

一.安装PIL PIL是Python Imaging Library简称,用于处理图片.PIL中已经有图片高斯模糊处理类,但有个bug(目前最新的1.1.7bug还存在),就是模糊半径写死的是2,不能设置.在源码ImageFilter.py的第160行: 所以,我们在这里自己改一下就OK了. 项目地址:http://www.pythonware.com/products/pil/ 二.修改后的代码 代码如下: #-*- coding: utf-8 -*- from PIL import Imag