Skip to content

Commit 4372f06

Browse files
Update project to add TensorFlow through dependency
1 parent a9c6ccf commit 4372f06

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ dependencies {
4242
})
4343
compile 'com.android.support:appcompat-v7:27.0.2'
4444
testCompile 'junit:junit:4.12'
45-
compile files('libs/libandroid_tensorflow_inference_java.jar')
45+
compile 'org.tensorflow:tensorflow-android:1.2.0'
4646
compile 'com.wonderkiln:camerakit:0.13.1'
4747
}
-26.2 KB
Binary file not shown.

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

+20-21
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import android.content.res.AssetManager;
2020
import android.graphics.Bitmap;
21-
import android.os.Trace;
21+
import android.support.v4.os.TraceCompat;
2222
import android.util.Log;
2323

2424
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;
@@ -41,7 +41,7 @@
4141
*/
4242
public class TensorFlowImageClassifier implements Classifier {
4343

44-
private static final String TAG = "TensorFlowImageClassifier";
44+
private static final String TAG = "ImageClassifier";
4545

4646
// Only return this many results with at least this confidence.
4747
private static final int MAX_RESULTS = 3;
@@ -63,6 +63,8 @@ public class TensorFlowImageClassifier implements Classifier {
6363

6464
private TensorFlowInferenceInterface inferenceInterface;
6565

66+
private boolean runStats = false;
67+
6668
private TensorFlowImageClassifier() {
6769
}
6870

@@ -105,10 +107,7 @@ public static Classifier create(
105107
}
106108
br.close();
107109

108-
c.inferenceInterface = new TensorFlowInferenceInterface();
109-
if (c.inferenceInterface.initializeTensorFlow(assetManager, modelFilename) != 0) {
110-
throw new RuntimeException("TF initialization failed");
111-
}
110+
c.inferenceInterface = new TensorFlowInferenceInterface(assetManager, modelFilename);
112111
// The shape of the output is [N, NUM_CLASSES], where N is the batch size.
113112
int numClasses =
114113
(int) c.inferenceInterface.graph().operation(outputName).output(0).shape().size(1);
@@ -133,9 +132,9 @@ public static Classifier create(
133132
@Override
134133
public List<Recognition> recognizeImage(final Bitmap bitmap) {
135134
// Log this method so that it can be analyzed with systrace.
136-
Trace.beginSection("recognizeImage");
135+
TraceCompat.beginSection("recognizeImage");
137136

138-
Trace.beginSection("preprocessBitmap");
137+
TraceCompat.beginSection("preprocessBitmap");
139138
// Preprocess the image data from 0-255 int to normalized float based
140139
// on the provided parameters.
141140
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
@@ -145,23 +144,23 @@ public List<Recognition> recognizeImage(final Bitmap bitmap) {
145144
floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - imageMean) / imageStd;
146145
floatValues[i * 3 + 2] = ((val & 0xFF) - imageMean) / imageStd;
147146
}
148-
Trace.endSection();
147+
TraceCompat.endSection();
149148

150149
// Copy the input data into TensorFlow.
151-
Trace.beginSection("fillNodeFloat");
152-
inferenceInterface.fillNodeFloat(
153-
inputName, new int[]{1, inputSize, inputSize, 3}, floatValues);
154-
Trace.endSection();
150+
TraceCompat.beginSection("feed");
151+
inferenceInterface.feed(
152+
inputName, floatValues, new long[]{1, inputSize, inputSize, 3});
153+
TraceCompat.endSection();
155154

156155
// Run the inference call.
157-
Trace.beginSection("runInference");
158-
inferenceInterface.runInference(outputNames);
159-
Trace.endSection();
156+
TraceCompat.beginSection("run");
157+
inferenceInterface.run(outputNames, runStats);
158+
TraceCompat.endSection();
160159

161160
// Copy the output Tensor back into the output array.
162-
Trace.beginSection("readNodeFloat");
163-
inferenceInterface.readNodeFloat(outputName, outputs);
164-
Trace.endSection();
161+
TraceCompat.beginSection("fetch");
162+
inferenceInterface.fetch(outputName, outputs);
163+
TraceCompat.endSection();
165164

166165
// Find the best classifications.
167166
PriorityQueue<Recognition> pq =
@@ -186,13 +185,13 @@ public int compare(Recognition lhs, Recognition rhs) {
186185
for (int i = 0; i < recognitionsSize; ++i) {
187186
recognitions.add(pq.poll());
188187
}
189-
Trace.endSection(); // "recognizeImage"
188+
TraceCompat.endSection(); // "recognizeImage"
190189
return recognitions;
191190
}
192191

193192
@Override
194193
public void enableStatLogging(boolean debug) {
195-
inferenceInterface.enableStatLogging(debug);
194+
runStats = debug;
196195
}
197196

198197
@Override
Binary file not shown.

0 commit comments

Comments
 (0)