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!
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).
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>
To use the on-device text recognition model, run the text detector as described below:
FirebaseVisionImage
object from your image.To create a FirebaseVisionImage
from an image File
object:
final File imageFile = getImageFile();
final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(imageFile);
TextDetector
and pass visionImage
to detectInImage().
final TextDetector detector = FirebaseVision.instance.getTextDetector();
final List<TextBlock> blocks = await detector.detectInImage(visionImage);
detector.close();
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) {
// ...
}
}
}
See the example
directory for a complete sample app using Google ML Kit for Firebase.
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;
}
}
Add this to your package's pubspec.yaml file:
dependencies:
firebase_ml_vision: ^0.0.1
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.
Now in your Dart code, you can use:
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.6.0+1 | Feb 21, 2019 |
|
|
0.6.0 | Feb 20, 2019 |
|
|
0.5.1+1 | Feb 15, 2019 |
|
|
0.5.1 | Feb 14, 2019 |
|
|
0.5.0+1 | Feb 13, 2019 |
|
|
0.4.0+1 | Feb 8, 2019 |
|
|
0.3.0 | Jan 25, 2019 |
|
|
0.2.1 | Jan 9, 2019 |
|
|
0.2.0+2 | Nov 16, 2018 |
|
|
0.2.0+1 | Oct 12, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
96
|
Health:
Code health derived from static analysis.
[more]
|
100
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
90
|
Overall:
Weighted score of the above.
[more]
|
96
|
We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
Package is pre-v0.1 release. (-10 points)
While nothing is inherently wrong with versions of 0.0.*
, it might mean 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.11 | ||
meta | 1.1.6 | 1.1.7 | |
sky_engine | 0.0.99 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 | ||
Dev dependencies | |||
flutter_test |