[深度应用]·实战掌握Dlib人脸识别开发教程

[深度应用]·实战掌握Dlib人脸识别开发教程

我的网站--> http://www.yansongsong.cn/python

项目GitHub地址--> https://github.com/xiaosongshine/dlib_face_recognitiongit

1.背景介绍

Dlib是一个深度学习开源工具,基于C++开发,也支持Python开发接口,功能相似于TensorFlow与PyTorch。可是因为Dlib对于人脸特征提取支持很好,有不少训练好的人脸特征提取模型供开发者使用,因此Dlib人脸识别开发很适合作人脸项目开发。github

上面所说的人脸识别开发,主要是指人脸验证,就是输入两张人脸照片,系统会对比输出0或者1,表明判断是不是同一我的。通常的人脸识别开发能够简单分为1.人脸特征建模2.使用人脸特征模型进行验证(其实还应包括人脸对齐等,这些也能够划分到1中)。使用Dlib进行开发时,咱们直接可使用训练好的人脸特征提取模型,主要的工做就变成了如何进行人脸的验证。bash

人脸的验证其实就是计算类似度,同一我的的类似度就会大,不一样的人就会比较小。能够采用余弦类似度或者欧式距离来计算类似度。其中余弦类似度就是计算角度,欧式距离就是指平方差。均可以用来表示两个特征的类似度(距离)。app

 

2.环境搭建

安装能够参考个人这篇博客:[深度学习工具]·极简安装Dlib人脸识别库,下面说一下须要注意的点::工具

此博文针对Windows10安装,其余平台能够仿照这个步骤来安装学习

  • 安装Miniconda

使用conda指令来安装Dlib库,使用Miniconda与Anaconda均可以,我习惯用Miniconda,简单占用内存小。
推荐使用清华源,下载安装,选择合适的平台版本。python==3.6测试

  • 安装dlib
    注意必定要以管理员身份进入CMD,执行(若是是Linux Mac 就使用 sudo)
conda install -c conda-forge dlib
  • 须要imageio 库,可使用下述命令安装
conda install imageio

3.开发实战 

 

1.实现人脸检测标记网站

face_test.pythis

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)能够将标记的框显示在原图上。

原始照片
原始照片
输出照片
输出照片

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

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个特征点。反映在图中就是蓝色的线。

 

原始图片

输出图片

注意运行这段代码须要这个文件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) 

  

输出结果

检测到了 1 我的脸
检测到了 1 我的脸
检测到了 1 我的脸
检测到了 1 我的脸

feature 1 shape (128, 1)

diff distance is 0.254767715912
same distance is 0.0620976363391

咱们能够看出,每张人脸都被提取为了128维的向量,咱们能够理解为128维的坐标(xyz是三维,128维就是有128个轴组成),咱们下面须要作的就是计算两个特征的距离,设定好合适的阈值,小于这个阈值则识别为同一我的。代码正确运行须要这个文件face_rec_model_path = 'dlib_face_recognition_resnet_model_v1.dat',我已经放在本身的github中,方便你们使用。

咱们从上面测试的结果能够看出,不一样的距离为0.25,同一我的为0.06,阈值就能够先设置为其间的一个值。我这里先设置为0.09,这个阈值也是须要大量数据来计算的,选择的准则为使错误识别为最低。

下面咱们把阈值设置为0.09,来测试系统可否区分出不一样的人:在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]))

  

 

输出结果

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

从上面能够看出,已基本知足对人脸区分的功能,若是如要实用化则须要继续调优阈值与代码,调优的准则就是选择合适的阈值使错误识别为最低。

Hope this helps

我的网站--> http://www.yansongsong.cn/

项目GitHub地址--> https://github.com/xiaosongshine/dlib_face_recognition

相关文章
相关标签/搜索