aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/utils/synchronise_timestamped_data.py
blob: 0c2207268c01a32efc4c07de907d979453354db3 (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







 # Synchronise video and gaze
        vts_ts, vts = tobii_vts.pop_first()
        vts_offset = (vts_ts - vts.vts) / 1000

        print(f'Init >>> vts_offset = {vts_offset}')

        last_gaze_position_index = -1
        closest_gaze_position_ts, closest_gaze_position = None, None

        last_fixation_index = -1
        closest_fixation_ts, closest_fixation = None, None

        for frame_ts, frame in tobii_segment_video.frames():

            frame_ts = frame_ts / 1000

            if frame_ts > vts.vts / 1000:
                if len(tobii_vts) > 0:
                    vts_ts, vts = tobii_vts.pop_first()
                    vts_offset = (vts_ts - vts.vts) / 1000
                    print(f'{frame_ts / 1000} >>> New vts_offset = {vts_offset / 1000}')

            # Find closest gaze position
            closest_index = bisect.bisect_left(list(generic_ts_gaze_positions.keys()), frame_ts + vts_offset) - 1
            if closest_index > last_gaze_position_index:
                
                # pop data until closest_index
                while last_gaze_position_index <= closest_index:
                    closest_gaze_position_ts, closest_gaze_position = generic_ts_gaze_positions.pop_first()
                    last_gaze_position_index += 1

                print(f'{frame_ts / 1000} *** New closest gaze position: index = {closest_index}, ts = {closest_gaze_position_ts / 1000}, {closest_gaze_position}')

            # Find closest fixation
            closest_index = bisect.bisect_left(list(fixation_analyser.fixations.keys()), frame_ts + vts_offset) - 1
            if closest_index > last_fixation_index:
                
                # pop data until closest_index
                while last_fixation_index <= closest_index:
                    closest_ts, closest_fixation = fixation_analyser.fixations.pop_first()
                    last_fixation_index += 1

                print(f'{frame_ts / 1000} /// New closest fixation: index= {closest_index}, ts = {closest_ts / 1000}, {closest_fixation}')