-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGazeCalibrationWindow.py
385 lines (317 loc) · 10.9 KB
/
GazeCalibrationWindow.py
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from math import atan2, degrees, pi, sqrt, pow
import logging
from PySide import QtGui, QtCore
xResolution, yResolution = 3, 3
instructions = '''
Please sit comfortably.
A target will appear on the screen.
Follow the target with your eyes.
Press any key to begin gaze calibration
'''.replace('\n', '<br>')
class CalibrationWindow(QtGui.QWidget):
closed = QtCore.Signal()
def __init__(self, device=None):
super().__init__()
if device is None:
import GazeDevice
self.gazeTracker = GazeDevice.getGazeDevice()
else:
self.gazeTracker = device
self.setStyleSheet("background-color: #ddd;");
self.movementTime = 750
self.pointCaptureDelay = 500
self.pointCaptureDuration = 1000
self.started = False
self.eyes = EyeballWidget(self)
self.target = TargetWidget(parent=self)
self.target.hide()
self.eyeTimer = QtCore.QTimer()
self.eyeTimer.setSingleShot(False)
self.eyeTimer.timeout.connect(self.moveEyes)
self.gazeTimer = QtCore.QTimer()
self.gazeTimer.setSingleShot(False)
self.gazeTimer.timeout.connect(self.trackGazeWithTarget)
self.pointLabels = []
self.showCalibratedLabels()
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.showFullScreen()
self.centerChildAt(self.eyes)
self.animation = QtCore.QPropertyAnimation(self.target, 'pos');
self.animation.setEasingCurve(QtCore.QEasingCurve.InOutQuad)
self.pulseAnimation = QtCore.QPropertyAnimation(self.target, 'scale');
self.pulseAnimation.setStartValue(1.0)
self.pulseAnimation.setKeyValueAt(0.5, 1.0/3.0)
self.pulseAnimation.setEndValue(1.0)
self.pulseAnimation.setEasingCurve(QtCore.QEasingCurve.InOutQuad)
self.pulseAnimation.setLoopCount(-1)
self.gazeTracker.eyesAppeared.connect(self.setEyesGood)
self.gazeTracker.eyesDisappeared.connect(self.setEyesBad)
self.eyeTimer.start(1000/30)
self.gazeTimer.start(1000/30)
self.gazeTracker.startPolling()
self.points = None
def setEyesBad(self):
self.eyes.ok = False
def setEyesGood(self):
self.eyes.ok = True
def trackGazeWithTarget(self):
gaze = self.gazeTracker.getGaze()
self.centerChildAt(self.target, gaze)
self.target.show()
def moveEyes(self):
if self.eyes.ok or True:
eyePos = self.gazeTracker.getEyePositions()
x1, x2 = eyePos[0][0], eyePos[1][0]
y1, y2 = eyePos[0][1], eyePos[1][1]
if (x1 == 0 and y1 == 0) or (x2 == 0 and y2 == 0):
pass
else:
x = (x1 + x2)/2
y = (y1 + y2)/2
if x > 0 and y > 0:
dx = x2 - x1
dy = y2 - y1
distance = sqrt(pow(dx,2) + pow(dy,2))
self.eyes.scale = distance * 5
rads = atan2(-dy,dx)
rads %= 2*pi
self.eyes.angle = -degrees(rads)
desktopSize = QtGui.QDesktopWidget().screenGeometry()
self.centerChildAt(self.eyes, [x*desktopSize.width(), y*desktopSize.height()])
def keyPressEvent(self, event):
super().keyPressEvent(event)
if event.key() in [QtCore.Qt.Key_Escape, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return]:
self.close()
if self.gazeTracker.isCalibrating():
self.gazeTracker.cancelCalibration()
elif event.key() == QtCore.Qt.Key_Space:
if self.gazeTracker.isCalibrating():
self.gazeTracker.cancelCalibration()
self.started = True
self.startCalibration()
self.eyes.opacity = 0.15
def startCalibration(self, points=None):
logging.debug("calibration started")
for l in self.pointLabels:
l.hide()
self.pointLabels = []
self.gazeTimer.stop()
self.pulseAnimation.setDuration(self.pointCaptureDuration / 3)
self.points = points
try:
self.animation.finished.disconnect()
except:
pass
self.animation.setDuration(self.movementTime * 2)
self.animation.finished.connect(self._startCalibration)
self.goToPoint([self.width()/2, self.height()/2])
def _startCalibration(self):
self.animation.finished.disconnect()
self.animation.finished.connect(self.startPointCaptureSoon)
self.animation.setDuration(self.movementTime)
if self.points is None:
desktopSize = QtGui.QDesktopWidget().screenGeometry()
self.centerChildAt(self.target)
self.goToPoint(self.gazeTracker.startCalibration(xResolution, yResolution, desktopSize.width(), desktopSize.height()))
else:
logging.debug("redo-ing calibration")
self.goToPoint(self.gazeTracker.redoCalibration(self.points))
def centerChildAt(self, child, pos=None):
if pos is None:
pos = [self.width()/2, self.height()/2]
child.move(
pos[0] - child.width()/2,
pos[1] - child.height()/2
)
def goToPoint(self, point):
self.animation.setStartValue(self.target.pos())
self.animation.setEndValue(QtCore.QPoint(
point[0] - self.target.width()/2,
point[1] - self.target.height()/2
))
self.animation.start()
def startPointCaptureSoon(self):
QtCore.QTimer.singleShot(self.pointCaptureDelay, self.startPointCapture)
def startPointCapture(self):
if self.isVisible():
self.gazeTracker.beginPointCapture()
self.pulseAnimation.start()
QtCore.QTimer.singleShot(self.pointCaptureDuration, self.endPointCapture)
def endPointCapture(self):
self.pulseAnimation.stop()
if self.isVisible():
nextPoint = self.gazeTracker.endPointCapture()
if nextPoint != None:
self.goToPoint(nextPoint)
else:
calibration = self.gazeTracker.getCalibration()
badPoints = []
for point in calibration.points:
if point.state < 2:
logging.debug("Bad Coordinates : %s" % point.cp)
logging.debug("\tstate : %d" % point.state)
logging.debug("\taccuracy : %d" % point.ad)
logging.debug("\tmean error: %d" % point.mep)
logging.debug("\tstd dev : %d" % point.asd)
badPoints.append([point.cp.x, point.cp.y])
if len(badPoints) > 0:
logging.debug("%d bad points during gaze calibration" % len(badPoints))
self.pointCaptureDuration = self.pointCaptureDuration * 1.25
badPoints.reverse()
self.startCalibration(badPoints)
else:
self.showCalibratedLabels(calibration)
self.showScore()
def showScore(self):
calibration = self.gazeTracker.getCalibration()
if not calibration.result or calibration.points is None or len(calibration.points) == 0:
text = 'Calibration failed :('
else:
worstAccuracy = None
worstMeanErr = None
worstStdDev = None
for point in calibration.points:
if worstAccuracy is None or point.ad < worstAccuracy:
worstAccuracy = point.ad
if worstMeanErr is None or point.mep > worstMeanErr:
worstMeanErr = point.mep
if worstStdDev is None or point.asd > worstStdDev:
worstStdDev = point.asd
text = 'Average error: %0.2d\n'
text = text + '----------------------\n'
text = text + 'Worst accuracy: %0.2d\n'
text = text + 'Worst mean error: %0.2d\n'
text = text + 'Worst std dev: %0.2d'
text = text % (calibration.deg, worstAccuracy, worstMeanErr, worstStdDev)
QtGui.QMessageBox.information(self, 'Results', text)
def showCalibratedLabels(self, calibration=None):
self.gazeTimer.start(1000/30)
if calibration is None:
calibration = self.gazeTracker.getCalibration()
if not (calibration is None or calibration.points is None):
for point in calibration.points:
text = '''
acc:%d
err:%d
dev:%d
''' % (point.ad, point.mep, point.asd)
label = QtGui.QLabel('<font size="12">%s</font>' % text.replace('\n', '<br>'), self)
label.setStyleSheet("background-color: transparent;");
font = self.font()
font.setStyleHint(QtGui.QFont.Monospace)
font.setFamily("Courier New")
label.setFont(font)
label.show()
self.centerChildAt(label, [point.cp.x, point.cp.y])
self.pointLabels.append(label)
logging.debug("Calibration point : %s" % point.cp)
logging.debug("\taccuracy : %d" % point.ad)
logging.debug("\tmean error: %d" % point.mep)
logging.debug("\tstd dev : %d" % point.asd)
def targetScaled(self):
self.centerChildAt(self.target)
def closeEvent(self, e):
super().closeEvent(e)
self.pulseAnimation.stop()
self.animation.stop()
self.gazeTimer.stop()
self.eyeTimer.stop()
try:
self.gazeTracker.endPointCapture()
except:
pass
self.closed.emit()
class TargetWidget(QtGui.QWidget):
def __init__(self, parent):
super().__init__(parent)
self.resize(64, 64)
self._scale = 1
def getScale(self):
return self._scale
def setScale(self, scale):
self._scale = scale
self.repaint()
def paintEvent(self, event):
super().paintEvent(event)
painter = QtGui.QPainter(self)
painter.setPen(QtGui.QColor(0, 0, 0))
painter.setBrush(QtGui.QColor(128, 128, 128))
painter.setRenderHint(painter.RenderHint.Antialiasing)
size = self.width() * self.scale
painter.drawEllipse(
(self.width() - size)/2+1,
(self.height() - size)/2+1,
size-2,
size-2
)
colors = [QtGui.QColor(0, 0, 0), QtGui.QColor(255, 255, 255)]
for color in colors:
size = size / 3.5
painter.setBrush(color)
painter.drawEllipse(
(self.width() - size)/2+1,
(self.height() - size)/2+1,
size-2,
size-2
)
scale = QtCore.Property(float, getScale, setScale)
class EyeballWidget(QtGui.QWidget):
def __init__(self, parent):
super().__init__(parent)
self.resize(720, 720)
self._opacity = 1.0
self._scale = 1.0
self._angle = 0
self._ok = False
self.eyePixmaps = {
True: QtGui.QPixmap.fromImage(QtGui.QImage('assets/eyes-good.png')),
False: QtGui.QPixmap.fromImage(QtGui.QImage('assets/eyes-bad.png'))
}
def getOpacity(self):
return self._opacity
def setOpacity(self, opacity):
self._opacity = opacity
def getScale(self):
return self._scale
def setScale(self, scale):
self._scale = scale
self.repaint()
def getAngle(self):
return self._angle
def setAngle(self, angle):
self._angle = angle
self.repaint()
def getOk(self):
return self._ok
def setOk(self, ok):
self._ok = ok
self.repaint()
def paintEvent(self, event):
super().paintEvent(event)
painter = QtGui.QPainter(self)
painter.setRenderHint(painter.RenderHint.SmoothPixmapTransform)
painter.setOpacity(self._opacity)
painter.translate(self.width()/2, self.height()/2)
painter.rotate(self._angle)
painter.scale(self._scale, self._scale)
painter.drawPixmap(-90, -90, self.eyePixmaps[self._ok])
painter.translate(-self.width()/2, -self.height()/2)
opacity = QtCore.Property(float, getOpacity, setOpacity)
scale = QtCore.Property(float, getScale, setScale)
angle = QtCore.Property(float, getAngle, setAngle)
ok = QtCore.Property(bool, getOk, setOk)
if __name__ == '__main__':
import sys, os, inspect, time, logging
logging.basicConfig(
format='%(levelname)-8s %(asctime)s %(message)s',
filename='logs/%d.log' % int(time.time()),
level=logging.DEBUG,
)
src_dir = os.path.dirname(inspect.getfile(inspect.currentframe()))
arch_dir = 'lib/x64' if sys.maxsize > 2**32 else 'lib/x86'
sys.path.insert(0, os.path.abspath(os.path.join(src_dir, 'lib')))
sys.path.insert(0, os.path.abspath(os.path.join(src_dir, arch_dir)))
app = QtGui.QApplication(sys.argv)
window = CalibrationWindow()
window.show()
app.exec_()