Professional Documents
Culture Documents
Objectif :
Part I : Configuration
1. Convert your Model to TensorFlow Lite format
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter =tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
# path to the SavedModel directory
tflite_model = converter.convert()
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
assets:
- assets/
Model.tflite
Labels.txt
…….
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());
}
loadmodel() async {
await Tflite.loadModel(
model: "assets/model.tflite",
labels: "assets/labels.txt",
);
}
setState(() {
file = File(image!.path);
});
detectimage(file!);
} catch (e) {
print('Error picking image: $e');
}
}
});
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(
),
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'
),
);
}
}