tensorflow_lite 0.0.3
tensorflow_lite #
A Flutter plugin to access TensorFlow Lite apis. TensorFlow Lite is TensorFlow’s lightweight solution for mobile and embedded devices. With TensorFlow Lite you can deploy machine learning models on phones in your Android/iOS app.
Usage #
Add tensorflow_lite
to your pubspec.yaml
Copy your models to an asset dir like assets/mobilenet_quant_v1_224.tflite
And add it to your pubspec.yaml
assets:
- assets/mobilenet_quant_v1_224.tflite
Import tensorflow_lite in your app
import 'package:tensorflow_lite/tensorflow_lite.dart';
Create a new Interpreter instance based on your tflite model file
Interpreter model = await Interpreter.createInstance(modelFilePath: modelPath);
Pass some bytes to the model to get the output
dynamic result = await _interpreter.run(imageToByteList(image), new Uint8List(_labelList.length));
Image Classification example #
tensorflow_lite
also includes a wrapper for image classification models which can be easily loaded
without much of boilerplate code.
Future<Null> loadRecognitions() async {
var classifier = await TFLiteImageClassifier.createInstance(
assets: rootBundle,
modelPath: "assets/mobilenet_quant_v1_224.tflite",
labelPath: "assets/labels.txt",
inputSize: 224,
);
print('Classifier ready');
var imageBytes = (await rootBundle.load("assets/cat500.png")).buffer;
img.Image image = img.decodePng(imageBytes.asUint8List());
image = img.copyResize(image, 224, 224);
_recognitions = await classifier.recognizeImage(image);
setState(() {});
await classifier.close();
}
Please check the example for full usage.
Note #
- Works only on Android
- Tested only on image classification
Contributing #
I am new to Flutter and I haven't worked on iOS yet. So if you are an iOS developer, i'd be glad to receive some contribution. Just send a PR or open up an issue!
[0.0.2] - TODO: Add release date.
- Added TFLiteImageClassifier
- Improved example app
[0.0.2] - TODO: Add release date.
- Fixed README
[0.0.1] - TODO: Add release date.
- Added support for instantiating and running Interpreter
- Handles loading of assets natively
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image/image.dart' as img;
import 'package:tensorflow_lite/tensorflow_lite.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Recognition> _recognitions;
@override
void initState() {
super.initState();
loadRecognitions();
}
Future<Null> loadRecognitions() async {
var classifier = await TFLiteImageClassifier.createInstance(
assets: rootBundle,
modelPath: "assets/mobilenet_quant_v1_224.tflite",
labelPath: "assets/labels.txt",
inputSize: 224,
);
print('Classifier ready');
var imageBytes = (await rootBundle.load("assets/cat500.png")).buffer;
img.Image image = img.decodePng(imageBytes.asUint8List());
image = img.copyResize(image, 224, 224);
_recognitions = await classifier.recognizeImage(image);
setState(() {});
await classifier.close();
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: "TFLite",
theme: new ThemeData.light(),
home: new Scaffold(
appBar: new AppBar(title: new Text("TFLite Flutter"),),
body: _recognitions == null ? new Center(
child: new CircularProgressIndicator(),)
: new ListView.builder(
itemCount: _recognitions.length,
itemBuilder: (BuildContext ctx, int index) {
var item = _recognitions[index];
return new ListTile(
leading: new Text(item.id),
title: new Text(item.title),
trailing: new Text(item.confidence.toString()),
);
},
)
),
);
}
}
Use this package as a library
1. Depend on it
Add this to your package's pubspec.yaml file:
dependencies:
tensorflow_lite: ^0.0.3
2. Install it
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter pub get
Alternatively, your editor might support pub get
or flutter pub get
.
Check the docs for your editor to learn more.
3. Import it
Now in your Dart code, you can use:
import 'package:tensorflow_lite/tensorflow_lite.dart';
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
38
|
Health:
Code health derived from static analysis.
[more]
|
--
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
--
|
Overall:
Weighted score of the above.
[more]
|
19
|
The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.
Analysis issues and suggestions
Support Dart 2 in pubspec.yaml
.
The SDK constraint in pubspec.yaml
doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://dart.dev/dart-2#migration.
Maintenance issues and suggestions
Make sure dartdoc
successfully runs on your package's source files. (-10 points)
Dependencies were not resolved.
Dependencies
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=1.8.0 <2.0.0 |