Skip to content

Commit 0ce41eb

Browse files
committed
2 parents 92e8161 + 87f9937 commit 0ce41eb

6 files changed

+62
-50
lines changed

lib/src/barcode_detector.dart

-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ class BarcodeDetectorOptions {
241241
final BarcodeFormat barcodeFormats;
242242
}
243243

244-
// TODO(bparrishMines): Normalize default string values. Some values return null on iOS while Android returns empty string.
245244
/// Represents a single recognized barcode and its value.
246245
class Barcode {
247246
Barcode._(Map<dynamic, dynamic> _data)

lib/src/face_detector.dart

+10-13
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ class FaceDetector {
6565
bool _isClosed = false;
6666

6767
/// Detects faces in the input image.
68-
Future<List<Face>> processImage(FirebaseVisionImage visionImage) async {
68+
Stream<List<Face>> startDetection() {
6969
assert(!_isClosed);
7070

7171
_hasBeenOpened = true;
72-
final List<dynamic> reply =
73-
await FirebaseVision.channel.invokeListMethod<dynamic>(
74-
'FaceDetector#processImage',
72+
Stream<dynamic> data = Stream.empty();
73+
FirebaseVision.channel.invokeListMethod<dynamic>(
74+
'FaceDetector#startDetection',
7575
<String, dynamic>{
7676
'handle': _handle,
7777
'options': <String, dynamic>{
@@ -82,15 +82,12 @@ class FaceDetector {
8282
'minFaceSize': options.minFaceSize,
8383
'mode': _enumToString(options.mode),
8484
},
85-
}..addAll(visionImage._serialize()),
86-
);
87-
88-
final List<Face> faces = <Face>[];
89-
for (dynamic data in reply) {
90-
faces.add(Face._(data));
91-
}
92-
93-
return faces;
85+
},
86+
).then((onValue){
87+
const EventChannel resultsChannel = EventChannel('plugins.flutter.io/firebase_mlvision_results');
88+
data = resultsChannel.receiveBroadcastStream();
89+
});
90+
return data;
9491
}
9592

9693
/// Release resources used by this detector.

lib/src/firebase_vision.dart

+8-5
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,11 @@ class FirebaseVision extends ValueNotifier<FirebaseCameraValue> {
303303
}
304304

305305
/// Creates an instance of [FaceDetector].
306-
FaceDetector faceDetector([FaceDetectorOptions options]) {
307-
return FaceDetector._(options ?? const FaceDetectorOptions(),
308-
nextHandle++,);
306+
Stream<List<Face>> addFaceDetector([FaceDetectorOptions options]) {
307+
FaceDetector detector = FaceDetector._(options ?? const FaceDetectorOptions(),
308+
nextHandle++,
309+
);
310+
return detector.startDetection();
309311
}
310312

311313
/// Creates an instance of [ModelManager].
@@ -314,12 +316,13 @@ class FirebaseVision extends ValueNotifier<FirebaseCameraValue> {
314316
}
315317

316318
/// Creates an on device instance of [ImageLabeler].
317-
ImageLabeler imageLabeler([ImageLabelerOptions options]) {
318-
return ImageLabeler._(
319+
Stream<List<ImageLabel>> addImageLabeler([ImageLabelerOptions options]) {
320+
ImageLabeler labeler = ImageLabeler._(
319321
options: options ?? const ImageLabelerOptions(),
320322
modelType: ModelType.onDevice,
321323
handle: nextHandle++,
322324
);
325+
return labeler.startDetection();
323326
}
324327

325328
/// Creates an instance of [TextRecognizer].

lib/src/image_labeler.dart

+13-9
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,32 @@ class ImageLabeler {
4545
bool _isClosed = false;
4646

4747
/// Finds entities in the input image.
48-
Future<List<ImageLabel>> processImage(FirebaseVisionImage visionImage) async {
48+
Stream<List<ImageLabel>> startDetection() {
4949
assert(!_isClosed);
5050

5151
_hasBeenOpened = true;
52-
final List<dynamic> reply =
53-
await FirebaseVision.channel.invokeListMethod<dynamic>(
54-
'ImageLabeler#processImage',
52+
Stream<dynamic> data = Stream.empty();
53+
FirebaseVision.channel.invokeListMethod<dynamic>(
54+
'ImageLabeler#startDetection',
5555
<String, dynamic>{
5656
'handle': _handle,
5757
'options': <String, dynamic>{
5858
'modelType': _enumToString(modelType),
5959
'confidenceThreshold': _options.confidenceThreshold,
6060
},
61-
}..addAll(visionImage._serialize()),
62-
);
61+
},
62+
).then((onValue){
63+
const EventChannel resultsChannel = EventChannel('plugins.flutter.io/firebase_mlvision_results');
64+
data = resultsChannel.receiveBroadcastStream();
65+
});
6366

67+
/*
6468
final List<ImageLabel> labels = <ImageLabel>[];
65-
for (dynamic data in reply) {
69+
for (dynamic data in data) {
6670
labels.add(ImageLabel._(data));
6771
}
68-
69-
return labels;
72+
*/
73+
return data;
7074
}
7175

7276
/// Release resources used by this labeler.

lib/src/text_recognizer.dart

+10-7
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ class TextRecognizer {
3232
bool _isClosed = false;
3333

3434
/// Detects [VisionText] from a [FirebaseVisionImage].
35-
Future<VisionText> processImage(FirebaseVisionImage visionImage) async {
35+
Stream<List<VisionText>> startDetection() {
3636
assert(!_isClosed);
3737

3838
_hasBeenOpened = true;
39-
final Map<String, dynamic> reply =
40-
await FirebaseVision.channel.invokeMapMethod<String, dynamic>(
41-
'TextRecognizer#processImage',
39+
Stream<dynamic> data = Stream.empty();
40+
FirebaseVision.channel.invokeListMethod<dynamic>(
41+
'TextRecognizer#startDetection',
4242
<String, dynamic>{
4343
'handle': _handle,
4444
'options': <String, dynamic>{
4545
'modelType': _enumToString(modelType),
4646
},
47-
}..addAll(visionImage._serialize()),
48-
);
47+
},
4948

50-
return VisionText._(reply);
49+
).then((onValue){
50+
const EventChannel resultsChannel = EventChannel('plugins.flutter.io/firebase_mlvision_results');
51+
data = resultsChannel.receiveBroadcastStream();
52+
});
53+
return data;
5154
}
5255

5356
/// Release resources used by this recognizer.

lib/src/vision_edge.dart

+21-15
Original file line numberDiff line numberDiff line change
@@ -42,50 +42,56 @@ class VisionEdgeImageLabeler {
4242
bool _isClosed = false;
4343

4444
/// Finds entities in the input image.
45-
Future<List<VisionEdgeImageLabel>> processImage(
46-
FirebaseVisionImage visionImage) async {
45+
Stream<List<VisionEdgeImageLabel>> startDetection() {
4746
assert(!_isClosed);
4847

4948
_hasBeenOpened = true;
50-
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
5149
// https://github.com/flutter/flutter/issues/26431
5250
// ignore: strong_mode_implicit_dynamic_method
5351
if (_modelLocation == ModelLocation.Local) {
54-
final List<dynamic> reply = await FirebaseVision.channel.invokeMethod(
52+
Stream<dynamic> data = Stream.empty();
53+
FirebaseVision.channel.invokeListMethod<dynamic>(
5554
'VisionEdgeImageLabeler#processLocalImage',
5655
<String, dynamic>{
5756
'handle': _handle,
5857
'options': <String, dynamic>{
5958
'dataset': _dataset,
6059
'confidenceThreshold': _options.confidenceThreshold,
6160
},
62-
}..addAll(visionImage._serialize()),
63-
);
64-
61+
},
62+
).then((onValue){
63+
const EventChannel resultsChannel = EventChannel('plugins.flutter.io/firebase_mlvision_results');
64+
data = resultsChannel.receiveBroadcastStream();
65+
});
66+
/*
6567
final List<VisionEdgeImageLabel> labels = <VisionEdgeImageLabel>[];
6668
for (dynamic data in reply) {
6769
labels.add(VisionEdgeImageLabel._(data));
6870
}
69-
70-
return labels;
71+
*/
72+
return data;
7173
} else {
72-
final List<dynamic> reply = await FirebaseVision.channel.invokeMethod(
74+
Stream<dynamic> data = Stream.empty();
75+
FirebaseVision.channel.invokeListMethod<dynamic>(
7376
'VisionEdgeImageLabeler#processRemoteImage',
7477
<String, dynamic>{
7578
'handle': _handle,
7679
'options': <String, dynamic>{
7780
'dataset': _dataset,
7881
'confidenceThreshold': _options.confidenceThreshold,
7982
},
80-
}..addAll(visionImage._serialize()),
81-
);
82-
83+
},
84+
).then((onValue){
85+
const EventChannel resultsChannel = EventChannel('plugins.flutter.io/firebase_mlvision_results');
86+
data = resultsChannel.receiveBroadcastStream();
87+
});
88+
/*
8389
final List<VisionEdgeImageLabel> labels = <VisionEdgeImageLabel>[];
8490
for (dynamic data in reply) {
8591
labels.add(VisionEdgeImageLabel._(data));
8692
}
87-
88-
return labels;
93+
*/
94+
return data;
8995
}
9096
}
9197

0 commit comments

Comments
 (0)