Skip to content

feat!: Migrate firebase_ml_vision to sound null safety #5474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a3a5eff
Merge pull request #1 from FirebaseExtended/master
AlexHartford Mar 25, 2021
4ba19a0
Bump sdk and flutter version
AlexHartford Mar 25, 2021
6b2ec18
Migrate firebase_ml_vision to sound null safety
AlexHartford Mar 25, 2021
e81ef68
Migrate tests to sound null safety
AlexHartford Mar 25, 2021
3c000cf
Bump package version to 0.12.0
AlexHartford Mar 25, 2021
44f508e
Format files
AlexHartford Mar 25, 2021
0125670
Update example pubspec deps
AlexHartford Mar 25, 2021
420f78e
Update example to sound null safety
AlexHartford Mar 25, 2021
b1f75e0
Update example tests to sound null safety
AlexHartford Mar 25, 2021
840c65f
Addressed TODOs in example
AlexHartford Mar 25, 2021
f642f40
Addressed TODOs in package tests
AlexHartford Mar 25, 2021
f681fc6
Formatted files
AlexHartford Mar 25, 2021
b6d7c8d
Remove aspectRatio no longer needed in camera >=0.7.0
AlexHartford Mar 25, 2021
43b4ec4
Revert making height and width required params for plane metadata
AlexHartford Mar 25, 2021
c2b8952
Fix a few bugs/errors in example project
AlexHartford Mar 25, 2021
da1644c
Format files
AlexHartford Mar 25, 2021
8f7a0b9
Revert package version bump
AlexHartford Mar 26, 2021
1a6ce6b
Update packages/firebase_ml_vision/lib/src/image_labeler.dart
AlexHartford Mar 29, 2021
442e629
Merge branch 'master' into firebase_ml_vision_null_safety
AlexHartford Mar 29, 2021
a7a6105
Address most PR review
AlexHartford Mar 29, 2021
58c3d38
FirebaseVisionImageMetadata rawFormat & planeData non-required nullable
AlexHartford Mar 29, 2021
e9792ea
Merge pull request #2 from FirebaseExtended/master
AlexHartford Apr 1, 2021
f2fdb15
Merge branch 'master' of https://github.com/AlexHartford/flutterfire
AlexHartford Apr 1, 2021
8e2cff6
Merge master
AlexHartford Apr 1, 2021
eb9b4d1
Format files
AlexHartford Apr 1, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions packages/firebase_ml_vision/example/lib/camera_preview_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9

import 'package:camera/camera.dart';
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:flutter/foundation.dart';
Expand All @@ -13,15 +11,15 @@ import 'detector_painters.dart';
import 'scanner_utils.dart';

class CameraPreviewScanner extends StatefulWidget {
const CameraPreviewScanner({Key key}) : super(key: key);
const CameraPreviewScanner({Key? key}) : super(key: key);

@override
State<StatefulWidget> createState() => _CameraPreviewScannerState();
}

class _CameraPreviewScannerState extends State<CameraPreviewScanner> {
dynamic _scanResults;
CameraController _camera;
CameraController? _camera;
Detector _currentDetector = Detector.text;
bool _isDetecting = false;
CameraLensDirection _direction = CameraLensDirection.back;
Expand Down Expand Up @@ -55,20 +53,20 @@ class _CameraPreviewScannerState extends State<CameraPreviewScanner> {
: ResolutionPreset.medium,
enableAudio: false,
);
await _camera.initialize();
await _camera!.initialize();

await _camera.startImageStream((CameraImage image) {
await _camera!.startImageStream((CameraImage image) {
if (_isDetecting) return;

_isDetecting = true;

ScannerUtils.detect(
image: image,
detectInImage: _getDetectionMethod(),
detectInImage: _getDetectionMethod()!,
imageRotation: description.sensorOrientation,
).then(
(dynamic results) {
if (_currentDetector == null) return;
if (!mounted) return;
setState(() {
_scanResults = results;
});
Expand All @@ -77,7 +75,7 @@ class _CameraPreviewScannerState extends State<CameraPreviewScanner> {
});
}

Future<dynamic> Function(FirebaseVisionImage image) _getDetectionMethod() {
Future<dynamic> Function(FirebaseVisionImage image)? _getDetectionMethod() {
switch (_currentDetector) {
case Detector.text:
return _recognizer.processImage;
Expand All @@ -94,24 +92,22 @@ class _CameraPreviewScannerState extends State<CameraPreviewScanner> {
case Detector.face:
return _faceDetector.processImage;
}

return null;
}

Widget _buildResults() {
const Text noResultsText = Text('No results!');

if (_scanResults == null ||
_camera == null ||
!_camera.value.isInitialized) {
!_camera!.value.isInitialized) {
return noResultsText;
}

CustomPainter painter;

final Size imageSize = Size(
_camera.value.previewSize.height,
_camera.value.previewSize.width,
_camera!.value.previewSize!.height,
_camera!.value.previewSize!.width,
);

switch (_currentDetector) {
Expand Down Expand Up @@ -159,7 +155,7 @@ class _CameraPreviewScannerState extends State<CameraPreviewScanner> {
: Stack(
fit: StackFit.expand,
children: <Widget>[
CameraPreview(_camera),
CameraPreview(_camera!),
_buildResults(),
],
),
Expand All @@ -173,8 +169,8 @@ class _CameraPreviewScannerState extends State<CameraPreviewScanner> {
_direction = CameraLensDirection.back;
}

await _camera.stopImageStream();
await _camera.dispose();
await _camera!.stopImageStream();
await _camera!.dispose();

setState(() {
_camera = null;
Expand Down Expand Up @@ -238,7 +234,7 @@ class _CameraPreviewScannerState extends State<CameraPreviewScanner> {

@override
void dispose() {
_camera.dispose().then((_) {
_camera!.dispose().then((_) {
_barcodeDetector.close();
_faceDetector.close();
_imageLabeler.close();
Expand All @@ -247,7 +243,6 @@ class _CameraPreviewScannerState extends State<CameraPreviewScanner> {
_cloudRecognizer.close();
});

_currentDetector = null;
super.dispose();
}
}
2 changes: 0 additions & 2 deletions packages/firebase_ml_vision/example/lib/colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9

import 'package:flutter/material.dart';

const Color kShrinePink50 = Color(0xFFFEEAE6);
Expand Down
44 changes: 21 additions & 23 deletions packages/firebase_ml_vision/example/lib/detector_painters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9

import 'dart:ui' as ui;

import 'package:firebase_ml_vision/firebase_ml_vision.dart';
Expand All @@ -23,7 +21,7 @@ class BarcodeDetectorPainter extends CustomPainter {
BarcodeDetectorPainter(this.absoluteImageSize, this.barcodeLocations);

final Size absoluteImageSize;
final List<Barcode> barcodeLocations;
final List<Barcode>? barcodeLocations;

@override
void paint(Canvas canvas, Size size) {
Expand All @@ -32,18 +30,18 @@ class BarcodeDetectorPainter extends CustomPainter {

Rect scaleRect(Barcode barcode) {
return Rect.fromLTRB(
barcode.boundingBox.left * scaleX,
barcode.boundingBox.top * scaleY,
barcode.boundingBox.right * scaleX,
barcode.boundingBox.bottom * scaleY,
barcode.boundingBox!.left * scaleX,
barcode.boundingBox!.top * scaleY,
barcode.boundingBox!.right * scaleX,
barcode.boundingBox!.bottom * scaleY,
);
}

final Paint paint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 2.0;

for (final Barcode barcode in barcodeLocations) {
for (final Barcode barcode in barcodeLocations!) {
paint.color = Colors.green;
canvas.drawRect(scaleRect(barcode), paint);
}
Expand All @@ -60,7 +58,7 @@ class FaceDetectorPainter extends CustomPainter {
FaceDetectorPainter(this.absoluteImageSize, this.faces);

final Size absoluteImageSize;
final List<Face> faces;
final List<Face>? faces;

@override
void paint(Canvas canvas, Size size) {
Expand All @@ -72,7 +70,7 @@ class FaceDetectorPainter extends CustomPainter {
..strokeWidth = 2.0
..color = Colors.red;

for (final Face face in faces) {
for (final Face face in faces!) {
canvas.drawRect(
Rect.fromLTRB(
face.boundingBox.left * scaleX,
Expand All @@ -96,7 +94,7 @@ class LabelDetectorPainter extends CustomPainter {
LabelDetectorPainter(this.absoluteImageSize, this.labels);

final Size absoluteImageSize;
final List<ImageLabel> labels;
final List<ImageLabel>? labels;

@override
void paint(Canvas canvas, Size size) {
Expand All @@ -108,9 +106,9 @@ class LabelDetectorPainter extends CustomPainter {
);

builder.pushStyle(ui.TextStyle(color: Colors.green));
for (final ImageLabel label in labels) {
for (final ImageLabel label in labels!) {
builder.addText('Label: ${label.text}, '
'Confidence: ${label.confidence.toStringAsFixed(2)}\n');
'Confidence: ${label.confidence!.toStringAsFixed(2)}\n');
}
builder.pop();

Expand All @@ -135,7 +133,7 @@ class TextDetectorPainter extends CustomPainter {
TextDetectorPainter(this.absoluteImageSize, this.visionText);

final Size absoluteImageSize;
final VisionText visionText;
final VisionText? visionText;

@override
void paint(Canvas canvas, Size size) {
Expand All @@ -144,18 +142,18 @@ class TextDetectorPainter extends CustomPainter {

Rect scaleRect(TextContainer container) {
return Rect.fromLTRB(
container.boundingBox.left * scaleX,
container.boundingBox.top * scaleY,
container.boundingBox.right * scaleX,
container.boundingBox.bottom * scaleY,
container.boundingBox!.left * scaleX,
container.boundingBox!.top * scaleY,
container.boundingBox!.right * scaleX,
container.boundingBox!.bottom * scaleY,
);
}

final Paint paint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 2.0;

for (final TextBlock block in visionText.blocks) {
for (final TextBlock block in visionText!.blocks) {
for (final TextLine line in block.lines) {
for (final TextElement element in line.elements) {
paint.color = Colors.green;
Expand Down Expand Up @@ -192,10 +190,10 @@ class DocumentTextDetectorPainter extends CustomPainter {

Rect scaleRect(DocumentTextContainer container) {
return Rect.fromLTRB(
container.boundingBox.left * scaleX,
container.boundingBox.top * scaleY,
container.boundingBox.right * scaleX,
container.boundingBox.bottom * scaleY,
container.boundingBox!.left * scaleX,
container.boundingBox!.top * scaleY,
container.boundingBox!.right * scaleX,
container.boundingBox!.bottom * scaleY,
);
}

Expand Down
2 changes: 0 additions & 2 deletions packages/firebase_ml_vision/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9

import 'package:flutter/material.dart';

import 'camera_preview_scanner.dart';
Expand Down
Loading