dlib+OpenCV实现人脸登录系统

0. 写在最前面

本文持续更新地址:https://haoqchen.site/2019/02/28/log-system/

你的Star是作者坚持下去的最大动力哦~~~

本文介绍了使用dlib的人脸检测、对齐、识别功能加上OpenCV的XML文件读写功能实现了一个登录系统。可以实现用户的用户名、密码、人脸特征注册、登录、数据的本地保存等功能。目前只实现了命令行版本,需要界面可以自己开发。

github地址:https://github.com/HaoQChen/log_system

本文将不再介绍dlib以及OpenCV的安装,强烈建议仔细看一下官网的要求!!!

1. dlib相关

dlib官网介绍:
Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software in C++ to solve real world problems. It is used in both industry and academia in a wide range of domains including robotics, embedded devices, mobile phones, and large high performance computing environments. Dlib's open source licensing allows you to use it in any application, free of charge.

当程序运行很慢的时候http://dlib.net/faq.html#Whyisdlibslow
if you are using dlib’s face detector then you should turn on either SSE4 or AVX instructions since this makes it run much faster
可用cat /proc/cpuinfo | grep flags查看CPU支持的指令集
也可以cmake时加上命令-DUSE_AVX_INSTRUCTIONS=ON或者在CMakeLists文件中添加下面这句命令:
set(USE_AVX_INSTRUCTIONS ON)

The library is also covered by the very liberal Boost Software License so feel free to use it any way you like. However, if you use dlib in your research then please cite its Journal of Machine Learning Research paper when publishing.

注意事项描述了一些全局定义。

代码风格,作者有自己一套严格的代码风格,如果想贡献代码,只能按照要求来

主页右边的The Library下是将各个模块进行分类,可以根据需要进行查找

对dlib的所有组件进行测试

cd dlib/test
mkdir build
cd build
cmake ..
cmake --build . --config Release
./dtest --runall

2. 人脸相关examples

建议仔细看下代码:

  • face_detection_ex:用经典HOG特征+线性分类器+图像金字塔+滑动窗口检测。来源于fhog_object_detector_ex,该检测器还可以用于其他的刚体检测,人脸模型已经训练好,可以直接用。
  • dnn_mmod_face_detection_ex:基于CNN的人脸检测,模型已经训练好。效果要比face_detection_ex好。但需要更多的计算资源,所以意味着需要运行在有GPU的电脑。没有GPU根本跑不了。。。我8G内存+10G交换空间,内存99%,交换空间也去到70%。
  • face_landmark_detection_ex:使用face_detection_ex的检测方法来得到人脸区域。然后根据文章One Millisecond Face Alignment with an Ensemble of Regression Trees by Vahid Kazemi and Josephine Sullivan, CVPR 2014这篇文章的方法来找到人脸轮廓,找人脸轮廓时使用的是iBUG 300-W数据集,该数据集不能商用,你也可以自己用train_shape_predictor_ex训练模型
  • dnn_face_recognition_ex:举例说明如何使用训练好的模型来进行人脸识别,该模型在标准LFW人脸识别基准中达到99.38%准确率。该example其实是用相关函数进行人脸聚类。基于dnn_imagenet_ex稍微修改的模型。
  • webcam_face_pose_ex:仅仅只是将face_landmark_detection_ex改成输入为OpenCV's VideoCapture对象。

不建议将libdlib.a拿出来然后自己链接这个静态库。最后链接的时候会有很多依赖关系需要添加,建议直接按照examples的方法add_subdirectory(../dlib dlib_build),这样可以将这些依赖关系也一并包含进来。虽然这样会增加一点点文件大小,但无关痛痒。

3. log_system

  • 获取特征
    如何获取人脸的128维特征请看log_system.cpp的这个函数:
    bool GetFaceDescriptor(const dlib::matrix<dlib::rgb_pixel>& img, matrix<float,0,1>& fd)

  • 特征比对
    使用余弦相似度作为评判标准

  • 数据本地保存与读取
    使用OpenCV的FileStorage类实现按格式保存到XML文件。使用的是官方文档中所说的map的格式,即feature间使用{}括号来实现人名、密码、人脸特征的存储。当然你也可以扩展声纹、性别等。

  • dlib与OpenCV间的数据转换

matrix<float,0,1>->cv::Mat

toMat(matrix)

cv::Mat->matrix<float,0,1>

cv_image<float> cfd(mat);
matrix<float> dfd(dlib::mat(cfd));
//or
cv_image<bgr_pixel> image(mat);
matrix<rgb_pixel> matrix;
assign_image(matrix, image);

剩下一些基本逻辑test.cpp已经说得很清楚了,也可以看下CMakeLists文件。本工程在Ubuntu1404 64bit下测试成功,相信移植到其他系统也是可以的


喜欢我的文章的话Star一下呗Star

版权声明:本文为白夜行的狼原创文章,未经允许不得以任何形式转载

原文地址:https://www.cnblogs.com/HaoQChen/p/11048587.html