-
Notifications
You must be signed in to change notification settings - Fork 0
/
GazeTrackerManager.java
297 lines (255 loc) · 9.2 KB
/
GazeTrackerManager.java
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package visual.camp.sample.app;
import android.content.Context;
import android.view.TextureView;
import camp.visual.gazetracker.GazeTracker;
import camp.visual.gazetracker.callback.CalibrationCallback;
import camp.visual.gazetracker.callback.GazeCallback;
import camp.visual.gazetracker.callback.UserStatusCallback;
import camp.visual.gazetracker.callback.GazeTrackerCallback;
import camp.visual.gazetracker.callback.ImageCallback;
import camp.visual.gazetracker.callback.InitializationCallback;
import camp.visual.gazetracker.callback.StatusCallback;
import camp.visual.gazetracker.constant.AccuracyCriteria;
import camp.visual.gazetracker.constant.CalibrationModeType;
import camp.visual.gazetracker.constant.InitializationErrorType;
import camp.visual.gazetracker.constant.StatusErrorType;
import camp.visual.gazetracker.constant.UserStatusOption;
import camp.visual.gazetracker.gaze.GazeInfo;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import visual.camp.sample.app.calibration.CalibrationDataStorage;
public class GazeTrackerManager {
private List<InitializationCallback> initializationCallbacks = new ArrayList<>();
private List<GazeCallback> gazeCallbacks = new ArrayList<>();
private List<CalibrationCallback> calibrationCallbacks = new ArrayList<>();
private List<StatusCallback> statusCallbacks = new ArrayList<>();
private List<ImageCallback> imageCallbacks = new ArrayList<>();
private List<UserStatusCallback> userStatusCallbacks = new ArrayList<>();
static private GazeTrackerManager mInstance = null;
private WeakReference<TextureView> cameraPreview = null;
private final WeakReference<Context> mContext;
GazeTracker gazeTracker = null;
// TODO: change licence key
String SEESO_LICENSE_KEY = "dev_icy315p5woufcvjibubfx4513ar1b4yn6phn8khz";
static public GazeTrackerManager makeNewInstance(Context context) {
if (mInstance != null) {
mInstance.deinitGazeTracker();
}
mInstance = new GazeTrackerManager(context);
return mInstance;
}
static public GazeTrackerManager getInstance() {
return mInstance;
}
private GazeTrackerManager(Context context) {
this.mContext = new WeakReference<>(context);
}
public boolean hasGazeTracker() {
return gazeTracker != null;
}
public void initGazeTracker(InitializationCallback callback, UserStatusOption option) {
initializationCallbacks.add(callback);
GazeTracker.initGazeTracker(mContext.get(), SEESO_LICENSE_KEY, initializationCallback, option);
}
public void deinitGazeTracker() {
if (hasGazeTracker()) {
GazeTracker.deinitGazeTracker(gazeTracker);
gazeTracker = null;
}
}
public void setGazeTrackerCallbacks(GazeTrackerCallback... callbacks) {
for(GazeTrackerCallback callback : callbacks) {
if (callback instanceof GazeCallback) {
gazeCallbacks.add((GazeCallback)callback);
} else if (callback instanceof CalibrationCallback) {
calibrationCallbacks.add((CalibrationCallback) callback);
} else if (callback instanceof ImageCallback) {
imageCallbacks.add((ImageCallback)callback);
} else if (callback instanceof StatusCallback) {
statusCallbacks.add((StatusCallback) callback);
} else if (callback instanceof UserStatusCallback) {
userStatusCallbacks.add((UserStatusCallback) callback);
}
}
}
public void removeCallbacks(GazeTrackerCallback... callbacks) {
for (GazeTrackerCallback callback : callbacks) {
gazeCallbacks.remove(callback);
calibrationCallbacks.remove(callback);
imageCallbacks.remove(callback);
statusCallbacks.remove(callback);
}
}
public boolean startGazeTracking() {
if (hasGazeTracker()) {
gazeTracker.startTracking();
return true;
}
return false;
}
public boolean stopGazeTracking() {
if (isTracking()) {
gazeTracker.stopTracking();
return true;
}
return false;
}
public boolean startCalibration(CalibrationModeType modeType, AccuracyCriteria criteria) {
if (hasGazeTracker()) {
return gazeTracker.startCalibration(modeType, criteria);
}
return false;
}
public boolean stopCalibration() {
if (isCalibrating()) {
gazeTracker.stopCalibration();
return true;
}
return false;
}
public boolean startCollectingCalibrationSamples() {
if (isCalibrating()) {
return gazeTracker.startCollectSamples();
}
return false;
}
public boolean isTracking() {
if (hasGazeTracker()) {
return gazeTracker.isTracking();
}
return false;
}
public boolean isCalibrating() {
if (hasGazeTracker()) {
return gazeTracker.isCalibrating();
}
return false;
}
public enum LoadCalibrationResult {
SUCCESS,
FAIL_DOING_CALIBRATION,
FAIL_NO_CALIBRATION_DATA,
FAIL_HAS_NO_TRACKER
}
public LoadCalibrationResult loadCalibrationData() {
if (!hasGazeTracker()) {
return LoadCalibrationResult.FAIL_HAS_NO_TRACKER;
}
double[] calibrationData = CalibrationDataStorage.loadCalibrationData(mContext.get());
if (calibrationData != null) {
if (!gazeTracker.setCalibrationData(calibrationData)) {
return LoadCalibrationResult.FAIL_DOING_CALIBRATION;
} else {
return LoadCalibrationResult.SUCCESS;
}
} else {
return LoadCalibrationResult.FAIL_NO_CALIBRATION_DATA;
}
}
public void setCameraPreview(TextureView preview) {
this.cameraPreview = new WeakReference<>(preview);
if (hasGazeTracker()) {
gazeTracker.setCameraPreview(preview);
}
}
public void removeCameraPreview(TextureView preview) {
if (this.cameraPreview.get() == preview) {
this.cameraPreview = null;
if (hasGazeTracker()) {
gazeTracker.removeCameraPreview();
}
}
}
// GazeTracker Callbacks
private final InitializationCallback initializationCallback = new InitializationCallback() {
@Override
public void onInitialized(GazeTracker gazeTracker, InitializationErrorType initializationErrorType) {
setGazeTracker(gazeTracker);
for (InitializationCallback initializationCallback : initializationCallbacks) {
initializationCallback.onInitialized(gazeTracker, initializationErrorType);
}
initializationCallbacks.clear();
if (gazeTracker != null) {
gazeTracker.setCallbacks(gazeCallback, calibrationCallback, imageCallback, statusCallback, userStatusCallback);
if (cameraPreview != null) {
gazeTracker.setCameraPreview(cameraPreview.get());
}
}
}
};
private final GazeCallback gazeCallback = new GazeCallback() {
@Override
public void onGaze(GazeInfo gazeInfo) {
for (GazeCallback gazeCallback : gazeCallbacks) {
gazeCallback.onGaze(gazeInfo);
}
}
};
private UserStatusCallback userStatusCallback = new UserStatusCallback() {
@Override
public void onAttention(long timestampBegin, long timestampEnd, float attentionScore) {
for (UserStatusCallback userStatusCallback : userStatusCallbacks) {
userStatusCallback.onAttention(timestampBegin, timestampEnd, attentionScore);
}
}
@Override
public void onBlink(long timestamp, boolean isBlinkLeft, boolean isBlinkRight, boolean isBlink, float eyeOpenness) {
for (UserStatusCallback userStatusCallback : userStatusCallbacks) {
userStatusCallback.onBlink(timestamp, isBlinkLeft, isBlinkRight, isBlink, eyeOpenness);
}
}
@Override
public void onDrowsiness(long timestamp, boolean isDrowsiness) {
for (UserStatusCallback userStatusCallback : userStatusCallbacks) {
userStatusCallback.onDrowsiness(timestamp, isDrowsiness);
}
}
};
private CalibrationCallback calibrationCallback = new CalibrationCallback() {
@Override
public void onCalibrationProgress(float v) {
for (CalibrationCallback calibrationCallback : calibrationCallbacks) {
calibrationCallback.onCalibrationProgress(v);
}
}
@Override
public void onCalibrationNextPoint(float v, float v1) {
for (CalibrationCallback calibrationCallback : calibrationCallbacks) {
calibrationCallback.onCalibrationNextPoint(v, v1);
}
}
@Override
public void onCalibrationFinished(double[] doubles) {
CalibrationDataStorage.saveCalibrationData(mContext.get(), doubles);
for (CalibrationCallback calibrationCallback : calibrationCallbacks) {
calibrationCallback.onCalibrationFinished(doubles);
}
}
};
private final ImageCallback imageCallback = new ImageCallback() {
@Override
public void onImage(long l, byte[] bytes) {
for (ImageCallback imageCallback : imageCallbacks) {
imageCallback.onImage(l, bytes);
}
}
};
private final StatusCallback statusCallback = new StatusCallback() {
@Override
public void onStarted() {
for (StatusCallback statusCallback : statusCallbacks) {
statusCallback.onStarted();
}
}
@Override
public void onStopped(StatusErrorType statusErrorType) {
for (StatusCallback statusCallback : statusCallbacks) {
statusCallback.onStopped(statusErrorType);
}
}
};
private void setGazeTracker(GazeTracker gazeTracker) {
this.gazeTracker = gazeTracker;
}
}