Skip to content

Commit

Permalink
[firebase_ml_vision] Move firebase_ml_vision examples to this repo (f…
Browse files Browse the repository at this point in the history
  • Loading branch information
bparrishMines authored Jun 18, 2019
1 parent 4e649b8 commit 71d3b85
Show file tree
Hide file tree
Showing 9 changed files with 1,100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ android {

defaultConfig {
applicationId "io.flutter.plugins.firebasemlvisionexample"
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/firebase_ml_vision/example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSCameraUsageDescription</key>
<string>Can I use the camera please?</string>
</dict>
</plist>
230 changes: 230 additions & 0 deletions packages/firebase_ml_vision/example/lib/camera_preview_scanner.dart
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();
}
}
10 changes: 10 additions & 0 deletions packages/firebase_ml_vision/example/lib/colors.dart
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);
16 changes: 14 additions & 2 deletions packages/firebase_ml_vision/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'package:flutter/material.dart';

import 'camera_preview_scanner.dart';
import 'material_barcode_scanner.dart';
import 'picture_scanner.dart';

void main() {
Expand All @@ -12,6 +14,10 @@ void main() {
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => _ExampleList(),
'/$PictureScanner': (BuildContext context) => PictureScanner(),
'/$CameraPreviewScanner': (BuildContext context) =>
CameraPreviewScanner(),
'/$MaterialBarcodeScanner': (BuildContext context) =>
const MaterialBarcodeScanner(),
},
),
);
Expand All @@ -23,7 +29,11 @@ class _ExampleList extends StatefulWidget {
}

class _ExampleListState extends State<_ExampleList> {
static final List<String> _exampleWidgetNames = <String>['$PictureScanner'];
static final List<String> _exampleWidgetNames = <String>[
'$PictureScanner',
'$CameraPreviewScanner',
'$MaterialBarcodeScanner',
];

@override
Widget build(BuildContext context) {
Expand All @@ -37,7 +47,9 @@ class _ExampleListState extends State<_ExampleList> {
final String widgetName = _exampleWidgetNames[index];

return Container(
decoration: BoxDecoration(border: Border.all(color: Colors.grey)),
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.grey)),
),
child: ListTile(
title: Text(widgetName),
onTap: () => Navigator.pushNamed(context, '/$widgetName'),
Expand Down
Loading

0 comments on commit 71d3b85

Please sign in to comment.