Using Tensorflow Lite with Flutter Apps

TensorFlow Lite is an open-source framework that provides a set of tools which enables developers to execute their trained models on mobile, embedded, and IoT devices and desktops, enabling on-device machine learning.

Tensorflow Lite supports training three types of models while this article is being published:

  1. Image Project
  2. Audio Project
  3. Pose Project

You can explore more about tensorflow teachable machine by click here.

image.png

How to Train your model

  • All you need is an idea of what you want your application to do, and then the next stage is to collect data for the machine to learn.
  • Data gathering might be difficult, depending on the sort of problem you are attempting to address.
  • After you've gathered your data and organised it into separate classes, the next step is to send it to Teachable Machine for training.
  • We may easily upload our bulk data from our computer or Gdrive and label that class.The label of the classified data in your application will be the name you assign to a class.

image.png

  • Add several types of photos to the classes, then train and export your model.

image.png

  • Press download my model button and download the file.

Next Step, Flutter!

So, the next step is to implement this model in the Flutter app. Google has not yet released an official TensorFlow Lite package, however there is one that is well maintained. I used this package tflite in my project.

We start by adding this package in pubspec file:

dependencies:
 tflite: ^1.0.5

Click here to go tflite package documentation.

You need to also add two more dependencies to your pubspec.yaml file.

Image picker:-click here

Meta :-click here

The model that we trained using the teachable machine must be placed in the assets folder and added to the pubspec.yaml file.

1.Make a folder named “assets” and put the the model inside it.

image.png

2.Add the assets into the pubspec.yaml file.

image.png

3.In this we have to create another file named HomePage.dart.you can download the code by click here.

4.Make changes to your main.dart file as follows:

import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'HomePage.dart';
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Image Picker Demo',
      debugShowCheckedModeBanner: false,
      home:HomePage() ,
    );
  }
}

5.Pubspec.yaml looks like this:

name: teachablemachineapp
description: A new Flutter project.

publish_to: 'none' 

version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  tflite: ^1.1.1
  meta: ^1.1.1
  image_picker: 

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:

  uses-material-design: true

  assets:
    - assets/labels.txt
    - assets/model_unquant.tflite

6.Load the tflite model into HomePage.dart file:-

//Load the Tflite model
loadModel() async {
    await Tflite.loadModel(
      model: "assets/model_unquant.tflite",
      labels: "assets/labels.txt",
    );
}

7.Classification of the image through model:-

classifyImage(image) async {
    var output = await Tflite.runModelOnImage(
      path: image.path,
      numResults: 2,
      threshold: 0.5,
      imageMean: 127.5,
      imageStd: 127.5,
    );
    setState(() {
      _loading = false;
      //Declare List _outputs in the class which will be used to show the classified classs name and confidence
      _outputs = output;
    });
  }

Conclusion:- Flutter works well with Tensorflow Lite; we can quickly create a variety of apps and test them as proofs of concept; all we need is an idea and some training data. This technique is best suited for users who don't want to get their hands dirty with the TensorFlow platform by writing Python code and then going through the hassle of converting the trained model to tflite type; Teachable Machine will perform all of that for you so you can focus on the application.