aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/utils/replay_tobii_session.py
blob: 0471506e9ddcc4d9e6a769428c564df072748e71 (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
#!/usr/bin/env python

import argparse
import bisect

from argaze import GazeFeatures
from argaze.TobiiGlassesPro2 import TobiiEntities, TobiiVideo, TobiiData

import numpy

import cv2 as cv

def main():
    """
    Replay Tobii segment video
    """

    # manage arguments
    parser = argparse.ArgumentParser(description=main.__doc__.split('-')[0])
    parser.add_argument('-s', '--segment_path', metavar='SEGMENT_PATH', type=str, default=None, help='segment path')
    parser.add_argument('-r', '--time_range', metavar=('START_TIME', 'END_TIME'), nargs=2, type=float, default=(0., None), help='start and end time (in second)')
    args = parser.parse_args()

    if args.segment_path != None:

        # Load a tobii segment
        tobii_segment = TobiiEntities.TobiiSegment(args.segment_path, int(args.time_range[0] * 1000000), int(args.time_range[1] * 1000000) if args.time_range[1] != None else None)

        # Load a tobii segment video
        tobii_segment_video = tobii_segment.load_video()
        print(f'Video duration: {tobii_segment_video.get_duration()/1000000}, width: {tobii_segment_video.get_width()}, height: {tobii_segment_video.get_height()}')

        # Load a tobii segment data
        tobii_segment_data = tobii_segment.load_data()
        print(f'Data keys: {tobii_segment_data.keys()}')

        # Access to timestamped gaze position data buffer
        tobii_ts_gaze_positions = tobii_segment_data.gidx_l_gp
        print(f'{len(tobii_ts_gaze_positions)} gaze positions loaded')

        # Video and data replay loop
        try:

            # Iterate on video frames activating video / data synchronisation through vts data buffer
            for video_ts, video_frame in tobii_segment_video.frames(tobii_segment_data.vts):

                try:

                    # Get closest gaze position before video timestamp and remove all gaze positions before
                    closest_gaze_ts, closest_gaze_position = tobii_ts_gaze_positions.pop_first_until(video_ts)

                    # Draw video synchronized gaze pointer
                    pointer = (int(closest_gaze_position.gp[0] * video_frame.width), int(closest_gaze_position.gp[1] * video_frame.height))
                    cv.circle(video_frame.matrix, pointer, 4, (0, 255, 255), -1)

                # When expected values can't be found
                except (KeyError, AttributeError, ValueError):
                    pass

                # Close window using 'Esc' key
                if cv.waitKey(1) == 27:
                    break

                cv.imshow(f'Segment {tobii_segment.get_id()} video', video_frame.matrix)

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

        # Stop frame display
        cv.destroyAllWindows()

if __name__ == '__main__':

    main()