How To Use TensorFlow lite in Flutter?

Yanwei Liu
1 min readMar 11, 2020

--

Assuming that we have the tflite model, then:

1.Adding this package in pubspec file:

dependencies:
tflite: ^1.0.5

2.add model in the assets folder and update pubspec file:

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

3.Load the model:

static Future<String> loadModel() async{  return Tflite.loadModel(
model: "assets/model_unquant.tflite",
labels: "assets/labels.txt",
);
}
void initState() {
super.initState();
//Load TFLite Model
TFLiteHelper.loadModel().then((value) {
setState(() {
modelLoaded = true;
});
});
}

4.use the output from Camera Controller to feed to provide frames to TensorFlow lite.

await Tflite.runModelOnFrame(
bytesList: image.planes.map((plane) {
return plane.bytes;
}).toList(),
numResults: 5)
.then((value) {
if (value.isNotEmpty) {
//Do something with the results
}});

--

--

No responses yet