Plant Disease Classification with TensorFlow Lite on Android Part 2
For this project, we are going to create an end-to-end Android application with TFLite. We opte to develop an Android application that detects plant diseases.
The project is broken down into two steps:
- Building and creating a machine learning model using TensorFlow with Keras. More details here
- Deploying the model to an Android application using TFLite. This step
Add TFLite model in our Android Project
First — load the model in our Android project, we put plant_disease_model.tflite
andplant_labels.txt
into assets/
directory. plant_disease_model.tflite
is the result of our previous colab notebook. We need to add TFLite dependency to app/build.gradle
file.
Don’t forget to add the undermentioned snippet to prevent compressing the model.
Dive into the code
Create classifier class to load our model and read the file with labels:
Where Recognition
is our humble result data class :
When we have an instance of Interpreter
, we need to convert the preprocessed bitmap into ByteBuffer
then we create a method that will take an image as an argument and return a list of labels with assigned probabilities to them:
Here’s how we convert a bitmap into ByteBuffer:
Our only layout looks like this:
Here is most of the MainActivity
code, we’ll use in our app:
Where scaleImage
method allows us to resize the image because our model expects the exact input shape (224x224 pixels
), therefore we need to rescale a delivered bitmap to fit into these constraints:
finally we get this app:
App Demo
The source code of the project can be found on Github .
I can not finish this project without saying thank to my Coach Karthik M Swamy.