facecomparison.cpp
4.17 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
98
99
100
101
#include "facecomparison.h"
bool FaceComparison::inference(string image1_path,string image2_path){
bool result=false;
cv::Mat image1=cv::imread(image1_path);
cv::Mat image2=cv::imread(image2_path);
if(image1.empty()||image2.empty()){
return false;
}
vector<Bbox> box1=face_det.inference(image1);
vector<Bbox> box2=face_det.inference(image2);
if(box1.empty()||box2.empty()){
return false;
}
int max_box1=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=face_landm.inference(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=face_rec.preprocess_face(face_area1,land1);
for(int j=0;j<box2.size();++j){
Rect rect2=Rect(box2[j].xmin,box2[j].ymin,box2[j].xmax-box2[j].xmin,box2[j].ymax-box2[j].ymin);
Mat face_area2=image2(rect2);
vector<vector<float>> landms2=face_landm.inference(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=face_rec.preprocess_face(face_area2,land2);
double samilar_score=face_rec.inference(align_resize_image1,align_resize_image2);
if(samilar_score>face_recongnize_thr){
result=true;
}
}
return result;
}
bool FaceComparison::inference(Mat image1,Mat image2){
bool result=false;
vector<Bbox> box1=face_det.inference(image1);
vector<Bbox> box2=face_det.inference(image2);
if(box1.empty()||box2.empty()){
return false;
}
int max_box1=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=face_landm.inference(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=face_rec.preprocess_face(face_area1,land1);
for(int j=0;j<box2.size();++j){
Rect rect2=Rect(box2[j].xmin,box2[j].ymin,box2[j].xmax-box2[j].xmin,box2[j].ymax-box2[j].ymin);
Mat face_area2=image2(rect2);
vector<vector<float>> landms2=face_landm.inference(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=face_rec.preprocess_face(face_area2,land2);
double samilar_score=face_rec.inference(align_resize_image1,align_resize_image2);
if(samilar_score>face_recongnize_thr){
result=true;
}
}
return result;
}