Let's make machine learning simple.
Building a custom image classifier for your Android application using TensorFlow Lite.
TFLite-Image for Android - TensorFlow Lite inception model image library for Android
Move your trained model to asset folder or prepare a new image inception model using Google teachablemachine machine learning library.
You can use the sample inception quant or float model that we used in this project with 299 image dimension.
Add the below repository into your project level build.gradle file.
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the below dependency into your module level build.gradle
file.
dependencies {
...
implementation 'com.github.aslamanver:tflite-image:v1.0.9'
}
Make sure you have added no compress config for your model files
android {
....
aaptOptions {
noCompress "tflite"
noCompress "lite"
}
}
You need to pass the model file, label text and the model type.
TFLiteImage tfLite = TFLiteImage.getInstance(activity, "your_model_file.tflite", "labels.txt", TFLiteImage.TYPE.QUANT, IMG_DIM_SIZE);
List<Map<String, String>> results = tfLite.predictImage(image view or bitmap image);
IMG_DIM_SIZE
is 299 or 224 according to your model, you can visualize your model data to checkIMG_DIM_SIZE
.
Inception model types
TFLiteImage.TYPE.QUANT
TFLiteImage.TYPE.FLOAT
TFLiteImage tfLite = TFLiteImage.getInstance(this, "inception_quant.tflite", "labels.txt", TFLiteImage.TYPE.QUANT);
List<Map<String, String>> results = tfLite.predictImage(binding.imgView);
for (Map<String, String> map : results) {
Log.e("RESULT", map.get("LABEL") + " - " + map.get("CONFIDENCE"));
}
Result
map.get("LABEL");
map.get("CONFIDENCE");
Sunglass - 99%
Glass - 85%
Jeans - 70%
Test the sample app that I made for you: TFLite-Image-v1.0.apk
Made with ❤️ by Aslam Anver