python dlib学习(五):比对人脸

前言
在前面的博客中介绍了,如何使用dlib标定人脸(python dlib学习(一):人脸检测),提取68个特征点(python dlib学习(二):人脸特征点标定)。这次要在这两个工作的基础之上,将人脸的信息提取成一个128维的向量空间。在这个向量空间上,同一个人脸的更接近,不同人脸的距离更远。度量采用欧式距离,欧氏距离计算不算复杂。
二维情况下:
distance=(x1−x2)2+(y1−y2)2−−−−−−−−−−−−−−−−−−√
distance=(x1−x2)2+(y1−y2)2

三维情况下:
distance=(x1−x2)2+(y1−y2)2+(z1−z2)2−−−−−−−−−−−−−−−−−−−−−−−−−−−−√
distance=(x1−x2)2+(y1−y2)2+(z1−z2)2

将其扩展到128维的情况下即可。
通常使用的判别阈值是0.6,即如果两个人脸的向量空间的欧式距离超过了0.6,即认定不是同一个人;如果欧氏距离小于0.6,则认为是同一个人。这个距离也可以由自己定,只要效果能更好。
实验中使用了两个模型:

shape_predictor_68_face_landmarks.dat:
http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

dlib_face_recognition_resnet_model_v1.dat:
http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2

文件夹目录:

两个模型放在model文件夹中,测试图片放在faces中,图片自己随便下几张就行。

完整工程下载链接:
http://pan.baidu.com/s/1boCDZ7T

程序1
不说废话了,直接上代码。

# -*- coding: utf-8 -*-
import sys
import dlib
import cv2
import os
import glob

current_path = os.getcwd() # 获取当前路径
# 模型路径
predictor_path = current_path + "\\model\\shape_predictor_68_face_landmarks.dat"
face_rec_model_path = current_path + "\\model\\dlib_face_recognition_resnet_model_v1.dat"
#测试图片路径
faces_folder_path = current_path + "\\faces\\"

# 读入模型
detector = dlib.get_frontal_face_detector()
shape_predictor = dlib.shape_predictor(predictor_path)
face_rec_model = dlib.face_recognition_model_v1(face_rec_model_path)

for img_path in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(img_path))
# opencv 读取图片,并显示
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
# opencv的bgr格式图片转换成rgb格式
b, g, r = cv2.split(img)
img2 = cv2.merge([r, g, b])

dets = detector(img, 1) # 人脸标定
print("Number of faces detected: {}".format(len(dets)))

for index, face in enumerate(dets):
print(‘face {}; left {}; top {}; right {}; bottom {}‘.format(index, face.left(), face.top(), face.right(), face.bottom()))

shape = shape_predictor(img2, face) # 提取68个特征点
for i, pt in enumerate(shape.parts()):
#print(‘Part {}: {}‘.format(i, pt))
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 2, (255, 0, 0), 1)
#print(type(pt))
#print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
cv2.namedWindow(img_path+str(index), cv2.WINDOW_AUTOSIZE)
cv2.imshow(img_path+str(index), img)

face_descriptor = face_rec_model.compute_face_descriptor(img2, shape) # 计算人脸的128维的向量
print(face_descriptor)

k = cv2.waitKey(0)
cv2.destroyAllWindows()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
程序1结果

部分打印结果:

F:\Python\my_dlib_codes\face_recognition>python my_face_recogniton.py
Processing file: F:\Python\my_dlib_codes\face_recognition\faces\jobs.jpg
Number of faces detected: 1
face 0; left 184; top 64; right 339; bottom 219
-0.179784
0.15487
0.10509
-0.0973604
-0.19153
0.000418252
-0.0357536
-0.0206766
0.129741
-0.0628359
....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
后面的那一堆数字就是人脸在128维向量空间上的值。

程序2
前面只是测试了一下,把要用的值给求到了。这里我封装了一下,把比对功能实现了。没加多少东西,所以不做赘述了。

# -*- coding: utf-8 -*-
import sys
import dlib
import cv2
import os
import glob
import numpy as np

def comparePersonData(data1, data2):
diff = 0
# for v1, v2 in data1, data2:
# diff += (v1 - v2)**2
for i in xrange(len(data1)):
diff += (data1[i] - data2[i])**2
diff = np.sqrt(diff)
print diff
if(diff < 0.6):
print "It‘s the same person"
else:
print "It‘s not the same person"

def savePersonData(face_rec_class, face_descriptor):
if face_rec_class.name == None or face_descriptor == None:
return
filePath = face_rec_class.dataPath + face_rec_class.name + ‘.npy‘
vectors = np.array([])
for i, num in enumerate(face_descriptor):
vectors = np.append(vectors, num)
# print(num)
print(‘Saving files to :‘+filePath)
np.save(filePath, vectors)
return vectors

def loadPersonData(face_rec_class, personName):
if personName == None:
return
filePath = face_rec_class.dataPath + personName + ‘.npy‘
vectors = np.load(filePath)
print(vectors)
return vectors

class face_recognition(object):
def __init__(self):
self.current_path = os.getcwd() # 获取当前路径
self.predictor_path = self.current_path + "\\model\\shape_predictor_68_face_landmarks.dat"
self.face_rec_model_path = self.current_path + "\\model\\dlib_face_recognition_resnet_model_v1.dat"
self.faces_folder_path = self.current_path + "\\faces\\"
self.dataPath = self.current_path + "\\data\\"
self.detector = dlib.get_frontal_face_detector()
self.shape_predictor = dlib.shape_predictor(self.predictor_path)
self.face_rec_model = dlib.face_recognition_model_v1(self.face_rec_model_path)

self.name = None
self.img_bgr = None
self.img_rgb = None
self.detector = dlib.get_frontal_face_detector()
self.shape_predictor = dlib.shape_predictor(self.predictor_path)
self.face_rec_model = dlib.face_recognition_model_v1(self.face_rec_model_path)

def inputPerson(self, name=‘people‘, img_path=None):
if img_path == None:
print(‘No file!\n‘)
return

# img_name += self.faces_folder_path + img_name
self.name = name
self.img_bgr = cv2.imread(self.current_path+img_path)
# opencv的bgr格式图片转换成rgb格式
b, g, r = cv2.split(self.img_bgr)
self.img_rgb = cv2.merge([r, g, b])

def create128DVectorSpace(self):
dets = self.detector(self.img_rgb, 1)
print("Number of faces detected: {}".format(len(dets)))
for index, face in enumerate(dets):
print(‘face {}; left {}; top {}; right {}; bottom {}‘.format(index, face.left(), face.top(), face.right(), face.bottom()))

shape = self.shape_predictor(self.img_rgb, face)
face_descriptor = self.face_rec_model.compute_face_descriptor(self.img_rgb, shape)
# print(face_descriptor)
# for i, num in enumerate(face_descriptor):
# print(num)
# print(type(num))

return face_descriptor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
程序2结果
测试代码1:

import face_rec as fc
face_rec = fc.face_recognition() # 创建对象
face_rec.inputPerson(name=‘jobs‘, img_path=‘\\faces\\jobs.jpg‘) # name中写第一个人名字,img_name为图片名字,注意要放在faces文件夹中
vector = face_rec.create128DVectorSpace() # 提取128维向量,是dlib.vector类的对象
person_data1 = fc.savePersonData(face_rec, vector ) # 将提取出的数据保存到data文件夹,为便于操作返回numpy数组,内容还是一样的

# 导入第二张图片,并提取特征向量
face_rec.inputPerson(name=‘jobs2‘, img_path=‘\\faces\\jobs2.jpg‘)
vector = face_rec.create128DVectorSpace() # 提取128维向量,是dlib.vector类的对象
person_data2 = fc.savePersonData(face_rec, vector )

# 计算欧式距离,判断是否是同一个人
fc.comparePersonData(person_data1, person_data2)
1
2
3
4
5
6
7
8
9
10
11
12
13
如果data文件夹中已经有了模型文件,可以直接导入:

import face_rec as fc
face_rec = fc.face_recognition() # 创建对象
person_data1 = fc.loadPersonData(face_rec , ‘jobs‘) # 创建一个类保存相关信息,后面还要跟上人名,程序会在data文件中查找对应npy文件,比如这里就是‘jobs.npy‘
person_data2 = fc.loadPersonData(face_rec , ‘jobs2‘) # 导入第二张图片
fc.comparePersonData(person_data1, person_data2) # 计算欧式距离,判断是否是同一个人
1
2
3
4
5
程序2结果
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import face_rec as fc
>>> face_rec = fc.face_recognition()
>>> face_rec.inputPerson(name=‘jobs‘, img_path=‘\\faces\\jobs.jpg‘)
>>> vector = face_rec.create128DVectorSpace()
Number of faces detected: 1
face 0; left 184; top 64; right 339; bottom 219
>>> person_data1 = fc.savePersonData(face_rec, vector )
Saving files to :F:\Python\my_dlib_codes\face_recognition\data\jobs.npy
>>> face_rec.inputPerson(name=‘jobs2‘, img_path=‘\\faces\\jobs2.jpg‘)
>>> vector = face_rec.create128DVectorSpace()
Number of faces detected: 1
face 0; left 124; top 39; right 253; bottom 168
>>> person_data2 = fc.savePersonData(face_rec, vector )
Saving files to :F:\Python\my_dlib_codes\face_recognition\data\jobs2.npy
>>> fc.comparePersonData(person_data1, person_data2)
0.490491048429
It‘s the same person
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
官方例程
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
# This example shows how to use dlib‘s face recognition tool. This tool maps
# an image of a human face to a 128 dimensional vector space where images of
# the same person are near to each other and images from different people are
# far apart. Therefore, you can perform face recognition by mapping faces to
# the 128D space and then checking if their Euclidean distance is small
# enough.
#
# When using a distance threshold of 0.6, the dlib model obtains an accuracy
# of 99.38% on the standard LFW face recognition benchmark, which is
# comparable to other state-of-the-art methods for face recognition as of
# February 2017. This accuracy means that, when presented with a pair of face
# images, the tool will correctly identify if the pair belongs to the same
# person or is from different people 99.38% of the time.
#
# Finally, for an in-depth discussion of how dlib‘s tool works you should
# refer to the C++ example program dnn_face_recognition_ex.cpp and the
# attendant documentation referenced therein.
#
#
#
#
# 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. This code will also use CUDA if you have CUDA and cuDNN
# installed.
#
# 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) != 4:
print(
"Call this program like this:\n"
" ./face_recognition.py shape_predictor_68_face_landmarks.dat dlib_face_recognition_resnet_model_v1.dat ../examples/faces\n"
"You can download a trained facial shape predictor and recognition model from:\n"
" http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2\n"
" http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2")
exit()

predictor_path = sys.argv[1]
face_rec_model_path = sys.argv[2]
faces_folder_path = sys.argv[3]

# Load all the models we need: a detector to find the faces, a shape predictor
# to find face landmarks so we can precisely localize the face, and finally the
# face recognition model.
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)
facerec = dlib.face_recognition_model_v1(face_rec_model_path)

win = dlib.image_window()

# Now process all the images
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)))

# Now process each face we found.
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 = sp(img, d)
# Draw the face landmarks on the screen so we can see what face is currently being processed.
win.clear_overlay()
win.add_overlay(d)
win.add_overlay(shape)

# Compute the 128D vector that describes the face in img identified by
# shape. In general, if two face descriptor vectors have a Euclidean
# distance between them less than 0.6 then they are from the same
# person, otherwise they are from different people. Here we just print
# the vector to the screen.
face_descriptor = facerec.compute_face_descriptor(img, shape)
print(face_descriptor)
# It should also be noted that you can also call this function like this:
# face_descriptor = facerec.compute_face_descriptor(img, shape, 100)
# The version of the call without the 100 gets 99.13% accuracy on LFW
# while the version with 100 gets 99.38%. However, the 100 makes the
# call 100x slower to execute, so choose whatever version you like. To
# explain a little, the 3rd argument tells the code how many times to
# jitter/resample the image. When you set it to 100 it executes the
# face descriptor extraction 100 times on slightly modified versions of
# the face and returns the average result. You could also pick a more
# middle value, such as 10, which is only 10x slower but still gets an
# LFW accuracy of 99.3%.

dlib.hit_enter_to_continue()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
吐槽:
dlib的确很方便,不用花多少时间就能自己做到一些目标功能。官方文档讲的很详细,很容易入门。看这个文档(dlib python api)差不多就能学会用了。导师已经安排了研究生阶段的学习任务了,后面也要忙起来了。dlib的学习虽然是我10月份才开的坑,为了善始善终我也要尽快整理完这些东西。以后要回到”泡馆”生活了。

原文链接:https://blog.csdn.net/hongbin_xu/article/details/78390982

原文地址:https://www.cnblogs.com/Ph-one/p/12037471.html

时间: 2024-10-07 09:38:49

python dlib学习(五):比对人脸的相关文章

Python基础学习五

Python基础学习五 迭代 for x in 变量: 其中变量可以是字符串.列表.字典.集合. 当迭代字典时,通过字典的内置函数value()可以迭代出值:通过字典的内置函数items()可以迭代出键值对. for key in dict: #迭代键 for val in dict.value(): #迭代值 for k,v in dict.items(): #迭代键值对 当迭代列表时,通过内置函数enumerate()可以迭代出索引加值. for i in list #迭代列表 for i

Python Tutorial 学习(五)--Data Structures

5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它可不仅仅只有那么一点点,这里给出一个更详细一点的说明.来吧骚连,打开你的命令行窗口 >>>help(list) 看看会出来一些什么~~` list.append(x) 向一个序列里面追加元素 x a = [] a.append(x) # 假设x已经定义了 a[len(a):] = [x] l

Python scikit-learn 学习笔记—PCA+SVM人脸识别

人脸识别是一项实用的技术.但是这种技术总是感觉非常神秘,在sklearn中看到了人脸识别的example,代码网址如下: http://scikit-learn.org/0.13/auto_examples/applications/face_recognition.html#example-applications-face-recognition-py 首先介绍一些PCA和SVM的功能,PCA叫做主元分析,它可以从多元事物中解析出主要影响因素,揭示事物的本质,简化复杂的问题.计算主成分的目的

Python爬虫学习路线,强烈建议收藏这十一条

(一)如何学习Python 学习Python大致可以分为以下几个阶段: 1.刚上手的时候肯定是先过一遍Python最基本的知识,比如说:变量.数据结构.语法等,基础过的很快,基本上1~2周时间就能过完了,我当时是在这儿看的基础:Python 简介 | 菜鸟教程 2.看完基础后,就是做一些小项目巩固基础,比方说:做一个终端计算器,如果实在找不到什么练手项目,可以在 Codecademy - learn to code, interactively, for free 上面进行练习. 如果时间充裕的

《Python爬虫学习系列教程》学习笔记

转自:http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己实际写的一些小爬虫,在这里跟大家一同分享,希望对Python爬虫感兴趣的童鞋有帮助,如果有机会期待与大家的交流. 一.Python入门 1. Python爬虫入门一之综述 2. Python爬虫入门二之爬虫基础了解 3. Python爬虫入门三之Urllib库的基本使用 4. Python爬虫

Python爬虫学习系列教程

Python爬虫学习系列教程 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己实际写的一些小爬虫,在这里跟大家一同分享,希望对Python爬虫感兴趣的童鞋有帮助,如果有机会期待与大家的交流. Python版本:2.7 一.爬虫入门 1. Python爬虫入门一之综述 2. Python爬虫入门二之爬虫基础了解 3. Python爬虫入门三之Urllib库的基本使用 4. Python爬虫入门四之Urllib库

Python基础学习(十)

Python I/O模型 同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 概念说明 在进行解释之前,首先要说明几个概念: 用户空间和内核空间 进程切换 进程的阻塞 文件描述符 缓存 I/O 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方).操作系统的核心是内核,独立于普通的应用程序,可以访问受保护的内存空间,也有访问底层硬件

python基础学习09(核心编程第二版)部分

# -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #==================== __author__ = 'Administrator' #什么是函数 #就是引用,创建,使用 #例子 def foo(): print '233' foo() #返回与函数类型 def foo1():#是一个过程 print 'hello world!' foo1() foo

图解Python 【第五篇】:面向对象-类-初级基础篇

由于类的内容比较多,分为类-初级基础篇和类-进阶篇 类的内容总览图: 本节内容一览图: 今天只讲类的基础的面向对象的特性 前言总结介绍: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个"函数"供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实例用于调用被包装在类中的函数,对象是一个类的实例 实例(instance):一个对象的实例化实现. 标识(identity):每个对象的实例都需要一个可