facerecognize.h
3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#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);
}
}
private:
//人脸检测部分
vector<vector<float>> priorBox(vector<float> image_size);
vector<Bbox> decode(Mat loc,Mat score,Mat 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);
public:
//人脸识别整体推理
bool face_recognize(string image1_path,string image2_path);
bool face_recognize_image(Mat image1,Mat image2);
};
#endif