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

from typing import TypeVar
import datetime
import json
import os

from argaze.TobiiGlassesPro2 import TobiiData, TobiiVideo

import av
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_INFO_FILENAME = "segment.json"
TOBII_SEGMENT_VIDEO_FILENAME = "fullstream.mp4"
TOBII_SEGMENT_DATA_FILENAME = "livedata.json.gz"

DatetimeType = TypeVar('datetime', bound="datetime")
# Type definition for type annotation convenience

class TobiiSegment:
    """Handle Tobii Glasses Pro 2 segment info."""

    def __init__(self, segment_path, start_timestamp:int = 0, end_timestamp:int = None):
        """Load segment info from segment directory.  
        Optionnaly select a time range in microsecond."""

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

        self.__start_timestamp = start_timestamp
        self.__end_timestamp = min(end_timestamp, int(item["seg_length"] * 1e6)) if end_timestamp != None else int(item["seg_length"] * 1e6)

        if self.__start_timestamp >= self.__end_timestamp:
            raise ValueError('start time is equal or greater than end time.')

        self.__calibrated = bool(item["seg_calibrated"])

        self.__start_date = datetime.datetime.strptime(item["seg_t_start"], TOBII_DATETIME_FORMAT)
        self.__stop_date = datetime.datetime.strptime(item["seg_t_stop"], TOBII_DATETIME_FORMAT)

    @property
    def path(self) -> str:
        """Get segment path."""

        return self.__path

    @property
    def id(self) -> str:
        """Get segment id."""
        return self.__id

    @property
    def start_timestamp(self) -> int:
        """Get the timestamp where the segment loading starts."""

        return self.__start_timestamp

    @property
    def end_timestamp(self) -> int:
        """Get the timestamp where the segment loading ends."""

        return self.__end_timestamp

    @property
    def start_date(self) -> DatetimeType:
        """Get the date when the segment has started."""

        return self.__start_date

    @property
    def stop_date(self) -> DatetimeType:
        """Get the date when the segment has stopped."""

        return self.__stop_date

    @property
    def calibrated(self) -> bool:
        """Is the segment has been calibrated?"""

        return self.__calibrated

    def load_data(self) -> "TobiiData.TobiiDataSegment":
        """Load recorded data stream."""

        return TobiiData.TobiiDataSegment(os.path.join(self.__path, TOBII_SEGMENT_DATA_FILENAME), self.__start_timestamp, self.__end_timestamp)

    def load_video(self) -> "TobiiVideo.TobiiVideoSegment":
        """Load recorded video stream."""

        return TobiiVideo.TobiiVideoSegment(os.path.join(self.__path, TOBII_SEGMENT_VIDEO_FILENAME), self.__start_timestamp, self.__end_timestamp)

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

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

        self.__id = os.path.basename(recording_path)
        self.__path = recording_path

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

        self.__name = item["rec_info"]["Name"]
        self.__creation_date = datetime.datetime.strptime(item["rec_created"], TOBII_DATETIME_FORMAT)
        
        self.__length = int(item["rec_length"])
        self.__et_samples = int(item["rec_et_samples"])
        self.__et_samples_valid = int(item["rec_et_valid_samples"])
        self.__participant_id = item["rec_participant"]

    @property
    def path(self) -> str:
        """Get recording path."""

        return self.__path

    @property
    def id(self) -> str:
        """Get recording id."""
        return self.__id

    @property
    def name(self) -> str:
        """Get recording name."""

        return self.__name

    @property
    def creation_date(self) -> DatetimeType:
        """Get date when the recording has been done."""

        return self.__creation_date

    @property
    def length(self):
        """Get record duration in second."""

        return self.__length

    @property
    def eyetracker_samples(self) -> int:
        """Get numbers of recorded eye detecter samples."""

        return self.__et_samples

    @property
    def eyetracker_samples_valid(self) -> int:
        """Get numbers of recorded eye detecter valid samples."""

        return self.__et_samples_valid

    @property
    def project(self) -> "TobiiProject":
        """Get project to which it belongs."""

        project_path = os.path.dirname(os.path.dirname(os.path.abspath(self.__path)))

        return TobiiProject(project_path)

    @property
    def participant(self) -> "TobiiParticipant":
        """Get participant to which it belongs."""

        project_path = os.path.dirname(os.path.dirname(os.path.abspath(self.__path)))

        return TobiiParticipant(project_path + '/participants/' + self.__participant_id)

    def segments(self) -> list["TobiiSegment"]:
        """Get all recorded segments."""

        all_segments = []
        segments_path = os.path.join(self.__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.__id = os.path.basename(participant_path)
        self.__path = participant_path

        with open(os.path.join(self.__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.__name = item["pa_info"]["Name"]

    @property
    def path(self) -> str:
        """Get participant path."""

        return self.__path

    @property
    def id(self) -> str:
        """Get participant id."""
        return self.__id

    @property
    def name(self) -> str:
        """Get participant name."""

        return self.__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.__id = os.path.basename(project_path)
        self.__path = project_path

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

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

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

    @property
    def path(self) -> str:
        """Get project path."""

        return self.__path

    @property
    def id(self) -> str:
        """Get project id."""
        return self.__id

    @property
    def name(self) -> str:
        """Get project name."""

        return self.__name

    @property
    def creation_date(self) -> DatetimeType:
        """Get date when the project has been created."""

        return self.__creation_date

    def participants(self) -> list["TobiiParticipant"]:
        """Get all participants."""

        all_participants = []
        participants_path = os.path.join(self.__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 recordings(self) -> list["TobiiRecording"]:
        """Get all recordings."""

        all_recordings = []
        recordings_path = os.path.join(self.__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.__path = drive_path

    @property
    def path(self) -> str:
        """Get drive path."""

        return self.__path

    def projects(self) -> list["TobiiProject"]:
        """Get all projects."""

        all_projects = []
        projects_path = os.path.join(self.__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