c7e48e8e by 乔峰昇

the first submit

0 parents
opencv4.5.5
模型初始化
FaceRecognize face_rec=FaceRecognize(det_model_path,landm_model_path,rec_model_path)
det_model_path:人脸检测模型retinaface的onnx模型路径
landm_model_path:106人脸关键点模型的onnx模型路径
rec_model_path:人脸识别模型的onnx模型路径
重要参数(.h文件)
bool use_gpu=true; //是否使用gpu
float confidence_threshold = 0.5; //人脸检测阈值
float nms_threshold = 0.4; //nms阈值
double face_recongnize_thr = 0.2; //人脸相似度阈值
接口(返回结果 bool:true/false)
bool face_recognize(string image1_path,string image2_path);参数为两张图像地址,其中iamge1_path为face_id图像输入
bool face_recognize_image(Mat image1,Mat image2);参数为两张opencv读取的图像矩阵,其中iamge1为face_id图像输入
编译
g++ main.cpp -L . -lfacerecognize -o main
如果报错无法找到改用:g++ main.cpp -L . -lfacerecognize -o main pkg-config --libs --cflags opencv4
./main
No preview for this file type
No preview for this file type
#include "facerecognize.h"
#include<iostream>
//人脸检测部分
// 生成anchors
vector<vector<float>> FaceRecognize::priorBox(vector<float> image_size){
vector<int> tmp1={16,32};
vector<int> tmp2={64,128};
vector<int> tmp3={256,512};
vector<vector<int>> min_sizes_;
min_sizes_.push_back(tmp1);
min_sizes_.push_back(tmp2);
min_sizes_.push_back(tmp3);
vector<int> steps={8,16,32};
vector<vector<int>> feature_maps;
vector<vector<float>> anchors;
for(int &step:steps){
vector<int> tmp(2,0);
tmp[0]=ceil(image_size[0]/step);
tmp[1]=ceil(image_size[1]/step);
feature_maps.push_back(tmp);
}
for(int k=0;k<feature_maps.size();k++){
vector<int> min_sizes=min_sizes_[k];
for(int i=0;i<feature_maps[k][0];i++){
for(int j=0;j<feature_maps[k][1];j++){
for(int &min_size:min_sizes){
float s_kx=float(min_size)/float(image_size[1]);
float s_ky=float(min_size)/float(image_size[0]);
float dense_cx=float((float(j)+float(0.5))*steps[k])/float(image_size[1]);
float dense_cy=float((float(i)+float(0.5))*steps[k])/float(image_size[1]);
vector<float> tmp_anchor={dense_cx,dense_cy,s_kx,s_ky};
anchors.push_back(tmp_anchor);
}
}
}
}
return anchors;
}
// 解析bounding box 包含置信度
vector<Bbox> FaceRecognize::decode(vector<vector<float>> loc,vector<vector<float>> score,vector<vector<float>>pre,vector<vector<float>> priors,vector<float> variances){
vector<Bbox> boxes;
for(int i=0;i<priors.size();++i){
float b1=priors[i][0]+loc[i][0]*variances[0]*priors[i][2];
float b2=priors[i][1]+loc[i][1]*variances[0]*priors[i][3];
float b3=priors[i][2]*exp(loc[i][2]*variances[1]);
float b4=priors[i][3]*exp(loc[i][3]*variances[1]);
b1=b1-b3/float(2);
b2=b2-b4/float(2);
b3=b3+b1;
b4=b4+b2;
float l1=priors[i][0]+pre[i][0]*variances[0]*priors[i][2];
float l2=priors[i][1]+pre[i][1]*variances[0]*priors[i][3];
float l3=priors[i][0]+pre[i][2]*variances[0]*priors[i][2];
float l4=priors[i][1]+pre[i][3]*variances[0]*priors[i][3];
float l5=priors[i][0]+pre[i][4]*variances[0]*priors[i][2];
float l6=priors[i][1]+pre[i][5]*variances[0]*priors[i][3];
float l7=priors[i][0]+pre[i][6]*variances[0]*priors[i][2];
float l8=priors[i][1]+pre[i][7]*variances[0]*priors[i][3];
float l9=priors[i][0]+pre[i][8]*variances[0]*priors[i][2];
float l10=priors[i][1]+pre[i][9]*variances[0]*priors[i][3];
Bbox tmp_box={.xmin=b1*input_size[0]/resize_scale,.ymin=b2*input_size[1]/resize_scale,.xmax=b3*input_size[0]/resize_scale,.ymax=b4*input_size[1]/resize_scale,
.score=float(score[i][0]),.x1=(l1*input_size[0])/resize_scale,.y1=l2*input_size[1]/resize_scale,.x2=l3*input_size[0]/resize_scale,.y2=l4*input_size[1]/resize_scale,
.x3=l5*input_size[0]/resize_scale,.y3=l6*input_size[1]/resize_scale,.x4=l7*input_size[0]/resize_scale,.y4=l8*input_size[1]/resize_scale,.x5=l9*input_size[0]/resize_scale,.y5=l10*input_size[1]/resize_scale};
boxes.push_back(tmp_box);
}
return boxes;
}
//NMS
void FaceRecognize::nms_cpu(std::vector<Bbox> &bboxes, float threshold){
if (bboxes.empty()){
return ;
}
// 1.之前需要按照score排序
std::sort(bboxes.begin(), bboxes.end(), [&](Bbox b1, Bbox b2){return b1.score>b2.score;});
// 2.先求出所有bbox自己的大小
std::vector<float> area(bboxes.size());
for (int i=0; i<bboxes.size(); ++i){
area[i] = (bboxes[i].xmax - bboxes[i].xmin + 1) * (bboxes[i].ymax - bboxes[i].ymin + 1);
}
// 3.循环
for (int i=0; i<bboxes.size(); ++i){
for (int j=i+1; j<bboxes.size(); ){
float left = std::max(bboxes[i].xmin, bboxes[j].xmin);
float right = std::min(bboxes[i].xmax, bboxes[j].xmax);
float top = std::max(bboxes[i].ymin, bboxes[j].ymin);
float bottom = std::min(bboxes[i].ymax, bboxes[j].ymax);
float width = std::max(right - left + 1, 0.f);
float height = std::max(bottom - top + 1, 0.f);
float u_area = height * width;
float iou = (u_area) / (area[i] + area[j] - u_area);
if (iou>=threshold){
bboxes.erase(bboxes.begin()+j);
area.erase(area.begin()+j);
}else{
++j;
}
}
}
}
// Mat转vector
vector<vector<float>> FaceRecognize::mat2vector(Mat mat){
vector<vector<float>> vec;
for(int i=0;i<mat.rows;++i){
vector<float> m;
for(int j=0;j<mat.cols;++j){
m.push_back(mat.at<float>(i,j));
}
vec.push_back(m);
}
return vec;
}
// 根据阈值筛选
vector<Bbox> FaceRecognize::select_score(vector<Bbox> bboxes,float threshold,float w_r,float h_r){
vector<Bbox> results;
for(Bbox &box:bboxes){
if (float(box.score)>=threshold){
box.xmin=box.xmin/w_r;
box.ymin=box.ymin/h_r;
box.xmax=box.xmax/w_r;
box.ymax=box.ymax/h_r;
box.x1=box.x1/w_r;
box.y1=box.y1/h_r;
box.x2=box.x2/w_r;
box.y2=box.y2/h_r;
box.x3=box.x3/w_r;
box.y3=box.y3/h_r;
box.x4=box.x4/w_r;
box.y4=box.y4/h_r;
box.x5=box.x5/w_r;
box.y5=box.y5/h_r;
results.push_back(box);
}
}
return results;
}
// 数据后处理
vector<Bbox> FaceRecognize::bbox_process(vector<Bbox> bboxes,float frame_w,float frame_h){
vector<Bbox> result_bboxes;
for(Bbox &bbox:bboxes){
Bbox new_bbox;
float face_w=bbox.xmax-bbox.xmin;
float face_h=bbox.ymax-bbox.ymin;
new_bbox.xmin=bbox.xmin-face_w*0.15;
new_bbox.xmax=bbox.xmax+face_w*0.15;
new_bbox.ymin=bbox.ymin;
new_bbox.ymax=bbox.ymax+face_h*0.15;
new_bbox.xmin=new_bbox.xmin>0?new_bbox.xmin:0;
new_bbox.ymin=new_bbox.ymin>0?new_bbox.ymin:0;
new_bbox.xmax=new_bbox.xmax>frame_w?frame_w:new_bbox.xmax;
new_bbox.ymax=new_bbox.ymax>frame_h?frame_h:new_bbox.ymax;
new_bbox.score=bbox.score;
new_bbox.x1=bbox.x1>0?bbox.x1:0;
new_bbox.y1=bbox.y1>0?bbox.y1:0;
new_bbox.x2=bbox.x2>0?bbox.x2:0;
new_bbox.y2=bbox.y2>0?bbox.y2:0;
new_bbox.x3=bbox.x3>0?bbox.x3:0;
new_bbox.y3=bbox.y3>0?bbox.y3:0;
new_bbox.x4=bbox.x4>0?bbox.x4:0;
new_bbox.y4=bbox.y4>0?bbox.y4:0;
new_bbox.x5=bbox.x5>0?bbox.x5:0;
new_bbox.y5=bbox.y5>0?bbox.y5:0;
result_bboxes.push_back(new_bbox);
}
return result_bboxes;
}
// 推理
vector<Bbox> FaceRecognize::detect(string image_path){
cv::Mat image = cv::imread(image_path); // 读取图片
float w_r=float(640)/float(image.cols);
float h_r=float(640)/float(image.rows);
cv::Mat blob = cv::dnn::blobFromImage(image,resize_scale,Size(input_size[0],input_size[1]),Scalar(mean_data[0],mean_data[1],mean_data[2])); // 由图片加载数据 这里还可以进行缩放、归一化等预处理
det_net.setInput(blob); // 设置模型输入
std::vector<cv::Mat> det_netOutputImg;
det_net.forward(det_netOutputImg,det_net.getUnconnectedOutLayersNames()); // 推理出结果
cv::Mat scores_ = det_netOutputImg[2].reshape(1,16800);
cv::Mat boxes_ = det_netOutputImg[0].reshape(1,16800);
cv::Mat landms_ = det_netOutputImg[1].reshape(1,16800);
std::vector<vector<float>> scores,boxes,landms;
scores=mat2vector(scores_);
boxes=mat2vector(boxes_);
landms=mat2vector(landms_);
vector<vector<float>> anchors=priorBox(input_size);
vector<Bbox> result_boxes=decode(boxes,scores,landms,anchors,variances);
vector<Bbox> results=select_score(result_boxes,confidence_threshold,w_r,h_r);
// vector<Bbox> result_bboxes=vec2Bbox(boxes,w_r,h_r);
nms_cpu(results,nms_threshold);
if(is_bbox_process){
vector<Bbox> res_bboxes=bbox_process(results,input_size[0],input_size[1]);
return res_bboxes;
}else{
return results;
}
}
vector<Bbox> FaceRecognize::detect_image(Mat image){
float w_r=float(640)/float(image.cols);
float h_r=float(640)/float(image.rows);
cv::Mat blob = cv::dnn::blobFromImage(image,resize_scale,Size(input_size[0],input_size[1]),Scalar(mean_data[0],mean_data[1],mean_data[2])); // 由图片加载数据 这里还可以进行缩放、归一化等预处理
det_net.setInput(blob); // 设置模型输入
std::vector<cv::Mat> det_netOutputImg;
det_net.forward(det_netOutputImg,det_net.getUnconnectedOutLayersNames()); // 推理出结果
cv::Mat scores_ = det_netOutputImg[2].reshape(1,16800);
cv::Mat boxes_ = det_netOutputImg[0].reshape(1,16800);
cv::Mat landms_ = det_netOutputImg[1].reshape(1,16800);
std::vector<vector<float>> scores,boxes,landms;
scores=mat2vector(scores_);
boxes=mat2vector(boxes_);
landms=mat2vector(landms_);
vector<vector<float>> anchors=priorBox(input_size);
vector<Bbox> result_boxes=decode(boxes,scores,landms,anchors,variances);
vector<Bbox> results=select_score(result_boxes,confidence_threshold,w_r,h_r);
// vector<Bbox> result_bboxes=vec2Bbox(boxes,w_r,h_r);
nms_cpu(results,nms_threshold);
if(is_bbox_process){
vector<Bbox> res_bboxes=bbox_process(results,image.cols,image.rows);
return res_bboxes;
}else{
return results;
}
}
//人脸关键点部分
vector<vector<float>> FaceRecognize::detect_landmarks(string image_path){
cv::Mat image=cv::imread(image_path);
float w_r=image.cols/112;
float h_r=image.rows/112;
cv::Mat blob = cv::dnn::blobFromImage(image,1.0,Size(112,112)); // 由图片加载数据 这里还可以进行缩放、归一化等预处理
landm_net.setInput(blob); // 设置模型输入
std::vector<cv::Mat> landm_netOutputImg;
landm_net.forward(landm_netOutputImg,landm_net.getUnconnectedOutLayersNames()); // 推理出结果
cv::Mat predict_landmarks = landm_netOutputImg[0].reshape(1,106);
vector<vector<float>> result_landmarks;
for(int i=0;i<predict_landmarks.rows;++i){
vector<float> tmp_landm={predict_landmarks.at<float>(i,0)*w_r,predict_landmarks.at<float>(i,1)*h_r};
result_landmarks.push_back(tmp_landm);
}
return result_landmarks;
}
vector<vector<float>> FaceRecognize::detect_image_landmarks(cv::Mat image){
float w_r=image.cols/float(112);
float h_r=image.rows/float(112);
cv::Mat blob = cv::dnn::blobFromImage(image,1.0,Size(112,112)); // 由图片加载数据 这里还可以进行缩放、归一化等预处理
landm_net.setInput(blob); // 设置模型输入
std::vector<cv::Mat> landm_netOutputImg;
landm_net.forward(landm_netOutputImg,landm_net.getUnconnectedOutLayersNames()); // 推理出结果
cv::Mat predict_landmarks = landm_netOutputImg[0].reshape(1,106);
vector<vector<float>> result_landmarks;
for(int i=0;i<predict_landmarks.rows;++i){
vector<float> tmp_landm={predict_landmarks.at<float>(i,0)*w_r,predict_landmarks.at<float>(i,1)*h_r};
result_landmarks.push_back(tmp_landm);
}
return result_landmarks;
}
//人脸识别部分
cv::Mat FaceRecognize::meanAxis0(const cv::Mat &src)
{
int num = src.rows;
int dim = src.cols;
// x1 y1
// x2 y2
cv::Mat output(1,dim,CV_32F);
for(int i = 0 ; i < dim; i ++)
{
float sum = 0 ;
for(int j = 0 ; j < num ; j++)
{
sum+=src.at<float>(j,i);
}
output.at<float>(0,i) = sum/num;
}
return output;
}
cv::Mat FaceRecognize::elementwiseMinus(const cv::Mat &A,const cv::Mat &B)
{
cv::Mat output(A.rows,A.cols,A.type());
assert(B.cols == A.cols);
if(B.cols == A.cols)
{
for(int i = 0 ; i < A.rows; i ++)
{
for(int j = 0 ; j < B.cols; j++)
{
output.at<float>(i,j) = A.at<float>(i,j) - B.at<float>(0,j);
}
}
}
return output;
}
cv::Mat FaceRecognize::varAxis0(const cv::Mat &src)
{
cv:Mat temp_ = elementwiseMinus(src,meanAxis0(src));
cv::multiply(temp_ ,temp_ ,temp_ );
return meanAxis0(temp_);
}
int FaceRecognize::MatrixRank(cv::Mat M)
{
Mat w, u, vt;
SVD::compute(M, w, u, vt);
Mat1b nonZeroSingularValues = w > 0.0001;
int rank = countNonZero(nonZeroSingularValues);
return rank;
}
cv::Mat FaceRecognize::similarTransform(cv::Mat src,cv::Mat dst) {
int num = src.rows;
int dim = src.cols;
cv::Mat src_mean = meanAxis0(src);
cv::Mat dst_mean = meanAxis0(dst);
cv::Mat src_demean = elementwiseMinus(src, src_mean);
cv::Mat dst_demean = elementwiseMinus(dst, dst_mean);
cv::Mat A = (dst_demean.t() * src_demean) / static_cast<float>(num);
cv::Mat d(dim, 1, CV_32F);
d.setTo(1.0f);
if (cv::determinant(A) < 0) {
d.at<float>(dim - 1, 0) = -1;
}
Mat T = cv::Mat::eye(dim + 1, dim + 1, CV_32F);
cv::Mat U, S, V;
SVD::compute(A, S,U, V);
// the SVD function in opencv differ from scipy .
int rank = MatrixRank(A);
if (rank == 0) {
assert(rank == 0);
} else if (rank == dim - 1) {
if (cv::determinant(U) * cv::determinant(V) > 0) {
T.rowRange(0, dim).colRange(0, dim) = U * V;
} else {
int s = d.at<float>(dim - 1, 0) = -1;
d.at<float>(dim - 1, 0) = -1;
T.rowRange(0, dim).colRange(0, dim) = U * V;
cv::Mat diag_ = cv::Mat::diag(d);
cv::Mat twp = diag_*V; //np.dot(np.diag(d), V.T)
Mat B = Mat::zeros(3, 3, CV_8UC1);
Mat C = B.diag(0);
T.rowRange(0, dim).colRange(0, dim) = U* twp;
d.at<float>(dim - 1, 0) = s;
}
}
else{
cv::Mat diag_ = cv::Mat::diag(d);
cv::Mat twp = diag_*V.t(); //np.dot(np.diag(d), V.T)
cv::Mat res = U* twp; // U
T.rowRange(0, dim).colRange(0, dim) = -U.t()* twp;
}
cv::Mat var_ = varAxis0(src_demean);
float val = cv::sum(var_).val[0];
cv::Mat res;
cv::multiply(d,S,res);
float scale = 1.0/val*cv::sum(res).val[0];
T.rowRange(0, dim).colRange(0, dim) = - T.rowRange(0, dim).colRange(0, dim).t();
cv::Mat temp1 = T.rowRange(0, dim).colRange(0, dim); // T[:dim, :dim]
cv::Mat temp2 = src_mean.t(); //src_mean.T
cv::Mat temp3 = temp1*temp2; // np.dot(T[:dim, :dim], src_mean.T)
cv::Mat temp4 = scale*temp3;
T.rowRange(0, dim).colRange(dim, dim+1)= -(temp4 - dst_mean.t()) ;
T.rowRange(0, dim).colRange(0, dim) *= scale;
return T;
}
Mat FaceRecognize::preprocess_face(Mat image,vector<vector<float>> land){
Mat out;
cv::resize(image,out,Size(112,112));
float default1[5][2] = {
{38.2946f, 51.6963f},
{73.5318f, 51.6963f},
{56.0252f, 71.7366f},
{41.5493f, 92.3655f},
{70.7299f, 92.3655f}
};
float lands[5][2]={
{float(land[0][0]*112.0)/float(image.cols),float(land[0][1]*112.0)/float(image.rows)},
{float(land[1][0]*112.0)/float(image.cols),float(land[1][1]*112.0)/float(image.rows)},
{float(land[2][0]*112.0)/float(image.cols),float(land[2][1]*112.0)/float(image.rows)},
{float(land[3][0]*112.0)/float(image.cols),float(land[3][1]*112.0)/float(image.rows)},
{float(land[4][0]*112.0)/float(image.cols),float(land[4][1]*112.0)/float(image.rows)}
};
cv::Mat src(5,2,CV_32FC1,default1);
memcpy(src.data, default1, 2 * 5 * sizeof(float));
cv::Mat dst(5,2,CV_32FC1,lands);
memcpy(dst.data, lands, 2 * 5 * sizeof(float));
cv::Mat M = similarTransform(dst, src);
float M_[2][3]={
{M.at<float>(0,0),M.at<float>(0,1),M.at<float>(0,2)},
{M.at<float>(1,0),M.at<float>(1,1),M.at<float>(1,2)},
};
cv::Mat M__(2,3,CV_32FC1,M_);
cv::Mat align_image;
cv::warpAffine(out,align_image,M__,Size(112, 112));
return align_image;
}
double FaceRecognize::getMold(const vector<double>& vec)
{
int n = vec.size();
double sum = 0.0;
for (int i = 0; i < n; ++i)
sum += vec[i] * vec[i];
return sqrt(sum);
}
double FaceRecognize::cos_distance(const vector<double>& base, const vector<double>& target)
{
int n = base.size();
assert(n == target.size());
double tmp = 0.0;
for (int i = 0; i < n; ++i)
tmp += base[i] * target[i];
double simility = tmp / (getMold(base)*getMold(target));
return simility;
}
double FaceRecognize::get_samilar(Mat image1,Mat image2){
cv::Mat blob1 = cv::dnn::blobFromImage(image1,1.0/127.5,Size(112,112),Scalar(127.5,127.5,127.5),true); // 由图片加载数据 这里还可以进行缩放、归一化等预处理
rec_net.setInput(blob1); // 设置模型输入
std::vector<cv::Mat> rec_netOutputImg1;
rec_net.forward(rec_netOutputImg1,rec_net.getUnconnectedOutLayersNames()); // 推理出结果
cv::Mat blob2 = cv::dnn::blobFromImage(image2,1.0/127.5,Size(112,112),Scalar(127.5,127.5,127.5),true); // 由图片加载数据 这里还可以进行缩放、归一化等预处理
rec_net.setInput(blob2); // 设置模型输入
std::vector<cv::Mat> rec_netOutputImg2;
rec_net.forward(rec_netOutputImg2,rec_net.getUnconnectedOutLayersNames()); // 推理出结果
cv::Mat feature1=rec_netOutputImg1[0].reshape(1,512);
cv::Mat feature2=rec_netOutputImg2[0].reshape(1,512);
vector<double> v1,v2;
for(int i=0;i<feature1.rows;i++){
v1.push_back((double)feature1.at<float>(i,0));
v2.push_back((double)feature2.at<float>(i,0));
}
double cos_score=cos_distance(v1,v2);
return cos_score;
}
//整体pipeline
bool FaceRecognize::face_recognize(string image1_path,string image2_path){
bool result=false;
cv::Mat image1=cv::imread(image1_path);
cv::Mat image2=cv::imread(image2_path);
vector<Bbox> box1=detect_image(image1);
vector<Bbox> box2=detect_image(image2);
int max_box1=0,max_box2=0;
double max_area1=0,max_area2=0;
for(int i=0;i<box1.size();++i){
double tmp_area1=(box1[i].ymax-box1[i].ymin)*(box1[i].xmax-box1[i].xmin);
if(tmp_area1>max_area1){
max_box1=i;
max_area1=tmp_area1;
}
}
Rect rect1=Rect(box1[max_box1].xmin,box1[max_box1].ymin,box1[max_box1].xmax-box1[max_box1].xmin,box1[max_box1].ymax-box1[max_box1].ymin);
Mat face_area1=image1(rect1);
vector<vector<float>> landms1=detect_image_landmarks(face_area1);
vector<vector<float>> land1={
{float(landms1[104][0]),float(landms1[104][1])},
{float(landms1[105][0]),float(landms1[105][1])},
{float(landms1[46][0]),float(landms1[46][1])},
{float(landms1[84][0]),float(landms1[84][1])},
{float(landms1[90][0]),float(landms1[90][1])}
};
Mat align_resize_image1=preprocess_face(face_area1,land1);
for(int j=0;j<box2.size();++j){
Rect rect2=Rect(box2[max_box2].xmin,box2[max_box2].ymin,box2[max_box2].xmax-box2[max_box2].xmin,box2[max_box2].ymax-box2[max_box2].ymin);
Mat face_area2=image2(rect2);
vector<vector<float>> landms2=detect_image_landmarks(face_area2);
vector<vector<float>> land2={
{float(landms2[104][0]),float(landms2[104][1])},
{float(landms2[105][0]),float(landms2[105][1])},
{float(landms2[46][0]),float(landms2[46][1])},
{float(landms2[84][0]),float(landms2[84][1])},
{float(landms2[90][0]),float(landms2[90][1])}
};
Mat align_resize_image2=preprocess_face(face_area2,land2);
double samilar_score=get_samilar(align_resize_image1,align_resize_image2);
if(samilar_score>face_recongnize_thr){
result=true;
}
}
return result;
}
bool FaceRecognize::face_recognize_image(Mat image1,Mat image2){
bool result=false;
vector<Bbox> box1=detect_image(image1);
vector<Bbox> box2=detect_image(image2);
int max_box1=0,max_box2=0;
double max_area1=0,max_area2=0;
for(int i=0;i<box1.size();++i){
double tmp_area1=(box1[i].ymax-box1[i].ymin)*(box1[i].xmax-box1[i].xmin);
if(tmp_area1>max_area1){
max_box1=i;
max_area1=tmp_area1;
}
}
Rect rect1=Rect(box1[max_box1].xmin,box1[max_box1].ymin,box1[max_box1].xmax-box1[max_box1].xmin,box1[max_box1].ymax-box1[max_box1].ymin);
Mat face_area1=image1(rect1);
vector<vector<float>> landms1=detect_image_landmarks(face_area1);
vector<vector<float>> land1={
{float(landms1[104][0]),float(landms1[104][1])},
{float(landms1[105][0]),float(landms1[105][1])},
{float(landms1[46][0]),float(landms1[46][1])},
{float(landms1[84][0]),float(landms1[84][1])},
{float(landms1[90][0]),float(landms1[90][1])}
};
Mat align_resize_image1=preprocess_face(face_area1,land1);
for(int j=0;j<box2.size();++j){
Rect rect2=Rect(box2[max_box2].xmin,box2[max_box2].ymin,box2[max_box2].xmax-box2[max_box2].xmin,box2[max_box2].ymax-box2[max_box2].ymin);
Mat face_area2=image2(rect2);
vector<vector<float>> landms2=detect_image_landmarks(face_area2);
vector<vector<float>> land2={
{float(landms2[104][0]),float(landms2[104][1])},
{float(landms2[105][0]),float(landms2[105][1])},
{float(landms2[46][0]),float(landms2[46][1])},
{float(landms2[84][0]),float(landms2[84][1])},
{float(landms2[90][0]),float(landms2[90][1])}
};
Mat align_resize_image2=preprocess_face(face_area2,land2);
double samilar_score=get_samilar(align_resize_image1,align_resize_image2);
if(samilar_score>face_recongnize_thr){
result=true;
}
}
return result;
}
\ No newline at end of file
#ifndef FACERECOGNIZE_H
#define FACERECOGNIZE_H
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
struct Bbox{
float xmin;
float ymin;
float xmax;
float ymax;
float score;
float x1;
float y1;
float x2;
float y2;
float x3;
float y3;
float x4;
float y4;
float x5;
float y5;
};
class FaceRecognize{
private:
//是否使用gpu?
bool use_gpu=true;
//人脸检测
vector<float> input_size={640,640};
vector<float> variances={0.1,0.2};
vector<float> mean_data={104,117,128};
float confidence_threshold = 0.5; //人脸检测阈值
float keep_top_k = 100;
float vis_threshold = 0.5;
float nms_threshold = 0.4;
float resize_scale = 1.0;
bool is_bbox_process=true; //人脸外扩
//人脸识别
double face_recongnize_thr = 0.2; //人脸相似度阈值
cv::dnn::Net det_net,landm_net,rec_net;
public:
FaceRecognize();
FaceRecognize(string face_det_model_path,string face_landm_model_path,string face_rec_model_path){
det_net = cv::dnn::readNetFromONNX(face_det_model_path);
landm_net = cv::dnn::readNetFromONNX(face_landm_model_path);
rec_net = cv::dnn::readNetFromONNX(face_rec_model_path);
if(use_gpu){
det_net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
det_net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
landm_net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
landm_net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
rec_net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
rec_net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
}
}
//人脸检测部分
vector<vector<float>> priorBox(vector<float> image_size);
vector<Bbox> decode(vector<vector<float>> loc,vector<vector<float>> score,vector<vector<float>>pre,vector<vector<float>> priors,vector<float> variances);
void nms_cpu(std::vector<Bbox> &bboxes, float threshold);
vector<vector<float>> mat2vector(Mat mat);
vector<Bbox> select_score(vector<Bbox> bboxes,float threshold,float w_r,float h_r);
vector<Bbox> bbox_process(vector<Bbox> bboxes,float frame_w,float frame_h);
vector<Bbox> detect(string image_path);
vector<Bbox> detect_image(Mat image);
//人脸关键点部分
vector<vector<float>> detect_landmarks(string image_path);
vector<vector<float>> detect_image_landmarks(cv::Mat image);
//人脸识部分
cv::Mat meanAxis0(const cv::Mat &src);
cv::Mat elementwiseMinus(const cv::Mat &A,const cv::Mat &B);
cv::Mat varAxis0(const cv::Mat &src);
int MatrixRank(cv::Mat M);
cv::Mat similarTransform(cv::Mat src,cv::Mat dst);
Mat preprocess_face(Mat image,vector<vector<float>> land);
double getMold(const vector<double>& vec);
double cos_distance(const vector<double>& base, const vector<double>& target);
double get_samilar(Mat image1,Mat image2);
//整体
bool face_recognize(string image1_path,string image2_path);
bool face_recognize_image(Mat image1,Mat image2);
};
#endif
\ No newline at end of file
No preview for this file type
No preview for this file type
#include"facerecognize.h"
#include "ctime"
int main(){
FaceRecognize face_rec=FaceRecognize("/home/situ/qfs/sdk_project/mobile_face_recognize/det_face_retina_torch_1.4_v0.0.2.onnx","/home/situ/qfs/sdk_project/mobile_face_recognize/det_landmarks_106_v0.0.1.onnx","/home/situ/qfs/sdk_project/mobile_face_recognize/ms1mv3_r18.onnx");
clock_t start, end;
cv::Mat image1=cv::imread("/data/face_recognize/pipeline_test/59297ec0094211ecaf3d00163e514671/310faceImageContent163029410817774.jpg");
cv::Mat image2=cv::imread("/data/face_recognize/pipeline_test/59297ec0094211ecaf3d00163e514671/310cardImageContent163029410836583.jpg");
cout<<"start"<<endl;
start = clock();
bool result=face_rec.face_recognize_image(image1,image2);
end = clock();
double elapsedTime = static_cast<double>(end-start) / CLOCKS_PER_SEC ;
printf("PROCESSING TIME: %f",elapsedTime);
}
This file is too large to display.
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!