Skip to content

Commit f614b29

Browse files
Implement camera and object detection
1 parent 8ebd6d3 commit f614b29

File tree

5 files changed

+188
-10
lines changed

5 files changed

+188
-10
lines changed

app/build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ android {
2121
buildToolsVersion "25.0.2"
2222
defaultConfig {
2323
applicationId "com.mindorks.tensorflowexample"
24-
minSdkVersion 14
24+
minSdkVersion 16
2525
targetSdkVersion 25
2626
versionCode 1
2727
versionName "1.0"
@@ -43,4 +43,5 @@ dependencies {
4343
compile 'com.android.support:appcompat-v7:25.1.1'
4444
testCompile 'junit:junit:4.12'
4545
compile files('libs/libandroid_tensorflow_inference_java.jar')
46+
compile 'com.flurgle:camerakit:0.9.13'
4647
}

app/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
android:icon="@mipmap/ic_launcher"
2424
android:label="@string/app_name"
2525
android:supportsRtl="true"
26+
android:largeHeap="true"
2627
android:theme="@style/AppTheme">
2728
<activity android:name=".MainActivity">
2829
<intent-filter>

app/src/main/java/com/mindorks/tensorflowexample/MainActivity.java

+126-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,139 @@
1616

1717
package com.mindorks.tensorflowexample;
1818

19-
import android.support.v7.app.AppCompatActivity;
19+
import android.graphics.Bitmap;
20+
import android.graphics.BitmapFactory;
2021
import android.os.Bundle;
22+
import android.support.v7.app.AppCompatActivity;
23+
import android.text.method.ScrollingMovementMethod;
24+
import android.view.View;
25+
import android.widget.Button;
26+
import android.widget.ImageView;
27+
import android.widget.TextView;
28+
29+
import com.flurgle.camerakit.CameraListener;
30+
import com.flurgle.camerakit.CameraView;
31+
32+
import java.util.List;
33+
import java.util.concurrent.Executor;
34+
import java.util.concurrent.Executors;
2135

2236
public class MainActivity extends AppCompatActivity {
2337

38+
private static final int INPUT_SIZE = 224;
39+
private static final int IMAGE_MEAN = 117;
40+
private static final float IMAGE_STD = 1;
41+
private static final String INPUT_NAME = "input";
42+
private static final String OUTPUT_NAME = "output";
43+
44+
private static final String MODEL_FILE = "file:///android_asset/tensorflow_inception_graph.pb";
45+
private static final String LABEL_FILE =
46+
"file:///android_asset/imagenet_comp_graph_label_strings.txt";
47+
48+
private Classifier classifier;
49+
private Executor executor = Executors.newSingleThreadExecutor();
50+
private TextView textViewResult;
51+
private Button btnDetectObject, btnToggleCamera;
52+
private ImageView imageViewResult;
53+
private CameraView cameraView;
54+
2455
@Override
2556
protected void onCreate(Bundle savedInstanceState) {
2657
super.onCreate(savedInstanceState);
2758
setContentView(R.layout.activity_main);
59+
cameraView = (CameraView) findViewById(R.id.cameraView);
60+
imageViewResult = (ImageView) findViewById(R.id.imageViewResult);
61+
textViewResult = (TextView) findViewById(R.id.textViewResult);
62+
textViewResult.setMovementMethod(new ScrollingMovementMethod());
63+
64+
btnToggleCamera = (Button) findViewById(R.id.btnToggleCamera);
65+
btnDetectObject = (Button) findViewById(R.id.btnDetectObject);
66+
67+
cameraView.setCameraListener(new CameraListener() {
68+
@Override
69+
public void onPictureTaken(byte[] picture) {
70+
super.onPictureTaken(picture);
71+
72+
Bitmap bitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length);
73+
74+
bitmap = Bitmap.createScaledBitmap(bitmap, INPUT_SIZE, INPUT_SIZE, false);
75+
76+
imageViewResult.setImageBitmap(bitmap);
77+
78+
final List<Classifier.Recognition> results = classifier.recognizeImage(bitmap);
79+
80+
textViewResult.setText(results.toString());
81+
}
82+
});
83+
84+
btnToggleCamera.setOnClickListener(new View.OnClickListener() {
85+
@Override
86+
public void onClick(View v) {
87+
cameraView.toggleFacing();
88+
}
89+
});
90+
91+
btnDetectObject.setOnClickListener(new View.OnClickListener() {
92+
@Override
93+
public void onClick(View v) {
94+
cameraView.captureImage();
95+
}
96+
});
97+
98+
initTensorFlowAndLoadModel();
99+
}
100+
101+
@Override
102+
protected void onResume() {
103+
super.onResume();
104+
cameraView.start();
105+
}
106+
107+
@Override
108+
protected void onPause() {
109+
cameraView.stop();
110+
super.onPause();
111+
}
112+
113+
@Override
114+
protected void onDestroy() {
115+
super.onDestroy();
116+
executor.execute(new Runnable() {
117+
@Override
118+
public void run() {
119+
classifier.close();
120+
}
121+
});
122+
}
123+
124+
private void initTensorFlowAndLoadModel() {
125+
executor.execute(new Runnable() {
126+
@Override
127+
public void run() {
128+
try {
129+
classifier = TensorFlowImageClassifier.create(
130+
getAssets(),
131+
MODEL_FILE,
132+
LABEL_FILE,
133+
INPUT_SIZE,
134+
IMAGE_MEAN,
135+
IMAGE_STD,
136+
INPUT_NAME,
137+
OUTPUT_NAME);
138+
makeButtonVisible();
139+
} catch (final Exception e) {
140+
throw new RuntimeException("Error initializing TensorFlow!", e);
141+
}
142+
}
143+
});
144+
}
145+
146+
private void makeButtonVisible() {
147+
runOnUiThread(new Runnable() {
148+
@Override
149+
public void run() {
150+
btnDetectObject.setVisibility(View.VISIBLE);
151+
}
152+
});
28153
}
29154
}

app/src/main/res/layout/activity_main.xml

+57-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<!--
1+
<?xml version="1.0" encoding="utf-8"?><!--
32
~ Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED
43
~
54
~ Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,7 +14,7 @@
1514
~ limitations under the License.
1615
-->
1716

18-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
17+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
1918
xmlns:tools="http://schemas.android.com/tools"
2019
android:id="@+id/activity_main"
2120
android:layout_width="match_parent"
@@ -26,8 +25,58 @@
2625
android:paddingTop="@dimen/activity_vertical_margin"
2726
tools:context="com.mindorks.tensorflowexample.MainActivity">
2827

29-
<TextView
30-
android:layout_width="wrap_content"
31-
android:layout_height="wrap_content"
32-
android:text="Hello World!" />
33-
</RelativeLayout>
28+
<com.flurgle.camerakit.CameraView
29+
android:id="@+id/cameraView"
30+
android:layout_width="300dp"
31+
android:layout_height="300dp"
32+
android:layout_gravity="center|top" />
33+
34+
35+
<LinearLayout
36+
android:layout_width="match_parent"
37+
android:layout_height="80dp"
38+
android:layout_gravity="center|top"
39+
android:layout_marginTop="300dp"
40+
android:gravity="center"
41+
android:orientation="horizontal">
42+
43+
<ImageView
44+
android:id="@+id/imageViewResult"
45+
android:layout_width="75dp"
46+
android:layout_height="75dp"
47+
android:padding="2dp" />
48+
49+
<TextView
50+
android:id="@+id/textViewResult"
51+
android:layout_width="match_parent"
52+
android:layout_height="80dp"
53+
android:fadeScrollbars="false"
54+
android:maxLines="15"
55+
android:scrollbars="vertical"
56+
android:gravity="center"
57+
android:textColor="@android:color/black" />
58+
59+
</LinearLayout>
60+
61+
62+
<Button
63+
android:id="@+id/btnToggleCamera"
64+
android:layout_width="match_parent"
65+
android:layout_height="48dp"
66+
android:layout_gravity="bottom|center"
67+
android:layout_marginBottom="50dp"
68+
android:text="@string/toggle_camera"
69+
android:textAllCaps="false"
70+
android:textColor="@android:color/black" />
71+
72+
<Button
73+
android:id="@+id/btnDetectObject"
74+
android:layout_width="match_parent"
75+
android:layout_height="48dp"
76+
android:layout_gravity="bottom|center"
77+
android:text="@string/detect_object"
78+
android:textAllCaps="false"
79+
android:textColor="@android:color/black"
80+
android:visibility="gone" />
81+
82+
</FrameLayout>

app/src/main/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616

1717
<resources>
1818
<string name="app_name">AndroidTensorFlowMachineLearningExample</string>
19+
<string name="toggle_camera">Toggle Camera</string>
20+
<string name="detect_object">Detect Object</string>
1921
</resources>

0 commit comments

Comments
 (0)