Skip to content

Commit b696585

Browse files
committed
merge master
2 parents 8442d59 + f58b237 commit b696585

File tree

3 files changed

+81
-69
lines changed

3 files changed

+81
-69
lines changed

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

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <jni.h>
22
#include <string>
3-
4-
53
#include <dirent.h>
64
#include <unordered_map>
75
#include <android/log.h>
@@ -13,22 +11,19 @@
1311
#include <dlib/image_processing.h>
1412
#include <dlib/svm/svm_multiclass_linear_trainer.h>
1513

16-
17-
1814
using namespace std;
1915
using namespace dlib;
2016

21-
2217
#define LOG_TAG "Native"
2318
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
2419
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
2520

2621

2722
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>>>;
2924

3025
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>>>>>>;
3227

3328
template <int N, template <typename> class BN, int stride, typename SUBNET>
3429
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_
8580
FILE *file1 = fopen("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat", "r+");
8681
FILE *file2 = fopen("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat",
8782
"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+");
8984

9085
if (file1 != NULL && file2 != NULL ) {
9186
fclose(file1);
@@ -108,7 +103,7 @@ Java_com_google_android_gms_samples_vision_face_facetracker_FaceTrackerActivity_
108103
d = opendir("/storage/emulated/0/Download");
109104
if (d)
110105
{
111-
// LOGI("Loading feature vectors using *.vec", p1); AL: p1 not initialized
106+
LOGI("Loading feature vectors using *.vec", p1);
112107
while ((dir = readdir(d)) != NULL)
113108
{
114109
p1=strtok(dir->d_name,".");
@@ -134,9 +129,7 @@ Java_com_google_android_gms_samples_vision_face_facetracker_FaceTrackerActivity_
134129
}
135130

136131
return 0;
137-
}
138-
139-
extern "C"
132+
}extern "C"
140133
JNIEXPORT jint JNICALL
141134
Java_dlib_android_FaceRecognizer_loadResourcesPart1(JNIEnv *env, jobject instance) {
142135

@@ -160,7 +153,7 @@ Java_dlib_android_FaceRecognizer_loadResourcesPart1(JNIEnv *env, jobject instanc
160153
d = opendir("/storage/emulated/0/Download");
161154
if (d)
162155
{
163-
// LOGI("Loading feature vectors using *.vec", p1); AL: p1 not initialized
156+
LOGI("Loading feature vectors using *.vec", p1);
164157
while ((dir = readdir(d)) != NULL)
165158
{
166159
p1=strtok(dir->d_name,".");
@@ -208,10 +201,12 @@ Java_dlib_android_FaceRecognizer_loadResourcesPart2(JNIEnv *env, jobject instanc
208201
}
209202
return 0;
210203
}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;
215210

216211
AndroidBitmapInfo infocolor;
217212
void *pixelscolor;
@@ -221,14 +216,16 @@ Java_dlib_android_FaceRecognizer_recognizeNative1(JNIEnv *env,
221216
array2d<rgb_pixel> img;
222217
if ((ret = AndroidBitmap_getInfo(env, bmp, &infocolor)) < 0) {
223218
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
224-
return env->NewStringUTF("Image broken");
219+
//return env->NewStringUTF("Image broken");
220+
return strarr;
225221
}
226222
LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",
227223
infocolor.width, infocolor.height, infocolor.stride, infocolor.format, infocolor.flags);
228224

229225
if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
230226
LOGE("Bitmap format is not RGBA_8888 !");
231-
return env->NewStringUTF("Image broken 2");
227+
//return env->NewStringUTF("Image broken 2");
228+
return strarr;
232229
}
233230

234231
if ((ret = AndroidBitmap_lockPixels(env, bmp, &pixelscolor)) < 0) {
@@ -238,8 +235,7 @@ Java_dlib_android_FaceRecognizer_recognizeNative1(JNIEnv *env,
238235
img.set_size(infocolor.height, infocolor.width);
239236

240237
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
243239
argb *line = (argb *) pixelscolor;
244240
for (x = 0; x < infocolor.width; ++x) {
245241
rgb_pixel p(line[x].alpha, line[x].red, line[x].green);
@@ -248,15 +244,12 @@ Java_dlib_android_FaceRecognizer_recognizeNative1(JNIEnv *env,
248244
pixelscolor = (char *) pixelscolor + infocolor.stride;
249245
}
250246

251-
std::string returnValue = "Num Faces: ";
247+
//dlib::save_bmp(img, "/sdcard/Download/res1.bmp");
252248

253-
std::vector<dlib::rectangle> dets = detector1(img,0);
249+
std::vector<dlib::rectangle> dets = detector1(img);
254250
if (dets.size() == 0){
255-
std::string name = "Unknown";
256-
return env->NewStringUTF(name.c_str());
251+
return strarr;
257252
}
258-
returnValue += std::to_string(dets.size());
259-
returnValue += ". ";
260253

261254
std::vector<matrix<rgb_pixel>> faces;
262255
for (auto face : dets)
@@ -272,24 +265,29 @@ Java_dlib_android_FaceRecognizer_recognizeNative1(JNIEnv *env,
272265
for (size_t i = 0; i < face_descriptors.size(); ++i)
273266
{
274267
std::string name = "Unknown";
275-
for (auto& j : known_faces) {
268+
for (auto j : known_faces) {
276269
float dist = length(face_descriptors[i] - j.second);
277270
if (dist < FACE_RECOGNIZE_THRESH) {
278271
name = j.first;
279272
break;
280273
}
281274
}
282-
returnValue += name;
283-
returnValue += " ";
275+
names.push_back(name);
284276
}
285277

286278
AndroidBitmap_unlockPixels(env, bmp);
287-
return env->NewStringUTF(returnValue.c_str());
288-
}
289279

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"
291289
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) {
293291

294292
AndroidBitmapInfo infocolor;
295293
void *pixelscolor;
@@ -314,7 +312,7 @@ Java_dlib_android_FaceRecognizer_recognizeNative2(JNIEnv *env, jobject instance,
314312
}
315313

316314
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
318316
argb *line = (argb *) pixelscolor;
319317
for (x = 0; x < infocolor.width; ++x) {
320318
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,
323321
pixelscolor = (char *) pixelscolor + infocolor.stride;
324322
}
325323

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");
328326

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());
331329

332330
float min_dist = 0.0;
333331
if(dets.size() > 0 ){
334332
auto face = dets.front();
335333
std::vector<matrix<rgb_pixel>> faces;
336334
int x = face.left();
337-
338335
int y = face.top();
339336
int width = face.width();
340337
int height = face.height();
@@ -354,20 +351,19 @@ Java_dlib_android_FaceRecognizer_recognizeNative2(JNIEnv *env, jobject instance,
354351
if (dist < min_dist){
355352
min_dist = dist;
356353
}
357-
if( dist < FACE_RECOGNIZE_THRESH) //to do: extract thresh
354+
if( dist < FACE_RECOGNIZE_THRESH)
358355
{
359-
LOGI("recognized");
356+
//LOGI("recognized");
360357
return env->NewStringUTF(i.first.c_str());
361358
}
362359
}
363360
}
364361
LOGI("not recognized, max dist %0.2f", min_dist);
365362
}
366363

367-
LOGI("unlocking pixels");
364+
// LOGI("unlocking pixels");
368365
AndroidBitmap_unlockPixels(env, bmp);
369366

370-
//std::string returnValue = "Unknown" + std::to_string(min_dist);
371367
std::string returnValue = "Unknown";
372368
return env->NewStringUTF(returnValue.c_str());
373-
}
369+
}

visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/CustomDetector.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,19 @@
33
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
55
import android.graphics.ImageFormat;
6+
import android.graphics.Matrix;
67
import android.graphics.Rect;
78
import android.graphics.YuvImage;
8-
import android.os.Environment;
9-
import android.util.Log;
109
import android.util.SparseArray;
1110

1211
import com.google.android.gms.vision.Detector;
1312
import com.google.android.gms.vision.Frame;
1413
import com.google.android.gms.vision.face.Face;
1514

1615
import java.io.ByteArrayOutputStream;
17-
import java.io.File;
18-
import java.io.FileOutputStream;
19-
import java.io.FileWriter;
20-
import java.io.IOException;
2116

2217
import dlib.android.FaceRecognizer;
2318

24-
import static android.os.Environment.getExternalStorageDirectory;
25-
2619
interface RecognitionInterface
2720
{
2821
void onRecognized(String obj);
@@ -77,14 +70,37 @@ public SparseArray<Face> detect(Frame frame) {
7770
if (!IsBusy && y > 0 && recognitionHandler != null) {
7871
IsBusy = true;
7972

73+
int fHeight = frame.getMetadata().getHeight();
74+
int fWidth = frame.getMetadata().getWidth();
8075
YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21,
81-
frame.getMetadata().getWidth(), frame.getMetadata().getHeight(), null);
76+
fWidth, fHeight, null);
8277
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
83-
yuvImage.compressToJpeg(new Rect(0, 0, frame.getMetadata().getWidth(),
84-
frame.getMetadata().getHeight()), 100, byteArrayOutputStream);
78+
yuvImage.compressToJpeg(new Rect(0, 0, fWidth, fHeight),
79+
100, byteArrayOutputStream);
8580
byte[] jpegArray = byteArrayOutputStream.toByteArray();
8681
Bitmap tmpBitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);
87-
final Bitmap cropped = Bitmap.createBitmap(tmpBitmap, x, y, w, h);
82+
final Bitmap cropped;
83+
Matrix rot = new Matrix();
84+
switch (frame.getMetadata().getRotation())
85+
{
86+
case 1:
87+
rot.postRotate(90);
88+
cropped = Bitmap.createBitmap(tmpBitmap, y, fHeight - (x + w), h, w,
89+
rot,false );
90+
break;
91+
case 2:
92+
rot.postRotate(180);
93+
cropped = Bitmap.createBitmap(tmpBitmap, fWidth - (x + w),
94+
fHeight - (y + h), w, h, rot, false);
95+
break;
96+
case 3:
97+
rot.postRotate(270);
98+
cropped = Bitmap.createBitmap(tmpBitmap, fWidth - (y + h), x, h, w, rot, false);
99+
break;
100+
default:
101+
cropped = Bitmap.createBitmap(tmpBitmap, x, y, w, h);
102+
break;
103+
}
88104
// try {
89105
//
90106
// File file = new File (getExternalStorageDirectory(), "/Download/test1.bmp");

visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/FaceTrackerActivity.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public final class FaceTrackerActivity extends AppCompatActivity {
6262
private GraphicOverlay mGraphicOverlay;
6363
private Button mBtnDetect;
6464
private CustomDetector customDetector;
65-
private FaceDetector mPictureDetector;
65+
//private FaceDetector mPictureDetector;
6666

6767
private static final int RC_HANDLE_GMS = 9001;
6868
// permission request codes need to be < 256
@@ -185,16 +185,16 @@ private void createCameraSource() {
185185
.setClassificationType(FaceDetector.NO_CLASSIFICATIONS)
186186
.setProminentFaceOnly(false)
187187
.setMode(FaceDetector.ACCURATE_MODE)
188-
.setMinFaceSize(0.05f)
188+
.setMinFaceSize(0.015f)
189189
.build();
190190

191-
mPictureDetector = new FaceDetector.Builder(context)
192-
.setTrackingEnabled(false)
193-
.setProminentFaceOnly(false)
194-
.setMinFaceSize(0.015f) // 80 / 5312 detect up to 80 pixels head width
195-
.setMode(FaceDetector.ACCURATE_MODE)
196-
.setClassificationType(FaceDetector.NO_CLASSIFICATIONS)
197-
.build();
191+
// mPictureDetector = new FaceDetector.Builder(context)
192+
// .setTrackingEnabled(false)
193+
// .setProminentFaceOnly(false)
194+
// .setMinFaceSize(0.015f) // 80 / 5312 detect up to 80 pixels head width
195+
// .setMode(FaceDetector.ACCURATE_MODE)
196+
// .setClassificationType(FaceDetector.NO_CLASSIFICATIONS)
197+
// .build();
198198

199199
//mFaceRecognizer
200200
customDetector = new CustomDetector(detector, mFaceRecognizer);
@@ -215,9 +215,9 @@ private void createCameraSource() {
215215
Log.w(TAG, "Face detector dependencies are not yet available.");
216216
}
217217

218-
if (!mPictureDetector.isOperational()) {
219-
Log.w(TAG, "mPictureDetector dependencies are not yet available.");
220-
}
218+
// if (!mPictureDetector.isOperational()) {
219+
// Log.w(TAG, "mPictureDetector dependencies are not yet available.");
220+
// }
221221

222222
//.setRequestedPreviewSize(640, 480)
223223
//.setRequestedFps(30.0f)

0 commit comments

Comments
 (0)