tensorflow_lite 0.0.3

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 87

tensorflow_lite

pub package

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

example/lib/main.dart

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 Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages 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';
  
Version Uploaded Documentation Archive
0.0.3 Apr 24, 2018 Go to the documentation of tensorflow_lite 0.0.3 Download tensorflow_lite 0.0.3 archive
0.0.2 Apr 23, 2018 Go to the documentation of tensorflow_lite 0.0.2 Download tensorflow_lite 0.0.2 archive
0.0.1 Apr 23, 2018 Go to the documentation of tensorflow_lite 0.0.1 Download tensorflow_lite 0.0.1 archive

Analysis

We analyzed this package on Jul 13, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3
  • Flutter: 0.5.4

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
75 / 100
Health:
Code health derived from static analysis. [more]
97 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
87
Learn more about scoring.

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 3 hints.

    Run flutter format to format lib/src/classifier.dart.

    Run flutter format to format lib/src/tflite_image_classifier.dart.

    Similar analysis of the following files failed:

    • lib/tensorflow_lite.dart (hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0
collection ^1.14.6 1.14.6 1.14.10
flutter 0.0.0
image ^1.1.31 1.1.33 2.0.4
meta ^1.1.2 1.1.5
Transitive dependencies
archive 1.0.33 2.0.0
args 1.4.3
browser 0.10.0+3
charcode 1.1.2
convert 2.0.1
crypto 2.0.5
matcher 0.12.3
path 1.6.0 1.6.1
petitparser 1.7.6
sky_engine 0.0.99
stack_trace 1.9.2
typed_data 1.1.5
vector_math 2.0.6 2.0.7
xml 3.0.0