forked from firebase/flutterfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[firebase_ml_vision] Move firebase_ml_vision examples to this repo (f…
- Loading branch information
1 parent
4e649b8
commit 71d3b85
Showing
9 changed files
with
1,100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
packages/firebase_ml_vision/example/lib/camera_preview_scanner.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
// Copyright 2019 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:camera/camera.dart'; | ||
import 'package:firebase_ml_vision/firebase_ml_vision.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'detector_painters.dart'; | ||
import 'scanner_utils.dart'; | ||
|
||
class CameraPreviewScanner extends StatefulWidget { | ||
@override | ||
State<StatefulWidget> createState() => _CameraPreviewScannerState(); | ||
} | ||
|
||
class _CameraPreviewScannerState extends State<CameraPreviewScanner> { | ||
dynamic _scanResults; | ||
CameraController _camera; | ||
Detector _currentDetector = Detector.text; | ||
bool _isDetecting = false; | ||
CameraLensDirection _direction = CameraLensDirection.back; | ||
|
||
final BarcodeDetector _barcodeDetector = | ||
FirebaseVision.instance.barcodeDetector(); | ||
final FaceDetector _faceDetector = FirebaseVision.instance.faceDetector(); | ||
final ImageLabeler _imageLabeler = FirebaseVision.instance.imageLabeler(); | ||
final ImageLabeler _cloudImageLabeler = | ||
FirebaseVision.instance.cloudImageLabeler(); | ||
final TextRecognizer _recognizer = FirebaseVision.instance.textRecognizer(); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_initializeCamera(); | ||
} | ||
|
||
void _initializeCamera() async { | ||
final CameraDescription description = | ||
await ScannerUtils.getCamera(_direction); | ||
|
||
_camera = CameraController( | ||
description, | ||
defaultTargetPlatform == TargetPlatform.iOS | ||
? ResolutionPreset.low | ||
: ResolutionPreset.medium, | ||
); | ||
await _camera.initialize(); | ||
|
||
_camera.startImageStream((CameraImage image) { | ||
if (_isDetecting) return; | ||
|
||
_isDetecting = true; | ||
|
||
ScannerUtils.detect( | ||
image: image, | ||
detectInImage: _getDetectionMethod(), | ||
imageRotation: description.sensorOrientation, | ||
).then( | ||
(dynamic results) { | ||
if (_currentDetector == null) return; | ||
setState(() { | ||
_scanResults = results; | ||
}); | ||
}, | ||
).whenComplete(() => _isDetecting = false); | ||
}); | ||
} | ||
|
||
Future<dynamic> Function(FirebaseVisionImage image) _getDetectionMethod() { | ||
switch (_currentDetector) { | ||
case Detector.text: | ||
return _recognizer.processImage; | ||
case Detector.barcode: | ||
return _barcodeDetector.detectInImage; | ||
case Detector.label: | ||
return _imageLabeler.processImage; | ||
case Detector.cloudLabel: | ||
return _cloudImageLabeler.processImage; | ||
case Detector.face: | ||
return _faceDetector.processImage; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
Widget _buildResults() { | ||
const Text noResultsText = Text('No results!'); | ||
|
||
if (_scanResults == null || | ||
_camera == null || | ||
!_camera.value.isInitialized) { | ||
return noResultsText; | ||
} | ||
|
||
CustomPainter painter; | ||
|
||
final Size imageSize = Size( | ||
_camera.value.previewSize.height, | ||
_camera.value.previewSize.width, | ||
); | ||
|
||
switch (_currentDetector) { | ||
case Detector.barcode: | ||
if (_scanResults is! List<Barcode>) return noResultsText; | ||
painter = BarcodeDetectorPainter(imageSize, _scanResults); | ||
break; | ||
case Detector.face: | ||
if (_scanResults is! List<Face>) return noResultsText; | ||
painter = FaceDetectorPainter(imageSize, _scanResults); | ||
break; | ||
case Detector.label: | ||
if (_scanResults is! List<ImageLabel>) return noResultsText; | ||
painter = LabelDetectorPainter(imageSize, _scanResults); | ||
break; | ||
case Detector.cloudLabel: | ||
if (_scanResults is! List<ImageLabel>) return noResultsText; | ||
painter = LabelDetectorPainter(imageSize, _scanResults); | ||
break; | ||
default: | ||
assert(_currentDetector == Detector.text); | ||
if (_scanResults is! VisionText) return noResultsText; | ||
painter = TextDetectorPainter(imageSize, _scanResults); | ||
} | ||
|
||
return CustomPaint( | ||
painter: painter, | ||
); | ||
} | ||
|
||
Widget _buildImage() { | ||
return Container( | ||
constraints: const BoxConstraints.expand(), | ||
child: _camera == null | ||
? const Center( | ||
child: Text( | ||
'Initializing Camera...', | ||
style: TextStyle( | ||
color: Colors.green, | ||
fontSize: 30.0, | ||
), | ||
), | ||
) | ||
: Stack( | ||
fit: StackFit.expand, | ||
children: <Widget>[ | ||
CameraPreview(_camera), | ||
_buildResults(), | ||
], | ||
), | ||
); | ||
} | ||
|
||
void _toggleCameraDirection() async { | ||
if (_direction == CameraLensDirection.back) { | ||
_direction = CameraLensDirection.front; | ||
} else { | ||
_direction = CameraLensDirection.back; | ||
} | ||
|
||
await _camera.stopImageStream(); | ||
await _camera.dispose(); | ||
|
||
setState(() { | ||
_camera = null; | ||
}); | ||
|
||
_initializeCamera(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text('ML Vision Example'), | ||
actions: <Widget>[ | ||
PopupMenuButton<Detector>( | ||
onSelected: (Detector result) { | ||
_currentDetector = result; | ||
}, | ||
itemBuilder: (BuildContext context) => <PopupMenuEntry<Detector>>[ | ||
const PopupMenuItem<Detector>( | ||
child: Text('Detect Barcode'), | ||
value: Detector.barcode, | ||
), | ||
const PopupMenuItem<Detector>( | ||
child: Text('Detect Face'), | ||
value: Detector.face, | ||
), | ||
const PopupMenuItem<Detector>( | ||
child: Text('Detect Label'), | ||
value: Detector.label, | ||
), | ||
const PopupMenuItem<Detector>( | ||
child: Text('Detect Cloud Label'), | ||
value: Detector.cloudLabel, | ||
), | ||
const PopupMenuItem<Detector>( | ||
child: Text('Detect Text'), | ||
value: Detector.text, | ||
), | ||
], | ||
), | ||
], | ||
), | ||
body: _buildImage(), | ||
floatingActionButton: FloatingActionButton( | ||
onPressed: _toggleCameraDirection, | ||
child: _direction == CameraLensDirection.back | ||
? const Icon(Icons.camera_front) | ||
: const Icon(Icons.camera_rear), | ||
), | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_camera.dispose().then((_) { | ||
_barcodeDetector.close(); | ||
_faceDetector.close(); | ||
_imageLabeler.close(); | ||
_cloudImageLabeler.close(); | ||
_recognizer.close(); | ||
}); | ||
|
||
_currentDetector = null; | ||
super.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright 2019 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
const Color kShrinePink50 = Color(0xFFFEEAE6); | ||
const Color kShrinePink100 = Color(0xFFFEDBD0); | ||
const Color kShrineFrameBrown = Color(0x8A442C2E); | ||
const Color kShrineScrim = Color(0x73442C2E); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.