Skip to content

Commit c7e7768

Browse files
Merge pull request #3 from hoopoe/permissions
request permissions for camera and sdcard together
2 parents 3dcab26 + dc5a8f1 commit c7e7768

File tree

6 files changed

+94
-151
lines changed

6 files changed

+94
-151
lines changed

FaceTracker/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
android:hardwareAccelerated="true"
2121
android:icon="@drawable/icon"
2222
android:theme="@style/Theme.AppCompat"
23-
android:label="FaceTracker"
23+
android:label="Face Recognition"
2424
android:largeHeap="true">
2525

2626
<meta-data android:name="com.google.android.gms.version"
@@ -33,7 +33,7 @@
3333
<activity
3434
android:name="com.google.android.gms.samples.vision.face.facetracker.FaceTrackerActivity"
3535
android:icon="@drawable/icon"
36-
android:label="SG"
36+
android:label="Face Recognition"
3737
android:theme="@style/Theme.AppCompat.NoActionBar"
3838
android:screenOrientation="fullSensor">
3939
<intent-filter>

FaceTracker/app/src/main/cpp/OCV-native-lib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern "C"
2222

2323
void detect(Mat &gray) {
2424

25-
String face_cascade_name = "/storage/emulated/0/Movies/haarcascade_frontalface_default.xml";
25+
String face_cascade_name = "/sdcard/Download/haarcascade_frontalface_default.xml";
2626

2727
CascadeClassifier face_cascade;
2828
std::vector<Rect> faces;

FaceTracker/app/src/main/cpp/native-lib.cpp

Lines changed: 20 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -72,85 +72,23 @@ float FACE_RECOGNIZE_THRESH = 0.55;
7272

7373
extern "C"
7474
JNIEXPORT jint JNICALL
75-
Java_com_google_android_gms_samples_vision_face_facetracker_FaceTrackerActivity_loadResources(
76-
JNIEnv *env, jobject instance)
77-
{
78-
79-
LOGI("load resource");
80-
FILE *file1 = fopen("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat", "r+");
81-
FILE *file2 = fopen("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat",
82-
"r+");
83-
//FILE *file3 = fopen("/storage/emulated/0/Download/faces_linear.svm", "r+");
84-
85-
if (file1 != NULL && file2 != NULL ) {
86-
fclose(file1);
87-
fclose(file2);
88-
//fclose(file3);
89-
dlib::deserialize("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat")
90-
>> sp;
91-
dlib::deserialize("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat")
92-
>> sp1;
93-
dlib::deserialize("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat")
94-
>> net;
95-
dlib::deserialize("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat")
96-
>> net1;
97-
//dlib::deserialize("/storage/emulated/0/Download/faces_linear.svm") >> df;
98-
99-
DIR *d;
100-
char *p1,*p2;
101-
int ret;
102-
struct dirent *dir;
103-
d = opendir("/storage/emulated/0/Download");
104-
if (d)
105-
{
106-
LOGI("Loading feature vectors using *.vec", p1);
107-
while ((dir = readdir(d)) != NULL)
108-
{
109-
p1=strtok(dir->d_name,".");
110-
p2=strtok(NULL,".");
111-
if(p2!=NULL)
112-
{
113-
ret=strcmp(p2,"vec");
114-
if(ret==0)
115-
{
116-
std::string name = std::string(p1);
117-
std::string file = name + ".vec";
118-
matrix<float, 0, 1> face_vector;
119-
dlib::deserialize("/storage/emulated/0/Download/" + file) >> face_vector;
120-
known_faces.insert({name, face_vector});
121-
}
122-
}
123-
124-
}
125-
closedir(d);
126-
}
127-
} else {
128-
return 1; //failed
129-
}
130-
131-
return 0;
132-
}extern "C"
133-
JNIEXPORT jint JNICALL
13475
Java_dlib_android_FaceRecognizer_loadResourcesPart1(JNIEnv *env, jobject instance) {
13576

13677
LOGI("load resource part1");
137-
FILE *file1 = fopen("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat", "r+");
138-
FILE *file2 = fopen("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat",
139-
"r+");
78+
FILE *file1 = fopen("/sdcard/Download/shape_predictor_5_face_landmarks.dat", "r+");
79+
FILE *file2 = fopen("/sdcard/Download/dlib_face_recognition_resnet_model_v1.dat", "r+");
14080

14181
if (file1 != NULL && file2 != NULL ) {
14282
fclose(file1);
14383
fclose(file2);
144-
dlib::deserialize("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat")
145-
>> sp;
146-
dlib::deserialize("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat")
147-
>> net;
84+
dlib::deserialize("/sdcard/Download/shape_predictor_5_face_landmarks.dat") >> sp;
85+
dlib::deserialize("/sdcard/Download/dlib_face_recognition_resnet_model_v1.dat") >> net;
14886

14987
DIR *d;
15088
char *p1,*p2;
15189
int ret;
15290
struct dirent *dir;
153-
d = opendir("/storage/emulated/0/Download");
91+
d = opendir("/sdcard/Download");
15492
if (d)
15593
{
15694
LOGI("Loading feature vectors using *.vec", p1);
@@ -166,7 +104,7 @@ Java_dlib_android_FaceRecognizer_loadResourcesPart1(JNIEnv *env, jobject instanc
166104
std::string name = std::string(p1);
167105
std::string file = name + ".vec";
168106
matrix<float, 0, 1> face_vector;
169-
dlib::deserialize("/storage/emulated/0/Download/" + file) >> face_vector;
107+
dlib::deserialize("/sdcard/Download/" + file) >> face_vector;
170108
known_faces.insert({name, face_vector});
171109
}
172110
}
@@ -175,6 +113,7 @@ Java_dlib_android_FaceRecognizer_loadResourcesPart1(JNIEnv *env, jobject instanc
175113
closedir(d);
176114
}
177115
} else {
116+
LOGI("Failed to load resources part1");
178117
return -1; //failed
179118
}
180119

@@ -185,18 +124,16 @@ JNIEXPORT jint JNICALL
185124
Java_dlib_android_FaceRecognizer_loadResourcesPart2(JNIEnv *env, jobject instance) {
186125

187126
LOGI("load resource part2");
188-
FILE *file1 = fopen("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat", "r+");
189-
FILE *file2 = fopen("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat",
190-
"r+");
127+
FILE *file1 = fopen("/sdcard/Download/shape_predictor_5_face_landmarks.dat", "r+");
128+
FILE *file2 = fopen("/sdcard/Download/dlib_face_recognition_resnet_model_v1.dat", "r+");
191129

192130
if (file1 != NULL && file2 != NULL ) {
193131
fclose(file1);
194132
fclose(file2);
195-
dlib::deserialize("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat")
196-
>> sp1;
197-
dlib::deserialize("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat")
198-
>> net1;
133+
dlib::deserialize("/sdcard/Download/shape_predictor_5_face_landmarks.dat") >> sp1;
134+
dlib::deserialize("/sdcard/Download/dlib_face_recognition_resnet_model_v1.dat") >> net1;
199135
} else{
136+
LOGI("Failed to load resources part2");
200137
return -1;
201138
}
202139
return 0;
@@ -234,7 +171,7 @@ Java_dlib_android_FaceRecognizer_recognizeFaces(JNIEnv *env,
234171

235172
img.set_size(infocolor.height, infocolor.width);
236173

237-
LOGI("size w=%d h=%d", infocolor.width, infocolor.height);
174+
// LOGI("size w=%d h=%d", infocolor.width, infocolor.height);
238175
for (y = 0; y < infocolor.height; y++) { //todo: performance
239176
argb *line = (argb *) pixelscolor;
240177
for (x = 0; x < infocolor.width; ++x) {
@@ -302,6 +239,7 @@ Java_dlib_android_FaceRecognizer_recognizeFace(JNIEnv *env, jobject instance, jo
302239
LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",
303240
infocolor.width, infocolor.height, infocolor.stride, infocolor.format, infocolor.flags);
304241

242+
LOGI("known_faces size %d", known_faces.size());
305243
if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
306244
LOGE("Bitmap format is not RGBA_8888 !");
307245
return env->NewStringUTF("Image broken 2");
@@ -328,7 +266,7 @@ Java_dlib_android_FaceRecognizer_recognizeFace(JNIEnv *env, jobject instance, jo
328266
LOGI("detected size %d", dets.size());
329267

330268
float min_dist = 0.0;
331-
if(dets.size() > 0 ){
269+
if(dets.size() > 0 ){
332270
auto face = dets.front();
333271
std::vector<matrix<rgb_pixel>> faces;
334272
int x = face.left();
@@ -337,16 +275,18 @@ Java_dlib_android_FaceRecognizer_recognizeFace(JNIEnv *env, jobject instance, jo
337275
int height = face.height();
338276

339277
auto shape = sp(img, face);
278+
// LOGI("shape predictor");
340279
matrix<rgb_pixel> face_chip;
341280
extract_image_chip(img, get_face_chip_details(shape, 150, 0.25), face_chip);
342281
faces.push_back(move(face_chip));
343282

283+
// LOGI("before recognized size %d", -1);
344284
std::vector<matrix<float, 0, 1>> face_descriptors = net(faces);
345-
285+
// LOGI("after recognized size %d", face_descriptors.size());
346286
if (face_descriptors.size() > 0)
347287
{
348288
matrix<float, 0, 1> face_desc = face_descriptors[0];
349-
for (auto& i : known_faces) {
289+
for (auto i : known_faces) {
350290
float dist = length(face_desc - i.second );
351291
if (dist < min_dist){
352292
min_dist = dist;
@@ -361,7 +301,7 @@ Java_dlib_android_FaceRecognizer_recognizeFace(JNIEnv *env, jobject instance, jo
361301
LOGI("not recognized, max dist %0.2f", min_dist);
362302
}
363303

364-
// LOGI("unlocking pixels");
304+
LOGI("unlocking pixels");
365305
AndroidBitmap_unlockPixels(env, bmp);
366306

367307
std::string returnValue = "Unknown";

0 commit comments

Comments
 (0)