Skip to content

Commit 7badd21

Browse files
committed
Switch to a new MLKit library which runs on-device
1 parent 502409a commit 7badd21

File tree

12 files changed

+72
-145
lines changed

12 files changed

+72
-145
lines changed

README.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@ First, add `flutter_camera_ml_vision` as a dependency.
1616
dependencies:
1717
flutter:
1818
sdk: flutter
19-
flutter_camera_ml_vision: ^2.2.4
19+
flutter_camera_ml_vision: ^3.0.1
2020
...
2121
```
2222

23-
## Configure Firebase
24-
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).
25-
26-
2723
### iOS
2824

2925
Add two rows to the ios/Runner/Info.plist:
@@ -42,10 +38,10 @@ Or in text format add the key:
4238
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.
4339

4440
```
45-
pod 'Firebase/MLVisionBarcodeModel'
46-
pod 'Firebase/MLVisionFaceModel'
47-
pod 'Firebase/MLVisionLabelModel'
48-
pod 'Firebase/MLVisionTextModel'
41+
pod 'GoogleMLKit/BarcodeScanning'
42+
pod 'GoogleMLKit/FaceDetection'
43+
pod 'GoogleMLKit/ImageLabeling'
44+
pod 'GoogleMLKit/TextRecognition'
4945
```
5046

5147
### Android
@@ -65,7 +61,7 @@ android {
6561
dependencies {
6662
// ...
6763
68-
api 'com.google.firebase:firebase-ml-vision-image-label-model:19.0.0'
64+
api 'com.google.mlkit:image-labeling:17.0.5'
6965
}
7066
}
7167
```
@@ -78,7 +74,7 @@ Optional but recommended: If you use the on-device API, configure your app to au
7874
<application ...>
7975
...
8076
<meta-data
81-
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
77+
android:name="com.google.mlkit.vision.DEPENDENCIES"
8278
android:value="ocr" />
8379
<!-- To use multiple models: android:value="ocr,label,barcode,face" -->
8480
</application>
@@ -90,7 +86,7 @@ Optional but recommended: If you use the on-device API, configure your app to au
9086

9187
```dart
9288
CameraMlVision<List<Barcode>>(
93-
detector: FirebaseVision.instance.barcodeDetector().detectInImage,
89+
detector: GoogleMlKit.vision.barcodeScanner().processImage,
9490
onResult: (List<Barcode> barcodes) {
9591
if (!mounted || resultSent) {
9692
return;
@@ -101,15 +97,14 @@ CameraMlVision<List<Barcode>>(
10197
)
10298
```
10399

104-
`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.
105-
The detector parameter can take all the different FirebaseVision Detector. Here is a list :
100+
`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.
101+
The detector parameter can take all the different MLKit Vision Detectors. Here is a list :
106102

107103
```
108-
FirebaseVision.instance.barcodeDetector().detectInImage
109-
FirebaseVision.instance.cloudLabelDetector().detectInImage
110-
FirebaseVision.instance.faceDetector().processImage
111-
FirebaseVision.instance.labelDetector().detectInImage
112-
FirebaseVision.instance.textRecognizer().processImage
104+
GoogleMlKit.vision.barcodeScanner().processImage
105+
GoogleMlKit.vision.imageLabeler().processImage
106+
GoogleMlKit.vision.faceDetector().processImage
107+
GoogleMlKit.vision.textDetector().processImage
113108
```
114109

115110
Then when something is detected the onResult callback is called with the data in the parameter of the function.

example/android/app/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ flutter {
5555
}
5656

5757
dependencies {
58-
api 'com.google.firebase:firebase-ml-vision-barcode-model:16.1.2'
58+
implementation 'com.google.mlkit:barcode-scanning:17.0.0'
5959
testImplementation 'junit:junit:4.12'
6060
androidTestImplementation 'androidx.test:runner:1.1.1'
6161
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
6262
}
63-
64-
apply plugin: 'com.google.gms.google-services'

example/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</intent-filter>
3232
</activity>
3333
<meta-data
34-
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
34+
android:name="com.google.mlkit.vision.DEPENDENCIES"
3535
android:value="barcode" />
3636
</application>
3737
</manifest>

example/lib/main.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import 'dart:developer';
12
import 'dart:ui';
23

3-
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
4+
import 'package:google_ml_kit/google_ml_kit.dart';
45
import 'package:flutter/material.dart';
56
import 'package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart';
67

@@ -55,7 +56,7 @@ class _MyHomePageState extends State<MyHomePage> {
5556
}
5657

5758
setState(() {
58-
data.add(barcode.displayValue);
59+
data.add(barcode.value.displayValue);
5960
});
6061
},
6162
child: Text('Scan product'),
@@ -78,7 +79,7 @@ class ScanPage extends StatefulWidget {
7879

7980
class _ScanPageState extends State<ScanPage> {
8081
bool resultSent = false;
81-
BarcodeDetector detector = FirebaseVision.instance.barcodeDetector();
82+
BarcodeScanner scanner = GoogleMlKit.vision.barcodeScanner();
8283

8384
@override
8485
Widget build(BuildContext context) {
@@ -97,7 +98,7 @@ class _ScanPageState extends State<ScanPage> {
9798
),
9899
);
99100
},
100-
detector: detector.detectInImage,
101+
detector: scanner.processImage,
101102
onResult: (List<Barcode> barcodes) {
102103
if (!mounted ||
103104
resultSent ||
@@ -109,7 +110,7 @@ class _ScanPageState extends State<ScanPage> {
109110
Navigator.of(context).pop<Barcode>(barcodes.first);
110111
},
111112
onDispose: () {
112-
detector.close();
113+
scanner.close();
113114
},
114115
),
115116
),

example/lib/main_face.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:camera/camera.dart';
2-
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
2+
import 'package:google_ml_kit/google_ml_kit.dart';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart';
55

@@ -32,7 +32,7 @@ class _MyHomePageState extends State<MyHomePage> {
3232
final _scanKey = GlobalKey<CameraMlVisionState>();
3333
CameraLensDirection cameraLensDirection = CameraLensDirection.front;
3434
FaceDetector detector =
35-
FirebaseVision.instance.faceDetector(FaceDetectorOptions(
35+
GoogleMlKit.vision.faceDetector(FaceDetectorOptions(
3636
enableTracking: true,
3737
mode: FaceDetectorMode.accurate,
3838
));

example/lib/main_live.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
1+
import 'package:google_ml_kit/google_ml_kit.dart';
22
import 'package:flutter/material.dart';
33
import 'package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart';
44

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

3434
@override
3535
Widget build(BuildContext context) {
@@ -42,21 +42,21 @@ class _MyHomePageState extends State<MyHomePage> {
4242
children: [
4343
CameraMlVision<List<Barcode>>(
4444
key: _scanKey,
45-
detector: detector.detectInImage,
45+
detector: scanner.processImage,
4646
resolution: ResolutionPreset.high,
4747
onResult: (barcodes) {
4848
if (barcodes == null ||
4949
barcodes.isEmpty ||
50-
data.contains(barcodes.first.displayValue) ||
50+
data.contains(barcodes.first.value.displayValue) ||
5151
!mounted) {
5252
return;
5353
}
5454
setState(() {
55-
data.add(barcodes.first.displayValue);
55+
data.add(barcodes.first.value.displayValue);
5656
});
5757
},
5858
onDispose: () {
59-
detector.close();
59+
scanner.close();
6060
},
6161
),
6262
Container(

example/pubspec.lock

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.5.0"
10+
version: "2.8.1"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -42,7 +42,7 @@ packages:
4242
name: charcode
4343
url: "https://pub.dartlang.org"
4444
source: hosted
45-
version: "1.2.0"
45+
version: "1.3.1"
4646
clock:
4747
dependency: transitive
4848
description:
@@ -106,34 +106,6 @@ packages:
106106
url: "https://pub.dartlang.org"
107107
source: hosted
108108
version: "6.1.0"
109-
firebase_core:
110-
dependency: transitive
111-
description:
112-
name: firebase_core
113-
url: "https://pub.dartlang.org"
114-
source: hosted
115-
version: "1.1.0"
116-
firebase_core_platform_interface:
117-
dependency: transitive
118-
description:
119-
name: firebase_core_platform_interface
120-
url: "https://pub.dartlang.org"
121-
source: hosted
122-
version: "4.0.0"
123-
firebase_core_web:
124-
dependency: transitive
125-
description:
126-
name: firebase_core_web
127-
url: "https://pub.dartlang.org"
128-
source: hosted
129-
version: "1.0.2"
130-
firebase_ml_vision:
131-
dependency: transitive
132-
description:
133-
name: firebase_ml_vision
134-
url: "https://pub.dartlang.org"
135-
source: hosted
136-
version: "0.12.0+1"
137109
flutter:
138110
dependency: "direct main"
139111
description: flutter
@@ -145,24 +117,19 @@ packages:
145117
path: ".."
146118
relative: true
147119
source: path
148-
version: "3.0.0+1"
120+
version: "3.0.1"
149121
flutter_test:
150122
dependency: "direct dev"
151123
description: flutter
152124
source: sdk
153125
version: "0.0.0"
154-
flutter_web_plugins:
155-
dependency: transitive
156-
description: flutter
157-
source: sdk
158-
version: "0.0.0"
159-
js:
126+
google_ml_kit:
160127
dependency: transitive
161128
description:
162-
name: js
129+
name: google_ml_kit
163130
url: "https://pub.dartlang.org"
164131
source: hosted
165-
version: "0.6.3"
132+
version: "0.7.3"
166133
matcher:
167134
dependency: transitive
168135
description:
@@ -176,7 +143,7 @@ packages:
176143
name: meta
177144
url: "https://pub.dartlang.org"
178145
source: hosted
179-
version: "1.3.0"
146+
version: "1.7.0"
180147
path:
181148
dependency: transitive
182149
description:
@@ -265,7 +232,7 @@ packages:
265232
name: source_span
266233
url: "https://pub.dartlang.org"
267234
source: hosted
268-
version: "1.8.0"
235+
version: "1.8.1"
269236
stack_trace:
270237
dependency: transitive
271238
description:
@@ -307,7 +274,7 @@ packages:
307274
name: test_api
308275
url: "https://pub.dartlang.org"
309276
source: hosted
310-
version: "0.2.19"
277+
version: "0.4.2"
311278
typed_data:
312279
dependency: transitive
313280
description:

example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dev_dependencies:
3030
flutter_test:
3131
sdk: flutter
3232

33+
publish_to: none
3334

3435
# For information on the generic Dart part of this file, see the
3536
# following page: https://www.dartlang.org/tools/pub/pubspec

lib/flutter_camera_ml_vision.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'dart:ui';
88
import 'package:camera/camera.dart';
99
import 'package:collection/collection.dart';
1010
import 'package:device_info/device_info.dart';
11-
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
11+
import 'package:google_ml_kit/google_ml_kit.dart';
1212
import 'package:flutter/foundation.dart';
1313
import 'package:flutter/material.dart';
1414
import 'package:flutter/services.dart';
@@ -18,7 +18,7 @@ export 'package:camera/camera.dart';
1818

1919
part 'utils.dart';
2020

21-
typedef HandleDetection<T> = Future<T> Function(FirebaseVisionImage image);
21+
typedef HandleDetection<T> = Future<T> Function(InputImage image);
2222
typedef ErrorWidgetBuilder = Widget Function(
2323
BuildContext context, CameraError error);
2424

@@ -66,7 +66,7 @@ class CameraMlVisionState<T> extends State<CameraMlVision<T>>
6666
XFile? _lastImage;
6767
final _visibilityKey = UniqueKey();
6868
CameraController? _cameraController;
69-
ImageRotation? _rotation;
69+
InputImageRotation? _rotation;
7070
_CameraState _cameraMlVisionState = _CameraState.loading;
7171
CameraError _cameraError = CameraError.unknown;
7272
bool _alreadyCheckingImage = false;
@@ -149,7 +149,7 @@ class CameraMlVisionState<T> extends State<CameraMlVision<T>>
149149

150150
CameraValue? get cameraValue => _cameraController?.value;
151151

152-
ImageRotation? get imageRotation => _rotation;
152+
InputImageRotation? get imageRotation => _rotation;
153153

154154
Future<void> Function() get prepareForVideoRecording =>
155155
_cameraController!.prepareForVideoRecording;

0 commit comments

Comments
 (0)