-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathmlkit.dart
178 lines (158 loc) · 5.2 KB
/
mlkit.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mlkit/mlkit.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
File _file;
List<VisionText> _currentLabels = <VisionText>[];
FirebaseVisionTextDetector detector = FirebaseVisionTextDetector.instance;
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Text recognition from image'),
),
body: _buildBody(),
floatingActionButton: new FloatingActionButton(
onPressed: () async {
try {
//var file = await ImagePicker.pickImage(source: ImageSource.camera);
var file =
await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_file = file;
});
try {
var currentLabels = await detector.detectFromPath(_file?.path);
setState(() {
_currentLabels = currentLabels;
});
} catch (e) {
print(e.toString());
}
} catch (e) {
print(e.toString());
}
},
child: new Icon(Icons.camera),
),
),
);
}
Widget _buildImage() {
return SizedBox(
height: 500.0,
child: new Center(
child: _file == null
? Text('No Image')
: new FutureBuilder<Size>(
future: _getImageSize(Image.file(_file, fit: BoxFit.fitWidth)),
builder: (BuildContext context, AsyncSnapshot<Size> snapshot) {
if (snapshot.hasData) {
return Container(
foregroundDecoration:
TextDetectDecoration(_currentLabels, snapshot.data),
child: Image.file(_file, fit: BoxFit.fitWidth));
} else {
return new Text('Detecting...');
}
},
),
),
);
}
Future<Size> _getImageSize(Image image) {
Completer<Size> completer = new Completer<Size>();
image.image.resolve(new ImageConfiguration()).addListener(
(ImageInfo info, bool _) => completer.complete(
Size(info.image.width.toDouble(), info.image.height.toDouble())));
return completer.future;
}
Widget _buildBody() {
return Container(
child: Column(
children: <Widget>[
_buildImage(),
_buildList(_currentLabels),
],
),
);
}
Widget _buildList(List<VisionText> texts) {
if (texts.length == 0) {
return Text('Empty');
}
return Expanded(
child: Container(
child: ListView.builder(
padding: const EdgeInsets.all(1.0),
itemCount: texts.length,
itemBuilder: (context, i) {
return _buildRow(texts[i].text);
}),
),
);
}
Widget _buildRow(String text) {
return ListTile(
title: Text(
"Text: ${text}",
),
dense: true,
);
}
}
class TextDetectDecoration extends Decoration {
final Size _originalImageSize;
final List<VisionText> _texts;
TextDetectDecoration(List<VisionText> texts, Size originalImageSize)
: _texts = texts,
_originalImageSize = originalImageSize;
@override
BoxPainter createBoxPainter([VoidCallback onChanged]) {
return new _TextDetectPainter(_texts, _originalImageSize);
}
}
class _TextDetectPainter extends BoxPainter {
final List<VisionText> _texts;
final Size _originalImageSize;
_TextDetectPainter(texts, originalImageSize)
: _texts = texts,
_originalImageSize = originalImageSize;
@override
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
final paint = new Paint()
..strokeWidth = 2.0
..color = Colors.red
..style = PaintingStyle.stroke;
// print("original Image Size : ${_originalImageSize}");
final _heightRatio = _originalImageSize.height / configuration.size.height;
final _widthRatio = _originalImageSize.width / configuration.size.width;
for (var text in _texts) {
// print("text : ${text.text}, rect : ${text.rect}");
final _rect = Rect.fromLTRB(
offset.dx + text.rect.left / _widthRatio,
offset.dy + text.rect.top / _heightRatio,
offset.dx + text.rect.right / _widthRatio,
offset.dy + text.rect.bottom / _heightRatio);
// print("_rect : ${_rect}");
canvas.drawRect(_rect, paint);
}
// print("offset : ${offset}");
// print("configuration : ${configuration}");
final rect = offset & configuration.size;
// print("rect container : ${rect}");
canvas.restore();
}
}