firebase_ml_vision 0.0.1

  • 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

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


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 for step by step details).


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 ...>
      android:value="ocr" />
  <!-- To use multiple models: android:value="ocr,model2,model3" -->

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);

  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.


  • Initial release with text detector.


// 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 {
  _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:;

    setState(() {
      _imageFile = imageFile;

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

    setState(() {
      _textLocations = blocks;


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

    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(
              style: const TextStyle(
                fontSize: 30.0,
          : new CustomPaint(
              painter: new ScannedTextPainter(_imageSize, _textLocations),

  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) {
        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;

  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, * scaleY,
        container.boundingBox.right * scaleX,
        container.boundingBox.bottom * scaleY,

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

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

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

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

  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:

  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
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
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


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.


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.


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