1
1
#include < jni.h>
2
2
#include < string>
3
-
4
-
5
3
#include < dirent.h>
6
4
#include < unordered_map>
7
5
#include < android/log.h>
13
11
#include < dlib/image_processing.h>
14
12
#include < dlib/svm/svm_multiclass_linear_trainer.h>
15
13
16
-
17
-
18
14
using namespace std ;
19
15
using namespace dlib ;
20
16
21
-
22
17
#define LOG_TAG " Native"
23
18
#define LOGI (...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
24
19
#define LOGE (...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
25
20
26
21
27
22
template <template <int ,template <typename >class ,int ,typename > class block , int N, template <typename >class BN , typename SUBNET>
28
- using residual = dlib::add_prev1<block<N,BN,1 ,tag1<SUBNET>>>;
23
+ using residual = dlib::add_prev1<block<N,BN,1 ,dlib:: tag1<SUBNET>>>;
29
24
30
25
template <template <int ,template <typename >class ,int ,typename > class block , int N, template <typename >class BN , typename SUBNET>
31
- using residual_down = dlib::add_prev2<dlib::avg_pool<2 ,2 ,2 ,2 ,skip1<tag2<block<N,BN,2 ,tag1<SUBNET>>>>>>;
26
+ using residual_down = dlib::add_prev2<dlib::avg_pool<2 ,2 ,2 ,2 ,dlib:: skip1<dlib:: tag2<block<N,BN,2 ,dlib:: tag1<SUBNET>>>>>>;
32
27
33
28
template <int N, template <typename > class BN , int stride, typename SUBNET>
34
29
using block = BN<dlib::con<N,3 ,3 ,1 ,1 ,dlib::relu<BN<dlib::con<N,3 ,3 ,stride,stride,SUBNET>>>>>;
@@ -85,7 +80,7 @@ Java_com_google_android_gms_samples_vision_face_facetracker_FaceTrackerActivity_
85
80
FILE *file1 = fopen (" /storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat" , " r+" );
86
81
FILE *file2 = fopen (" /storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat" ,
87
82
" r+" );
88
- // FILE *file3 = fopen("/storage/emulated/0/Movies /faces_linear.svm", "r+");
83
+ // FILE *file3 = fopen("/storage/emulated/0/Download /faces_linear.svm", "r+");
89
84
90
85
if (file1 != NULL && file2 != NULL ) {
91
86
fclose (file1);
@@ -108,7 +103,7 @@ Java_com_google_android_gms_samples_vision_face_facetracker_FaceTrackerActivity_
108
103
d = opendir (" /storage/emulated/0/Download" );
109
104
if (d)
110
105
{
111
- // LOGI("Loading feature vectors using *.vec", p1); AL: p1 not initialized
106
+ LOGI (" Loading feature vectors using *.vec" , p1);
112
107
while ((dir = readdir (d)) != NULL )
113
108
{
114
109
p1=strtok (dir->d_name ," ." );
@@ -134,9 +129,7 @@ Java_com_google_android_gms_samples_vision_face_facetracker_FaceTrackerActivity_
134
129
}
135
130
136
131
return 0 ;
137
- }
138
-
139
- extern " C"
132
+ }extern " C"
140
133
JNIEXPORT jint JNICALL
141
134
Java_dlib_android_FaceRecognizer_loadResourcesPart1 (JNIEnv *env, jobject instance) {
142
135
@@ -160,7 +153,7 @@ Java_dlib_android_FaceRecognizer_loadResourcesPart1(JNIEnv *env, jobject instanc
160
153
d = opendir (" /storage/emulated/0/Download" );
161
154
if (d)
162
155
{
163
- // LOGI("Loading feature vectors using *.vec", p1); AL: p1 not initialized
156
+ LOGI (" Loading feature vectors using *.vec" , p1);
164
157
while ((dir = readdir (d)) != NULL )
165
158
{
166
159
p1=strtok (dir->d_name ," ." );
@@ -208,10 +201,12 @@ Java_dlib_android_FaceRecognizer_loadResourcesPart2(JNIEnv *env, jobject instanc
208
201
}
209
202
return 0 ;
210
203
}extern " C"
211
- JNIEXPORT jstring JNICALL
212
- Java_dlib_android_FaceRecognizer_recognizeNative1 (JNIEnv *env,
213
- jobject instance,
214
- jobject bmp) {
204
+ JNIEXPORT jobjectArray JNICALL
205
+ Java_dlib_android_FaceRecognizer_recognizeFaces (JNIEnv *env,
206
+ jobject instance,
207
+ jobject bmp) {
208
+ jobjectArray strarr;
209
+ std::vector<string> names;
215
210
216
211
AndroidBitmapInfo infocolor;
217
212
void *pixelscolor;
@@ -221,14 +216,16 @@ Java_dlib_android_FaceRecognizer_recognizeNative1(JNIEnv *env,
221
216
array2d<rgb_pixel> img;
222
217
if ((ret = AndroidBitmap_getInfo (env, bmp, &infocolor)) < 0 ) {
223
218
LOGE (" AndroidBitmap_getInfo() failed ! error=%d" , ret);
224
- return env->NewStringUTF (" Image broken" );
219
+ // return env->NewStringUTF("Image broken");
220
+ return strarr;
225
221
}
226
222
LOGI (" color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d" ,
227
223
infocolor.width , infocolor.height , infocolor.stride , infocolor.format , infocolor.flags );
228
224
229
225
if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
230
226
LOGE (" Bitmap format is not RGBA_8888 !" );
231
- return env->NewStringUTF (" Image broken 2" );
227
+ // return env->NewStringUTF("Image broken 2");
228
+ return strarr;
232
229
}
233
230
234
231
if ((ret = AndroidBitmap_lockPixels (env, bmp, &pixelscolor)) < 0 ) {
@@ -238,8 +235,7 @@ Java_dlib_android_FaceRecognizer_recognizeNative1(JNIEnv *env,
238
235
img.set_size (infocolor.height , infocolor.width );
239
236
240
237
LOGI (" size w=%d h=%d" , infocolor.width , infocolor.height );
241
- // to do: performance
242
- for (y = 0 ; y < infocolor.height ; y++) {
238
+ for (y = 0 ; y < infocolor.height ; y++) { // todo: performance
243
239
argb *line = (argb *) pixelscolor;
244
240
for (x = 0 ; x < infocolor.width ; ++x) {
245
241
rgb_pixel p (line[x].alpha , line[x].red , line[x].green );
@@ -248,15 +244,12 @@ Java_dlib_android_FaceRecognizer_recognizeNative1(JNIEnv *env,
248
244
pixelscolor = (char *) pixelscolor + infocolor.stride ;
249
245
}
250
246
251
- std::string returnValue = " Num Faces: " ;
247
+ // dlib::save_bmp(img, "/sdcard/Download/res1.bmp") ;
252
248
253
- std::vector<dlib::rectangle> dets = detector1 (img, 0 );
249
+ std::vector<dlib::rectangle> dets = detector1 (img);
254
250
if (dets.size () == 0 ){
255
- std::string name = " Unknown" ;
256
- return env->NewStringUTF (name.c_str ());
251
+ return strarr;
257
252
}
258
- returnValue += std::to_string (dets.size ());
259
- returnValue += " . " ;
260
253
261
254
std::vector<matrix<rgb_pixel>> faces;
262
255
for (auto face : dets)
@@ -272,24 +265,29 @@ Java_dlib_android_FaceRecognizer_recognizeNative1(JNIEnv *env,
272
265
for (size_t i = 0 ; i < face_descriptors.size (); ++i)
273
266
{
274
267
std::string name = " Unknown" ;
275
- for (auto & j : known_faces) {
268
+ for (auto j : known_faces) {
276
269
float dist = length (face_descriptors[i] - j.second );
277
270
if (dist < FACE_RECOGNIZE_THRESH) {
278
271
name = j.first ;
279
272
break ;
280
273
}
281
274
}
282
- returnValue += name;
283
- returnValue += " " ;
275
+ names.push_back (name);
284
276
}
285
277
286
278
AndroidBitmap_unlockPixels (env, bmp);
287
- return env->NewStringUTF (returnValue.c_str ());
288
- }
289
279
290
- extern " C"
280
+ if (names.size () > 0 ) {
281
+ strarr = env->NewObjectArray (names.size (), env->FindClass (" java/lang/String" ), nullptr );
282
+ for (int i = 0 ; i < names.size (); ++i)
283
+ {
284
+ env->SetObjectArrayElement (strarr, i, env->NewStringUTF (names[i].c_str ()));
285
+ }
286
+ }
287
+ return strarr;
288
+ }extern " C"
291
289
JNIEXPORT jstring JNICALL
292
- Java_dlib_android_FaceRecognizer_recognizeNative2 (JNIEnv *env, jobject instance, jobject bmp) {
290
+ Java_dlib_android_FaceRecognizer_recognizeFace (JNIEnv *env, jobject instance, jobject bmp) {
293
291
294
292
AndroidBitmapInfo infocolor;
295
293
void *pixelscolor;
@@ -314,7 +312,7 @@ Java_dlib_android_FaceRecognizer_recognizeNative2(JNIEnv *env, jobject instance,
314
312
}
315
313
316
314
img.set_size (infocolor.height , infocolor.width );
317
- for (y = 0 ; y < infocolor.height ; y++) { // to do : performance
315
+ for (y = 0 ; y < infocolor.height ; y++) { // todo : performance
318
316
argb *line = (argb *) pixelscolor;
319
317
for (x = 0 ; x < infocolor.width ; ++x) {
320
318
rgb_pixel p (line[x].alpha , line[x].red , line[x].green );
@@ -323,18 +321,17 @@ Java_dlib_android_FaceRecognizer_recognizeNative2(JNIEnv *env, jobject instance,
323
321
pixelscolor = (char *) pixelscolor + infocolor.stride ;
324
322
}
325
323
326
- // to do : smth wrong with colors
327
- // dlib::save_bmp(img, "/storage/emulated/0/ Download/test .bmp");
324
+ // todo : smth wrong with colors
325
+ // dlib::save_bmp(img, "/sdcard/ Download/res .bmp");
328
326
329
- std::vector<dlib::rectangle> dets = detector (img, 0 );
330
- LOGI (" detected size %d" , ( int ) dets.size ());
327
+ std::vector<dlib::rectangle> dets = detector (img);
328
+ LOGI (" detected size %d" , dets.size ());
331
329
332
330
float min_dist = 0.0 ;
333
331
if (dets.size () > 0 ){
334
332
auto face = dets.front ();
335
333
std::vector<matrix<rgb_pixel>> faces;
336
334
int x = face.left ();
337
-
338
335
int y = face.top ();
339
336
int width = face.width ();
340
337
int height = face.height ();
@@ -354,20 +351,19 @@ Java_dlib_android_FaceRecognizer_recognizeNative2(JNIEnv *env, jobject instance,
354
351
if (dist < min_dist){
355
352
min_dist = dist;
356
353
}
357
- if ( dist < FACE_RECOGNIZE_THRESH) // to do: extract thresh
354
+ if ( dist < FACE_RECOGNIZE_THRESH)
358
355
{
359
- LOGI (" recognized" );
356
+ // LOGI("recognized");
360
357
return env->NewStringUTF (i.first .c_str ());
361
358
}
362
359
}
363
360
}
364
361
LOGI (" not recognized, max dist %0.2f" , min_dist);
365
362
}
366
363
367
- LOGI (" unlocking pixels" );
364
+ // LOGI("unlocking pixels");
368
365
AndroidBitmap_unlockPixels (env, bmp);
369
366
370
- // std::string returnValue = "Unknown" + std::to_string(min_dist);
371
367
std::string returnValue = " Unknown" ;
372
368
return env->NewStringUTF (returnValue.c_str ());
373
- }
369
+ }
0 commit comments