liuxiaolong
2019-05-06 573d1e33a32da5c55c638df2ee622d972976c1d0
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
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
 
#include "RgbDetect.h"
#include "GrayDetect.h"
#include "LivenessDetect.h"
 
#include "FiStdDefEx.h"
//#include "THFaceLive_i.h"
 
 
#include <memory>
#include "THFeature_i.h"
#include "FaceDetectHelper.h"
#include "LogUtil.h"
#include <memory>
#include <sstream>
#include "TrackingTrigger.h"
#include <map>
 
#include <memory>
#include "THFeature_i.h"
#include <sys/time.h>
#include <time.h>
#include <iostream>
#include <string>
#include <cstring>
 
 
#include <android/log.h>
 
//定义TAG之后,我们可以在LogCat通过TAG过滤出NDK打印的日志
#define TAG "jni ndk "
// 定义info信息
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__)
// 定义debug信息
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
// 定义error信息
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__)
 
using namespace std;
 
FaceDetectHelper *FaceDetectHelper::instance = nullptr;
 
FaceDetectHelper *FaceDetectHelper::getInstance() {
    if (instance == nullptr) {
        instance = new FaceDetectHelper();
    }
    return instance;
}
 
//get current system time
double msecond()
{
    struct timeval tv;
    gettimeofday(&tv, 0);
    return (tv.tv_sec * 1.0e3 + tv.tv_usec * 1.0e-3);
}
 
FaceDetectHelper::FaceDetectHelper() {}
 
BYTE* feature1 = nullptr;
BYTE* idcard_feature = nullptr;
int featureSize = 0;
bool FaceDetectHelper::initSdk(const char* modelPath) {
    if (!init_sdk_success) {
        THFI_Param detParam;
        detParam.nMinFaceSize = 50;
        detParam.nRollAngle = 30;
        detParam.bOnlyDetect = false;
        detParam.dwReserved = NULL;
        THFI_SetDir(modelPath, modelPath);
        int ret = THFI_Create(1, &detParam);
        if (ret < 0) {
            printf("THFI_Create failed!(ret=%d)\n", ret);
            init_sdk_success = false;
            return init_sdk_success;
        }
//        THFL_SDK_SetDir(modelPath, modelPath);
//        ret = THFL_Create(1);
//        if (ret < 0) {
//            printf("THFL_Create failed!(ret=%d)\n", ret);
//            THFI_Release();
//            init_sdk_success = false;
//            return init_sdk_success;
//        }
        EF_SetDir(modelPath, modelPath);
        ret=EF_Init(1);
        if (ret < 0)
        {
            printf("EF_Init failed!(ret=%d)\n",ret);
            THFI_Release();
            init_sdk_success = false;
            return init_sdk_success;
        }
        featureSize = EF_Size();
        feature1 = new BYTE[featureSize];
        idcard_feature = new BYTE[featureSize];
        init_sdk_success = true;
    }
    return init_sdk_success;
}
 
TrackingTrigger trackingTrigger(0.8);
ScoredRect sr;
long track_id[10];
bool liveness[10];
string featureNames[10];
 
double prevExtractFeatureTime=0;
 
string
FaceDetectHelper::detectRealFace(const char* modelPath, const char *rgbFileName, const char *grayFileName, int width,
                                 int height, const char* baseFeatureName, int shouldExtractFeature, bool useGrayCamera, int detectFaceCount) {
    int featureCount = 0;
    string faces = "";
    if (rgbFileName != nullptr)
        trackingTrigger.triggerLine();
    if (initSdk(modelPath)) {
        RgbDetect rgbDetect(rgbFileName, width, height);
        GrayDetect grayDetect(grayFileName, width, height);
        LivenessDetect livenessDetect;
        if (rgbFileName != nullptr) {
            if (rgbDetect.detect(detectFaceCount)) {
                stringstream ss;
                int face_nums = RgbDetect::face_nums;
                int face_nums_size = rgbDetect.facePos.get()->size();
                for (int i = 0; i < face_nums && i < face_nums_size; i++) {
                    try {
                        THFI_FacePos *fps;
                        fps = &rgbDetect.facePos.get()->at(i);
                        ScoredRect scoredRect{fps[0].fAngle.confidence, {
                                fps[0].rcFace.left,
                                fps[0].rcFace.top,
                                fps[0].rcFace.right,
                                fps[0].rcFace.bottom
                        }, -1, map<string, string>()};
                        bool not_duplicate = trackingTrigger.triggerOnce(scoredRect);
                        bool track_id_changed = (track_id[i] != scoredRect.id);
                        track_id[i] = scoredRect.id;
                        if (track_id[i] == 1) {
                            int m = 0;
                        }
                        if (track_id[i] == -1) {
                            continue;
                        }
                        if (useGrayCamera) {
                            grayDetect.detect(detectFaceCount);
                            liveness[i] = livenessDetect.detect(i);
                        }
                        int featureId = i;
                        double d1 = msecond();
                        string featurePath = "";
                        if (shouldExtractFeature > 0) {
                            if ((track_id_changed || (msecond() - prevExtractFeatureTime) > 1000)
                                    ) {
                                memset(feature1, 0, featureSize);
                                int ret=EF_Extract(0, rgbDetect.rgb_image_buffer.get()->data(), width, height, 3, &fps[0], feature1);
 
                                if (ret == 1) {
                                    featureCount++;
                                    prevExtractFeatureTime = msecond();
                                    string featureName(baseFeatureName);
                                    featureName.append("_");
                                    featureName.append(to_string(track_id[i]));
                                    featureName.append("_");
                                    featureName.append(to_string(i));
                                    featureNames[i] = featureName;
                                    FILE *file = fopen(featureName.c_str(), "wb");
                                    fwrite(feature1, sizeof(BYTE), featureSize, file);
                                    fclose(file);
                                } else {
                                    featureId = -1;
                                }
                            }
                            featurePath = featureNames[i];
                        }
                        if (shouldExtractFeature == 0) {
                            featureId = -1;
                            featurePath = "";
                        }
                        ss << fps[0].rcFace.left << "," << fps[0].rcFace.top << ","
                           << fps[0].rcFace.right
                           << "," << fps[0].rcFace.bottom << "," << fps[0].fAngle.confidence
                           << "," << track_id[i] << "," << not_duplicate<<","<<(liveness[i]?"活体":"非活体")
                           <<","<<featureId<<","<<featurePath<<","<<fps[0].fAngle.yaw<<","<<fps[0].fAngle.pitch<<","<<fps[0].fAngle.roll;
                        if (i == 0 && face_nums > 1 && i != (face_nums - 1)) {
                            ss << "|";
                        }
                    } catch (exception &e) {
                        LogUtil::log(e.what());
                    }
                }
                return ss.str();
            } else {
                LogUtil::log("红外人脸检测失败");
            }
        } else {
            LogUtil::log("初始化SDK失败");
        }
        return faces;
    } else {
//        LOGI("initSdk init failed");
    }
 
}
 
jbyteArray
FaceDetectHelper::extractFeature(JNIEnv *env, const char *jpgFileName) {
    cv::Mat image;
    image = cv::imread(jpgFileName, 1);
    if( !image.data )
    {
        printf( " No image data \n " );
        return nullptr;
    }
    THFI_FacePos fps1[1];
    int height = image.rows;
    int width = image.cols;
    BYTE* bgrData = image.data;
    int face_nums = THFI_DetectFace(0, bgrData, 24, width, height, fps1, 1,360);
    if (face_nums == 1) {
        int ret=EF_Extract(0, bgrData, width, height, 3, fps1, idcard_feature);
        if (ret == 1) {
            jbyteArray featureByteArray = env->NewByteArray (featureSize);
            env->SetByteArrayRegion (featureByteArray, 0, featureSize, reinterpret_cast<jbyte*>(idcard_feature));
            return featureByteArray;
        }
    }
    return nullptr;
}
 
 
jbyteArray
FaceDetectHelper::extractFeature(JNIEnv *env, jbyteArray bgrByteArray, int width, int height) {
    THFI_FacePos fps1[1];
    int len = env->GetArrayLength (bgrByteArray);
    shared_ptr<vector<unsigned char>> rgb_image_buffer = make_shared<vector<unsigned char>>(len);
    env->GetByteArrayRegion (bgrByteArray, 0, len, reinterpret_cast<jbyte*>(rgb_image_buffer.get()->data()));
    BYTE* bgrData = rgb_image_buffer.get()->data();
    int face_nums = THFI_DetectFace(0, bgrData, 24, width, height, fps1, 1,360);
    if (face_nums == 1) {
        int ret=EF_Extract(0, bgrData, width, height, 3, fps1, idcard_feature);
        if (ret == 1) {
            jbyteArray featureByteArray = env->NewByteArray (featureSize);
            env->SetByteArrayRegion (featureByteArray, 0, featureSize, reinterpret_cast<jbyte*>(idcard_feature));
            return featureByteArray;
        }
    }
    return nullptr;
}