Skip to content

Commit 349fe5a

Browse files
committed
add identify button to use dlib from photo
1 parent 95bf942 commit 349fe5a

File tree

7 files changed

+159
-15
lines changed

7 files changed

+159
-15
lines changed

visionSamples/FaceTracker/app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ android {
55
config {
66
keyAlias 'test'
77
keyPassword 'test123321'
8-
storeFile file('C:/Work/android-vision/signing/test.jks')
8+
storeFile file('E:/Work/android-vision/signing/test.jks')
99
storePassword 'test123321'
1010
}
1111
}
1212
compileSdkVersion 24
1313
buildToolsVersion '27.0.3'
1414
defaultConfig {
1515
applicationId "com.google.android.gms.samples.vision.face.facetracker"
16-
minSdkVersion 9
16+
minSdkVersion 15
1717
targetSdkVersion 24
1818
versionCode 1
1919
versionName "1.0"
@@ -53,4 +53,5 @@ dependencies {
5353
implementation 'com.android.support:support-v4:24.2.0'
5454
implementation 'com.android.support:design:24.2.0'
5555
implementation 'com.google.android.gms:play-services-vision:9.4.0+'
56+
implementation 'com.shamanland:xdroid-toaster:0.2.4'
5657
}

visionSamples/FaceTracker/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1313

1414
<uses-sdk
15-
android:minSdkVersion="9"
15+
android:minSdkVersion="15"
1616
android:targetSdkVersion="23" />
1717

1818
<application
1919
android:allowBackup="true"
2020
android:hardwareAccelerated="true"
2121
android:icon="@drawable/icon"
2222
android:theme="@style/Theme.AppCompat"
23-
android:label="FaceTracker">
23+
android:label="FaceTracker"
24+
android:largeHeap="true">
2425

2526
<meta-data android:name="com.google.android.gms.version"
2627
android:value="@integer/google_play_services_version"/>

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ extern "C"
150150
LOGI("unlocking pixels");
151151
AndroidBitmap_unlockPixels(env, bmp);
152152

153-
std::string returnValue = "Unknown" + std::to_string(min_dist);
153+
//std::string returnValue = "Unknown" + std::to_string(min_dist);
154+
std::string returnValue = "Unknown";
154155
return env->NewStringUTF(returnValue.c_str());
155156
}
156157
}
@@ -213,4 +214,75 @@ Java_com_google_android_gms_samples_vision_face_facetracker_FaceTrackerActivity_
213214
}
214215

215216
return 0;
217+
}extern "C"
218+
JNIEXPORT jstring JNICALL
219+
Java_com_google_android_gms_samples_vision_face_facetracker_FaceTrackerActivity_recognizeFromImage(
220+
JNIEnv *env, jobject instance, jobject bmp) {
221+
222+
AndroidBitmapInfo infocolor;
223+
void *pixelscolor;
224+
int y;
225+
int x;
226+
int ret;
227+
array2d<rgb_pixel> img;
228+
if ((ret = AndroidBitmap_getInfo(env, bmp, &infocolor)) < 0) {
229+
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
230+
return env->NewStringUTF("Image broken");
231+
}
232+
LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",
233+
infocolor.width, infocolor.height, infocolor.stride, infocolor.format, infocolor.flags);
234+
235+
if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
236+
LOGE("Bitmap format is not RGBA_8888 !");
237+
return env->NewStringUTF("Image broken 2");
238+
}
239+
240+
if ((ret = AndroidBitmap_lockPixels(env, bmp, &pixelscolor)) < 0) {
241+
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
242+
}
243+
244+
img.set_size(infocolor.height, infocolor.width);
245+
246+
//LOGI("size w=%d h=%d", infocolor.width, infocolor.height);
247+
for (y = 0; y < infocolor.height; y++) { //todo: performance
248+
argb *line = (argb *) pixelscolor;
249+
for (x = 0; x < infocolor.width; ++x) {
250+
rgb_pixel p(line[x].alpha, line[x].red, line[x].green);
251+
img[y][x] = p;
252+
}
253+
pixelscolor = (char *) pixelscolor + infocolor.stride;
254+
}
255+
256+
std::string returnValue = "Num Faces: ";
257+
std::vector<dlib::rectangle> dets = detector(img);
258+
returnValue += std::to_string(dets.size());
259+
returnValue += ". ";
260+
261+
std::vector<matrix<rgb_pixel>> faces;
262+
for (auto face : dets)
263+
{
264+
auto shape = sp(img, face);
265+
matrix<rgb_pixel> face_chip;
266+
extract_image_chip(img, get_face_chip_details(shape, 150, 0.25), face_chip);
267+
faces.push_back(move(face_chip));
268+
}
269+
270+
std::vector<matrix<float, 0, 1>> face_descriptors = net(faces);
271+
272+
for (size_t i = 0; i < face_descriptors.size(); ++i)
273+
{
274+
std::string name = "Unknown";
275+
for (auto& j : known_faces) {
276+
float dist = length(face_descriptors[i] - j.second);
277+
if (dist < FACE_RECOGNIZE_THRESH) {
278+
name = j.first;
279+
break;
280+
}
281+
}
282+
returnValue += name;
283+
returnValue += " ";
284+
}
285+
286+
AndroidBitmap_unlockPixels(env, bmp);
287+
return env->NewStringUTF(returnValue.c_str());
216288
}

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

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
import android.content.DialogInterface;
2424
import android.content.pm.PackageManager;
2525
import android.graphics.Bitmap;
26+
import android.graphics.BitmapFactory;
2627
import android.os.Bundle;
2728
import android.support.design.widget.Snackbar;
2829
import android.support.v4.app.ActivityCompat;
2930
import android.support.v7.app.AppCompatActivity;
3031
import android.util.Log;
3132
import android.view.View;
33+
import android.widget.Button;
34+
import android.widget.Toast;
3235

3336
import com.google.android.gms.common.ConnectionResult;
3437
import com.google.android.gms.common.GoogleApiAvailability;
@@ -42,6 +45,8 @@
4245

4346
import java.io.IOException;
4447

48+
import xdroid.toaster.Toaster;
49+
4550
/**
4651
* Activity for the face tracker app. This app detects faces with the rear facing camera, and draws
4752
* overlay graphics to indicate the position, size, and ID of each face.
@@ -53,7 +58,9 @@ public final class FaceTrackerActivity extends AppCompatActivity {
5358

5459
private CameraSourcePreview mPreview;
5560
private GraphicOverlay mGraphicOverlay;
61+
private Button mBtnDetect;
5662
private CustomDetector customDetector;
63+
private FaceDetector mPictureDetector;
5764

5865
private static final int RC_HANDLE_GMS = 9001;
5966
// permission request codes need to be < 256
@@ -74,6 +81,7 @@ public void onCreate(Bundle icicle) {
7481

7582
mPreview = (CameraSourcePreview) findViewById(R.id.preview);
7683
mGraphicOverlay = (GraphicOverlay) findViewById(R.id.faceOverlay);
84+
mBtnDetect = (Button) findViewById(R.id.btnDetect);
7785

7886

7987
// Check for the sdcard write permission. If the
@@ -180,7 +188,19 @@ private void createCameraSource() {
180188
Context context = getApplicationContext();
181189

182190
FaceDetector detector = new FaceDetector.Builder(context)
183-
.setClassificationType(FaceDetector.ALL_CLASSIFICATIONS)
191+
.setTrackingEnabled(true)
192+
.setClassificationType(FaceDetector.NO_CLASSIFICATIONS)
193+
.setProminentFaceOnly(false)
194+
.setMode(FaceDetector.ACCURATE_MODE)
195+
.setMinFaceSize(0.05f)
196+
.build();
197+
198+
mPictureDetector = new FaceDetector.Builder(context)
199+
.setTrackingEnabled(false)
200+
.setProminentFaceOnly(false)
201+
.setMinFaceSize(0.015f) // 80 / 5312 detect up to 80 pixels head width
202+
.setMode(FaceDetector.ACCURATE_MODE)
203+
.setClassificationType(FaceDetector.NO_CLASSIFICATIONS)
184204
.build();
185205

186206
customDetector = new CustomDetector(detector);
@@ -201,14 +221,49 @@ private void createCameraSource() {
201221
Log.w(TAG, "Face detector dependencies are not yet available.");
202222
}
203223

224+
if (!mPictureDetector.isOperational()) {
225+
Log.w(TAG, "mPictureDetector dependencies are not yet available.");
226+
}
227+
204228
//.setRequestedPreviewSize(640, 480)
205229
//.setRequestedFps(30.0f)
206230
//.setFacing(CameraSource.CAMERA_FACING_BACK)
207231
mCameraSource = new CameraSource.Builder(context, customDetector)
208-
.setRequestedPreviewSize(1024, 768)
209-
.setFacing(CameraSource.CAMERA_FACING_FRONT)
210-
.setRequestedFps(20.0f)
232+
.setRequestedPreviewSize(1920, 1080)
233+
.setFacing(CameraSource.CAMERA_FACING_BACK)
234+
.setRequestedFps(10)
211235
.build();
236+
237+
mBtnDetect.setOnClickListener(new View.OnClickListener() {
238+
@Override
239+
public void onClick(View v) {
240+
mCameraSource.takePicture(null, new CameraSource.PictureCallback() {
241+
@Override
242+
public void onPictureTaken(byte[] bytes) {
243+
BitmapFactory.Options options = new BitmapFactory.Options();
244+
final Bitmap temp = BitmapFactory.decodeByteArray(bytes, 0,
245+
bytes.length, options);
246+
247+
//Log.d(TAG, temp.getWidth() + " " + temp.getHeight());
248+
// Frame frame = new Frame.Builder().setBitmap(temp).build();
249+
// SparseArray<Face> faces = mPictureDetector.detect(frame);
250+
// Log.d(TAG, String.format("Num of faces %d", faces.size()));
251+
252+
Toast toast = Toast.makeText(getApplicationContext(),
253+
"Long detection started...(~1min)", Toast.LENGTH_SHORT);
254+
toast.show();
255+
256+
new Thread(new Runnable() {
257+
@Override
258+
public void run() {
259+
final String res = recognizeFromImage(temp);
260+
Toaster.toastLong(res);
261+
}
262+
}).start();
263+
}
264+
});
265+
}
266+
});
212267
}
213268

214269
/**
@@ -354,8 +409,7 @@ private class GraphicFaceTracker extends Tracker<Face> {
354409
* Start tracking the detected face instance within the face overlay.
355410
*/
356411
@Override
357-
public void onNewItem(int faceId, Face item)
358-
{
412+
public void onNewItem(int faceId, Face item) {
359413
mFaceGraphic.setId(faceId);
360414
}
361415

@@ -389,4 +443,5 @@ public void onDone() {
389443
}
390444

391445
public native int loadResources();
446+
public native String recognizeFromImage(Bitmap bmp);
392447
}

visionSamples/FaceTracker/app/src/main/res/layout-land/main.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
android:layout_width="match_parent"
88
android:layout_height="match_parent"
99
android:keepScreenOn="true">
10-
10+
<Button
11+
android:id="@+id/btnDetect"
12+
android:layout_width="35dp"
13+
android:layout_height="wrap_content"
14+
android:text="Identify" />
1115
<com.google.android.gms.samples.vision.face.facetracker.ui.camera.CameraSourcePreview
1216
android:id="@+id/preview"
1317
android:layout_width="match_parent"
@@ -20,4 +24,6 @@
2024

2125
</com.google.android.gms.samples.vision.face.facetracker.ui.camera.CameraSourcePreview>
2226

27+
28+
2329
</LinearLayout>

visionSamples/FaceTracker/app/src/main/res/layout/main.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@
88
android:layout_height="match_parent"
99
android:keepScreenOn="true">
1010

11-
<com.google.android.gms.samples.vision.face.facetracker.ui.camera.CameraSourcePreview
11+
12+
<Button
13+
android:id="@+id/btnDetect"
14+
android:layout_width="350dp"
15+
android:layout_height="wrap_content"
16+
android:text="Identify" />
17+
18+
<com.google.android.gms.samples.vision.face.facetracker.ui.camera.CameraSourcePreview
1219
android:id="@+id/preview"
1320
android:layout_width="match_parent"
1421
android:layout_height="match_parent">
1522

1623
<com.google.android.gms.samples.vision.face.facetracker.ui.camera.GraphicOverlay
1724
android:id="@+id/faceOverlay"
1825
android:layout_width="match_parent"
19-
android:layout_height="match_parent" />
26+
android:layout_height="match_parent">
27+
28+
</com.google.android.gms.samples.vision.face.facetracker.ui.camera.GraphicOverlay>
2029

2130
</com.google.android.gms.samples.vision.face.facetracker.ui.camera.CameraSourcePreview>
2231

visionSamples/FaceTracker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.1.1'
9+
classpath 'com.android.tools.build:gradle:3.1.2'
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files

0 commit comments

Comments
 (0)