/
seeso_provider.dart
277 lines (246 loc) · 7.43 KB
/
seeso_provider.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import 'dart:async';
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:seeso_flutter/event/calibration_info.dart';
import 'package:seeso_flutter/event/gaze_info.dart';
import 'package:seeso_flutter/event/status_info.dart';
import 'package:seeso_flutter/event/user_status_info.dart';
import 'package:seeso_flutter/one_euro_filter_manager.dart';
import 'package:seeso_flutter/seeso.dart';
import 'package:seeso_flutter/seeso_initialized_result.dart';
import 'package:seeso_flutter/seeso_plugin_constants.dart';
final seesoProvider = ChangeNotifierProvider<SeeSoProvider>((ref) {
return SeeSoProvider();
});
//todo Please enter the key value for development issued by the SeeSo.io
const String licenseKey = "Input your license key.";
enum SeeSoState {
first,
none,
initializing,
initialized,
tracking,
}
class SeeSoProvider with ChangeNotifier {
final _seeso = SeeSo();
bool hasCameraPermission = false;
bool useUserStatus = false;
bool _useFilter = false;
GazeInfo? gazeInfo;
Point nextPoint = const Point(0, 0);
double progress = 0;
CalibrationMode calibrationMode = CalibrationMode.FIVE;
SeeSoState state = SeeSoState.first;
String? failedReason;
List<double>? calibrationData;
bool isCalibrating = false;
final OneEuroFilterManager _filterManager = OneEuroFilterManager(count: 2);
UserStatusInfo? attentionInfo, blinkInfo, drowsinessInfo;
SeeSoProvider() {
checkCameraPermission();
}
void checkCameraPermission() async {
hasCameraPermission = await _seeso.checkCameraPermission();
if (hasCameraPermission) {
state = SeeSoState.none;
}
notifyListeners();
}
void requestCameraPermission() async {
hasCameraPermission = await _seeso.requestCameraPermission();
if (hasCameraPermission) {
state = SeeSoState.none;
}
notifyListeners();
}
void initGazeTracker(
bool useAttention, bool useBlink, bool useDrowsiness) async {
state = SeeSoState.initializing;
notifyListeners();
if (useAttention || useBlink || useDrowsiness) {
useUserStatus = true;
} else {
useUserStatus = false;
}
try {
InitializedResult? initResult = await _seeso.initGazeTracker(
licenseKey: licenseKey,
useAttention: useAttention,
useBlink: useBlink,
useDrowsiness: useDrowsiness);
if (initResult != null && initResult.result == true) {
state = SeeSoState.initialized;
failedReason = null;
if (useUserStatus) {
_seeso.setAttentionInterval(10);
}
setListener();
} else {
state = SeeSoState.none;
failedReason =
initResult != null ? initResult.message : "Unknown error";
}
notifyListeners();
} on PlatformException catch (e) {
if (kDebugMode) {
print("failed seeso's initGazeTracker call : ${e.message}");
}
state = SeeSoState.none;
failedReason = e.message;
notifyListeners();
}
}
void deinitGazeTracker() {
_seeso.deinitGazeTracker();
useUserStatus = false;
gazeInfo = null;
nextPoint = const Point(0, 0);
progress = 0;
failedReason = null;
attentionInfo = null;
blinkInfo = null;
drowsinessInfo = null;
state = SeeSoState.none;
notifyListeners();
}
void startTracking() {
try {
_seeso.startTracking();
} on PlatformException catch (e) {
if (kDebugMode) {
print("failed seeso's startTracking call : ${e.message}");
}
failedReason = e.message;
notifyListeners();
}
}
void stopTracking() {
try {
_seeso.stopTracking();
} on PlatformException catch (e) {
if (kDebugMode) {
print("failed seeso's stopTracking call : ${e.message}");
}
failedReason = e.message;
notifyListeners();
}
}
void startCalibration(CalibrationMode calibrationMode) async {
if (state == SeeSoState.tracking) {
try {
_seeso.startCalibration(calibrationMode);
checkCalibrating();
} on PlatformException catch (e) {
if (kDebugMode) {
print("failed seeso's startCalibration call : ${e.message}");
}
failedReason = e.message;
notifyListeners();
}
} else {
failedReason = "SeeSo Not tracking State";
notifyListeners();
}
}
void checkCalibrating() async {
try {
isCalibrating = await _seeso.isCalibrating() ?? false;
notifyListeners();
} on PlatformException catch (e) {
if (kDebugMode) {
print("failed seeso's isCalibration call : ${e.message}");
}
failedReason = e.message;
notifyListeners();
}
}
void stopCalibration() {
try {
_seeso.stopCalibration();
isCalibrating = false;
notifyListeners();
} on PlatformException catch (e) {
if (kDebugMode) {
print("failed seeso's stopCalibration call : ${e.message}");
}
failedReason = e.message;
notifyListeners();
}
}
void startCollectSamples() {
try {
Timer(const Duration(milliseconds: 500), () {
_seeso.startCollectSamples();
});
} on PlatformException catch (e) {
if (kDebugMode) {
print("failed seeso's startCollectSamples call : ${e.message}");
}
failedReason = e.message;
notifyListeners();
}
}
void setListener() {
_seeso.getGazeEvent().listen((event) {
GazeInfo gazeInfo = GazeInfo(event);
this.gazeInfo = gazeInfo;
if (_useFilter) {
bool isFiltered = _filterManager
.filterValues(gazeInfo.timestamp, [gazeInfo.x, gazeInfo.y]);
if (isFiltered) {
this.gazeInfo?.setGaze(_filterManager.getFilteredValues()[0],
_filterManager.getFilteredValues()[1]);
}
}
notifyListeners();
});
_seeso.getStatusEvent().listen((event) {
StatusInfo statusInfo = StatusInfo(event);
if (statusInfo.type == StatusType.START) {
state = SeeSoState.tracking;
} else {
state = SeeSoState.initialized;
}
notifyListeners();
});
_seeso.getCalibrationEvent().listen((event) {
CalibrationInfo calibrationInfo = CalibrationInfo(event);
if (calibrationInfo.type == CalibrationType.CALIBRATION_NEXT_XY) {
if (calibrationInfo.nextX != null && calibrationInfo.nextY != null) {
nextPoint = Point(calibrationInfo.nextX!, calibrationInfo.nextY!);
progress = 0;
startCollectSamples();
} else {
isCalibrating = false;
}
} else if (calibrationInfo.type == CalibrationType.CALIBRATION_PROGRESS) {
progress = calibrationInfo.progress!;
} else {
calibrationData = calibrationInfo.calibrationData;
isCalibrating = false;
}
notifyListeners();
});
_seeso.getUserStatusEvent().listen((event) {
UserStatusInfo userStatusInfo = UserStatusInfo(event);
if (userStatusInfo.type == UserStatusEventType.ATTENTION) {
attentionInfo = userStatusInfo;
} else if (userStatusInfo.type == UserStatusEventType.BLINK) {
blinkInfo = userStatusInfo;
} else {
drowsinessInfo = userStatusInfo;
}
notifyListeners();
});
}
clearFailedReason() {
deinitGazeTracker();
failedReason = null;
notifyListeners();
}
void useGazeFilter(bool useFilter) {
_useFilter = useFilter;
}
}