Machine Learning in Flutter
People always hear others talking about machine learning but only have a fuzzy idea of what that means. Often they find it difficult to implement machine learning models but don’t worry, Teachable Machine got you covered.
Teachable Machine is a web-based tool by Google aiming to make AI easier for everyone. You can use Teachable Machine to recognize images, sounds or poses.
So in this article, I’m gonna share how you can create your machine learning model using Teachable Machine and integrate in Flutter.
Let’s have a quick look at what we are gonna build today
First go to Teachable Machine and open Image Project. To get started with training the model we will need to create two classes, namely “Dog” and “Cat” and upload training images for model to learn from. You can download the dataset from here
Change the Class 1
name to Cat
and click on the upload to upload the training images for the cats.
Now do the same for the Class 2
, change the Class 2
to Dog
and upload the training images for dogs. After uploading the training images, click on the Train Model to start the training.
It’s gonna take time if you have uploaded more number of training images, so sit back and wait for model to get train.
After your model is trained, click on the export model and download the Tensorflow Lite Floating Point Model.
Now it’s time to integrate the .tflite mode in Flutter
Create a new Flutter Project and add tflite
and image_picker
as a dependency in your pubspec.yaml
file.
In android/app/build.gradle
, add the following setting in android
block.
Create a assets
folder and place your labels.txt
file and model_unquant.tflite
file in assets
folder. In pubspec.yaml
do the following changes to specify files that should be included with the app
In main.dart
include import 'package:tflite/tflite.dart';
& import ‘package:image_picker/image_picker.dart’;
The image_picker
plugin will be used for picking images from the image library, and taking new pictures with the camera.
After importing libraries, it’s time to load your .tflite model in main.dart .
We will be using a bool
variable _loading
to show CircularProgressIndicator
while the model is loading.
Now, we will use image_picker
plugin to pick an image from the Gallery whenever the FloatingActionButton
is pressed. The image we pick will be passed as an argument to classifyImage
. We will use Tflite.runModelOnImage
to classify images of dogs and cats.
Tflite.runModelOnImage
returns Future<List>
Now it’s time to display the image we picked and the classification results on the screen.
It’s time to run the Flutter Application. Run flutter run
command in terminal to run your app.
Check out the GitHub Repository for the source code of above application.
Hey Readers, I’m a Mobile Application Developer who loves to explore deep sea of Flutter widgets. If you liked this article and found this helpful don’t forget to appreciate by a 50 claps :p
If you have any questions or suggestions feel free to comment below or you can connect with me on
Checkout my recent articles