Combat | Dlib learn how to use the fastest speed the development of face recognition?

GitHub project Address:
Combat | Dlib learn how to use the fastest speed the development of face recognition?
https://github.com/xiaosongshine/dlib_face_recognition

1. Background

Dlib is a deep learning open source tool, based on C ++ development, also supports Python development interface, with functions similar to TensorFlow PyTorch. However, due to Dlib for facial feature extraction support is very good, there are a lot of trained facial feature extraction model for developers to use, so it is suitable for the development of face recognition Dlib man face project development.
The development of the above mentioned recognition, mainly refers to the face verification, is to enter the two face pictures, the system will compare the output of 0 or 1, on behalf determine whether the same person. General face recognition can be simply divided to develop facial features 1. 2. Modeling and facial feature verification model (in fact, also including face alignment, etc., which can also be divided into 1). When using Dlib development, we can use directly trained facial feature extraction model, the main work becomes how to verify the person's face.
Face verification is actually calculating the similarity, the same person will be big similarity, different people will be relatively small. It may be employed Euclidean distance or cosine similarity calculated similarity. Wherein the angle cosine similarity is calculated, it refers to the Euclidean distance squared difference. It can be used to represent the similarity between two features (distance).

2. environment to build

Installation can refer to my blog post: [deep learning tool] · Minimalist installation Dlib face recognition library, said the following about the point to note ::
This blog post for Windows10 installation, other platforms can follow the steps to install
install Miniconda

Use conda instructions to install Dlib library, and Anaconda can use Miniconda, I used Miniconda, simple small memory.
Recommended Tsinghua source, download, install, choose the right platform version. python == 3.6
installation dlib

注意一定要以管理员身份进入CMD,执行(如果是Linux Mac 就使用 sudo)

conda install -c conda-forge dlib
需要imageio 库,可以使用下述命令安装
conda install imageio

3.开发实战

1.实现人脸检测标记

face_test.py
import dlib
from imageio import imread
import glob
detector = dlib.get_frontal_face_detector()
win = dlib.image_window()
path = "f1.jpg"
img = imread(path)
dets = detector(img)
print('检测到了 %d 个人脸' % len(dets))
for i, d in enumerate(dets):
print('- %d:Left %d Top %d Right %d Bottom %d' % (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()

代码很简单,通过imread读取照片,然后进行检测,输出结果为dets的list,有几张人脸就会有几个item, 每个item都有.left(), .top(), .right(), .bottom()四个元素,代表人脸框的四个边界位置。最后通过win.add_overlay(dets)可以将标记的框显示在原图上。
原始照片
Combat | Dlib learn how to use the fastest speed the development of face recognition?

输出照片
Combat | Dlib learn how to use the fastest speed the development of face recognition?

其实我们就可以使用这个功能做一个简单的应用,用来检测图片或者视频中人脸的个数。

2.人脸特征点提取

在实战1的基础上添加人脸特征提取功能。

import dlib
from imageio import imread
import glob
detector = dlib.get_frontal_face_detector()
win = dlib.image_window()
predictor_path = 'shape_predictor_68_face_landmarks.dat'
predictor = dlib.shape_predictor(predictor_path)
path = "f2.jpg"
img = imread(path)
dets = detector(img)
print('检测到了 %d 个人脸' % len(dets))
for i, d in enumerate(dets):
print('- %d: Left %d Top %d Right %d Bottom %d' % (i, d.left(), d.top(), d.right(), d.bottom()))
shape = predictor(img, d)
# 第 0 个点和第 1 个点的坐标
print('Part 0: {}, Part 1: {}'.format(shape.part(0), shape.part(1)))
win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
win.add_overlay(shape)
dlib.hit_enter_to_continue()

这段代码就是在test.py基础上加入了shape_predictor功能,使之可以在检测出人脸基础上,找到人脸的68个特征点。反映在图中就是蓝色的线。
原始图片
Combat | Dlib learn how to use the fastest speed the development of face recognition?

输出图片
Combat | Dlib learn how to use the fastest speed the development of face recognition?

注意运行这段代码需要这个文件predictor_path = 'shape_predictor_68_face_landmarks.dat',我会放在我的github中,方便大家下载使用。

3.人脸识别验证

在第二步的基础上,我们再进一步,实现将人脸提取为特征向量,从而我们就可以对特征向量进行比对来实现人脸的验证,这里采用的是对比欧式距离的方法。

face_recognition.py
import dlib
from imageio import imread
import glob
import numpy as np
detector = dlib.get_frontal_face_detector()
predictor_path = 'shape_predictor_68_face_landmarks.dat'
predictor = dlib.shape_predictor(predictor_path)
face_rec_model_path = 'dlib_face_recognition_resnet_model_v1.dat'
facerec = dlib.face_recognition_model_v1(face_rec_model_path)
def get_feature(path):
img = imread(path)
dets = detector(img)
print('检测到了 %d 个人脸' % len(dets))
# 这里假设每张图只有一个人脸
shape = predictor(img, dets[0])
face_vector = facerec.compute_face_descriptor(img, shape)
return(face_vector)
def distance(a,b):
a,b = np.array(a), np.array(b)
sub = np.sum((a-b)**2)
add = (np.sum(a**2)+np.sum(b**2))/2.
return sub/add
path_lists1 = ["f1.jpg","f2.jpg"]
path_lists2 = ["赵丽颖照片.jpg","赵丽颖测试.jpg"]
feature_lists1 = [get_feature(path) for path in path_lists1]
feature_lists2 = [get_feature(path) for path in path_lists2]
print("feature 1 shape",feature_lists1[0].shape)
out1 = distance(feature_lists1[0],feature_lists1[1])
out2 = distance(feature_lists2[0],feature_lists2[1])
print("diff distance is",out1)
print("same distance is",out2)
out1 = distance(feature_lists1[0],feature_lists1[1])
out2 = distance(feature_lists2[0],feature_lists2[1])

输出结果

检测到了 1 个人脸
检测到了 1 个人脸
检测到了 1 个人脸
检测到了 1 个人脸
feature 1 shape (128, 1)
diff distance is 0.254767715912
same distance is 0.0620976363391

We can see that, for each human face is extracted 128-dimensional vector, we can be understood as 128-dimensional coordinates (xyz three-dimensional, dimension 128 is composed of shaft 128), we need to do it is to calculate the following two distance feature is set up an appropriate threshold value smaller than this threshold value is identified as the same person. This file is required to run the code correctly face_rec_model_path = 'dlib_face_recognition_resnet_model_v1.dat', I have put my own github ( in https://github.com/xiaosongshine/dlib_face_recognition), to facilitate the use .
We can see from the results of the above tests, different distances of 0.25, 0.06 for the same person, you can set the threshold to a value between. I am here to set to 0.09, this threshold also requires a large amount of data to calculate, guidelines for the selection of misidentified as a minimum.
Now we set the threshold 0.09, the test system can distinguish between different persons: add the following code face_recognition.py

def classifier(a,b,t = 0.09):
if(distance(a,b)<=t):
 ret = True
else :
 ret = False
return(ret)
print("f1 is 赵丽颖",classifier(feature_lists1[0],feature_lists2[1]))
print("f2 is 赵丽颖",classifier(feature_lists1[1],feature_lists2[1]))
print("赵丽颖照片.jpg is 赵丽颖测试.jpg",classifier(feature_lists2[0],feature_lists2[1]))

Output

f1 is 赵丽颖 False
f2 is 赵丽颖 False
赵丽颖照片.jpg is 赵丽颖测试.jpg True

As can be seen from the above, basically meet the functional distinction between the human face, as if you need to continue to tune practical optimization criteria to the code threshold value, the tuning is to choose a suitable threshold so as to identify the minimum error.

Want more information on artificial intelligence
can be added to V ,, letter: hcgx0904 (Remarks "artificial intelligence")
click on the "Deep Learning & computer vision succinctly" , start learning! ! !

Guess you like

Origin blog.51cto.com/14352303/2412881