-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.dart
More file actions
165 lines (140 loc) · 4.82 KB
/
main.dart
File metadata and controls
165 lines (140 loc) · 4.82 KB
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
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as image_lib;
import 'package:logging/logging.dart';
import 'package:simple_frame_app/frame_vision_app.dart';
import 'package:simple_frame_app/simple_frame_app.dart';
import 'package:simple_frame_app/tx/plain_text.dart';
import 'helper/image_classification_helper.dart';
void main() => runApp(const MainApp());
final _log = Logger("MainApp");
class MainApp extends StatefulWidget {
const MainApp({super.key});
@override
MainAppState createState() => MainAppState();
}
class MainAppState extends State<MainApp> with SimpleFrameAppState, FrameVisionAppState {
// main state of image processing in progress
bool _processing = false;
// Classification
late ImageClassificationHelper _imageClassificationHelper;
String _top3 = '';
// the image and metadata to show
Image? _image;
ImageMetadata? _imageMeta;
MainAppState() {
Logger.root.level = Level.INFO;
Logger.root.onRecord.listen((record) {
debugPrint('${record.level.name}: ${record.time}: ${record.loggerName}: ${record.message}');
});
}
@override
void initState() {
super.initState();
_imageClassificationHelper = ImageClassificationHelper();
_imageClassificationHelper.initHelper();
// kick off the connection to Frame and start the app if possible
tryScanAndConnectAndStart(andRun: true);
}
@override
Future<void> onRun() async {
// initial message to display when running
await frame!.sendMessage(
TxPlainText(
msgCode: 0x0a,
text: '2-Tap: take photo'
)
);
}
@override
Future<void> onCancel() async {
// no app-specific cleanup required here
}
@override
Future<void> onTap(int taps) async {
switch (taps) {
case 2:
// check if there's processing in progress already and drop the request if so
if (!_processing) {
_processing = true;
// synchronously call the capture and processing (just display) of the photo
await capture().then(process);
}
break;
default:
}
}
/// The vision pipeline to run when a photo is captured
/// Which in this case is just displaying
FutureOr<void> process((Uint8List, ImageMetadata) photo) async {
var imageData = photo.$1;
var meta = photo.$2;
// Perform vision processing pipeline
// send image to classifier, produce some candidate classes (https://pub.dev/packages/tflite_flutter)
Map<String, double> classification = await _imageClassificationHelper.inferenceImage(image_lib.decodeJpg(imageData)!);
// classification map is unordered and can be long, sort it and pick the best 3 here
_top3 = (classification.entries.toList()
..sort((a, b) => a.value.compareTo(b.value),))
.reversed.take(3).toList().fold<String>('', (previousValue, element) => '$previousValue\n${element.key}: ${element.value.toStringAsFixed(2)}').trim();
_log.fine('Classification result: $_top3');
// Frame display
await frame!.sendMessage(TxPlainText(msgCode: 0x0a, text: _top3));
// UI display
setState(() {
_image = Image.memory(imageData, gaplessPlayback: true,);
_imageMeta = meta;
});
// finished processing, ready to start again
_processing = false;
}
/// cancel the current photo
@override
Future<void> cancel() async {
currentState = ApplicationState.ready;
if (mounted) setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Vision - Classification',
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text('Vision - Classification'),
actions: [getBatteryWidget()]
),
drawer: getCameraDrawer(),
onDrawerChanged: (isOpened) {
if (isOpened) {
// if the user opens the camera settings, stop streaming
_processing = false;
}
else {
// if the user closes the camera settings, send the updated settings to Frame
sendExposureSettings();
}
},
body: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
children: [
_image ?? Container(),
const Divider(),
if (_imageMeta != null) ImageMetadataWidget(meta: _imageMeta!),
const Divider(),
Text(_top3),
],
)
),
const Divider(),
],
),
floatingActionButton: getFloatingActionButtonWidget(const Icon(Icons.camera_alt), const Icon(Icons.cancel)),
persistentFooterButtons: getFooterButtonsWidget(),
),
);
}
}