Skip to content

Commit

Permalink
migrate to google_ml_kit from firebase_ml_kit
Browse files Browse the repository at this point in the history
have to set minSDK to 21... :/
  • Loading branch information
X-Wei committed Jul 30, 2021
1 parent 639c0c0 commit 4f967b3
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 76 deletions.
7 changes: 1 addition & 6 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {

defaultConfig {
applicationId "io.github.x_wei.flutter_catalog"
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down Expand Up @@ -99,11 +99,6 @@ dependencies {
implementation 'com.google.firebase:firebase-analytics:17.5.0'
implementation 'com.google.firebase:firebase-auth:19.4.0'
implementation 'com.google.firebase:firebase-firestore:21.6.0'
// Cf. https://firebase.google.com/docs/ml-kit/android/read-barcodes.
implementation 'com.google.firebase:firebase-ml-vision:24.0.3'
implementation 'com.google.firebase:firebase-ml-vision-barcode-model:16.0.1'
implementation 'com.google.firebase:firebase-ml-vision-image-label-model:20.0.1'
implementation 'com.google.firebase:firebase-ml-vision-face-model:20.0.1'
}

apply plugin: 'com.google.gms.google-services'
4 changes: 2 additions & 2 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
</intent-filter>
</activity>
<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="ocr,label,barcode,face" />
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="ocr,ica,barcode,face" />
<meta-data
android:name="flutterEmbedding"
android:value="2" />
Expand Down
1 change: 1 addition & 0 deletions lib/generated_plugin_registrant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Generated file. Do not edit.
//

// ignore_for_file: directives_ordering
// ignore_for_file: lines_longer_than_80_chars

import 'package:cloud_firestore_web/cloud_firestore_web.dart';
Expand Down
8 changes: 5 additions & 3 deletions lib/my_app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1057,12 +1057,14 @@ const kMyAppRoutesAdvanced = <MyRouteGroup>[
},
),
MyRoute(
child: FirebaseMLKitExample(),
child: GoogleMLKitExample(),
sourceFilePath: 'lib/routes/firebase_mlkit_ex.dart',
title: 'Firebase ML Kit',
title: 'Google ML Kit',
description: 'Image labelling, text OCR, barcode scan, face detection.',
links: {
'Doc': 'https://pub.dartlang.org/packages/firebase_ml_vision',
'pub.dev': 'https://pub.dev/packages/google_ml_kit',
'MLKit doc':
'https://developers.google.com/ml-kit/vision/text-recognition',
},
),
],
Expand Down
93 changes: 31 additions & 62 deletions lib/routes/firebase_mlkit_ex.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import 'dart:io';

import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:image_picker/image_picker.dart';
import 'package:transparent_image/transparent_image.dart'
show kTransparentImage;
import 'package:flutter/material.dart';

// NOTE: to add firebase support, first go to firebase console, generate the
// firebase json file, and add configuration lines in the gradle files.
// C.f. this commit: https://github.com/X-Wei/flutter_catalog/commit/48792cbc0de62fc47e0e9ba2cd3718117f4d73d1.

// Adapted from the flutter firestore "babyname voter" codelab:
// https://codelabs.developers.google.com/codelabs/flutter-firebase/#0
class FirebaseMLKitExample extends StatefulWidget {
const FirebaseMLKitExample({Key? key}) : super(key: key);
class GoogleMLKitExample extends StatefulWidget {
const GoogleMLKitExample({Key? key}) : super(key: key);

@override
_FirebaseMLKitExampleState createState() => _FirebaseMLKitExampleState();
_GoogleMLKitExampleState createState() => _GoogleMLKitExampleState();
}

class _FirebaseMLKitExampleState extends State<FirebaseMLKitExample> {
class _GoogleMLKitExampleState extends State<GoogleMLKitExample> {
File? _imageFile;
String _mlResult = '<no result>';
final _picker = ImagePicker();
Expand Down Expand Up @@ -78,16 +72,14 @@ class _FirebaseMLKitExampleState extends State<FirebaseMLKitExample> {
return;
}
String result = '';
final FirebaseVisionImage visionImage =
FirebaseVisionImage.fromFile(this._imageFile!);
final ImageLabeler labelDetector = FirebaseVision.instance.imageLabeler();
final List<ImageLabel> labels =
await labelDetector.processImage(visionImage);
final InputImage inputImage = InputImage.fromFile(this._imageFile!);
final ImageLabeler imageLabeler = GoogleMlKit.vision.imageLabeler();
final List<ImageLabel> labels = await imageLabeler.processImage(inputImage);
result += 'Detected ${labels.length} labels.\n';
for (final ImageLabel label in labels) {
final String text = label.text!;
final String entityId = label.entityId!;
final double confidence = label.confidence!;
final String text = label.label;
final int entityId = label.index;
final double confidence = label.confidence;
result +=
'\n#Label: $text($entityId), confidence=${confidence.toStringAsFixed(3)}';
}
Expand All @@ -102,20 +94,18 @@ class _FirebaseMLKitExampleState extends State<FirebaseMLKitExample> {
return;
}
String result = '';
final FirebaseVisionImage visionImage =
FirebaseVisionImage.fromFile(this._imageFile!);
final TextRecognizer textRecognizer =
FirebaseVision.instance.textRecognizer();
final VisionText visionText =
await textRecognizer.processImage(visionImage);
final String text = visionText.text!;
final InputImage inputImage = InputImage.fromFile(this._imageFile!);
final TextDetector textDetector = GoogleMlKit.vision.textDetector();
final RecognisedText recognizedText =
await textDetector.processImage(inputImage);
final String text = recognizedText.text;
debugPrint('Recognized text: "$text"');
result += 'Detected ${visionText.blocks.length} text blocks.\n';
for (final TextBlock block in visionText.blocks) {
final Rect boundingBox = block.boundingBox!;
result += 'Detected ${recognizedText.blocks.length} text blocks.\n';
for (final TextBlock block in recognizedText.blocks) {
final Rect boundingBox = block.rect;
final List<Offset> cornerPoints = block.cornerPoints;
final String text = block.text!;
final List<RecognizedLanguage> languages = block.recognizedLanguages;
final String text = block.text;
final List<String> languages = block.recognizedLanguages;
result += '\n# Text block:\n '
'bbox=$boundingBox\n '
'cornerPoints=$cornerPoints\n '
Expand All @@ -138,39 +128,20 @@ class _FirebaseMLKitExampleState extends State<FirebaseMLKitExample> {
return;
}
String result = '';
final FirebaseVisionImage visionImage =
FirebaseVisionImage.fromFile(this._imageFile!);
final BarcodeDetector barcodeDetector =
FirebaseVision.instance.barcodeDetector();
final InputImage inputImage = InputImage.fromFile(this._imageFile!);
final BarcodeScanner barcodeScanner = GoogleMlKit.vision.barcodeScanner();

final List<Barcode> barcodes =
await barcodeDetector.detectInImage(visionImage);
await barcodeScanner.processImage(inputImage);
result += 'Detected ${barcodes.length} barcodes.\n';
for (final Barcode barcode in barcodes) {
final Rect boundingBox = barcode.boundingBox!;
final List<Offset> cornerPoints = barcode.cornerPoints;

final String rawValue = barcode.rawValue!;
final valueType = barcode.valueType;
final Rect boundingBox = barcode.value.boundingBox!;
final String rawValue = barcode.value.rawValue!;
final valueType = barcode.type;
result += '\n# Barcode:\n '
'bbox=$boundingBox\n '
'cornerPoints=$cornerPoints\n '
'rawValue=$rawValue\n '
'vlaueType=$valueType';
// // See API reference for complete list of supported types
// switch (valueType) {
// case BarcodeValueType.wifi:
// final String ssid = barcode.wifi.ssid;
// final String password = barcode.wifi.password;
// final BarcodeWiFiEncryptionType type = barcode.wifi.encryptionType;
// break;
// case BarcodeValueType.url:
// final String title = barcode.url.title;
// final String url = barcode.url.url;
// break;
// default:
// break;
// }
'type=$valueType';
}
if (result.isNotEmpty) {
setState(() => this._mlResult = result);
Expand All @@ -183,16 +154,14 @@ class _FirebaseMLKitExampleState extends State<FirebaseMLKitExample> {
return;
}
String result = '';
final FirebaseVisionImage visionImage =
FirebaseVisionImage.fromFile(this._imageFile!);
final InputImage inputImage = InputImage.fromFile(this._imageFile!);
const options = FaceDetectorOptions(
enableLandmarks: true,
enableClassification: true,
enableTracking: true,
);
final FaceDetector faceDetector =
FirebaseVision.instance.faceDetector(options);
final List<Face> faces = await faceDetector.processImage(visionImage);
final FaceDetector faceDetector = GoogleMlKit.vision.faceDetector(options);
final List<Face> faces = await faceDetector.processImage(inputImage);
result += 'Detected ${faces.length} faces.\n';
for (final Face face in faces) {
final Rect boundingBox = face.boundingBox;
Expand Down
2 changes: 2 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#include "generated_plugin_registrant.h"

#include <url_launcher_linux/url_launcher_plugin.h>
Expand Down
2 changes: 2 additions & 0 deletions linux/flutter/generated_plugin_registrant.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#ifndef GENERATED_PLUGIN_REGISTRANT_
#define GENERATED_PLUGIN_REGISTRANT_

Expand Down
5 changes: 2 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ dependencies:
day_night_switcher: ^0.2.0+1
edge_detection: ^1.0.6
english_words: ^4.0.0
# cf. https://github.com/fluttercandies/extended_image/issues/309
extended_image: ^4.1.0
feature_discovery: ^0.14.0
ffi: ^1.1.2
Expand All @@ -45,11 +44,10 @@ dependencies:
firebase_core: ^1.4.0
firebase_core_web: ^1.1.0
firebase_database: ^7.1.2
firebase_ml_vision: ^0.12.0+2
firebase_storage: ^10.0.1
firebase_storage_web: ^3.0.0
fl_chart: ^0.36.3
flutter_bloc: ^7.0.1
flutter_bloc: ^7.1.0
flutter_gallery_assets: ^1.0.2
# flutter_gradients: ^1.0.0+2
flutter_markdown: ^0.6.2
Expand All @@ -61,6 +59,7 @@ dependencies:
fluttertoast: ^8.0.7
freezed_annotation: ^0.14.2
google_fonts: ^2.1.0
google_ml_kit: ^0.7.0
google_sign_in: ^5.0.5
graphview: ^1.0.0-nullsafety.1
hive: ^2.0.4
Expand Down

0 comments on commit 4f967b3

Please sign in to comment.