facerecognize.cpp
8.25 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
#include "facerecognize.h"
cv::Mat FaceRecognize::meanAxis0(const cv::Mat &src)
{
int num = src.rows;
int dim = src.cols;
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::resize(image1,image1,Size2d(input_size[0],input_size[1]));
cv::resize(image2,image2,Size2d(input_size[0],input_size[1]));
image1.convertTo(image1, CV_32F);
image2.convertTo(image2, CV_32F);
image1 = (image1-mean)*scale;
image2 = (image2-mean)*scale;
std::vector<std::vector<cv::Mat>> nChannels1;
std::vector<cv::Mat> rgbChannels1(3);
cv::split(image1, rgbChannels1);
nChannels1.push_back(rgbChannels1); // NHWC 转NCHW
auto *pvData1 = malloc(1 * 3 * input_size[1] * input_size[0] *sizeof(float));
int nPlaneSize = input_size[0] * input_size[1];
for (int c = 0; c < 3; ++c)
{
cv::Mat matPlane1 = nChannels1[0][c];
memcpy((float *)(pvData1) + c * nPlaneSize,\
matPlane1.data, nPlaneSize * sizeof(float));
}
auto inTensor1 = net->getSessionInput(session1, NULL);
net->resizeTensor(inTensor1, {1, 3, input_size[1],input_size[0]});
net->resizeSession(session1);
auto nchwTensor1 = new Tensor(inTensor1, Tensor::CAFFE);
::memcpy(nchwTensor1->host<float>(), pvData1, nPlaneSize * 3 * sizeof(float));
inTensor1->copyFromHostTensor(nchwTensor1);
// //推理
net->runSession(session1);
auto output1= net->getSessionOutput(session1, NULL);
std::vector<std::vector<cv::Mat>> nChannels2;
std::vector<cv::Mat> rgbChannels2(3);
cv::split(image2, rgbChannels2);
nChannels2.push_back(rgbChannels2); // NHWC 转NCHW
auto *pvData2 = malloc(1 * 3 * input_size[1] * input_size[0] *sizeof(float));
for (int c = 0; c < 3; ++c)
{
cv::Mat matPlane2 = nChannels2[0][c];
memcpy((float *)(pvData2) + c * nPlaneSize,\
matPlane2.data, nPlaneSize * sizeof(float));
}
auto inTensor2 = net->getSessionInput(session2, NULL);
net->resizeTensor(inTensor2, {1, 3, input_size[1],input_size[0]});
net->resizeSession(session2);
auto nchwTensor2 = new Tensor(inTensor2, Tensor::CAFFE);
::memcpy(nchwTensor2->host<float>(), pvData2, nPlaneSize * 3 * sizeof(float));
inTensor2->copyFromHostTensor(nchwTensor2);
// //推理
net->runSession(session2);
auto output2= net->getSessionOutput(session2, NULL);
MNN::Tensor feat_tensor1(output1, MNN::Tensor::CAFFE);
MNN::Tensor feat_tensor2(output2, MNN::Tensor::CAFFE);
output1->copyToHostTensor(&feat_tensor1);
output2->copyToHostTensor(&feat_tensor2);
auto feature1 = feat_tensor1.host<float>();
auto feature2 = feat_tensor2.host<float>();
vector<double> v1,v2;
for(int i=0;i<int(feat_tensor1.size()/4);i++){
v1.push_back((double)feature1[i]);
v2.push_back((double)feature2[i]);
}
double cos_score=cos_distance(v1,v2);
return cos_score;
}