aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/utils/live_tobii_session.py
blob: f71b18fde79c4421626d426860436bb2b4cc906d (plain)
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
#!/usr/bin/env python

import argparse
import os, time

from argaze import DataStructures, GazeFeatures
from argaze.TobiiGlassesPro2 import *

import cv2 as cv
import numpy

def main():
    """
    Capture video camera and gaze data streams and synchronise them.
    """

    # Manage arguments
    parser = argparse.ArgumentParser(description=main.__doc__.split('-')[0])
    parser.add_argument('-t', '--tobii_ip', metavar='TOBII_IP', type=str, default='192.168.1.12', help='tobii glasses ip')

    args = parser.parse_args()

    # Create tobii controller
    tobii_controller = TobiiController.TobiiController(args.tobii_ip, 'myProject', 'mySelf')

    # Calibrate tobii glasses
    tobii_controller.calibrate()

    # Enable tobii data stream
    tobii_data_stream = tobii_controller.enable_data_stream()

    # Enable tobii video stream
    tobii_video_stream = tobii_controller.enable_video_stream()

    # Start streaming
    tobii_controller.start_streaming()

    # Live video stream capture loop
    try:

        past_gaze_positions = DataStructures.TimeStampedBuffer()

        while tobii_video_stream.is_alive():

            video_ts, video_frame = tobii_video_stream.read()
                
            try:

                # Read data stream
                data_stream = tobii_data_stream.read()

                # Store received gaze positions
                past_gaze_positions.append(data_stream.gidx_l_gp)

                # Get last gaze position before video timestamp and remove all former gaze positions
                earliest_ts, earliest_gaze_position = past_gaze_positions.pop_first_until(video_ts)

                # Draw gaze position
                gaze_position = (int(earliest_gaze_position.gp[0] * video_frame.width), int(earliest_gaze_position.gp[1] * video_frame.height))
                cv.circle(video_frame.matrix, gaze_position, 4, (0, 255, 255), -1)

            # Wait for gaze position
            except (AttributeError, ValueError):
                continue
            
            # Close window using 'Esc' key
            if cv.waitKey(1) == 27:
                break

            cv.imshow(f'Live Tobii Camera', video_frame.matrix)

    # Exit on 'ctrl+C' interruption
    except KeyboardInterrupt:
        pass

    # Stop frame display
    cv.destroyAllWindows()

    # Stop streaming
    tobii_controller.stop_streaming()
    
if __name__ == '__main__':

    main()