本篇文章是在上一篇文章 opencv联合dlib人脸检测例子二(加快检测) 的基础上做了一个人脸识别功能。
本文章中的代码实现了人脸识别功能。检测目标图片中的人脸是不是库中的某张图片中的人脸,按照以下操作步骤实现效果:
1. 搜集一些目标人物的人脸图片,每张图片的名字为 名字 + 后缀(.jpg/.png. …)格式,存放到指定目录下,这里以faces
作为指定目录
2. 搜集目标人物的其他的人脸图片,作为验证使用
arvik是在linux环境下编写的代码,如果需要运行到win环境下,需要重新用windows代码实现getFiles()
函数。
源代码及其详细解释如下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifdef __cplusplus
extern "C"{
#endif
#include
#include
#include
#include
#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 fs);
template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual = dlib::add_prev11,dlib::tag1>>;
template <template <int,template<typename>class,int,typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual_down = dlib::add_prev22,2,2,2,dlib::skip12,dlib::tag1>>>>>;
template <int N, template <typename> class BN, int stride, typename SUBNET>
using block = BN3,3,1,1,dlib::relu3,3,stride,stride,SUBNET>>>>>;
template <int N, typename SUBNET> using ares = dlib::relu>;
template <int N, typename SUBNET> using ares_down = dlib::relu>;
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_metric128,dlib::avg_pool_everything<
alevel0<
alevel1<
alevel2<
alevel3<
alevel4<
dlib::max_pool<3,3,2,2,dlib::relu32,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
mapfloat ,0,1>, string> fdlib;
for(map<string, string>::iterator it = files.begin(); it != files.end(); it++ )
{
std::cout << "filename:" << it->second << " filepath:" <first<<std::endl;
cv::Mat frame = cv::imread(it->first);
cv::Mat src;
cv::cvtColor(frame, src, CV_BGR2GRAY);
dlib::array2d dimg;
dlib::assign_image(dimg, dlib::cv_image(src));
//haar级联分类器探测人脸区域,获取一系列人脸所在区域
std::vector objects;
std::vector 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 > faces;
std::vector shapes;
for(int i = 0; i < dets.size(); i++)
{
dlib::full_object_detection shape = sp(dimg, dets[i]); //获取指定一个区域的人脸形状
shapes.push_back(shape);
dlib::matrix 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<continue;
}
std::vectorfloat ,0,1>> face_descriptors = net(faces);
for(std::vectorfloat ,0,1>>::iterator iter = face_descriptors.begin(); iter != face_descriptors.end(); iter++ )
{
fdlib.insert(pairfloat,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 dimg;
dlib::assign_image(dimg, dlib::cv_image(src));
//haar级联分类器探测人脸区域,获取一系列人脸所在区域
std::vector objects;
std::vector 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] <return -1;
}
std::vector > faces;
std::vector shapes;
for(int i = 0; i < dets.size(); i++)
{
dlib::full_object_detection shape = sp(dimg, dets[i]); //获取指定一个区域的人脸形状
shapes.push_back(shape);
dlib::matrix 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] <return -1;
}
line_one_face_detections(frame, shapes);
std::vectorfloat ,0,1>> face_descriptors = net(faces);
//遍历库,查找相似图像
for(mapfloat ,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!" <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 fs)
{
int i, j;
for(j=0; jfor(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);
}
}
}
附录,工程代码:
linux安装好opencv和dlib后,解压工程代码到linux环境下,进入目录执行make,分别执行./t9 faces face_tao1.jpg
和 ./t9 faces face_tao2.jpg
即可进行人脸识别
点击 这里 下载本文章工程源代码。点击无效请访问 https://download.csdn.net/download/u012819339/10664602