opencv联合dlib人脸识别例子
Posted arvik
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv联合dlib人脸识别例子相关的知识,希望对你有一定的参考价值。
本篇文章是在上一篇文章 opencv联合dlib人脸检测例子二(加快检测) 的基础上做了一个人脸识别功能。
本文章中的代码实现了人脸识别功能。检测目标图片中的人脸是不是库中的某张图片中的人脸,按照以下操作步骤实现效果:
1. 搜集一些目标人物的人脸图片,每张图片的名字为 名字 + 后缀(.jpg/.png. …)格式,存放到指定目录下,这里以faces
作为指定目录
2. 搜集目标人物的其他的人脸图片,作为验证使用
arvik是在linux环境下编写的代码,如果需要运行到win环境下,需要重新用windows代码实现getFiles()
函数。
源代码及其详细解释如下
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/opencv.h>
#include <dlib/dnn.h>
#include <dlib/data_io.h>
#include <dlib/clustering.h>
#include <dlib/string.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <ctime>
#include <string>
#include <map>
#ifdef __cplusplus
extern "C"
#endif
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#ifdef __cplusplus
#endif
//由于dlib和opencv中有相当一部分类同名,故不能同时对它们使用using namespace,否则会出现一些莫名其妙的问题
//且dlib库和标准std库中的类发生冲突,如map,string 类等等
using namespace std;
using namespace cv;
//using namespace dlib;
void getFiles(std::string path, std::map<std::string, std::string> &files);
void line_one_face_detections(cv::Mat img, std::vector<dlib::full_object_detection> fs);
template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual = dlib::add_prev1<block<N,BN,1,dlib::tag1<SUBNET>>>;
template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual_down = dlib::add_prev2<dlib::avg_pool<2,2,2,2,dlib::skip1<dlib::tag2<block<N,BN,2,dlib::tag1<SUBNET>>>>>>;
template <int N, template <typename> class BN, int stride, typename SUBNET>
using block = BN<dlib::con<N,3,3,1,1,dlib::relu<BN<dlib::con<N,3,3,stride,stride,SUBNET>>>>>;
template <int N, typename SUBNET> using ares = dlib::relu<residual<block,N,dlib::affine,SUBNET>>;
template <int N, typename SUBNET> using ares_down = dlib::relu<residual_down<block,N,dlib::affine,SUBNET>>;
template <typename SUBNET> using alevel0 = ares_down<256,SUBNET>;
template <typename SUBNET> using alevel1 = ares<256,ares<256,ares_down<256,SUBNET>>>;
template <typename SUBNET> using alevel2 = ares<128,ares<128,ares_down<128,SUBNET>>>;
template <typename SUBNET> using alevel3 = ares<64,ares<64,ares<64,ares_down<64,SUBNET>>>>;
template <typename SUBNET> using alevel4 = ares<32,ares<32,ares<32,SUBNET>>>;
using anet_type = dlib::loss_metric<dlib::fc_no_bias<128,dlib::avg_pool_everything<
alevel0<
alevel1<
alevel2<
alevel3<
alevel4<
dlib::max_pool<3,3,2,2,dlib::relu<dlib::affine<dlib::con<32,7,7,2,2,
dlib::input_rgb_image_sized<150>
>>>>>>>>>>>>;
/*
识别一张图片是不是库里的某个人
方法:
统计出库文件夹中所有人的图片的face_descriptors,然后计算出当前图片中的人脸face_descriptors,二者之间距离小于0.6则视为同一个人
./t9 faces pic1
*/
int main(int argc, char *argv[])
time_t start_t, end_t;
if(argc != 3)
std::cout<< "you should specified a dir!"<<std::endl;
return 0;
time(&start_t);
std::map<string, string> files;
getFiles(argv[1], files);
if(files.empty())
std::cout<< "No pic files found in "<< argv[1] <<std::endl;
return 0;
//加载训练好的级联分类器,利用haar级联分类器快速找出人脸区域,然后交给dlib检测人脸部位
cv::CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml");
if(faceDetector.empty())
std::cout << "face detector is empty!" <<std::endl;
return 0;
//加载人脸形状探测器
dlib::shape_predictor sp;
dlib::deserialize("./shape_predictor_68_face_landmarks.dat") >> sp;
//加载负责人脸识别的DNN
anet_type net;
dlib::deserialize("dlib_face_recognition_resnet_model_v1.dat") >> net;
//人脸描述符库, face_descriptor ---> name
map<dlib::matrix<float,0,1>, string> fdlib;
for(map<string, string>::iterator it = files.begin(); it != files.end(); it++ )
std::cout << "filename:" << it->second << " filepath:" <<it->first<<std::endl;
cv::Mat frame = cv::imread(it->first);
cv::Mat src;
cv::cvtColor(frame, src, CV_BGR2GRAY);
dlib::array2d<dlib::bgr_pixel> dimg;
dlib::assign_image(dimg, dlib::cv_image<uchar>(src));
//haar级联分类器探测人脸区域,获取一系列人脸所在区域
std::vector<cv::Rect> objects;
std::vector<dlib::rectangle> dets;
faceDetector.detectMultiScale(src, objects);
for (int i = 0; i < objects.size(); i++)
//cv::rectangle(frame, objects[i], CV_RGB(200,0,0));
dlib::rectangle r(objects[i].x, objects[i].y, objects[i].x + objects[i].width, objects[i].y + objects[i].height);
dets.push_back(r); //正常情况下应该只检测到一副面容
if (dets.size() == 0)
continue;
std::vector<dlib::matrix<dlib::rgb_pixel>> faces;
std::vector<dlib::full_object_detection> shapes;
for(int i = 0; i < dets.size(); i++)
dlib::full_object_detection shape = sp(dimg, dets[i]); //获取指定一个区域的人脸形状
shapes.push_back(shape);
dlib::matrix<dlib::rgb_pixel> face_chip;
dlib::extract_image_chip(dimg, dlib::get_face_chip_details(shape,150,0.25), face_chip);
faces.push_back(move(face_chip));
if (faces.size() == 0)
cout << "No faces found in " << it->second<<endl;
continue;
std::vector<dlib::matrix<float,0,1>> face_descriptors = net(faces);
for(std::vector<dlib::matrix<float,0,1>>::iterator iter = face_descriptors.begin(); iter != face_descriptors.end(); iter++ )
fdlib.insert(pair<dlib::matrix<float,0,1>, string>(*iter, it->second));
time(&end_t);
std::cout << "ok, all pic in lib had been keep on. use time:"<< end_t - start_t << " s" <<std::endl;
time(&start_t);
//加载待检测的图片
cv::Mat frame = cv::imread(argv[2]);
cv::Mat src;
cv::cvtColor(frame, src, CV_BGR2GRAY);
dlib::array2d<dlib::bgr_pixel> dimg;
dlib::assign_image(dimg, dlib::cv_image<uchar>(src));
//haar级联分类器探测人脸区域,获取一系列人脸所在区域
std::vector<cv::Rect> objects;
std::vector<dlib::rectangle> dets;
faceDetector.detectMultiScale(src, objects);
for (int i = 0; i < objects.size(); i++)
cv::rectangle(frame, objects[i], CV_RGB(200,0,0));
dlib::rectangle r(objects[i].x, objects[i].y, objects[i].x + objects[i].width, objects[i].y + objects[i].height);
dets.push_back(r); //正常情况下应该只检测到一副面容
if (dets.size() == 0)
cout << "there is no faces found in " << argv[2] <<endl;
return -1;
std::vector<dlib::matrix<dlib::rgb_pixel>> faces;
std::vector<dlib::full_object_detection> shapes;
for(int i = 0; i < dets.size(); i++)
dlib::full_object_detection shape = sp(dimg, dets[i]); //获取指定一个区域的人脸形状
shapes.push_back(shape);
dlib::matrix<dlib::rgb_pixel> face_chip;
dlib::extract_image_chip(dimg, dlib::get_face_chip_details(shape,150,0.25), face_chip);
faces.push_back(move(face_chip));
if (faces.size() == 0)
cout << "No faces found in " << argv[2] <<endl;
return -1;
line_one_face_detections(frame, shapes);
std::vector<dlib::matrix<float,0,1>> face_descriptors = net(faces);
//遍历库,查找相似图像
for(map<dlib::matrix<float,0,1>, string>::iterator it=fdlib.begin(); it != fdlib.end(); it++ )
float distance = length(it->first - face_descriptors[0]);
if( distance < 0.6 )
cout << "the pic is " << it->second << "!, distance:" << distance << endl;
cv::Point org(objects[0].x, objects[0].y);
cv::putText(frame, it->second, org, cv::FONT_HERSHEY_SIMPLEX, 1.0, CV_RGB(0, 200, 0));
break;
time(&end_t);
std::cout << "Face recognition is done! Use time:"<< end_t - start_t << " s" <<std::endl;
cv::imshow("frame", frame);
cv::waitKey(0);
return 0;
void getFiles(string path, map<string, string> &files)
DIR *dir;
struct dirent *ptr;
char base[1000];
if(path[path.length()-1] != '/')
path = path + "/";
if((dir = opendir(path.c_str())) == NULL)
cout<<"open the dir: "<< path <<"error!" <<endl;
return;
while((ptr=readdir(dir)) !=NULL )
///current dir OR parrent dir
if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)
continue;
else if(ptr->d_type == 8) //file
string fn(ptr->d_name);
string name;
name = fn.substr(0, fn.find_last_of("."));
string p = path + string(ptr->d_name);
files.insert(pair<string, string>(p, name));
else if(ptr->d_type == 10) ///link file
else if(ptr->d_type == 4) ///dir
closedir(dir);
return ;
void line_one_face_detections(cv::Mat img, std::vector<dlib::full_object_detection> fs)
int i, j;
for(j=0; j<fs.size(); j++)
cv::Point p1, p2;
for(i = 0; i<67; i++)
// 下巴到脸颊 0 ~ 16
//左边眉毛 17 ~ 21
//右边眉毛 21 ~ 26
//鼻梁 27 ~ 30
//鼻孔 31 ~ 35
//左眼 36 ~ 41
//右眼 42 ~ 47
//嘴唇外圈 48 ~ 59
//嘴唇内圈 59 ~ 67
switch(i)
case 16:
case 21:
case 26:
case 30:
case 35:
case 41:
case 47:
case 59:
i++;
break;
default:
break;
p1.x = fs[j].part(i).x();
p1.y = fs[j].part(i).y();
p2.x = fs[j].part(i+1).x();
p2.y = fs[j].part(i+1).y();
cv::line(img, p1, p2, cv::Scalar(0,0,255), 1);
测试用faces
目录下的样本图片
测试用照片
运行效果
附录,工程代码:
linux安装好opencv和dlib后,解压工程代码到linux环境下,进入目录执行make,分别执行./t9 faces face_tao1.jpg
和 ./t9 faces face_tao2.jpg
即可进行人脸识别
点击 这里 下载本文章工程源代码。点击无效请访问 https://download.csdn.net/download/u012819339/10664602
以上是关于opencv联合dlib人脸识别例子的主要内容,如果未能解决你的问题,请参考以下文章