Intregation of tensorflow Lite with Flutter
Tags: machinelearning, dart
Published: 2020-04-03 10:49:26

Hello guys, today I am going to show you how we can integrate tensorflow in Flutter.
Before I would like to go deep into video, if you are interested in more flutter content like this then consider subscribing.to Code Decoders

![introduction](https://i.imgur.com/SuZ7zji.png)

TensorFlow offers multiple levels of abstraction so you can choose the right one for your needs.
If one need more flexibility, eager execution allows for immediate iteration and intuitive debugging for large ML training tasks
TensorFlow has always provided a direct path to production.
TensorFlow lets you train and deploy your model easily, no matter what language or platform you use.
Google has provided Teachable Machine which is a web-based tool that makes creating machine learning models fast, easy, and accessible to everyone.

There are supporting the training of three types of models:
1.Image Project
2.Audio Project
3.Pose

![teachable machine](https://i.imgur.com/ViKg61q.png)

How can I train a model with Teachable Machine?
Well, it’s pretty simple, all you need to have is an idea about what you want your application have to do.
After this, you have to gather the data.
Then train the models.
When the training was done, I am able to export the model within minutes.
So, now comes the next part which is using this model in the Flutter app. There is no official TensorFlow Lite package made by Google yet but there is one available which is well maintained. I used this package tflite in my project.

Firstly you will see the console of the teachable machine.

![console](https://i.imgur.com/hEuZxSi.png)

We have created three class named sad, funny and laughing.

![class](https://i.imgur.com/UIQJbUc.png)

Train the Model.

![train](https://i.imgur.com/TAMwIsU.png)

Export and download the model of Tensorflow Lite

![export](https://i.imgur.com/G4cEe99.png)

We are going to use the package tflite and image_picker.

![tflite](https://i.imgur.com/bR1CEjV.png)
![image_picker](https://i.imgur.com/QxxMuMs.png)

Let go to the Installing and copy the line and paste to pubspec.yaml file. 
 After this, we will copy the code and paste in the android/app/build.gradle and in android block we will paste the code.

![code](https://i.imgur.com/g0XM3Wg.png)

Also in tflite minSdkVersion should not be smaller than 16 because tflite does not support the version.

![minSdkVersion](https://i.imgur.com/iOJ2K0W.png)

In this, I have converted the version from 16 to 19.
In Downloads extract the zip file and copy and paste in the folder.
Now we will add the assets to your application.
We will make some function for our application.
Declare the variables.

![declare the variable](https://i.imgur.com/GcFuckB.png)

Import the packages the dart file.

![import package](https://i.imgur.com/xHsFN1P.png)

Create function of initState in which declare _loading is true.
We have declare the function of loadModel and in setState declare _loading is false.

![initState](https://i.imgur.com/kGpOYbu.png)

Create function of loadModel in which Tflite models and labels is declared to loadModel.

![loadModel](https://i.imgur.com/KRjIMoq.png)

Create a pickImage in which ImagePicker is used with source of gallery.
If image is null,it will null else in setState declare_loading is true and _image = image.
We will declare the classifyImage function in which _image is given.

![pickImage](https://i.imgur.com/N3mMgy1.png)

Create a classifyImage function which is in future we will run the models of tflite in here with path ,imageMean,imageStd,numResults,threshold,asynch.
In setState declare _loading is false and _outputs = output.

![classifyImage](https://i.imgur.com/q298Xh3.png)

Create function of dispose of Tflite.

![dispose](https://i.imgur.com/lNPyH4m.png) 

Come body:-
If loading is true then if _image is null then import the image form gallery else empty container. If _image is null then Also outputs is not empty then Text of labels is identified and displayed on screen.
We will declare SizedBox.
Then FloatingActionButton is declared with tooltip and in onPressed pickImage is declared.

![body](https://i.imgur.com/A1r2qgb.png)

Now debug the code.
As you can look here, we have choose the different image and easily identified by the tensorflow Lite and show the output on the screen.

![emulator](https://i.imgur.com/hU7jexo.png)

Flutter works great with Tensorflow Lite, we can make lots of different types of applications in no time and test them as our proof of concept, all it will need is an idea and the training data. This type of approach is most suited and getting into all that trouble of finally converting the trained model to tflite type, Teachable Machine will do all that for you so you can focus on the application.
To get full code ,click below the link :- 	
👨💻Source Code:
https://github.com/Kunal923/TensorFlow_Lite/blob/master/lib/tensorflow.dart

✌Subscribe to CodeDecoders: 
http://bit.ly/CodeDecoders
YouTube: 
https://www.youtube.com/watch?v=1NoM72IZoqY&feature=youtu.be