forked from AseemWangoo/flutter_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_labelling.dart
103 lines (91 loc) · 2.73 KB
/
image_labelling.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//import 'package:flutfire/mlkit/ml_detail.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:mlkit/mlkit.dart';
import 'dart:async';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
File _file;
List<VisionLabel> _currentLabels = <VisionLabel>[];
FirebaseVisionLabelDetector detector = FirebaseVisionLabelDetector.instance;
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Image Labeling Firebase'),
),
body: _buildBody(_file),
floatingActionButton: new FloatingActionButton(
onPressed: () async {
try {
var file =
await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_file = file;
});
var currentLabels =
await detector.detectFromBinary(_file?.readAsBytesSync());
setState(() {
_currentLabels = currentLabels;
});
} catch (e) {
print(e.toString());
}
},
child: new Icon(Icons.select_all),
),
),
);
}
//Build body
Widget _buildBody(File _file) {
return new Container(
child: new Column(
children: <Widget>[displaySelectedFile(_file), _buildList(_currentLabels)],
),
);
}
Widget _buildList(List<VisionLabel> labels) {
if (labels == null || labels.length == 0 ) {
return new Text('Empty', textAlign: TextAlign.center);
}
return new Expanded(
child: new Container(
child: new ListView.builder(
padding: const EdgeInsets.all(1.0),
itemCount: labels.length,
itemBuilder: (context, i) {
return _buildRow(labels[i].label, labels[i].confidence.toString(), labels[i].entityID);
}),
),
);
}
Widget displaySelectedFile(File file) {
return new SizedBox(
// height: 200.0,
width: 150.0,
child: file == null
? new Text('Sorry nothing selected!!')
: new Image.file(file),
);
}
//Display labels
Widget _buildRow(String label, String confidence, String entityID) {
return new ListTile(
title: new Text(
"\nLabel: $label \nConfidence: $confidence \nEntityID: $entityID",
),
dense: true,
);
}
}