介绍
调用摄像头进行人脸识别, 支持多张人脸同时识别;
- 摄像头人脸录入
请不要离摄像头过近, 人脸超出摄像头范围时会有 "OUT OF RANGE" 提醒
- 提取特征建立人脸数据库
- 利用摄像头进行人脸识别
face_reco_from_camera.py, 对于每一帧都做检测识别
face_reco_from_camera_single_face.py, 对于人脸<=1, 只有新人脸出现才进行再识别来提高 FPS
face_reco_from_camera_ot.py, 利用 OT 来实现再识别提高 FPS
定制显示名字, 可以写中文
关于精度
- 当使用 0.6 的距离阈值时,dlib 模型在标准 LFW 人脸识别基准上获得了 99.38% 的准确率。
关于算法
- 基于残差网络(Residual Neural Network)的 CNN 模型;
- 该模型是一个具有 29 个卷积层的 ResNet 网络。 它本质上是 He、Zhang、Ren 和 Sun 的论文 Deep Residual Learning for Image Recognition 中 ResNet-34 网络的一个版本,去除了几层,每层的过滤器数量减少了一半。
概述
此项目中人脸识别的实现流程 (no OT, 每一帧都进行检测+识别)
实现流程(with OT, 初始帧进行检测+识别, 后续帧检测+质心跟踪)
如果利用 OT 来跟踪, 可以大大提高 FPS, 因为做识别时候需要提取特征描述子的耗时很多;
Steps
- 安装依赖库
pip install -r requirements.txt
- 下载源码
git clone https://github.com/coneypo/Dlib_face_recognition_from_camera
- 进行人脸信息采集录入
python3 get_face_from_camera.py
- 提取所有录入人脸数据存入 "features_all.csv"
python3 features_extraction_to_csv.py
- 调用摄像头进行实时人脸识别
python3 face_reco_from_camera.py
- 对于人脸数<=1, 调用摄像头进行实时人脸识别
python3 face_reco_from_camera_single_face.py
- 利用 OT 算法, 调用摄像头进行实时人脸识别
python3 face_reco_from_camera_ot.py
源代码
Repo 的 tree / 树状图:
. ├── get_faces_from_camera.py # Step 1. Face register ├── features_extraction_to_csv.py # Step 2. Feature extraction ├── face_reco_from_camera.py # Step 3. Face recognizer ├── face_reco_from_camera_single_face.py # Step 3. Face recognizer for single person ├── face_reco_from_camera_ot.py # Step 3. Face recognizer with OT ├── face_descriptor_from_camera.py # Face descriptor computation ├── how_to_use_camera.py # Use the default camera by opencv ├── data │ ├── data_dlib # Dlib's model │ │ ├── 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 │ │ └── person_2 │ │ └── img_face_1.jpg │ │ └── img_face_2.jpg │ └── features_all.csv # CSV to save all the features of known faces (will generate after step 2) ├── README.rst └── requirements.txt # Some python packages needed
用到的 Dlib 相关模型函数:
- Dlib 正向人脸检测器 (based on HOG), output: <class 'dlib.dlib.rectangles'>
detector = dlib.get_frontal_face_detector() faces = detector(img_gray, 0)
- Dlib 人脸 landmark 特征点检测器, output: <class 'dlib.dlib.full_object_detection'>, will use shape_predictor_68_face_landmarks.dat
# This is trained on the ibug 300-W dataset (https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/) # Also note that this model file is designed for use with dlib's HOG face detector. # That is, it expects the bounding boxes from the face detector to be aligned a certain way, the way dlib's HOG face detector does it. # It won't work as well when used with a face detector that produces differently aligned boxes, # such as the CNN based mmod_human_face_detector.dat face detector. predictor = dlib.shape_predictor("data/data_dlib/shape_predictor_68_face_landmarks.dat") shape = predictor(img_rd, faces[i])
- Dlib 特征描述子 Face recognition model, the object maps human faces into 128D vectors
face_rec = dlib.face_recognition_model_v1("data/data_dlib/dlib_face_recognition_resnet_model_v1.dat")
Python 源码介绍如下:
- get_face_from_camera.py:
进行 Face register / 人脸信息采集录入
- 请注意存储人脸图片时, 矩形框不要超出摄像头范围, 要不然无法保存到本地;
- 超出会有 "out of range" 的提醒;
- features_extraction_to_csv.py:
从上一步存下来的图像文件中, 提取人脸数据存入CSV;
- 会生成一个存储所有特征人脸数据的 "features_all.csv";
- size: n*128 , n means n people you registered and 128 means 128D features of the face
- face_reco_from_camera.py:
这一步将调用摄像头进行实时人脸识别; / This part will implement real-time face recognition;
- Compare the faces captured from camera with the faces you have registered which are saved in "features_all.csv"
- 将捕获到的人脸数据和之前存的人脸数据进行对比计算欧式距离, 由此判断是否是同一个人;
- face_reco_from_camera_single_face.py:
针对于人脸数 <=1 的场景, 区别于 face_reco_from_camera.py (对每一帧都进行检测+识别), 只有人脸出现的时候进行识别;
- face_reco_from_camera_ot.py:
只会对初始帧做检测+识别, 对后续帧做检测+质心跟踪;
- (optional) face_descriptor_from_camera.py
调用摄像头进行实时特征描述子计算; / Real-time face descriptor computation;