Machine Learning in Flutter

Ayush Bherwani
Analytics Vidhya
Published in
4 min readDec 6, 2019
Flutter + Tflite is a complete love story

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

Flutter Application to classify images of dogs and cats

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

Snap of Teachable Machine Learning Image Project

Change the Class 1 name to Cat and click on the upload to upload the training images for the cats.

Click on the upload and choose images from your folder.

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.

Model started 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.

Me waiting for my model to get trained.

After your model is trained, click on the export model and download the Tensorflow Lite Floating Point Model.

Exporting 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.

pubspec.yaml

In android/app/build.gradle, add the following setting in android block.

android/app/build.gradle

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

pubspec.yaml

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>

Output of Tflite.runModelOnImage

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.

P.S Let’s hope for no error

Check out the GitHub Repository for the source code of above application.

--

--

Ayush Bherwani
Analytics Vidhya

Organizer Flutter Vadodara | GSoC’20 at Dart | Mobile Developer | Solidity Developer