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

"""Load ArUcoCamera from a configuration file then, stream and process gaze positions and image from any worn eye-tracker device.

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 <http://www.gnu.org/licenses/>.
"""

__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"

import argparse
import contextlib

from argaze import GazeFeatures, DataFeatures
from argaze.ArUcoMarkers import ArUcoCamera

import cv2

# Manage arguments
parser = argparse.ArgumentParser(description=__doc__.split('-')[0])
parser.add_argument('configuration', metavar='CONFIGURATION', type=str, help='configuration filepath')
parser.add_argument('-p', '--patch', metavar='PATCH', type=str, help='configuration patch filepath')
parser.add_argument('-v', '--verbose', action='store_true', default=False, help='enable verbose mode to print information in console')
args = parser.parse_args()

def main():

	# Load ArUcoCamera configuration
	with ArUcoCamera.ArUcoCamera.from_json(args.configuration, args.patch) as aruco_camera:

		if args.verbose:

			print(aruco_camera)

		# DEBUG
		print(dir(aruco_camera))

		# Gaze position processing
		def gaze_position_callback(timestamped_gaze_position: GazeFeatures.GazePosition):

			# Project gaze position into environment
			try:

				aruco_camera.look(timestamped_gaze_position)

			# Handle exceptions
			except Exception as e:

				print(e)

		# Attach gaze position callback to provider
		aruco_camera.provider.attach(gaze_position_callback)

		# Image processing
		def image_callback(timestamp: int|float, image):

			# Detect ArUco code and project ArScenes
			try:

				# Watch ArUco markers into image and estimate camera pose
				aruco_camera.watch(image, timestamp=timestamp)

			# Handle exceptions
			except Exception as e:

				print(e)

		# Attach image callback to provider
		aruco_camera.provider.attach(image_callback)

		# Waiting for 'ctrl+C' interruption
		with contextlib.suppress(KeyboardInterrupt):

			# Visualisation loop
			while True:

				# Display camera frame image
				image = aruco_camera.image()

				cv2.imshow(aruco_camera.name, image)

				# Display each scene frames image
				for scene_frame in aruco_camera.scene_frames():

					cv2.imshow(scene_frame.name, scene_frame.image())

				# Key interaction
				key_pressed = cv2.waitKey(10)

				# Esc: close window
				if key_pressed == 27:
					
					raise KeyboardInterrupt()

		# Stop frame display
		cv2.destroyAllWindows()

if __name__ == '__main__':

	main()