facerecognize.cpp
21.7 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#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(Mat loc,Mat score,Mat 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.at<float>(i,0)*variances[0]*priors[i][2];
float b2=priors[i][1]+loc.at<float>(i,1)*variances[0]*priors[i][3];
float b3=priors[i][2]*exp(loc.at<float>(i,2)*variances[1]);
float b4=priors[i][3]*exp(loc.at<float>(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.at<float>(i,0)*variances[0]*priors[i][2];
float l2=priors[i][1]+pre.at<float>(i,1)*variances[0]*priors[i][3];
float l3=priors[i][0]+pre.at<float>(i,2)*variances[0]*priors[i][2];
float l4=priors[i][1]+pre.at<float>(i,3)*variances[0]*priors[i][3];
float l5=priors[i][0]+pre.at<float>(i,4)*variances[0]*priors[i][2];
float l6=priors[i][1]+pre.at<float>(i,5)*variances[0]*priors[i][3];
float l7=priors[i][0]+pre.at<float>(i,6)*variances[0]*priors[i][2];
float l8=priors[i][1]+pre.at<float>(i,7)*variances[0]*priors[i][3];
float l9=priors[i][0]+pre.at<float>(i,8)*variances[0]*priors[i][2];
float l10=priors[i][1]+pre.at<float>(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=score.at<float>(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);
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);
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);
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;
}