基于Dlib人脸识别-实战(UI界面、python代码)

人脸识别–实战

一、前言

1、介绍

  本文基于GitHub上一基于Dlib的人脸识别项目,进行学习、整理并根据遇到的问题做一些补充。项目源码:Dlib_face_recognition_from_camera

  Dlib(官网:http://dlib.net/)是一个现代c++工具包,包含机器学习算法和用c++创建复杂软件来解决现实世界问题的工具。它被广泛应用于工业和学术界,包括机器人、嵌入式设备、移动电话和大型高性能计算环境。Dlib的开源许可允许您在任何应用程序中免费使用它。

2、库和相关包的下载

python3.9下载dlib库的方法:

打开命令行窗口使用pip命令
1、pip install boost
2、pip install cmake
3、下载dlib-19.22.99-cp39-cp39-win_amd64.whl文件至命令窗口的目录位置,并执行pip install dlib-19.22.99-cp39-cp39-win_amd64.whl(注:其他版本的python找到相应的dlib包即可,若是python3.8版本,尾缀应该是cp38-cp38-win_amd64.whl)

相关人脸识别的包:

1、shape_predictor_68_face_landmarks

2、dlib_face_recognition_resnet_model_v1

(关于两个包的功能及使用,下文会介绍到)

dlib库(dlib-19.22.99-cp39-cp39-win_amd64.whl)以及两个包的下载链接:
https://download.csdn.net/download/Li_y_berty/86497465不需要积分即可下载,下载有问题可私我。

二、效果演示

1、人脸录入

在这里插入图片描述

  • Information: 显示先关信息,包括帧数、当前摄像头捕捉的人脸数,数据库已存入的人脸数。
  • Menu1: 输入要录入的人脸名称。(目前输入中文名称可能会有点点小问题)
  • Menu2: 保存当前的人脸照片,同一人可录入任意张人脸照片,建议录入三张。
  • Menu3: 清除数据库所有录入的人脸信息,包括提取的特征信息。

2、特征提取

在这里插入图片描述
  提取所有人脸的特征,将每人的人脸的128D特征以csv文件保存至features_all.csv中。(若一人录入多张人脸,计算该人这几张人脸的128D特征的均值并保存)

3、人脸识别

在这里插入图片描述

  • 左上角显示相关信息,Frame为记录捕捉的总帧数,FPS记录帧率,Faces记录当前捕捉的人脸数目。
  • 目前只限定于一张人脸时,开始匹配识别,按‘q’退出。

三、识别原理

1、关于检测

  采用Dlib 正向人脸检测器,输出人脸矩形的四个坐标点。该人脸检测器使用经典的方向梯度直方图(HOG)特征,结合线性分类器、图像金字塔和滑动窗口检测方案。详情可参照:http://dlib.net/face_detector.py.html

#   This face detector is made using the now classic Histogram of Oriented
#   Gradients (HOG) feature combined with a linear classifier, an image
#   pyramid, and sliding window detection scheme.  This type of object detector
#   is fairly general and capable of detecting many types of semi-rigid objects
#   in addition to human faces.  Therefore, if you are interested in making
#   your own object detectors then read the train_object_detector.py example
#   program. 

detector = dlib.get_frontal_face_detector()
faces = detector(img_gray, 0)

2、关于识别

  • 首先是调用人脸特征点检测器,使用shape_predictor_68_face_landmarks.dat模型,该模型在ibug 300-W数据集上训练,这个模型文件是为与dlib的HOG人脸检测器一起使用而设计的。它期望来自人脸检测器的边界框以某种方式对齐。
  • 然后是调用Dlib Resnet 人脸识别模型, 提取 128D 的特征矢量,最后通过比对数据库中的特征(计算欧氏距离,并设定合适的阈值)进行识别匹配。
# Dlib人脸特征点检测器 / Get face landmarks
predictor = dlib.shape_predictor("data/dlib_models/shape_predictor_68_face_landmarks.dat")

# Dlib Resnet人脸识别模型,提取128D的特征矢量 / Use Dlib resnet50 model to get 128D face descriptor
face_reco_model = dlib.face_recognition_model_v1("data/dlib_models/dlib_face_recognition_resnet_model_v1.dat")

for i in range(len(faces)):
    shape = predictor(img_rd, faces[i])
    self.current_frame_face_feature_list.append(
        face_reco_model.compute_face_descriptor(img_rd, shape))

四、源码

1、代码结构

├── data
│   ├── data_dlib            # Dlib's models
│   │   ├── dlib_face_recognition_resnet_model_v1.dat
│   │   └── shape_predictor_68_face_landmarks.dat
│   ├── data_faces_from_camera     # Face images captured from camera (will generate after step 1)
│   │   ├── person_1
│   │   │   ├── img_face_1.jpg
│   │   │   ├── img_face_2.jpg
│   │   │   └── img_face_3.jpg
│   │   └── person_2
│   │       └── img_face_1.jpg
│   │       ├── img_face_2.jpg
│   │       └── img_face_3.jpg
│   └── features_all.csv           # CSV to save all the features of known faces (will generate after Menu 2)
├── get_faces.py                   # Menu1: Face register GUI with Tkinter
├── extract_features.py            # Menu2: Feature extraction
└── face_reco.py                   # Menu3: Face recognizer for single person
get_faces.py:

  以界面的功能键为单位实现代码。

extract_features.py:

  提取录入照片的特征值,并以csv格式保存。

face_reco.py:
# 检测 -> 识别人脸, 新人脸出现 -> 再识别, 不会对于每一帧都进行识别

# 控制再识别的后续帧数
# 如果识别出 "unknown" 的脸, 将在 cnt_reclassify_interval 计数到 reclassify_interval 后, 对于人脸进行重新识别
self.cnt_reclassify_interval = 0
self.reclassify_interval = 10

# 按帧操作 
while stream isOpened():
    │
    ├── 人脸数未变化:1-->1  or  0-->0
    │   ├── 1."unknown",cnt_reclassify_interval += 1
    │   │   2.len(face) == 1:
    │   │       cnt_reclassify_interval =  reclassify_interval:
    │   │           识别、比对人脸
    │   │       cnt_reclassify_interval !=  reclassify_interval:
    │   └──         正常的框选显示操作
    │
    ├── 人脸数发生变化:1-->0  or 0-->1
    │   ├── 0-->1
    │   │   1.将人物初始化为"unknown"
    │   │   2.识别、比对人脸
    │   │   3.若仍不认识,识别为"unknown", cnt_reclassify_interval += 1
    │   │   
    │   └── 1-->01.初始化,cnt_reclassify_interval清零
    │ 
    └── 信息显示           

2、源码地址

https://download.csdn.net/download/Li_y_berty/86500365下载有问题可私我。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值