文章目录

  • 概述
  • 方法实现
    • 实现步骤
    • 示例代码
    • 重载方法

概述

此示例演示如何使用dlib作为人脸识别工具,dlib提供一个方法可将人脸图片数据映射到128维度的空间向量,如果两张图片来源于同一个人,那么两个图片所映射的空间向量距离就很近,否则就会很远。因此,可以通过提取图片并映射到128维空间向量再度量它们的欧氏距离(Euclidean distance)是否足够小来判定是否为同一个人。

当设置向量距离阈值为0.6时,2007年,在与其他先进的人脸识别方法的比赛中,dlib模型在LFW人脸数据集基线测试准确率为99.38%。这个准确率意味着,在判断一对照片是否为同一个人时,dlib工具将具有99.38%的准确率。

方法实现

实现步骤

  1. 实例化人脸检测模型、人脸关键点检测模型、人脸识别模型
  2. 加载一对图片
  3. 分别获取图片中的人脸图片所映射的空间向量,即人脸特征值
  4. 计算特征向量欧氏距离,根据阈值判断是否为同一个人

示例代码

import sys
import os
import dlib
import glob
import numpy as npdef find_euclidean_distance(source_representation, test_representation):"""计算向量的欧氏距离"""euclidean_distance = source_representation - test_representationeuclidean_distance = np.sum(np.multiply(euclidean_distance, euclidean_distance))euclidean_distance = np.sqrt(euclidean_distance)return euclidean_distance# 加载模型
face_detect_model_path = '../models/mmod_human_face_detector.dat'
face_shape_predictor_path = '../models/shape_predictor_5_face_landmarks.dat'
face_rec_model_path = '../models/dlib_face_recognition_resnet_model_v1.dat'
face_detector = dlib.cnn_face_detection_model_v1(face_detect_model_path)
face_shape_predictor = dlib.shape_predictor(face_shape_predictor_path)
face_recognition_model = dlib.face_recognition_model_v1(face_rec_model_path)image_path = r'sample1.jpg'
image_cmp_path = r'sample12.jpg'
image=dlib.load_rgb_image(image_path)
image_cmp = dlib.load_rgb_image(image_cmp_path)face_detections = face_detector(image, 1)
# 假定每张对比图片只有一张人脸
face_shape=face_shape_predictor(image, face_detections[0].rect)
# 获取人脸图片128维向量
face_descriptor = face_recognition_model.compute_face_descriptor(image,face_shape,10,0.35)
face_feature = np.array(face_descriptor)# 获取对比人脸图片的128维向量
face_cmp_detections = face_detector(image_cmp, 1)
face_cmp_shape = face_shape_predictor(image_cmp, face_cmp_detections[0].rect)
face_cmp_descriptor = face_recognition_model.compute_face_descriptor(image_cmp, face_cmp_shape,10,0.35)
face_cmp_feature = np.array(face_cmp_descriptor)# 获取向量欧式距离
distance = find_euclidean_distance(face_feature, face_cmp_feature)
print(distance)

获取人脸向量方法,可继续添加参数如下 :
face_descriptor = face_recognition_model.compute_face_descriptor(img, shape, 100, 0.25)

  • 在LFW数据集测试中,不传入100这个参数,得到的正确率是99.13%,传入参数100,正确率为99.38%.然而,传入100这个参数,使得这个方法的执行速度慢了100倍,所以按需选择即可。进一步解释一下第三个参数,第三个参数用来告诉函数执行多少次人脸提取(jitter/resample),当设置为100时,会提取100次稍作修改的人脸图片并取平均值,再去映射为空间向量,这个数值可以设置小一点,例如 10,那么执行速度将会慢10倍而不是100倍,正确率却依然有99.3%.

  • 第四个参数值padding(0.25)是人脸图形的内边距.设置padding为0将会沿着人脸区域剪切,padding值越大,剪切的图片将会向外延伸,padding设置为0.5时,图像宽度变为原来的2倍,padding设置为1时为三倍,以此类推.

重载方法

另外一种获取人脸特征向量的方法(直接传入已对齐的人脸图片):

# 获取人脸对齐图片,必须是默认的尺寸(150*150)
face_chip = dlib.get_face_chip(img, shape)# 获取特征向量
face_feature_from_prealigned_image = face_recognition_model.compute_face_descriptor(face_chip)

重载方法汇总:

1. compute_face_descriptor(self, img: numpy.ndarray[(rows,cols,3),uint8], face: dlib.full_object_detection, num_jitters: int=0, padding: float=0.25) -> dlib.vectorTakes an image and a full_object_detection that references a face in that image and converts it into a 128D face descriptor. If num_jitters>1 then each face will be randomly jittered slightly num_jitters times, each run through the 128D projection, and the average used as the face descriptor.Optionally allows to override default padding of 0.25 around the face.2. compute_face_descriptor(self, img: numpy.ndarray[(rows,cols,3),uint8], num_jitters: int=0) -> dlib.vectorTakes an aligned face image of size 150x150 and converts it into a 128D face descriptor.Note that the alignment should be done in the same way dlib.get_face_chip does it.If num_jitters>1 then image will be randomly jittered slightly num_jitters times, each run through the 128D projection, and the average used as the face descriptor. 3. compute_face_descriptor(self, img: numpy.ndarray[(rows,cols,3),uint8], faces: dlib.full_object_detections, num_jitters: int=0, padding: float=0.25) -> dlib.vectorsTakes an image and an array of full_object_detections that reference faces in that image and converts them into 128D face descriptors.  If num_jitters>1 then each face will be randomly jittered slightly num_jitters times, each run through the 128D projection, and the average used as the face descriptor. Optionally allows to override default padding of 0.25 around the face.4. compute_face_descriptor(self, batch_img: List[numpy.ndarray[(rows,cols,3),uint8]], batch_faces: List[dlib.full_object_detections], num_jitters: int=0, padding: float=0.25) -> dlib.vectorssTakes an array of images and an array of arrays of full_object_detections. `batch_faces[i]` must be an array of full_object_detections corresponding to the image `batch_img[i]`, referencing faces in that image. Every face will be converted into 128D face descriptors.  If num_jitters>1 then each face will be randomly jittered slightly num_jitters times, each run through the 128D projection, and the average used as the face descriptor. Optionally allows to override default padding of 0.25 around the face.5. compute_face_descriptor(self, batch_img: List[numpy.ndarray[(rows,cols,3),uint8]], num_jitters: int=0) -> dlib.vectorsTakes an array of aligned images of faces of size 150_x_150.Note that the alignment should be done in the same way dlib.get_face_chip does it.Every face will be converted into 128D face descriptors.  If num_jitters>1 then each face will be randomly jittered slightly num_jitters times, each run through the 128D projection, and the average used as the face descriptor.

dlib实现人脸识别方法相关推荐

  1. python人脸识别毕业设计-Python基于Dlib的人脸识别系统的实现

    之前已经介绍过人脸识别的基础概念,以及基于opencv的实现方式,今天,我们使用dlib来提取128维的人脸嵌入,并使用k临近值方法来实现人脸识别. 人脸识别系统的实现流程与之前是一样的,只是这里我们 ...

  2. python怎么另起一行阅读答案_使用Python+Dlib构建人脸识别系统(在Nvidia Jetson Nano 2GB开发板上)...

    Nvidia Jetson Nano 2GB开发板是一款新的单板机 售价59美元 运行带有GPU加速的人工智能软件.在2020年 你可以从一台售价59美元的单板计算机中获得令人惊叹的性能 让我们用它来 ...

  3. 使用Python+Dlib构建人脸识别系统(在Nvidia Jetson Nano 2GB开发板上)

    Nvidia Jetson Nano 2GB开发板是一款新的单板机,售价59美元,运行带有GPU加速的人工智能软件. 在2020年,你可以从一台售价59美元的单板计算机中获得令人惊叹的性能,让我们用它 ...

  4. MDFR:基于人脸图像复原和人脸转正联合模型的人脸识别方法

    AI 科技评论报道 编辑 | 陈大鑫 在现实生活中,许多因素可能会影响人脸识别系统的识别性能,例如大姿势,不良光照,低分辨率,模糊和噪声等.为了应对这些挑战,之前的人脸识别方法通常先把低质量的人脸图像 ...

  5. 人脸识别方法个人见解

    dodo:人脸识别方法个人见解(之一) TPAMI = IEEE Transactions on PAMI 这个杂志 PAMI  是指 pattern analysis and machine int ...

  6. 都说dlib是人脸识别的神器,那到底能不能识破妖怪的伪装?

    作者:盛光晓 原文链接:https://blog.csdn.net/esa72ya/article/details/89189987 众所周知,dlib是人脸识别的利器,被广泛应用于行为检测.安防工程 ...

  7. MDFR :基于人脸图像复原和人脸转正联合模型的人脸识别方法

    点上方计算机视觉联盟获取更多干货 仅作学术分享,不代表本公众号立场,侵权联系删除 转载于:AI 科技评论报道 编辑 | 陈大鑫 AI博士笔记系列推荐 周志华<机器学习>手推笔记正式开源!可 ...

  8. python调用摄像头人脸识别代码_OpenCV3-Python人脸识别方法—人脸识别与标记

    上接<OpenCV3-Python人脸识别方法-基于摄像头>,实际应用中,有时我们不仅需要检测人脸信息,可能还需对识别到的人脸进行判断(是否是某个特定的人)?接下来,本篇介绍基于openc ...

  9. (论文阅读)基于融合深度卷积神经网络的人脸识别方法研究-褚玉晓

    一.基于融合深度卷积神经网络人脸识别方法设计 1.PCA算法提取人脸特征 主要成分分析法(Principal Component Analysis,PCA) 第一步,计算人脸图像的平均值并执行归一化的 ...

最新文章

  1. 一段C语言和汇编的对应分析,揭示函数调用的本质
  2. 学习旧岛小程序 (4) 电影组件的实现
  3. erlang 架构原理_Erlang与Java内存架构
  4. javascript 西瓜一期 03 机器语言与高级语言
  5. golang xorm框架对应pg数据库字段类型参照表
  6. C语言 FileStreaming 键盘与屏幕IO
  7. NUC1474 Ants【水题】
  8. ExtJS 4无限制滚动条的Grid
  9. 应用程序中的服务器错误
  10. 去除A和B数组中的交集
  11. C语言 队列的实现(链表实现)
  12. 申请并部署阿里云SSL免费证书详细流程
  13. 一次简单的宾馆路由器后台破解
  14. ROS海龟跟随(坐标变换)
  15. 不在同一局域网?组建家庭局域网的方案和踩坑
  16. 儿子于靖洋180天的照片
  17. 计算机DNS怎么配置,如何设置电脑的dns地址
  18. cluego使用说明_生信分析绘图神器,你值得拥有!
  19. 【第十三讲】TMS320F28335开发板之DMA模块
  20. 渲染器引入KaTeX 模块——实现实时渲染

热门文章

  1. Windows设备信息获取:(摄像头,声卡为例)Qt,WindowsAPI对比说明(2)
  2. LaTex用模板的时候图片的caption标题无法左对齐
  3. 再谈BOM和DOM(1):BOM与DOM概述
  4. 右键没有WinRAR压缩的解决方法
  5. Ubuntu20.04 LTS 卡顿问题(卡顿间隔固定)
  6. APP定制开发的优势
  7. 空指针引用,导致linux内核panic(重启)
  8. 论文阅读:Gibberish, Assistant, or Master? Using Tweets Linking to News for Extractive Single-Document Su
  9. C|倒三角(trangle)
  10. 【软件工具】之下载微软官方正版 windows 系统