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
|
"""Handle network connection to Pupil Labs devices. Tested with Pupil Invisible.
Based on Pupil Labs' Realtime Python API."""
"""
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
"""
__author__ = "Damien Mouratille"
__credits__ = []
__copyright__ = "Copyright 2024, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"
import sys
import logging
import time
import threading
from dataclasses import dataclass
from argaze import ArFeatures, DataFeatures, GazeFeatures
from argaze.utils import UtilsFeatures
import numpy
import cv2
from pupil_labs.realtime_api.simple import discover_one_device
class LiveStream(ArFeatures.DataCaptureContext):
@DataFeatures.PipelineStepInit
def __init__(self, **kwargs):
# Init DataCaptureContext class
super().__init__()
def __enter__(self):
logging.info('Pupil-Labs Device connexion starts...')
# Init timestamp
self.__start_time = time.time()
# Look for devices. Returns as soon as it has found the first device.
self.__device = discover_one_device(max_search_duration_seconds=10)
if self.__device is None:
logging.info('No device found. Exit!')
raise SystemExit(-1)
else:
logging.info('Device found. Stream loading.')
# Open gaze stream
self.__gaze_thread = threading.Thread(target=self.__stream_gaze)
logging.debug('> starting gaze thread...')
self.__gaze_thread.start()
# Open video stream
self.__video_thread = threading.Thread(target=self.__stream_video)
logging.debug('> starting video thread...')
self.__video_thread.start()
return self
def __stream_gaze(self):
"""Stream gaze."""
logging.debug('Stream gaze from Pupil Device')
while self.is_running():
try:
while True:
gaze = self.__device.receive_gaze_datum()
gaze_timestamp = int((gaze.timestamp_unix_seconds - self.__start_time) * 1e3)
logging.debug('Gaze received at %i timestamp', gaze_timestamp)
# When gaze position is valid
if gaze.worn is True:
self._process_gaze_position(
timestamp=gaze_timestamp,
x=int(gaze.x),
y=int(gaze.y))
else:
# Process empty gaze position
logging.debug('Not worn at %i timestamp', gaze_timestamp)
self._process_gaze_position(timestamp=gaze_timestamp)
except KeyboardInterrupt:
pass
def __stream_video(self):
"""Stream video."""
logging.debug('Stream video from Pupil Device')
while self.is_running():
try:
while True:
scene_frame, frame_datetime = self.__device.receive_scene_video_frame()
scene_timestamp = int((frame_datetime - self.__start_time) * 1e3)
logging.debug('Video received at %i timestamp', scene_timestamp)
self._process_camera_image(
timestamp=scene_timestamp,
image=scene_frame)
except KeyboardInterrupt:
pass
@DataFeatures.PipelineStepExit
def __exit__(self, exception_type, exception_value, exception_traceback):
logging.debug('Pupil-Labs context stops...')
# Close data stream
self.stop()
# Stop streaming
threading.Thread.join(self.__gaze_thread)
threading.Thread.join(self.__video_thread)
|