代码拉取完成,页面将自动刷新
#include "faceidcmp.h"
FaceIdCmp* FaceIdCmp::m_FaceIdCmp = nullptr;
FaceIdCmp::FaceIdCmp(QObject *parent) : QObject(parent)
{
// deserialize("/TCrane/shape_predictor_68_face_landmarks.dat") >> predictor;
// deserialize("/TCrane/dlib_face_recognition_resnet_model_v1.dat") >> net;
// detector = get_frontal_face_detector();
// vec = CreateDataset();
connect(DataDictionary::GetInstance(),SIGNAL(PersonInfoChange()),\
this,SLOT(BuildCmpLib()));
}
FaceIdCmp::~FaceIdCmp()
{
}
FaceIdCmp* FaceIdCmp::GetInstance()
{
if(m_FaceIdCmp == nullptr){
m_FaceIdCmp = new FaceIdCmp;
}
return m_FaceIdCmp;
}
bool FaceIdCmp::Comparison(cv::Mat frame1)
{
qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
// matrix<rgb_pixel> img;
// cv::Mat frame = frame1.clone();
// array2d< bgr_pixel> arrimg(frame.rows, frame.cols);
// dlib::assign_image(img, cv_image<rgb_pixel>(frame));
// std::vector<dlib::rectangle> dets_test = detector(img);
// std::vector<matrix<float, 0, 1>> features;
// std::vector<matrix<rgb_pixel>> faces;//定义存放截取人脸数据组
////qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
// for (auto const& det : dets_test)
// {
// auto shape = predictor(img, det);
// matrix<rgb_pixel> face_chip;
// extract_image_chip(img, get_face_chip_details(shape, 150, 0.25), face_chip);//截取人脸部分,并将大小调为150*150
// faces.push_back(std::move(face_chip));
// }
////qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
// std::vector<matrix<float, 0, 1>> face_descriptors = net(faces);//将150*150人脸图像载入Resnet残差网络,返回128D人脸特征存于face_descriptors
//// for (auto const& feature : face_descriptors)
//// {
//// features.push_back(feature);
//// }
////qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
//// std::vector<int> face_id = GetLabelIndex(vec, face_descriptors);
//////qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
//// string name = "";
//// for (int const& index : face_id)
//// {
//// if (index == -1)
//// {
//// //name = "other";
//////qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
//// //CmpImg = Mat2QImage::Transform(CmpFrame);
//// QImage Img;
//// ComparisonResult(false,Img,CmpName);
//// qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
//// return false;
//// }
//// else
//// {
//// std::map<string,cv::Mat>::iterator it = files.begin();
//// for(int i = 0;i < index;i++,it++)
//// ;
//// qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
////// //CmpFrame = imread(it->first);
////// CmpName = QString::fromStdString(it->second);
////// CmpImg = Mat2QImage::Transform(CmpFrame);
//// CmpName = QString::fromStdString(it->first);
//// Mat Pic = it->second.clone();
//// CmpImg = Mat2QImage::Transform(Pic);
//// //CmpImg = ImgList[index];
////// qDebug() << index;
//// ComparisonResult(true,CmpImg,CmpName);
//////qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
//// return true;
//// }
//// //name = label[index];
//// }
// int face_id = GetLabelIndex(vec, face_descriptors);
// ComparisonScore((int)((1 - m_Score)*100));
// if (face_id == -1 || face_id >= files.size())
// {
// //name = "other";
// QImage Img;
// ComparisonResult(false,Img,CmpName,CmpUuid);
// return false;
// }
// else
// {
// std::map<string,cv::Mat>::iterator it = files.begin();
// for(int i = 0;i < face_id;i++,it++)
// ;
// CmpUuid = QString::fromStdString(it->first);
// CmpName = DataDictionary::GetInstance()->UuidSearchPersonInfo(CmpUuid).Name;
// Mat Pic = it->second.clone();
// CmpImg = Mat2QImage::Transform(Pic);
// //CmpImg = ImgList[index];
// qDebug() << face_id;
// ComparisonResult(true,CmpImg,CmpName,CmpUuid);
// return true;
// }
////qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
// QImage Img;
// ComparisonResult(false,Img,CmpName,CmpUuid);
//// qDebug() << __func__ << " : " << __LINE__ << " :" << QDateTime::currentMSecsSinceEpoch();
return false;
}
bool FaceIdCmp::Comparison1(cv::Mat frame)
{
// matrix<rgb_pixel> img;
// array2d< bgr_pixel> arrimg(frame.rows, frame.cols);
// dlib::assign_image(img, cv_image<rgb_pixel>(frame));
// std::vector<dlib::rectangle> dets_test = detector(img);
// std::vector<matrix<float, 0, 1>> features;
// std::vector<matrix<rgb_pixel>> faces;//定义存放截取人脸数据组
// for (auto const& det : dets_test)
// {
// auto shape = predictor(img, det);
// matrix<rgb_pixel> face_chip;
// extract_image_chip(img, get_face_chip_details(shape, 150, 0.25), face_chip);//截取人脸部分,并将大小调为150*150
// faces.push_back(std::move(face_chip));
// }
// std::vector<matrix<float, 0, 1>> face_descriptors = net(faces);//将150*150人脸图像载入Resnet残差网络,返回128D人脸特征存于face_descriptors
// for (auto const& feature : face_descriptors)
// {
// features.push_back(feature);
// }
//// std::vector<int> face_id = GetLabelIndex(vec, features);
//// string name = "";
//// for (int const& index : face_id)
//// {
//// if (index == -1)
//// {
//// //name = "other";
//// return false;
//// }
//// else
//// {
//// std::map<string,cv::Mat>::iterator it = files.begin();
//// for(int i = 0;i < index;i++,it++)
//// ;
//// return true;
//// }
//// }
// int face_id = GetLabelIndex(vec, features);
// if (face_id == -1)
// {
// //name = "other";
// return false;
// }
// else
// {
// std::map<string,cv::Mat>::iterator it = files.begin();
// for(int i = 0;i < face_id;i++,it++)
// ;
// return true;
// }
return false;
}
void FaceIdCmp::BuildCmpLib()
{
// vec = CreateDataset();
}
//std::vector<matrix<float, 0, 1>> FaceIdCmp::CreateDataset()
//{
// std::vector<matrix<float, 0, 1>> vec;
// //getFiles(FACE_ROOT_DIR"/face_data", files);
// getFiles(files);
// if(files.empty())
// {
// std::cout<< "No pic files found in "<< std::endl;
// return vec;
// }
// for(std::map<string, cv::Mat>::iterator it = files.begin(); it != files.end(); it++ )
// {
// matrix<rgb_pixel> img;
// //cv::Mat image = cv::imread(it->first);
// cv::Mat image = it->second;
// if(image.empty())
// {
// cout << it->first << " pic format is error" << endl;
// continue;
// }
// dlib::assign_image(img, cv_image<rgb_pixel>(image));
// std::vector<matrix<float, 0, 1>> features = GetFaceFeature(img);
// if (features.size() < 1)
// cout << "There is no face" << endl;
// else if (features.size() > 1)
// cout << "There is to many face" << endl;
// else
// vec.push_back(features[0]);
// }
// return vec;
//}
//int FaceIdCmp::GetLabelIndex(std::vector<matrix<float, 0, 1>> features, std::vector<matrix<float, 0, 1>> predict_value)
//{
// float v =((float)DataDictionary::GetInstance()->GetMisData(MIS_FACEID_CMP_VALUE).toInt()/100);
// float min_dis = 1 - v;
// int face_id = -1;
// m_Score = 1.0;
// qDebug() << "min_dis : " << min_dis;
// if(predict_value.size() > 0)
// {
// for (int j = 0; j < features.size(); j++)
// {
// float dis = (float)length(predict_value[0] - features[j]);
// if (min_dis > dis)
// {
// min_dis = dis;
// face_id = j;
// qDebug() << "dis : " << dis;
// }
// if(m_Score > dis)
// {
// m_Score = dis;
// }
// }
// }
// return face_id;
//}
////void FaceIdCmp::getFiles(string path, std::map<string, cv::Mat> &files)
void FaceIdCmp::getFiles(std::map<string, cv::Mat> &files)
{
DIR *dir;
struct dirent *ptr;
char base[1000];
files.clear();
ImgList.clear();
// 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);
QList<PersonInfo_t> List = DataDictionary::GetInstance()->GetPersonList();
for(int i = 0;i < List.size();i++)
{
QImage Img = QImage::fromData(QByteArray::fromBase64(List[i].Pic.toLocal8Bit()));
ImgList.append(Img);
files.insert(pair<string, cv::Mat>(List[i].Uuid.toStdString(), QImage2Mat::Transform(Img)));
}
return ;
}
//std::vector<matrix<float, 0, 1>> FaceIdCmp::GetFaceFeature(matrix<rgb_pixel> img)
//{
// std::vector<matrix<float, 0, 1>> features;
// std::vector<dlib::rectangle> dets = detector(img); //用dlib自带的人脸检测器检测人脸,然后将人脸位置大小信息存放到dets中
// std::vector<matrix<rgb_pixel>> faces;//定义存放截取人脸数据组
// for (auto const& det : dets)
// {
// auto shape = predictor(img, det);
// matrix<rgb_pixel> face_chip;
// extract_image_chip(img, get_face_chip_details(shape, 150, 0.25), face_chip);//截取人脸部分,并将大小调为150*150
// faces.push_back(std::move(face_chip));
// }
// std::vector<matrix<float, 0, 1>> face_descriptors = net(faces);//将150*150人脸图像载入Resnet残差网络,返回128D人脸特征存于face_descriptors
//// for (auto const& feature : face_descriptors)
//// {
//// features.push_back(feature);
//// }
// return face_descriptors;
//}
//std::vector<dlib::rectangle> FaceIdCmp::GetFaceArea(cv::Mat &frame)
//{
// cv::Mat dst;
// //提取灰度图
// cv::cvtColor(frame, dst, CV_BGR2GRAY);
//// //加载dlib的人脸识别器
//// dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
// //加载人脸形状探测器
//// dlib::shape_predictor sp;
//// dlib::deserialize("./shape_predictor_68_face_landmarks.dat") >> sp;
// //Mat转化为dlib的matrix
//// dlib::array2d<dlib::bgr_pixel> dimg;
//// dlib::assign_image(dimg, dlib::cv_image<uchar>(dst));
//// //获取一系列人脸所在区域
//// std::vector<dlib::rectangle> dets = detector(dimg);
//// std::cout << "Number of faces detected: " << dets.size() << std::endl;
//// return dets;
//}
bool FaceIdCmp::FaceIdDetection(cv::Mat frame)
{
// std::vector<dlib::rectangle> dets = GetFaceArea(frame);
// if (dets.size() == 0)
// return false;
return true;
}
bool FaceIdCmp::FaceIdAreaMark(cv::Mat &frame)
{
// std::vector<dlib::rectangle> dets = GetFaceArea(frame);
// if (dets.size() == 0)
// return false;
// cv::Mat dst;
// cv::cvtColor(frame, dst, CV_BGR2GRAY);
// dlib::array2d<dlib::bgr_pixel> dimg;
// dlib::assign_image(dimg, dlib::cv_image<uchar>(dst));
// //获取人脸特征点分布
// std::vector<dlib::full_object_detection> shapes;
// int i = 0;
// for(i = 0; i < dets.size(); i++)
// {
// dlib::full_object_detection shape = predictor(dimg, dets[i]); //获取指定一个区域的人脸形状
// shapes.push_back(shape);
// }
// //指出每个检测到的人脸的位置
// for(i=0; i<dets.size(); i++)
// {
// //画出人脸所在区域
// cv::Rect r;
// r.x = dets[i].left();
// r.y = dets[i].top();
// r.width = dets[i].width();
// r.height = dets[i].height();
// cv::rectangle(frame, r, cv::Scalar(0, 0, 255), 1, 1, 0);
// }
return true;
}
//bool FaceIdCmp::SaveFaceIdInfo(QString Name,cv::Mat &frame)
//{
// QString FileName = QString(FACE_ROOT_DIR"/face_data") + "/" + Name + ".jpg";
// QFile File(Name);
// if(File.exists())
// {
// return false;
// }
// if(imwrite(FileName.toStdString(),frame))
// {
// BuildCmpLib();
// return true;
// }
// else
// {
// return false;
// }
//}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。