Skip to content

Instantly share code, notes, and snippets.

@3t14
Created September 9, 2019 22:27
Show Gist options
  • Save 3t14/932ed0dab6053e47424b75fa4324a123 to your computer and use it in GitHub Desktop.
Save 3t14/932ed0dab6053e47424b75fa4324a123 to your computer and use it in GitHub Desktop.
画像ラベル判定のサンプルコード
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();
final ImageLabeler imageLabeler = FirebaseVision.instance.imageLabeler();
File _image;
List<Barcode> _barcodes;
List<Face> _faces;
List<ImageLabel> _imageLabels;
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);
}
// 画像のラベル判定
List<ImageLabel> imageLabels = await imageLabeler.processImage(visionImage);
if (imageLabels.length > 0) {
setState( () => _imageLabels = imageLabels);
}
}
}
@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';
}
if (_imageLabels != null && _imageLabels.length > 0) {
result += '画像ラベルの判定結果: ';
_imageLabels.forEach((imageLabel) {
result += imageLabel.text + '${imageLabel.confidence}, ';
});
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),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment