You are on page 1of 4

5IIR

Développement Multi plateforme

TP : Deploy Deep learning model in Flutter app (Image classification Problem)


Flutter tflite

Objectif :

Le but du Tp est de concevoir, réaliser et déployer un modèle de Deep Learning


permettant la classification d’images .

Part I : Configuration
1. Convert your Model to TensorFlow Lite format

import tensorflow as tf

# Convertir un modèle Keras en modèle TensorFlow Lite.

converter = tf.lite.TFLiteConverter.from_keras_model(model)

# convertir un SavedModel en un modèle TensorFlow Lite.

converter =tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
# path to the SavedModel directory

tflite_model = converter.convert()

# Save the model.


with open('model.tflite', 'wb') as f:
f.write(tflite_model)

2. To use TensorFlow in your Flutter app, you need to install the following package :
 tflite: allows you to access the native TensorFlow Lite library.
Open pubspec.yaml and add them in the dependencies section:

tflite: ^1.1.2

Then, run flutter pub get to get packages

3. Importing the Model to Flutter

assets:
- assets/
Model.tflite
Labels.txt

Dr. ANIBOU Chaimae


5IIR
Développement Multi plateforme

4. Add this code android/app/build.gradle

…….
Android{
aaptOptions{
noCompress ‘tflite’
noCompress ‘lite’
}
…..}
Part II : Code

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:tflite/tflite.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}

class HomePage extends StatefulWidget {


@override
_ HomePage State createState() => _I HomePage State();
}

class _ HomePageState extends State< HomePage > {


final ImagePicker _picker = ImagePicker();
File? file;
var outputs;
var v = "";
// var dataList = [];
@override
void initState() {
super.initState();
loadmodel().then((value) {
setState(() {});
});
}

Dr. ANIBOU Chaimae


5IIR
Développement Multi plateforme

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

Future<void> _pickImage() async {


try {
final XFile? image = await _picker.pickImage(source:
ImageSource.gallery);
if (image == null) return;

setState(() {

file = File(image!.path);
});
detectimage(file!);
} catch (e) {
print('Error picking image: $e');
}
}

Future detectimage(File image) async {


int startTime = new DateTime.now().millisecondsSinceEpoch;
var predictions = await Tflite.runModelOnImage(
path: image.path,
numResults: 1,
threshold: 0.05,
imageMean: 127.5,
imageStd: 127.5,
);
setState(() {
outputs = predictions;

});
print("//////////////////////////////////////////////////");
print(_predictions);
// print(dataList);
print("//////////////////////////////////////////////////");
int endTime = new DateTime.now().millisecondsSinceEpoch;
print("Inference took ${endTime - startTime}ms");
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(

Dr. ANIBOU Chaimae


5IIR
Développement Multi plateforme

title: Text('Image classification'),

),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_image != null ?
Image.file(
(_image!.path),
height: 200,
width: 200,
) : Text('No image selected'),
SizedBox(height: 20),
outputs!= null ?
Text(_outputs[0]['label'].toString().substring(2)) : Text(''),

],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_pickImage();
},
child: Icon(Icons.add_a_photo),
tooltip: 'Pick Image from Gallery'

),

);
}
}

Dr. ANIBOU Chaimae

You might also like