diff --git a/README.md b/README.md index cc8a9ec..173df2d 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,4 @@ # Android TensorFlow Machine Learning Example -[![Mindorks](https://img.shields.io/badge/mindorks-opensource-blue.svg)](https://mindorks.com/open-source-projects) -[![Mindorks Community](https://img.shields.io/badge/join-community-blue.svg)](https://mindorks.com/join-community) -[![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=102)](https://opensource.org/licenses/Apache-2.0) -[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/amitshekhariitbhu/AndroidTensorFlowMachineLearningExample/blob/master/LICENSE) - - ## About Android TensorFlow Machine Learning Example * This is an example project for integrating [TensorFlow](https://github.com/tensorflow/tensorflow) into Android application @@ -12,14 +6,16 @@ * How to build TensorFlow library(.so file and jar file) to use with Android Application. * This project include an example for object detection for an image taken from camera using TensorFlow library. -# [Read this article. It describes everything about building TensorFlow for Android.](https://blog.mindorks.com/android-tensorflow-machine-learning-example-ff0e9b2654cc) +## Prepare for Machine Learning Interview: [Machine Learning Interview Questions](https://github.com/amitshekhariitbhu/machine-learning-interview-questions) + +# [Check the Android TensorFlow Lite Machine Learning Example.](https://github.com/amitshekhariitbhu/Android-TensorFlow-Lite-Example)

- - - + + +

- +

@@ -29,11 +25,9 @@ ### Credits * The classifier example has been taken from Google TensorFlow example. -[Check out Mindorks awesome open source projects here](https://mindorks.com/open-source-projects) - ### License ``` - Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED + Copyright (C) 2022 Amit Shekhar Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/app/build.gradle b/app/build.gradle index 3b180e6..e73348b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,12 +17,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion "25.0.2" + compileSdkVersion 27 + buildToolsVersion "25.0.3" defaultConfig { applicationId "com.mindorks.tensorflowexample" minSdkVersion 16 - targetSdkVersion 25 + targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -40,8 +40,8 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.1.1' + compile 'com.android.support:appcompat-v7:27.0.2' testCompile 'junit:junit:4.12' - compile files('libs/libandroid_tensorflow_inference_java.jar') - compile 'com.flurgle:camerakit:0.9.13' + compile 'org.tensorflow:tensorflow-android:1.2.0' + compile 'com.wonderkiln:camerakit:0.13.1' } diff --git a/app/libs/libandroid_tensorflow_inference_java.jar b/app/libs/libandroid_tensorflow_inference_java.jar deleted file mode 100755 index 3b8d93b..0000000 Binary files a/app/libs/libandroid_tensorflow_inference_java.jar and /dev/null differ diff --git a/app/src/main/java/com/mindorks/tensorflowexample/MainActivity.java b/app/src/main/java/com/mindorks/tensorflowexample/MainActivity.java index f717838..036e7a2 100644 --- a/app/src/main/java/com/mindorks/tensorflowexample/MainActivity.java +++ b/app/src/main/java/com/mindorks/tensorflowexample/MainActivity.java @@ -17,7 +17,6 @@ package com.mindorks.tensorflowexample; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.method.ScrollingMovementMethod; @@ -26,8 +25,12 @@ import android.widget.ImageView; import android.widget.TextView; -import com.flurgle.camerakit.CameraListener; -import com.flurgle.camerakit.CameraView; +import com.wonderkiln.camerakit.CameraKitError; +import com.wonderkiln.camerakit.CameraKitEvent; +import com.wonderkiln.camerakit.CameraKitEventListener; +import com.wonderkiln.camerakit.CameraKitImage; +import com.wonderkiln.camerakit.CameraKitVideo; +import com.wonderkiln.camerakit.CameraView; import java.util.List; import java.util.concurrent.Executor; @@ -64,12 +67,21 @@ protected void onCreate(Bundle savedInstanceState) { btnToggleCamera = (Button) findViewById(R.id.btnToggleCamera); btnDetectObject = (Button) findViewById(R.id.btnDetectObject); - cameraView.setCameraListener(new CameraListener() { + cameraView.addCameraKitListener(new CameraKitEventListener() { @Override - public void onPictureTaken(byte[] picture) { - super.onPictureTaken(picture); + public void onEvent(CameraKitEvent cameraKitEvent) { - Bitmap bitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length); + } + + @Override + public void onError(CameraKitError cameraKitError) { + + } + + @Override + public void onImage(CameraKitImage cameraKitImage) { + + Bitmap bitmap = cameraKitImage.getBitmap(); bitmap = Bitmap.createScaledBitmap(bitmap, INPUT_SIZE, INPUT_SIZE, false); @@ -78,6 +90,12 @@ public void onPictureTaken(byte[] picture) { final List results = classifier.recognizeImage(bitmap); textViewResult.setText(results.toString()); + + } + + @Override + public void onVideo(CameraKitVideo cameraKitVideo) { + } }); diff --git a/app/src/main/java/com/mindorks/tensorflowexample/TensorFlowImageClassifier.java b/app/src/main/java/com/mindorks/tensorflowexample/TensorFlowImageClassifier.java index b53a9fb..78b6971 100644 --- a/app/src/main/java/com/mindorks/tensorflowexample/TensorFlowImageClassifier.java +++ b/app/src/main/java/com/mindorks/tensorflowexample/TensorFlowImageClassifier.java @@ -18,7 +18,7 @@ import android.content.res.AssetManager; import android.graphics.Bitmap; -import android.os.Trace; +import android.support.v4.os.TraceCompat; import android.util.Log; import org.tensorflow.contrib.android.TensorFlowInferenceInterface; @@ -41,7 +41,7 @@ */ public class TensorFlowImageClassifier implements Classifier { - private static final String TAG = "TensorFlowImageClassifier"; + private static final String TAG = "ImageClassifier"; // Only return this many results with at least this confidence. private static final int MAX_RESULTS = 3; @@ -63,6 +63,8 @@ public class TensorFlowImageClassifier implements Classifier { private TensorFlowInferenceInterface inferenceInterface; + private boolean runStats = false; + private TensorFlowImageClassifier() { } @@ -105,10 +107,7 @@ public static Classifier create( } br.close(); - c.inferenceInterface = new TensorFlowInferenceInterface(); - if (c.inferenceInterface.initializeTensorFlow(assetManager, modelFilename) != 0) { - throw new RuntimeException("TF initialization failed"); - } + c.inferenceInterface = new TensorFlowInferenceInterface(assetManager, modelFilename); // The shape of the output is [N, NUM_CLASSES], where N is the batch size. int numClasses = (int) c.inferenceInterface.graph().operation(outputName).output(0).shape().size(1); @@ -133,9 +132,9 @@ public static Classifier create( @Override public List recognizeImage(final Bitmap bitmap) { // Log this method so that it can be analyzed with systrace. - Trace.beginSection("recognizeImage"); + TraceCompat.beginSection("recognizeImage"); - Trace.beginSection("preprocessBitmap"); + TraceCompat.beginSection("preprocessBitmap"); // Preprocess the image data from 0-255 int to normalized float based // on the provided parameters. bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); @@ -145,23 +144,23 @@ public List recognizeImage(final Bitmap bitmap) { floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - imageMean) / imageStd; floatValues[i * 3 + 2] = ((val & 0xFF) - imageMean) / imageStd; } - Trace.endSection(); + TraceCompat.endSection(); // Copy the input data into TensorFlow. - Trace.beginSection("fillNodeFloat"); - inferenceInterface.fillNodeFloat( - inputName, new int[]{1, inputSize, inputSize, 3}, floatValues); - Trace.endSection(); + TraceCompat.beginSection("feed"); + inferenceInterface.feed( + inputName, floatValues, new long[]{1, inputSize, inputSize, 3}); + TraceCompat.endSection(); // Run the inference call. - Trace.beginSection("runInference"); - inferenceInterface.runInference(outputNames); - Trace.endSection(); + TraceCompat.beginSection("run"); + inferenceInterface.run(outputNames, runStats); + TraceCompat.endSection(); // Copy the output Tensor back into the output array. - Trace.beginSection("readNodeFloat"); - inferenceInterface.readNodeFloat(outputName, outputs); - Trace.endSection(); + TraceCompat.beginSection("fetch"); + inferenceInterface.fetch(outputName, outputs); + TraceCompat.endSection(); // Find the best classifications. PriorityQueue pq = @@ -186,13 +185,13 @@ public int compare(Recognition lhs, Recognition rhs) { for (int i = 0; i < recognitionsSize; ++i) { recognitions.add(pq.poll()); } - Trace.endSection(); // "recognizeImage" + TraceCompat.endSection(); // "recognizeImage" return recognitions; } @Override public void enableStatLogging(boolean debug) { - inferenceInterface.enableStatLogging(debug); + runStats = debug; } @Override diff --git a/app/src/main/jniLibs/armeabi-v7a/libtensorflow_inference.so b/app/src/main/jniLibs/armeabi-v7a/libtensorflow_inference.so deleted file mode 100755 index 9390465..0000000 Binary files a/app/src/main/jniLibs/armeabi-v7a/libtensorflow_inference.so and /dev/null differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 38a72a5..303639e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -25,7 +25,7 @@ android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.mindorks.tensorflowexample.MainActivity"> -