tflite 0.0.1 tflite: ^0.0.1 copied to clipboard
A Flutter plugin for accessing TensorFlow Lite.
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tflite/tflite.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
File _image;
List _recognitions;
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.camera);
recognizeImage(image);
setState(() {
_image = image;
});
}
@override
void initState() {
super.initState();
loadModel();
}
Future loadModel() async {
try {
String res = await Tflite.loadModel(
model: "assets/mobilenet_v1_1.0_224.tflite",
labels: "assets/labels.txt",
);
print(res);
} on PlatformException {
print('Failed to load model.');
}
}
Future recognizeImage(File image) async {
var recognitions = await Tflite.runModelOnImage(
path: image.path,
numResults: 6,
threshold: 0.05,
);
print(recognitions);
setState(() {
_recognitions = recognitions;
});
// await Tflite.close();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('tflite example app'),
),
body: Stack(
children: <Widget>[
Center(
child: _image == null
? Text('No image selected.')
: Image.file(_image),
),
Center(
child: Column(
children: _recognitions != null
? _recognitions.map((res) {
return Text(
"${res["index"]} - ${res["label"]}: ${res["confidence"].toString()}",
style: TextStyle(
color: Colors.black,
fontSize: 20.0,
background: Paint()..color = Colors.white,
),
);
}).toList()
: [],
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.add_a_photo),
),
),
);
}
}