-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathMiniAIFaceEngine.cpp
334 lines (275 loc) · 8.61 KB
/
MiniAIFaceEngine.cpp
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
#pragma once
#include "stdafx.h"
#include "MiniAIFaceEngine.h"
#include <string>
#include <mutex>
#include <stdio.h>
using namespace std;
#define NSCALE 32
#define FACENUM 5
MiniAIFaceEngine::MiniAIFaceEngine()
{
}
MiniAIFaceEngine::~MiniAIFaceEngine()
{
}
MRESULT MiniAIFaceEngine::CheckActiveSDK()
{
char hwid[256] = { 0 };
MAIGetCurrentHWID(hwid, sizeof(hwid));
char license[1024] = { 0 };
FILE* fp;
if (fopen_s(&fp, "license.txt", "rb") == 0) {
//if (fopen_s(&fp, "license.txt", "rb") == 0) {
int fileLen = 0;
fseek(fp, 0, SEEK_END);
fileLen = ftell(fp);
fseek(fp, 0, SEEK_SET);
fread(license, __min(sizeof(license), fileLen), 1, fp);
fclose(fp);
}
int ret = MAISetSerialNumber(license);
return ret;
}
MRESULT MiniAIFaceEngine::SetActiveInfo(CString license, int licenseLen)
{
CFile file;
if (file.Open("license.txt", CFile::modeCreate | CFile::modeWrite))
{
file.Write(license, licenseLen);
file.Close();
}
return MOK;
}
MRESULT MiniAIFaceEngine::InitEngine(MAI_DetectMode detectMode)
{
m_hEngine = NULL;
MInt32 mask = 0;
if (MAI_DETECT_MODE_IMAGE == detectMode)
{
mask = MAI_FACE_DETECT | MAI_FACERECOGNITION | MAI_AGE | MAI_GENDER | MAI_FACE3DANGLE | MAI_LIVENESS | MAI_IR_LIVENESS;
}
else
{
mask = MAI_FACE_DETECT | MAI_FACERECOGNITION | MAI_LIVENESS | MAI_IR_LIVENESS;
}
MRESULT res = MAIInitEngine(detectMode, MAI_OP_0_ONLY, NSCALE, FACENUM, mask, &m_hEngine);
return res;
}
MRESULT MiniAIFaceEngine::FacePairMatching(MFloat &confidenceLevel, MAI_FaceFeature feature1,
MAI_FaceFeature feature2, MAI_CompareModel compareModel)
{
int res = MAIFaceFeatureCompare(m_hEngine, &feature1, &feature2, &confidenceLevel, compareModel);
return res;
}
MRESULT MiniAIFaceEngine::SetLivenessThreshold(MFloat rgbLiveThreshold, MFloat irLiveThreshold)
{
MAI_LivenessThreshold threshold = { 0 };
threshold.thresholdmodel_BGR = rgbLiveThreshold;
threshold.thresholdmodel_IR = irLiveThreshold;
int res = MAISetLivenessParam(m_hEngine, &threshold);
return res;
}
MRESULT MiniAIFaceEngine::FaceMAIProcess(MAI_MultiFaceInfo detectedFaces, IplImage *img, MAI_AgeInfo &ageInfo,
MAI_GenderInfo &genderInfo, MAI_Face3DAngle &angleInfo, MAI_LivenessInfo& liveNessInfo)
{
if (!img)
{
return -1;
}
MInt32 lastMask = MAI_AGE | MAI_GENDER | MAI_FACE3DANGLE | MAI_LIVENESS;
IplImage* cutImg = cvCreateImage(cvSize(img->width - (img->width % 4), img->height), IPL_DEPTH_8U, img->nChannels);
PicCutOut(img, cutImg, 0, 0);
ASVLOFFSCREEN offscreen = { 0 };
ColorSpaceConversion(cutImg, ASVL_PAF_RGB24_B8G8R8, offscreen);
if (!cutImg)
{
cvReleaseImage(&cutImg);
return -1;
}
int res = MAIProcessEx(m_hEngine, &offscreen, &detectedFaces, lastMask);
res = MAIGetAge(m_hEngine, &ageInfo);
res = MAIGetGender(m_hEngine, &genderInfo);
res = MAIGetFace3DAngle(m_hEngine, &angleInfo);
res = MAIGetLivenessScore(m_hEngine, &liveNessInfo);
cvReleaseImage(&cutImg);
return res;
}
MRESULT MiniAIFaceEngine::FaceMAIProcess_IR(MAI_MultiFaceInfo detectedFaces, IplImage *img, MAI_LivenessInfo& irLiveNessInfo)
{
if (!img)
{
return -1;
}
cv::Mat grayMat;
cv::Mat matImg(img, false); //IplImage to Mat is set to ture as deep copy
cv::cvtColor(matImg, grayMat, CV_BGR2GRAY);
IplImage* iplGrayMat = &IplImage(grayMat); //mat to IplImage shallow copy
MInt32 lastMask = MAI_IR_LIVENESS;
IplImage* cutGrayImg = cvCreateImage(cvSize(iplGrayMat->width - (iplGrayMat->width % 4), iplGrayMat->height),
IPL_DEPTH_8U, iplGrayMat->nChannels);
PicCutOut(iplGrayMat, cutGrayImg, 0, 0);
ASVLOFFSCREEN offscreen = { 0 };
ColorSpaceConversion(cutGrayImg, ASVL_PAF_GRAY, offscreen);
if (!cutGrayImg)
{
cvReleaseImage(&cutGrayImg);
return -1;
}
int res = MAIProcessEx_IR(m_hEngine, &offscreen, &detectedFaces, lastMask);
res = MAIGetLivenessScore_IR(m_hEngine, &irLiveNessInfo);
cvReleaseImage(&cutGrayImg);
return res;
}
const MAI_VERSION MiniAIFaceEngine::GetVersion()
{
const MAI_VERSION pVersionInfo = MAIGetVersion();
return pVersionInfo;
}
MRESULT MiniAIFaceEngine::PreDetectFace(IplImage* image, MAI_SingleFaceInfo& faceRect, bool isRGB)
{
if (!image)
{
return -1;
}
if (image->width <= 0 || image->height <= 0)
{
return -1;
}
IplImage* cutImg = NULL;
MRESULT res = MOK;
MAI_MultiFaceInfo detectedFaces = { 0 };//Face Detection
cout << image->width << "," << image->height;
if (isRGB)
{
cutImg = cvCreateImage(cvSize(image->width - (image->width % 4), image->height),
IPL_DEPTH_8U, image->nChannels);
PicCutOut(image, cutImg, 0, 0);
ASVLOFFSCREEN offscreen = { 0 };
ColorSpaceConversion(cutImg, ASVL_PAF_RGB24_B8G8R8, offscreen);
res = MAIDetectFacesEx(m_hEngine, &offscreen, &detectedFaces);
}
else //IR Image
{
cv::Mat grayMat;
cv::Mat matImg(image, false); //IplImage to Mat is set to ture as deep copy
cv::cvtColor(matImg, grayMat, CV_BGR2GRAY);
IplImage* iplGrayMat = &IplImage(grayMat); //mat to IplImage shallow copy
cutImg = cvCreateImage(cvSize(iplGrayMat->width - (iplGrayMat->width % 4), iplGrayMat->height),
IPL_DEPTH_8U, iplGrayMat->nChannels);
PicCutOut(iplGrayMat, cutImg, 0, 0);
ASVLOFFSCREEN offscreen = { 0 };
ColorSpaceConversion(cutImg, ASVL_PAF_GRAY, offscreen);
res = MAIDetectFacesEx(m_hEngine, &offscreen, &detectedFaces);
}
if (res != MOK || detectedFaces.faceNum < 1)
{
cvReleaseImage(&cutImg);
return -1;
}
int max = 0;
int maxArea = 0;
for (int i = 0; i < detectedFaces.faceNum; i++)
{
if (detectedFaces.faceRect[i].left < 0)
detectedFaces.faceRect[i].left = 10;
if (detectedFaces.faceRect[i].top < 0)
detectedFaces.faceRect[i].top = 10;
if (detectedFaces.faceRect[i].right < 0 || detectedFaces.faceRect[i].right > cutImg->width)
detectedFaces.faceRect[i].right = cutImg->width - 10;
if (detectedFaces.faceRect[i].bottom < 0 || detectedFaces.faceRect[i].bottom > cutImg->height)
detectedFaces.faceRect[i].bottom = cutImg->height - 10;
if ((detectedFaces.faceRect[i].right - detectedFaces.faceRect[i].left)*
(detectedFaces.faceRect[i].bottom - detectedFaces.faceRect[i].top) > maxArea)
{
max = i;
maxArea = (detectedFaces.faceRect[i].right - detectedFaces.faceRect[i].left)*
(detectedFaces.faceRect[i].bottom - detectedFaces.faceRect[i].top);
}
}
faceRect.faceRect.left = detectedFaces.faceRect[max].left;
faceRect.faceRect.top = detectedFaces.faceRect[max].top;
faceRect.faceRect.right = detectedFaces.faceRect[max].right;
faceRect.faceRect.bottom = detectedFaces.faceRect[max].bottom;
faceRect.faceOrient = detectedFaces.faceOrient[max];
cvReleaseImage(&cutImg);
return res;
}
// Pre-extract facial features
MRESULT MiniAIFaceEngine::PreExtractFeature(IplImage* image, MAI_FaceFeature& feature, MAI_SingleFaceInfo& faceRect)
{
if (!image || image->imageData == NULL)
{
return -1;
}
IplImage* cutImg = cvCreateImage(cvSize(image->width - (image->width % 4), image->height),
IPL_DEPTH_8U, image->nChannels);
PicCutOut(image, cutImg, 0, 0);
if (!cutImg)
{
cvReleaseImage(&cutImg);
return -1;
}
MRESULT res = MOK;
MAI_FaceFeature detectFaceFeature = { 0 };//feature
ASVLOFFSCREEN offscreen = { 0 };
ColorSpaceConversion(cutImg, ASVL_PAF_RGB24_B8G8R8, offscreen);
res = MAIFaceFeatureExtractEx(m_hEngine, &offscreen, &faceRect, &detectFaceFeature);
if (MOK != res)
{
cvReleaseImage(&cutImg);
return res;
}
if (!feature.feature)
{
return -1;
}
memset(feature.feature, 0, detectFaceFeature.featureSize);
memcpy(feature.feature, detectFaceFeature.feature, detectFaceFeature.featureSize);
cvReleaseImage(&cutImg);
return res;
}
MRESULT MiniAIFaceEngine::UnInitEngine()
{
//destroy engine
return MAIUninitEngine(m_hEngine);
}
void PicCutOut(IplImage* src, IplImage* dst, int x, int y)
{
if (!src || !dst)
{
return;
}
if (dst->width <=0 || dst->height <=0)
{
return;
}
CvSize size = cvSize(dst->width, dst->height);//area size
cvSetImageROI(src, cvRect(x, y, size.width, size.height));//set source image ROI
cvCopy(src, dst); //copy image
cvResetImageROI(src);//When the source image is used up, empty ROI
}
//color space conversion
int ColorSpaceConversion(IplImage* image, MInt32 format, ASVLOFFSCREEN& offscreen)
{
switch (format)
{
case ASVL_PAF_RGB24_B8G8R8:
offscreen.u32PixelArrayFormat = (unsigned int)format;
offscreen.i32Width = image->width;
offscreen.i32Height = image->height;
offscreen.pi32Pitch[0] = image->widthStep;
offscreen.ppu8Plane[0] = (MUInt8*)image->imageData;
break;
case ASVL_PAF_GRAY:
offscreen.u32PixelArrayFormat = (unsigned int)format;
offscreen.i32Width = image->width;
offscreen.i32Height = image->height;
offscreen.pi32Pitch[0] = image->widthStep;
offscreen.ppu8Plane[0] = (MUInt8*)image->imageData;
break;
default:
return 0;
}
return 1;
}