Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:image_picker/image_picker.dart';
- import 'package:firebase_ml_vision/firebase_ml_vision.dart';
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- // This widget is the root of your application.
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: MyHomePage(title: 'Flutter Demo Home Page'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key key, this.title}) : super(key: key);
- final String title;
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- final BarcodeDetector barcodeDetector = FirebaseVision.instance.barcodeDetector();
- final FaceDetector faceDetector = FirebaseVision.instance.faceDetector();
- File _image;
- List<Barcode> _barcodes;
- List<Face> _faces;
- getImageFromGallary() {
- getImage(ImageSource.gallery);
- }
- getImageFromCamera() {
- getImage(ImageSource.camera);
- }
- Future getImage(ImageSource source) async {
- var image = await ImagePicker.pickImage(source: source);
- setState((){
- _image = image;
- });
- // 取得した画像を元に機械学習の推論を実行する
- if (image != null) {
- // 画像ファイル形式をFirebaseVisionImageに変換
- FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(image);
- // 画像からバーコードを検出
- List<Barcode> barcodes = await barcodeDetector.detectInImage(visionImage);
- if (barcodes.length > 0) {
- // バーコードの読み取り結果を格納
- setState(() => _barcodes = barcodes);
- }
- // 画像から顔を検出
- List<Face> faces = await faceDetector.processImage(visionImage);
- if (faces.length > 0) {
- // 顔の検出結果を格納
- setState(() => _faces = faces);
- }
- }
- }
- @override
- Widget build(BuildContext context) {
- String result = '<結果>\n';
- // バーコードの認識結果
- if (_barcodes != null && _barcodes.length > 0) {
- result += 'バーコードの認識結果: ';
- _barcodes.forEach((barcode) {
- result += barcode.displayValue + ", ";
- });
- result += '\n';
- }
- if (_faces != null && _faces.length > 0) {
- result += '顔の認識結果: ';
- _faces.forEach((face) {
- var boundingBox = face.boundingBox;
- result += boundingBox.toString() + ', ';
- });
- result += '\n';
- }
- return Scaffold(
- appBar: AppBar(
- title: Text(widget.title),
- ),
- body: ListView(children: <Widget>[
- Center(
- child: _image == null ? Text('画像が選択されていません'):
- Image.file(_image)
- ),
- Text(
- result,
- overflow: TextOverflow.ellipsis,
- maxLines: 6
- ),
- ]),
- floatingActionButton: Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- mainAxisAlignment: MainAxisAlignment.end,
- children: <Widget> [
- FloatingActionButton(
- onPressed: getImageFromCamera,
- tooltip: 'カメラ撮影',
- child:
- Icon(Icons.add_a_photo),
- ),
- FloatingActionButton(
- onPressed: getImageFromGallary,
- tooltip: '写真',
- child:
- Icon(Icons.photo_album),
- ),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement