firebase_ml_vision 0.0.1

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

Google ML Kit for Firebase

pub package

A Flutter plugin to use the Google ML Kit for Firebase API.

For Flutter plugins for other Firebase products, see FlutterFire.md.

Note: This plugin is still under development, and some APIs might not be available yet. Feedback and Pull Requests are most welcome!

Usage

To use this plugin, add firebase_ml_vision as a dependency in your pubspec.yaml file. You must also configure Firebase for each platform project: Android and iOS (see the example folder or https://codelabs.developers.google.com/codelabs/flutter-firebase/#4 for step by step details).

Android

Optional but recommended: If you use the on-device API, configure your app to automatically download the ML model to the device after your app is installed from the Play Store. To do so, add the following declaration to your app's AndroidManifest.xml file:

<application ...>
  ...
  <meta-data
      android:name="com.google.firebase.ml.vision.DEPENDENCIES"
      android:value="ocr" />
  <!-- To use multiple models: android:value="ocr,model2,model3" -->
</application>

On-device Text Recognition

To use the on-device text recognition model, run the text detector as described below:

  1. Create a FirebaseVisionImage object from your image.

To create a FirebaseVisionImage from an image File object:

final File imageFile = getImageFile();
final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(imageFile);
  1. Get an instance of TextDetector and pass visionImage to detectInImage().
final TextDetector detector = FirebaseVision.instance.getTextDetector();
final List<TextBlock> blocks = await detector.detectInImage(visionImage);

detector.close();
  1. Extract text and text locations from blocks of recognized text.
for (TextBlock block in textLocations) {
  final Rectangle<num> boundingBox = block.boundingBox;
  final List<Point<num>> cornerPoints = block.cornerPoints;
  final String text = block.text;

  for (TextLine line in block.lines) {
    // ...

    for (TextElement element in line.elements) {
      // ...
    }
  }
}

Getting Started

See the example directory for a complete sample app using Google ML Kit for Firebase.

0.0.1

  • Initial release with text detector.

example/lib/main.dart

// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_ml_vision/firebase_ml_vision.dart';

void main() => runApp(new MaterialApp(home: _MyHomePage()));

class _MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<_MyHomePage> {
  File _imageFile;
  Size _imageSize;
  List<TextBlock> _textLocations;

  Future<void> _getImage() async {
    setState(() {
      _imageFile = null;
      _imageSize = null;
      _textLocations = null;
    });

    final File imageFile =
        await ImagePicker.pickImage(source: ImageSource.gallery);

    setState(() {
      _imageFile = imageFile;
    });
  }

  Future<void> _scanImage() async {
    final FirebaseVisionImage visionImage =
        FirebaseVisionImage.fromFile(_imageFile);
    final TextDetector detector = FirebaseVision.instance.getTextDetector();
    final List<TextBlock> blocks = await detector.detectInImage(visionImage);

    setState(() {
      _textLocations = blocks;
    });

    detector.close();
  }

  Future<void> _getImageSize(Image image) async {
    final Completer<Size> completer = new Completer<Size>();
    image.image.resolve(const ImageConfiguration()).addListener(
      (ImageInfo info, bool _) {
        completer.complete(Size(
          info.image.width.toDouble(),
          info.image.height.toDouble(),
        ));
      },
    );

    final Size imageSize = await completer.future;
    setState(() {
      _imageSize = imageSize;
    });
  }

  Widget _buildImage() {
    return new Container(
      constraints: const BoxConstraints.expand(),
      decoration: new BoxDecoration(
        image: new DecorationImage(
          image: Image.file(_imageFile).image,
          fit: BoxFit.fill,
        ),
      ),
      child: _imageSize == null || _textLocations == null
          ? const Center(
              child: const Text(
              "Scanning...",
              style: const TextStyle(
                color: Colors.green,
                fontSize: 30.0,
              ),
            ))
          : new CustomPaint(
              painter: new ScannedTextPainter(_imageSize, _textLocations),
            ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: const Text('ML Vision Example'),
      ),
      body: _imageFile == null
          ? const Center(child: const Text("No image selected."))
          : _buildImage(),
      floatingActionButton: new FloatingActionButton(
        onPressed: () async {
          await _getImage();
          if (_imageFile != null) {
            _getImageSize(Image.file(_imageFile));
            _scanImage();
          }
        },
        tooltip: 'Pick Image',
        child: const Icon(Icons.add_a_photo),
      ),
    );
  }
}

// Paints rectangles around all the text in the image.
class ScannedTextPainter extends CustomPainter {
  ScannedTextPainter(this.absoluteImageSize, this.textLocations);

  final Size absoluteImageSize;
  final List<TextBlock> textLocations;

  @override
  void paint(Canvas canvas, Size size) {
    final double scaleX = size.width / absoluteImageSize.width;
    final double scaleY = size.height / absoluteImageSize.height;

    Rect scaleRect(TextContainer container) {
      return new Rect.fromLTRB(
        container.boundingBox.left * scaleX,
        container.boundingBox.top * scaleY,
        container.boundingBox.right * scaleX,
        container.boundingBox.bottom * scaleY,
      );
    }

    final Paint paint = new Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0;

    for (TextBlock block in textLocations) {
      for (TextLine line in block.lines) {
        for (TextElement element in line.elements) {
          paint.color = Colors.green;
          canvas.drawRect(scaleRect(element), paint);
        }

        paint.color = Colors.yellow;
        canvas.drawRect(scaleRect(line), paint);
      }

      paint.color = Colors.red;
      canvas.drawRect(scaleRect(block), paint);
    }
  }

  @override
  bool shouldRepaint(ScannedTextPainter oldDelegate) {
    return oldDelegate.absoluteImageSize != absoluteImageSize ||
        oldDelegate.textLocations != textLocations;
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  firebase_ml_vision: ^0.0.1

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:firebase_ml_vision/firebase_ml_vision.dart';
  
Version Uploaded Documentation Archive
0.0.1 Jun 28, 2018 Go to the documentation of firebase_ml_vision 0.0.1 Download firebase_ml_vision 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
79
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

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

  • Dart: 2.0.0-dev.69.0
  • pana: 0.11.7
  • Flutter: 0.5.5

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

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 of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.6 1.14.11
meta 1.1.5 1.1.6
sky_engine 0.0.99
typed_data 1.1.5 1.1.6
vector_math 2.0.6 2.0.7
Dev dependencies
flutter_test