Skip to content

Commit

Permalink
Switch to a new MLKit library which runs on-device
Browse files Browse the repository at this point in the history
  • Loading branch information
danhunsaker committed Dec 6, 2021
1 parent 502409a commit 7badd21
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 145 deletions.
33 changes: 14 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ First, add `flutter_camera_ml_vision` as a dependency.
dependencies:
flutter:
sdk: flutter
flutter_camera_ml_vision: ^2.2.4
flutter_camera_ml_vision: ^3.0.1
...
```

## Configure Firebase
You must also configure Firebase for each platform project: Android and iOS (see the `example` folder or https://firebase.google.com/codelabs/firebase-get-to-know-flutter#3 for step by step details).


### iOS

Add two rows to the ios/Runner/Info.plist:
Expand All @@ -42,10 +38,10 @@ Or in text format add the key:
If you're using one of the on-device APIs, include the corresponding ML Kit library model in your Podfile. Then run pod update in a terminal within the same directory as your Podfile.

```
pod 'Firebase/MLVisionBarcodeModel'
pod 'Firebase/MLVisionFaceModel'
pod 'Firebase/MLVisionLabelModel'
pod 'Firebase/MLVisionTextModel'
pod 'GoogleMLKit/BarcodeScanning'
pod 'GoogleMLKit/FaceDetection'
pod 'GoogleMLKit/ImageLabeling'
pod 'GoogleMLKit/TextRecognition'
```

### Android
Expand All @@ -65,7 +61,7 @@ android {
dependencies {
// ...
api 'com.google.firebase:firebase-ml-vision-image-label-model:19.0.0'
api 'com.google.mlkit:image-labeling:17.0.5'
}
}
```
Expand All @@ -78,7 +74,7 @@ Optional but recommended: If you use the on-device API, configure your app to au
<application ...>
...
<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="ocr" />
<!-- To use multiple models: android:value="ocr,label,barcode,face" -->
</application>
Expand All @@ -90,7 +86,7 @@ Optional but recommended: If you use the on-device API, configure your app to au

```dart
CameraMlVision<List<Barcode>>(
detector: FirebaseVision.instance.barcodeDetector().detectInImage,
detector: GoogleMlKit.vision.barcodeScanner().processImage,
onResult: (List<Barcode> barcodes) {
if (!mounted || resultSent) {
return;
Expand All @@ -101,15 +97,14 @@ CameraMlVision<List<Barcode>>(
)
```

`CameraMlVision` is a widget that shows the preview of the camera. It takes a detector as a parameter here we pass the `detectInImage` method of the `BarcodeDetector` object.
The detector parameter can take all the different FirebaseVision Detector. Here is a list :
`CameraMlVision` is a widget that shows the preview of the camera. It takes a detector as a parameter; here we pass the `processImage` method of the `BarcodeScanner` object.
The detector parameter can take all the different MLKit Vision Detectors. Here is a list :

```
FirebaseVision.instance.barcodeDetector().detectInImage
FirebaseVision.instance.cloudLabelDetector().detectInImage
FirebaseVision.instance.faceDetector().processImage
FirebaseVision.instance.labelDetector().detectInImage
FirebaseVision.instance.textRecognizer().processImage
GoogleMlKit.vision.barcodeScanner().processImage
GoogleMlKit.vision.imageLabeler().processImage
GoogleMlKit.vision.faceDetector().processImage
GoogleMlKit.vision.textDetector().processImage
```

Then when something is detected the onResult callback is called with the data in the parameter of the function.
Expand Down
4 changes: 1 addition & 3 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ flutter {
}

dependencies {
api 'com.google.firebase:firebase-ml-vision-barcode-model:16.1.2'
implementation 'com.google.mlkit:barcode-scanning:17.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

apply plugin: 'com.google.gms.google-services'
2 changes: 1 addition & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</intent-filter>
</activity>
<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="barcode" />
</application>
</manifest>
11 changes: 6 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:developer';
import 'dart:ui';

import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart';

Expand Down Expand Up @@ -55,7 +56,7 @@ class _MyHomePageState extends State<MyHomePage> {
}

setState(() {
data.add(barcode.displayValue);
data.add(barcode.value.displayValue);
});
},
child: Text('Scan product'),
Expand All @@ -78,7 +79,7 @@ class ScanPage extends StatefulWidget {

class _ScanPageState extends State<ScanPage> {
bool resultSent = false;
BarcodeDetector detector = FirebaseVision.instance.barcodeDetector();
BarcodeScanner scanner = GoogleMlKit.vision.barcodeScanner();

@override
Widget build(BuildContext context) {
Expand All @@ -97,7 +98,7 @@ class _ScanPageState extends State<ScanPage> {
),
);
},
detector: detector.detectInImage,
detector: scanner.processImage,
onResult: (List<Barcode> barcodes) {
if (!mounted ||
resultSent ||
Expand All @@ -109,7 +110,7 @@ class _ScanPageState extends State<ScanPage> {
Navigator.of(context).pop<Barcode>(barcodes.first);
},
onDispose: () {
detector.close();
scanner.close();
},
),
),
Expand Down
4 changes: 2 additions & 2 deletions example/lib/main_face.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:camera/camera.dart';
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart';

Expand Down Expand Up @@ -32,7 +32,7 @@ class _MyHomePageState extends State<MyHomePage> {
final _scanKey = GlobalKey<CameraMlVisionState>();
CameraLensDirection cameraLensDirection = CameraLensDirection.front;
FaceDetector detector =
FirebaseVision.instance.faceDetector(FaceDetectorOptions(
GoogleMlKit.vision.faceDetector(FaceDetectorOptions(
enableTracking: true,
mode: FaceDetectorMode.accurate,
));
Expand Down
12 changes: 6 additions & 6 deletions example/lib/main_live.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart';

Expand Down Expand Up @@ -29,7 +29,7 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
List<String> data = [];
final _scanKey = GlobalKey<CameraMlVisionState>();
BarcodeDetector detector = FirebaseVision.instance.barcodeDetector();
BarcodeScanner scanner = GoogleMlKit.vision.barcodeScanner();

@override
Widget build(BuildContext context) {
Expand All @@ -42,21 +42,21 @@ class _MyHomePageState extends State<MyHomePage> {
children: [
CameraMlVision<List<Barcode>>(
key: _scanKey,
detector: detector.detectInImage,
detector: scanner.processImage,
resolution: ResolutionPreset.high,
onResult: (barcodes) {
if (barcodes == null ||
barcodes.isEmpty ||
data.contains(barcodes.first.displayValue) ||
data.contains(barcodes.first.value.displayValue) ||
!mounted) {
return;
}
setState(() {
data.add(barcodes.first.displayValue);
data.add(barcodes.first.value.displayValue);
});
},
onDispose: () {
detector.close();
scanner.close();
},
),
Container(
Expand Down
51 changes: 9 additions & 42 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.8.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -42,7 +42,7 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -106,34 +106,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.0"
firebase_core:
dependency: transitive
description:
name: firebase_core
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
firebase_core_platform_interface:
dependency: transitive
description:
name: firebase_core_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
firebase_core_web:
dependency: transitive
description:
name: firebase_core_web
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
firebase_ml_vision:
dependency: transitive
description:
name: firebase_ml_vision
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.0+1"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -145,24 +117,19 @@ packages:
path: ".."
relative: true
source: path
version: "3.0.0+1"
version: "3.0.1"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
js:
google_ml_kit:
dependency: transitive
description:
name: js
name: google_ml_kit
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
version: "0.7.3"
matcher:
dependency: transitive
description:
Expand All @@ -176,7 +143,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -265,7 +232,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -307,7 +274,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.4.2"
typed_data:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dev_dependencies:
flutter_test:
sdk: flutter

publish_to: none

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec
Expand Down
8 changes: 4 additions & 4 deletions lib/flutter_camera_ml_vision.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'dart:ui';
import 'package:camera/camera.dart';
import 'package:collection/collection.dart';
import 'package:device_info/device_info.dart';
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand All @@ -18,7 +18,7 @@ export 'package:camera/camera.dart';

part 'utils.dart';

typedef HandleDetection<T> = Future<T> Function(FirebaseVisionImage image);
typedef HandleDetection<T> = Future<T> Function(InputImage image);
typedef ErrorWidgetBuilder = Widget Function(
BuildContext context, CameraError error);

Expand Down Expand Up @@ -66,7 +66,7 @@ class CameraMlVisionState<T> extends State<CameraMlVision<T>>
XFile? _lastImage;
final _visibilityKey = UniqueKey();
CameraController? _cameraController;
ImageRotation? _rotation;
InputImageRotation? _rotation;
_CameraState _cameraMlVisionState = _CameraState.loading;
CameraError _cameraError = CameraError.unknown;
bool _alreadyCheckingImage = false;
Expand Down Expand Up @@ -149,7 +149,7 @@ class CameraMlVisionState<T> extends State<CameraMlVision<T>>

CameraValue? get cameraValue => _cameraController?.value;

ImageRotation? get imageRotation => _rotation;
InputImageRotation? get imageRotation => _rotation;

Future<void> Function() get prepareForVideoRecording =>
_cameraController!.prepareForVideoRecording;
Expand Down
Loading

0 comments on commit 7badd21

Please sign in to comment.