-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpytribe.py
More file actions
1527 lines (1189 loc) · 45 KB
/
Copy pathpytribe.py
File metadata and controls
1527 lines (1189 loc) · 45 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# PyTribe: classes to communicate with EyeTribe eye trackers
#
# author: Edwin Dalmaijer
# email: edwin.dalmaijer@psy.ox.ac.uk
#
# version 4 (21-Jun-2016)
import os
import copy
import json
import time
import socket
import codecs
from threading import Lock, Thread
from multiprocessing import Event, Process, Queue
from py3compat import *
# # # # #
# EYETRIBE CLASS
# The original EyeTribe class from earlier versions of PyTribe.
class EyeTribe:
"""class for eye tracking and data collection using an EyeTribe tracker
"""
def __init__(self, logfilename='default', host='localhost', port=6555):
"""Initializes an EyeTribe instance
keyword arguments
logfilename -- string indicating the log file name, including
a full path to it's location and an extension
(default = 'default.txt')
"""
# initialize data collectors
self._logfile = codecs.open('%s.tsv' % (logfilename), 'w', u'utf-8')
self._separator = u'\t'
self._log_header()
self._queue = Queue()
# initialize connection
self._connection = connection(host=host, port=port)
self._tracker = tracker(self._connection)
self._heartbeat = heartbeat(self._connection)
# create a new Lock
self._lock = Lock()
# initialize heartbeat thread
self._beating = True
self._heartbeatinterval = self._tracker.get_heartbeatinterval() / 1000.0
self._hbthread = Thread(target=self._heartbeater, args=[self._heartbeatinterval])
self._hbthread.daemon = True
self._hbthread.name = 'heartbeater'
# initialize sample streamer
self._streaming = True
self._samplefreq = self._tracker.get_framerate()
self._intsampletime = 1.0 / self._samplefreq
self._clockdiff = None
self._newestframe = self._tracker.get_frame()
self._ssthread = Thread(target=self._stream_samples, args=[self._queue])
self._ssthread.daemon = True
self._ssthread.name = 'samplestreamer'
# initialize data processer
self._processing = True
self._logdata = False
self._currentsample = copy.deepcopy(self._newestframe)
self._dpthread = Thread(target=self._process_samples, args=[self._queue])
self._dpthread.daemon = True
self._dpthread.name = 'dataprocessor'
# start all threads
self._hbthread.start()
self._ssthread.start()
self._dpthread.start()
# initialize calibration
self.calibration = calibration(self._connection)
def start_recording(self):
"""Starts data recording
"""
# set self._logdata to True, so the data processing thread starts
# writing samples to the log file
if not self._logdata:
self._logdata = True
self.log_message("start_recording")
def stop_recording(self):
"""Stops data recording
"""
# consolidate the data file on the hard drive
# internal buffer to RAM
self._logfile.flush()
# RAM file cache to disk
os.fsync(self._logfile.fileno())
# set self._logdata to False, so the data processing thread does not
# write samples to the log file
if self._logdata:
self.log_message("stop_recording")
self._logdata = False
def log_message(self, message):
"""Logs a message to the logfile, time locked to the most recent
sample
"""
# Get the current time.
t = time.time()
# Make a string in the specific format that the EyeTribe uses:
# yyyy-mm-dd HH:MM:SS.000
ts = '%s.%d' % (time.strftime('%Y-%m-%d %H:%M:%S'), round(t % 1, 3)*1000)
# Correct the time to EyeTribe time
if self._clockdiff != None:
t = int(t + self._clockdiff)
else:
t = ''
# assemble line
line = self._separator.join(map(str,[u'MSG', ts, t, safe_decode(message)]))
# write message
self._logfile.write(line + u'\n') # to internal buffer
def sample(self):
"""Returns the most recent point of regard (=gaze location on screen)
coordinates (smoothed signal)
arguments
None
returns
gaze -- a (x,y) tuple indicating the point of regard
"""
if self._newestframe == None:
return None, None
else:
return (self._newestframe['avgx'],self._newestframe['avgy'])
def pupil_size(self):
"""Returns the most recent pupil size sample (an average of the size
of both pupils)
arguments
None
returns
pupsize -- a float indicating the pupil size (in arbitrary units)
"""
if self._currentsample == None:
return None
else:
return self._newestframe['psize']
def close(self):
"""Stops all data streaming, and closes both the connection to the
tracker and the logfile
"""
# if we are currently recording, stop doing so
if self._logdata:
self.stop_recording()
# signal all threads to halt
self._beating = False
self._streaming = False
self._processing = False
# close the log file
self._logfile.close()
# close the connection
self._connection.close()
def _wait_while_calibrating(self):
"""Waits until the tracker is not in the calibration state
"""
while self._tracker.get_iscalibrating():
pass
return True
def _heartbeater(self, heartbeatinterval):
"""Continuously sends heartbeats to the tracker, to let it know the
connection is still alive (it seems to think we could die any
moment now, and is very keen on reassurance of our good health;
almost like my grandparents...)
arguments
heartbeatinterval -- float indicating the heartbeatinterval in
seconds; note that this is different from
the value that the EyeTribe tracker reports:
that value is in milliseconds and should be
recalculated to seconds here!
"""
# keep beating until it is signalled that we should stop
while self._beating:
# do not bother the tracker when it is calibrating
#self._wait_while_calibrating()
# wait for the Threading Lock to be released, then lock it
self._lock.acquire(True)
# send heartbeat
self._heartbeat.beat()
# release the Threading Lock
self._lock.release()
# wait for a bit
time.sleep(heartbeatinterval)
def _stream_samples(self, queue):
"""Continuously polls the device, and puts all new samples in a
Queue instance
arguments
queue -- a multithreading.Queue instance, to put samples
into
"""
# keep streaming until it is signalled that we should stop
while self._streaming:
# do not bother the tracker when it is calibrating
#self._wait_while_calibrating()
# wait for the Threading Lock to be released, then lock it
self._lock.acquire(True)
# get a new sample
sample = self._tracker.get_frame()
t1 = time.time()
# put the sample in the Queue
queue.put(sample)
# release the Threading Lock
self._lock.release()
# Update the newest frame
self._newestframe = copy.deepcopy(sample)
# Calculate the clock difference
self._clockdiff = sample['time'] - t1
# pause for half the intersample time, to avoid an overflow
# (but to make sure to not miss any samples)
time.sleep(self._intsampletime/2)
def _process_samples(self, queue):
"""Continuously processes samples, updating the most recent sample
and writing data to a the log file when self._logdata is set to True
arguments
queue -- a multithreading.Queue instance, to read samples
from
"""
# keep processing until it is signalled that we should stop
while self._processing:
# wait for the Threading Lock to be released, then lock it
self._lock.acquire(True)
# read new item from the queue
if not queue.empty():
sample = queue.get()
else:
sample = None
# release the Threading Lock
self._lock.release()
# update newest sample
if sample != None:
# check if the new sample is the same as the current sample
if not self._currentsample['timestamp'] == sample['timestamp']:
# update current sample
self._currentsample = copy.deepcopy(sample)
# write to file if data logging is on
if self._logdata:
self._log_sample(sample)
def _log_sample(self, sample):
"""Writes a sample to the log file
arguments
sample -- a sample dict, as is returned by
tracker.get_frame
"""
# assemble new line
line = self._separator.join(map(str,[ sample['timestamp'],
sample['time'],
sample['fix'],
sample['state'],
sample['rawx'],
sample['rawy'],
sample['avgx'],
sample['avgy'],
sample['psize'],
sample['Lrawx'],
sample['Lrawy'],
sample['Lavgx'],
sample['Lavgy'],
sample['Lpsize'],
sample['Lpupilx'],
sample['Lpupily'],
sample['Rrawx'],
sample['Rrawy'],
sample['Ravgx'],
sample['Ravgy'],
sample['Rpsize'],
sample['Rpupilx'],
sample['Rpupily']
]))
# write line to log file
self._logfile.write(line + '\n') # to internal buffer
def _log_header(self):
"""Logs a header to the data file
"""
# write a header to the data file
header = self._separator.join(['timestamp','time','fix','state',
'rawx','rawy','avgx','avgy','psize',
'Lrawx','Lrawy','Lavgx','Lavgy','Lpsize','Lpupilx','Lpupily',
'Rrawx','Rrawy','Ravgx','Ravgy','Rpsize','Rpupilx','Rpupily'
])
self._logfile.write(header + '\n') # to internal buffer
self._logfile.flush() # internal buffer to RAM
os.fsync(self._logfile.fileno()) # RAM file cache to disk
self._firstlog = False
# # # # # #
# PARALLEL ClASS
# Ugly, but sod it: A global variable for the most recent sample.
global _current_sample
# Class to communicate with an EyeTribe tracker. The actual communications
# and logging actually run in a separate Process. This class just sends
# commands to that Process.
class ParallelEyeTribe:
def __init__(self, logfilename='default'):
# Set some standard stuff (hard coded now, but can potentially
# be passed to the __init__ method in the future.)
host = 'localhost'
port = 6555
# We need an Event that signals whether the connection to the
# EyeTribe is supposed to be open.
self._connection_alive = Event()
self._connection_alive.set()
# We also need a Queue to send commands through.
self._command_queue = Queue()
# And we need a Queue to receive commands through.
self._to_main_queue = Queue()
# Start a parallel process that will take care of all EyeTribe
# things. It will provide regular heartbeats to keep the connection
# alive, it will record gaze data to a file, and it will keep the
# most recent sample updated. This is all done in a separate
# Process (rather than in Threads) to so that it can be offloaded
# to a different CPU core. This prevents the ongoing experiment
# (or whatever you're doing in the main Thread) from interfering
# with the processing (and recording) of gaze data.
self.eyetribe_process = Process(target=_run_eyetribe_process, \
args=[logfilename, host, port, self._connection_alive, \
self._command_queue])
self.eyetribe_process.name = u'pygaze_eyetribe'
self.eyetribe_process.daemon = True
self.eyetribe_process.start()
def start_recording(self):
"""Starts data recording
"""
# Send a command to the EyeTribe Process
self._command_queue.put(('start_recording', ()))
def stop_recording(self):
"""Stops data recording
"""
# Send a command to the EyeTribe Process
self._command_queue.put(('stop_recording', ()))
def log_message(self, message):
"""Logs a message to the logfile, time locked to the most recent
sample
"""
# Send a command to the EyeTribe Process
self._command_queue.put(('log_message', (message)))
def sample(self):
"""Returns the most recent point of regard (=gaze location on screen)
coordinates (smoothed signal)
arguments
None
returns
gaze -- a (x,y) tuple indicating the point of regard
"""
global _current_sample
if _current_sample == None:
return None, None
else:
return (_current_sample['avgx'], _current_sample['avgy'])
def pupil_size(self):
"""Returns the most recent pupil size sample (an average of the size
of both pupils)
arguments
None
returns
pupsize -- a float indicating the pupil size (in arbitrary units)
"""
global _current_sample
if _current_sample == None:
return None
else:
return _current_sample['psize']
def close(self):
"""Stops all data streaming, and closes both the connection to the
tracker and the logfile
"""
# Send a command to the EyeTribe Process
self._command_queue.put(('close', ()))
# Function that can run in a parallel process, to keep a connection with the
# EyeTribe open, and to log data when appropriate.
def _run_eyetribe_process(logfilename, host, port, connection_alive, command_queue):
"""FOR INTERNAL USE ONLY
"""
# Ugly, but sod it: A global variable for the most recent sample.
global _current_sample
# Initialise a new _EyeTribe instance to open the connection to the
# EyeTribe.
tracker = EyeTribe(logfilename=logfilename, host=host, port=port)
# Run until the connection is closed.
while connection_alive.is_set():
# Check the incoming Queue.
if not command_queue.empty():
# Get the oldest command in the Queue. This is a tuple that
# contains a string (the command), and a tuple of values
# (what they are depends on the specific command).
cmd, value = command_queue.get()
# Start recording.
if cmd == 'start_recording':
tracker.start_recording()
# Stop recording.
elif cmd == 'stop_recording':
tracker.stop_recording()
# Log a message.
elif cmd == 'log_message':
tracker.log_message(value[0])
# Close the connection to the EyeTribe.
elif cmd == 'close':
tracker.close()
# Unset the Event that signals that the connection is
# alive.
connection_alive.clear()
# Update the current sample.
_current_sample = copy.deepcopy(tracker._currentsample)
# # # # #
# SUPPORTING CLASSES
class connection:
"""class for connections with the EyeTribe tracker"""
def __init__(self, host='localhost', port=6555):
"""Initializes the connection with the EyeTribe tracker
keyword arguments
host -- a string indicating the host IP, NOTE: currently only
'localhost' is supported (default = 'localhost')
port -- an integer indicating the port number, NOTE: currently
only 6555 is supported (default = 6555)
"""
# properties
self.host = host
self.port = port
self.resplist = []
self.DEBUG = False
# initialize a connection
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.host,self.port))
# Create lock
self._request_lock = Lock()
def request(self, category, request, values):
"""Send a message over the connection
arguments
category -- string indicating the query category
request -- string indicating the actual request of the message
values -- dict or list containing parameters of the request
"""
# create a JSON formatted string
msg = self.create_json(category, request, values)
# send the message over the connection
self._request_lock.acquire()
self.sock.send(msg)
# print request in DEBUG mode
if self.DEBUG:
print("REQUEST: '%s'" % msg)
# give the tracker a wee bit of time to reply
time.sleep(0.005)
# get new responses
success = self.get_response()
self._request_lock.release()
# return the appropriate response
if success:
for i in range(len(self.resplist)):
# check if the category matches
if self.resplist[i]['category'] == category:
# if this is a heartbeat, return
if self.resplist[i]['category'] == 'heartbeat':
return self.resplist.pop(i)
# if this is another category, check if the request
# matches
elif 'request' in self.resplist[i] and \
self.resplist[i]['request'] == request:
return self.resplist.pop(i)
# on a connection error, get_response returns False and a connection
# error should be returned
else:
return self.parse_json('{"statuscode":901,"values":{"statusmessage":"connection error"}}')
def get_response(self):
"""Asks for a response, and adds these to the list of all received
responses (basically a very simple queue)
"""
# try to get a new response
try:
response = self.sock.recv(32768)
# print reply in DEBUG mode
if self.DEBUG:
print("REPLY: '%s'" % response)
# if it fails, revive the connection and return a connection error
except socket.error:
print("reviving connection")
self.revive()
response = '{"statuscode":901,"values":{"statusmessage":"connection error"}}'
return False
# split the responses (in case multiple came in)
response = response.split('\n')
# add parsed responses to the internal list
for r in response:
if r:
self.resplist.append(self.parse_json(r))
return True
def create_json(self, category, request, values):
"""Creates a new json message, in the format that is required by the
EyeTribe tracker; these messages consist of a categort, a request and
a (list of) value(s), which can be thought of as class.method.value
(for more info, see: http://dev.theeyetribe.com/api/)
arguments
category -- query category (string), e.g. 'tracker',
'calibration', or 'heartbeat'
request -- the request message (string), e.g. 'get' for the
'tracker' category
values -- a dict of parameters and their values, e.g.
{"push":True, "version":1}
OR:
a list of parameters, e.g. ['push','iscalibrated']
OR:
None to pass no values at all
keyword arguments
None
returns
jsonmsg -- a string in json format, that can be directly sent to
the EyeTribe tracker
"""
# error if the values are anything other than a dict, tuple or list
if values is not None and type(values) not in [dict, list, tuple]:
raise Exception("values should be dict, tuple or list, not '%s' (values = %s)" % (type(values),values))
# create the json message
if request == None:
jsondict = {"category":category}
elif values == None:
jsondict = {"category":category, "request":request}
else:
jsondict = {"category":category, "request":request, "values":values}
return json.dumps(jsondict)
def parse_json(self, jsonmsg):
"""Parses a json message as those that are usually returned by the
EyeTribe tracker
(for more info, see: http://dev.theeyetribe.com/api/)
arguments
jsonmsg -- a string in json format
keyword arguments
None
returns
msg -- a dict containing the information in the json message;
this dict has the following content:
{ "category": "tracker",
"request": "get",
"statuscode": 200,
"values": { "push":True,
"iscalibrated":True
}
}
"""
# parse json message
parsed = json.loads(jsonmsg)
return parsed
def revive(self):
"""Re-establishes a connection
"""
# close old connection
self.close()
# initialize a connection
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.host,self.port))
def close(self):
"""Closes the connection to the EyeTribe tracker
"""
# close the socket connection
self.sock.close()
class tracker:
"""class for SDK Tracker state and information related requests"""
def __init__(self, connection):
"""Initializes a tracker instance
arguments
connection -- a pytribe.connection instance for the currently
attached EyeTribe tracker
"""
self.connection = connection
self.push = True
def set_connection(self, connection):
"""Set a new connection
arguments
connection -- a pytribe.connection instance for the currently
attached EyeTribe tracker
"""
self.connection = connection
def get_push(self):
"""Returns a Booleam reflecting the state: True for push mode,
False for pull mode (Boolean)
"""
# send the request
response = self.connection.request('tracker', 'get', ['push'])
# return value or error
if response['statuscode'] == 200:
return response['values']['push']
else:
raise Exception("Error in tracker.get_push: %s (code %d)" % (response['values']['statusmessage'],response['statuscode']))
def get_heartbeatinterval(self):
"""Returns the expected heartbeat interval in milliseconds
(integer)
"""
# send the request
response = self.connection.request('tracker', 'get', ['heartbeatinterval'])
# check if the tracker is in push mode
if response['statuscode'] == 200:
return response['values']['heartbeatinterval']
else:
raise Exception("Error in tracker.get_heartbeatinterval: %s (code %d)" % (response['values']['statusmessage'],response['statuscode']))
def get_version(self):
"""Returns the version number (integer)
"""
# send the request
response = self.connection.request('tracker', 'get', ['version'])
# return value or error
if response['statuscode'] == 200:
return response['values']['version']
else:
raise Exception("Error in tracker.get_version: %s (code %d)" % (response['values']['statusmessage'],response['statuscode']))
def get_trackerstate(self):
"""Returns the state of the physcial tracker (integer):
0: TRACKER_CONNECTED
tracker is detected and working
1: TRACKER_NOT_CONNECTED
tracker device is not connected
2: TRACKER_CONNECTED_BADFW
tracker device is connected, but not working due to
bad firmware
3: TRACKER_CONNECTED_NOUSB3
tracker device is connected, but not working due to
unsupported USB host
4: TRACKER_CONNECTED_NOSTREAM
tracker device is connected, but no stream could be
received
"""
# send the request
response = self.connection.request('tracker', 'get', ['trackerstate'])
# return value of error
if response['statuscode'] == 200:
return response['values']['trackerstate']
else:
raise Exception("Error in tracker.get_trackerstate: %s (code %d)" % (response['values']['statusmessage'],response['statuscode']))
def get_framerate(self):
"""Returns the frame rate that the tracker is running at (integer)
"""
# send the request
response = self.connection.request('tracker', 'get', ['framerate'])
# return value or error
if response['statuscode'] == 200:
return response['values']['framerate']
else:
raise Exception("Error in tracker.get_framerate: %s (code %d)" % (response['values']['statusmessage'],response['statuscode']))
def get_iscalibrated(self):
"""Indicates whether there is a calibration (Boolean)
"""
# send the request
response = self.connection.request('tracker', 'get', ['iscalibrated'])
# return value or error
if response['statuscode'] == 200:
return response['values']['iscalibrated']
else:
raise Exception("Error in tracker.get_iscalibrated: %s (code %d)" % (response['values']['statusmessage'],response['statuscode']))
def get_iscalibrating(self):
"""Indicates whether the tracker is in calibration mode (Boolean)
"""
# send the request
response = self.connection.request('tracker', 'get', ['iscalibrating'])
# return value or error
if response['statuscode'] == 200:
return response['values']['iscalibrating']
else:
raise Exception("Error in tracker.get_iscalibrating: %s (code %d)" % (response['values']['statusmessage'],response['statuscode']))
def get_calibresult(self):
"""Gets the latest valid calibration result
returns
WITHOUT CALIBRATION:
None
WITH CALIBRATION:
calibresults -- a dict containing the calibration results:
{ 'result': Boolean indicating whether the
calibration was succesful
'deg': float indicating the average error
in degrees of visual angle
'Ldeg': float indicating the left eye
error in degrees of visual angle
'Rdeg': float indicating the right eye
error in degrees of visual angle
'calibpoints': list, containing a dict for
each calibration point:
{'state': integer indicating
the state of the
calibration point
(0 means no useful
data has been
obtained and the
point should be
resampled; 1 means
the data is of
questionable
quality, consider
resampling; 2 means
the data is ok)
'cpx': x coordinate of the
calibration point
'cpy': y coordinate of the
calibration point
'mecpx': mean estimated x
coordinate of the
calibration point
'mecpy': mean estimated y
coordinate of the
calibration point
'acd': float indicating
the accuracy in
degrees of visual
angle
'Lacd': float indicating
the accuracy in
degrees of visual
angle (left eye)
'Racd': float indicating
the accuracy in
degrees of visual
angle (right eye)
'mepix': mean error in
pixels
'Lmepix': mean error in
pixels (left eye)
'Rmepix': mean error in
pixels (right eye)
'asdp': standard deviation
in pixels
'Lasdp': standard deviation
in pixels (left eye)
'Rasdp': standard deviation
in pixels (right eye)
}
}
"""
# send the request
response = self.connection.request('tracker', 'get', ['calibresult'])
# return value or error
if response['statuscode'] != 200:
raise Exception("Error in tracker.get_calibresult: %s (code %d)" % (response['values']['statusmessage'],response['statuscode']))
# return True if this was not the final calibration point
if not 'calibpoints' in response['values']:
return None
# if this was the final calibration point, return the results
else:
# return calibration dict
returndict = { 'result':response['values']['calibresult']['result'],
'deg':response['values']['calibresult']['deg'],
'Rdeg':response['values']['calibresult']['degl'],
'Ldeg':response['values']['calibresult']['degr'],
'calibpoints':[]
}
for pointdict in response['values']['calibresult']['calibpoints']:
returndict['calibpoints'].append({ 'state':pointdict['state'],
'cpx':pointdict['cp']['x'],
'cpy':pointdict['cp']['y'],
'mecpx':pointdict['mecp']['x'],
'mecpy':pointdict['mecp']['y'],
'acd':pointdict['acd']['ad'],
'Lacd':pointdict['acd']['adl'],
'Racd':pointdict['acd']['adr'],
'mepix':pointdict['mepix']['mep'],
'Lmepix':pointdict['mepix']['mepl'],
'Rmepix':pointdict['mepix']['mepr'],
'asdp':pointdict['asdp']['asd'],
'Lasdp':pointdict['asdp']['asdl'],
'Rasdp':pointdict['asdp']['asdr']
})
return returndict
def get_frame(self):
"""Returns the latest frame data (dict)
{ 'timestamp': string time representation,
'time': integer timestamp in milliseconds,
'fix': Boolean indicating whether there is a fixation,
'state': integer 32bit masked tracker state,
'rawx': integer raw x gaze coordinate in pixels,
'rawy': integer raw y gaze coordinate in pixels,
'avgx': integer smoothed x gaze coordinate in pixels,
'avgx': integer smoothed y gaze coordinate in pixels,
'psize': float average pupil size,
'Lrawx': integer raw x left eye gaze coordinate in pixels,
'Lrawy': integer raw y left eye gaze coordinate in pixels,
'Lavgx': integer smoothed x left eye gaze coordinate in pixels,
'Lavgx': integer smoothed y left eye gaze coordinate in pixels,
'Lpsize': float left eye pupil size,
'Lpupilx': integer raw left eye pupil centre x coordinate,
'Lpupily': integer raw left eye pupil centre y coordinate,
'Rrawx': integer raw x right eye gaze coordinate in pixels,
'Rrawy': integer raw y right eye gaze coordinate in pixels,
'Ravgx': integer smoothed x right eye gaze coordinate in pixels,
'Ravgx': integer smoothed y right eye gaze coordinate in pixels,
'Rpsize': float right eye pupil size,
'Rpupilx': integer raw right eye pupil centre x coordinate,
'Rpupily': integer raw right eye pupil centre y coordinate
}
"""
# send the request
response = self.connection.request('tracker', 'get', ['frame'])
# raise error if needed