├── .DS_Store ├── LICENSE ├── README.md ├── app ├── .DS_Store ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── .DS_Store │ ├── androidTest │ └── java │ │ └── app │ │ └── ij │ │ └── mlwithtensorflowlite │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── .DS_Store │ ├── AndroidManifest.xml │ ├── java │ │ ├── .DS_Store │ │ └── app │ │ │ ├── .DS_Store │ │ │ └── ij │ │ │ ├── .DS_Store │ │ │ └── mlwithtensorflowlite │ │ │ ├── .DS_Store │ │ │ └── MainActivity.java │ ├── ml │ │ └── model.tflite │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values-night │ │ └── themes.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ └── test │ └── java │ └── app │ └── ij │ └── mlwithtensorflowlite │ └── ExampleUnitTest.java ├── build.gradle ├── demo.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── local.properties ├── settings.gradle └── thumbnail.png /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IJ-Apps/Image-Classification-App-with-Teachable-Machine/40d67dc94c34ca13b0ebc28d8e47ba3328ad9bed/.DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 IJ Apps 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Image Classification App using Teachable Machine 2 | Learn how to make an Android app that can classify images fast and with high accuracy. 3 | 4 | We'll use Google's Teachable Machine to train a machine learning model on common objects, then deploy a TensorFlow Lite model in our Android app. 5 | 6 | Check out the [tutorial](https://youtu.be/jhGm4KDafKU). 7 | 8 | ## [Tutorial](https://youtu.be/jhGm4KDafKU) 9 | 10 |

11 | 12 | 13 | [![Thumbnail](https://github.com/IJ-Apps/Image-Classification-App-with-Teachable-Machine/blob/main/thumbnail.png)](https://youtu.be/jhGm4KDafKU) 14 |

15 | -------------------------------------------------------------------------------- /app/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IJ-Apps/Image-Classification-App-with-Teachable-Machine/40d67dc94c34ca13b0ebc28d8e47ba3328ad9bed/app/.DS_Store -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | } 4 | 5 | android { 6 | compileSdk 30 7 | 8 | defaultConfig { 9 | applicationId "app.ij.mlwithtensorflowlite" 10 | minSdk 23 11 | targetSdk 30 12 | versionCode 1 13 | versionName "1.0" 14 | 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | compileOptions { 25 | sourceCompatibility JavaVersion.VERSION_1_8 26 | targetCompatibility JavaVersion.VERSION_1_8 27 | } 28 | buildFeatures { 29 | mlModelBinding true 30 | } 31 | } 32 | 33 | dependencies { 34 | 35 | implementation 'androidx.appcompat:appcompat:1.3.1' 36 | implementation 'com.google.android.material:material:1.4.0' 37 | implementation 'androidx.constraintlayout:constraintlayout:2.1.1' 38 | implementation 'org.tensorflow:tensorflow-lite-support:0.1.0' 39 | implementation 'org.tensorflow:tensorflow-lite-metadata:0.1.0' 40 | testImplementation 'junit:junit:4.+' 41 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 42 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 43 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IJ-Apps/Image-Classification-App-with-Teachable-Machine/40d67dc94c34ca13b0ebc28d8e47ba3328ad9bed/app/src/.DS_Store -------------------------------------------------------------------------------- /app/src/androidTest/java/app/ij/mlwithtensorflowlite/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package app.ij.mlwithtensorflowlite; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | assertEquals("app.ij.mlwithtensorflowlite", appContext.getPackageName()); 25 | } 26 | } -------------------------------------------------------------------------------- /app/src/main/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IJ-Apps/Image-Classification-App-with-Teachable-Machine/40d67dc94c34ca13b0ebc28d8e47ba3328ad9bed/app/src/main/.DS_Store -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IJ-Apps/Image-Classification-App-with-Teachable-Machine/40d67dc94c34ca13b0ebc28d8e47ba3328ad9bed/app/src/main/java/.DS_Store -------------------------------------------------------------------------------- /app/src/main/java/app/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IJ-Apps/Image-Classification-App-with-Teachable-Machine/40d67dc94c34ca13b0ebc28d8e47ba3328ad9bed/app/src/main/java/app/.DS_Store -------------------------------------------------------------------------------- /app/src/main/java/app/ij/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IJ-Apps/Image-Classification-App-with-Teachable-Machine/40d67dc94c34ca13b0ebc28d8e47ba3328ad9bed/app/src/main/java/app/ij/.DS_Store -------------------------------------------------------------------------------- /app/src/main/java/app/ij/mlwithtensorflowlite/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IJ-Apps/Image-Classification-App-with-Teachable-Machine/40d67dc94c34ca13b0ebc28d8e47ba3328ad9bed/app/src/main/java/app/ij/mlwithtensorflowlite/.DS_Store -------------------------------------------------------------------------------- /app/src/main/java/app/ij/mlwithtensorflowlite/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Created by ishaanjav 3 | * github.com/ishaanjav 4 | */ 5 | 6 | package app.ij.mlwithtensorflowlite; 7 | 8 | import androidx.annotation.Nullable; 9 | import androidx.appcompat.app.AppCompatActivity; 10 | 11 | import android.Manifest; 12 | import android.content.Intent; 13 | import android.content.pm.PackageManager; 14 | import android.graphics.Bitmap; 15 | import android.media.ThumbnailUtils; 16 | import android.os.Bundle; 17 | import android.provider.MediaStore; 18 | import android.view.View; 19 | import android.widget.Button; 20 | import android.widget.ImageView; 21 | import android.widget.TextView; 22 | 23 | import org.tensorflow.lite.DataType; 24 | import org.tensorflow.lite.support.tensorbuffer.TensorBuffer; 25 | 26 | import java.io.IOException; 27 | import java.nio.ByteBuffer; 28 | import java.nio.ByteOrder; 29 | 30 | import app.ij.mlwithtensorflowlite.ml.Model; 31 | 32 | public class MainActivity extends AppCompatActivity { 33 | 34 | TextView result, confidence; 35 | ImageView imageView; 36 | Button picture; 37 | int imageSize = 224; 38 | 39 | @Override 40 | protected void onCreate(Bundle savedInstanceState) { 41 | super.onCreate(savedInstanceState); 42 | setContentView(R.layout.activity_main); 43 | 44 | result = findViewById(R.id.result); 45 | confidence = findViewById(R.id.confidence); 46 | imageView = findViewById(R.id.imageView); 47 | picture = findViewById(R.id.button); 48 | 49 | picture.setOnClickListener(new View.OnClickListener() { 50 | @Override 51 | public void onClick(View view) { 52 | // Launch camera if we have permission 53 | if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { 54 | Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 55 | startActivityForResult(cameraIntent, 1); 56 | } else { 57 | //Request camera permission if we don't have it. 58 | requestPermissions(new String[]{Manifest.permission.CAMERA}, 100); 59 | } 60 | } 61 | }); 62 | } 63 | 64 | public void classifyImage(Bitmap image){ 65 | try { 66 | Model model = Model.newInstance(getApplicationContext()); 67 | 68 | // Creates inputs for reference. 69 | TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.FLOAT32); 70 | ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * imageSize * imageSize * 3); 71 | byteBuffer.order(ByteOrder.nativeOrder()); 72 | 73 | // get 1D array of 224 * 224 pixels in image 74 | int [] intValues = new int[imageSize * imageSize]; 75 | image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight()); 76 | 77 | // iterate over pixels and extract R, G, and B values. Add to bytebuffer. 78 | int pixel = 0; 79 | for(int i = 0; i < imageSize; i++){ 80 | for(int j = 0; j < imageSize; j++){ 81 | int val = intValues[pixel++]; // RGB 82 | byteBuffer.putFloat(((val >> 16) & 0xFF) * (1.f / 255.f)); 83 | byteBuffer.putFloat(((val >> 8) & 0xFF) * (1.f / 255.f)); 84 | byteBuffer.putFloat((val & 0xFF) * (1.f / 255.f)); 85 | } 86 | } 87 | 88 | inputFeature0.loadBuffer(byteBuffer); 89 | 90 | // Runs model inference and gets result. 91 | Model.Outputs outputs = model.process(inputFeature0); 92 | TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer(); 93 | 94 | float[] confidences = outputFeature0.getFloatArray(); 95 | // find the index of the class with the biggest confidence. 96 | int maxPos = 0; 97 | float maxConfidence = 0; 98 | for(int i = 0; i < confidences.length; i++){ 99 | if(confidences[i] > maxConfidence){ 100 | maxConfidence = confidences[i]; 101 | maxPos = i; 102 | } 103 | } 104 | String[] classes = {"Banana", "Orange", "Pen", "Sticky Notes"}; 105 | result.setText(classes[maxPos]); 106 | 107 | String s = ""; 108 | for(int i = 0; i < classes.length; i++){ 109 | s += String.format("%s: %.1f%%\n", classes[i], confidences[i] * 100); 110 | } 111 | confidence.setText(s); 112 | 113 | 114 | // Releases model resources if no longer used. 115 | model.close(); 116 | } catch (IOException e) { 117 | // TODO Handle the exception 118 | } 119 | } 120 | 121 | 122 | @Override 123 | public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { 124 | if (requestCode == 1 && resultCode == RESULT_OK) { 125 | Bitmap image = (Bitmap) data.getExtras().get("data"); 126 | int dimension = Math.min(image.getWidth(), image.getHeight()); 127 | image = ThumbnailUtils.extractThumbnail(image, dimension, dimension); 128 | imageView.setImageBitmap(image); 129 | 130 | image = Bitmap.createScaledBitmap(image, imageSize, imageSize, false); 131 | classifyImage(image); 132 | } 133 | super.onActivityResult(requestCode, resultCode, data); 134 | } 135 | } -------------------------------------------------------------------------------- /app/src/main/ml/model.tflite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IJ-Apps/Image-Classification-App-with-Teachable-Machine/40d67dc94c34ca13b0ebc28d8e47ba3328ad9bed/app/src/main/ml/model.tflite -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 |