#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>
#include <sstream>
#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则视为同一个人
./t11 hls_faces huanlesong.mp4
*/
int main(int argc, char *argv[])
{
time_t start_t, end_t;
if(argc != 3)
{
std::cout<< "you should specified a dir and a video stream!"<<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");
//cv::CascadeClassifier faceDetector("./output/cascade.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;
//加载视频
VideoCapture capture(argv[2]);
while(true)
{
//加载待检测的图片
cv::Mat frame;
capture >> frame;
if (frame.empty())
break;
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)
{
continue;
}
line_one_face_detections(frame, shapes);
std::vector<dlib::matrix<float,0,1>> face_descriptors = net(faces);
//遍历库,查找相似图像
float min_distance = 0.7;
std::string similar_name = "unknown";
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.5 ) //应该计算一个最近值
{
if( distance <= min_distance)
{
mi