aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/TobiiGlassesPro2/TobiiEntities.py
blob: e243a5aaa9b99508df5aaa9637454aa237f06ec5 (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
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
#!/usr/bin/env python

import datetime
import json
import gzip
import os
import cv2 as cv

TOBII_DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S+%f'

TOBII_PROJECTS_DIRNAME = "projects"
TOBII_PROJECT_FILENAME = "project.json"

TOBII_PARTICIPANTS_DIRNAME = "participants"
TOBII_PARTICIPANT_FILENAME = "participant.json"

TOBII_RECORDINGS_DIRNAME = "recordings"
TOBII_RECORD_FILENAME = "recording.json"

TOBII_SEGMENTS_DIRNAME = "segments"
TOBII_SEGMENT_FILENAME = "segment.json"
TOBII_SEGMENT_VIDEO_FILENAME = "fullstream.mp4"
TOBII_SEGMENT_LIVEDATA_FILENAME = "livedata.json.gz"

class TobiiSegmentVideo:
    """Handle Tobii Glasses Pro 2 segment video file."""

    def __init__(self, segment_video_path):
        """Load segment video from segment directory."""

        self.__segment_video_path = segment_video_path
        
        cap = cv.VideoCapture(self.__segment_video_path)

        self.__width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
        self.__height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
        self.__fps = int(cap.get(cv.CAP_PROP_FPS))

    def get_path(self):
        return self.__segment_video_path

    def get_height(self):
        return self.__height

    def get_width(self):
        return self.__width

    def get_fps(self):
        return self.__fps

class TobiiSegment:
    """Handle Tobii Glasses Pro 2 segment info, data and video."""

    def __init__(self, segment_path):
        """Load segment info from segment directory."""

        self.__segment_id = os.path.basename(segment_path)
        self.__segment_path = segment_path
        
        with open(os.path.join(self.__segment_path, TOBII_SEGMENT_FILENAME)) as f:
            try:
                item = json.load(f)
            except:
                raise RuntimeError(f'JSON fails to load {self.__segment_path}/{TOBII_SEGMENT_FILENAME}')

        self.__length_us = int(item["seg_length_us"])
        self.__calibrated = bool(item["seg_calibrated"])
        self.__time_start = datetime.datetime.strptime(item["seg_t_start"], TOBII_DATETIME_FORMAT)
        self.__time_stop = datetime.datetime.strptime(item["seg_t_stop"], TOBII_DATETIME_FORMAT)

    def get_path(self):
        return self.__segment_path

    def get_id(self):
        return self.__segment_id

    def get_length_us(self):
        return self.__length_us

    def get_live_data_filepath(self):
        return os.path.join(self.__segment_path, self.__segment_id)

    def get_start_date_time(self):
        return self.__time_start

    def get_stop_date_time(self):
        return self.__time_stop

    def is_calibrated(self):
        return self.__calibrated

    def get_video(self):
        return TobiiSegmentVideo(os.path.join(self.__segment_path, TOBII_SEGMENT_VIDEO_FILENAME))

class TobiiRecording:
    """Handle Tobii Glasses Pro 2 recording info and segments."""

    def __init__(self, recording_path):
        """Load recording info from recording directory."""

        self.__recording_id = os.path.basename(recording_path)
        self.__recording_path = recording_path
        self.__project_path = os.path.dirname(os.path.dirname(os.path.abspath(self.__recording_path)))

        with open(os.path.join(self.__recording_path, TOBII_RECORD_FILENAME)) as f:
            try:
                item = json.load(f)
            except:
                raise RuntimeError(f'JSON fails to load {self.__recording_path}/{TOBII_RECORD_FILENAME}')

        self.__recording_created = datetime.datetime.strptime(item["rec_created"], TOBII_DATETIME_FORMAT)
        self.__recording_name = item["rec_info"]["Name"]
        self.__recording_length = int(item["rec_length"])
        self.__recording_segments = int(item["rec_segments"])
        self.__recording_et_samples = int(item["rec_et_samples"])
        self.__recording_et_valid_samples = int(item["rec_et_valid_samples"])
        self.__project = TobiiProject(self.__project_path)
        self.__participant = TobiiParticipant(self.__recording_path)

    def get_attributes(self):
        """Get recording attributes dictionnary."""

        attr = []
        names = []
        names.append("Recording name"); attr.append(self.__recording_name)
        names.append("Project name"); attr.append(self.__project.getName())
        names.append("Creation Date"); attr.append(str(self.__recording_created))
        names.append("Duration (s)"); attr.append(str(self.__recording_length))
        names.append("Participant name"); attr.append(self.__participant.getName())
        names.append("Segments"); attr.append(str(self.__recording_segments))
        names.append("Et samples"); attr.append(str(self.__recording_et_samples))
        names.append("Et valid samples"); attr.append(str(self.__recording_et_valid_samples))
        return (names, attr)

    def get_path(self):
        return self.__recording_path

    def get_creation_date(self):
        return self.__recording_created

    def get_et_samples(self):
        return self.__recording_et_samples

    def get_et_valid_samples(self):
        return self.__recording_et_valid_samples

    def get_id(self):
        return self.__recording_id

    def get_length(self):
        return self.__recording_length

    def get_name(self):
        return self.__recording_name

    def get_participant(self):
        return self.__participant

    def get_recording_directory(self):
        return self.__recording_dir

    def get_segment(self, segment_id):
        if segment_id > 0:
            return self.__segments[segment_id-1]
        raise ValueError('Cannot get segment less or equal to zero')

    def get_all_segments(self):

        all_segments = []
        segments_path = os.path.join(self.__recording_path, TOBII_SEGMENTS_DIRNAME)
        for item in os.listdir (segments_path):
            segment_path = os.path.join(segments_path, item)
            if os.path.isdir(segment_path):
                all_segments.append(TobiiSegment(segment_path))

        return all_segments

class TobiiParticipant:
    """Handle Tobii Glasses Pro 2 participant data."""

    def __init__(self, participant_path):
        """Load participant data from path"""

        self.__participant_id = os.path.basename(participant_path)
        self.__participant_path = participant_path

        with open(os.path.join(self.__participant_path, TOBII_PARTICIPANT_FILENAME)) as f:
            try:
                item = json.load(f)
            except:
                raise RuntimeError(f'JSON fails to load {source_dir}/{TOBII_PARTICIPANT_FILENAME}')

        self.__participant_name = item["pa_info"]["Name"]

    def get_path(self):
        return self.__participant_path

    def get_id(self):
        return self.__participant_id

    def get_name(self):
        return self.__participant_name

class TobiiProject:
    """Handle Tobii Glasses Pro 2 project data."""

    def __init__(self, project_path):
        """Load project data from projects directory and project id."""

        self.__project_id = os.path.basename(project_path)
        self.__project_path = project_path

        with open(os.path.join(self.__project_path, TOBII_PROJECT_FILENAME)) as f:
            try:
                item = json.load(f)
            except:
                raise RuntimeError(f'JSON fails to load {self.__project_path}/{TOBII_PROJECT_FILENAME}')

        self.__project_created = datetime.datetime.strptime(item["pr_created"], TOBII_DATETIME_FORMAT)

        try:
            self.__project_name = item["pr_info"]["Name"]
        except:
            self.__project_name = None

    def get_path(self):
        return self.__project_path

    def get_creation_date(self):
        return self.__project_created

    def get_id(self):
        return self.__project_id

    def get_name(self):
        return self.__project_name

    def get_all_participants(self):

        all_participants = []
        participants_path = os.path.join(self.__project_path, TOBII_PARTICIPANTS_DIRNAME)
        for item in os.listdir(participants_path):
            participant_path = os.path.join(participants_path, item)
            if os.path.isdir(participant_path):
                all_participants.append(TobiiParticipant(participant_path))

        return all_participants

    def get_all_recordings(self):

        all_recordings = []
        recordings_path = os.path.join(self.__project_path, TOBII_RECORDINGS_DIRNAME)
        for item in os.listdir(recordings_path):
            recording_path = os.path.join(recordings_path, item)
            if os.path.isdir(recording_path):
                all_recordings.append(TobiiRecording(recording_path))

        return all_recordings

class TobiiDrive:
    """Handle Tobii Glasses Pro 2 drive data."""

    def __init__(self, drive_path):
        """Load drive data from drive directory path."""

        self.__drive_path = drive_path

    def get_path(self):
        return self.__drive_path

    def get_all_projects(self):

        all_projects = []
        projects_path = os.path.join(self.__drive_path, TOBII_PROJECTS_DIRNAME)
        for item in os.listdir(projects_path):
            project_path = os.path.join(projects_path, item)
            if os.path.isdir(project_path):
                all_projects.append(TobiiProject(project_path))

        return all_projects