aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/utils/contexts/OpenCV.py
blob: e921db031bef687e2327c2563e102485fc826d92 (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
"""Define OpenCV window display context"""

"""
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__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"

import logging
import threading
import time

import cv2

from argaze import ArFeatures, DataFeatures


class Cursor(ArFeatures.ArContext):
	"""Capture cursor position over OpenCV window.

	!!! warning
	    It is assumed that an OpenCV window with the same name than the context is used to display context's pipeline image.
	"""

	@DataFeatures.PipelineStepInit
	def __init__(self, **kwargs):

		# Init DataCaptureContext class
		super().__init__()

	@DataFeatures.PipelineStepEnter
	def __enter__(self):

		logging.info('OpenCV.Cursor context starts...')

		# Create a window
		cv2.namedWindow(self.name, cv2.WINDOW_AUTOSIZE)

		# Init timestamp
		self._start_time = time.time()

		# Attach mouse event callback to window
		cv2.setMouseCallback(self.name, self.__on_mouse_event)
		
		return self

	@DataFeatures.PipelineStepExit
	def __exit__(self, exception_type, exception_value, exception_traceback):

		logging.info('OpenCV.Cursor context stops...')

		# Delete window
		cv2.destroyAllWindows()

	def __on_mouse_event(self, event, x, y, flags, param):
		"""Process pointer position."""

		logging.debug('OpenCV.Cursor.on_mouse_event %i %i', x, y)

		if not self.is_paused():

			# Process timestamped gaze position
			self._process_gaze_position(timestamp = int((time.time() - self._start_time) * 1e3), x = x, y = y)


class Movie(Cursor, ArFeatures.DataPlaybackContext):
	"""Playback movie images and capture cursor position over OpenCV window.

	!!! warning
	    It is assumed that an OpenCV window with the same name than the context is used to display context's pipeline image.
	"""
	@DataFeatures.PipelineStepInit
	def __init__(self, **kwargs):

		# Init Cursor class
		super().__init__()

		# Init private attributes
		self.__path = None
		self.__movie = None
		self.__movie_fps = None
		self.__movie_width = None
		self.__movie_height = None
		self.__movie_length = None

		self.__current_image_index = None
		self.__next_image_index = None
		self.__refresh = False

	@property
	def path(self) -> str:
		"""Movie file path."""
		return self.__path

	@path.setter
	def path(self, path: str):

		self.__path = path 

		# Load movie
		self.__movie = cv2.VideoCapture(self.__path)
		self.__movie_fps = self.__movie.get(cv2.CAP_PROP_FPS)
		self.__movie_width = int(self.__movie.get(cv2.CAP_PROP_FRAME_WIDTH))
		self.__movie_height = int(self.__movie.get(cv2.CAP_PROP_FRAME_HEIGHT))
		self.__movie_length = self.__movie.get(cv2.CAP_PROP_FRAME_COUNT)

	@DataFeatures.PipelineStepEnter
	def __enter__(self):

		logging.info('OpenCV.Movie context starts...')

		# Enter in Cursor context
		super().__enter__()

		# Open reading thread
		self.__reading_thread = threading.Thread(target=self.__read)

		logging.debug('> starting reading thread...')
		self.__reading_thread.start()
		
		return self

	def __read(self):
		"""Iterate on movie images."""

		# Init image selection
		_, current_image = self.__movie.read()
		current_image_time = self.__movie.get(cv2.CAP_PROP_POS_MSEC)
		self.__next_image_index = 0 #int(self.__start * self.__movie_fps)

		while self.is_running():

			# Check pause event (and stop event)
			while self.is_paused() and self.is_running():

				logging.debug('> reading is paused at %i', current_image_time)

				time.sleep(1)

			# Select a new image and detect markers once
			if self.__next_image_index != self.__current_image_index or self.__refresh:

				self.__movie.set(cv2.CAP_PROP_POS_FRAMES, self.__next_image_index)

				success, image = self.__movie.read()

				if success:

					# Refresh once
					self.__refresh = False

					self.__current_image_index = self.__movie.get(cv2.CAP_PROP_POS_FRAMES) - 1
					current_image_time = self.__movie.get(cv2.CAP_PROP_POS_MSEC)

					# Timestamp image
					image = DataFeatures.TimestampedImage(image, timestamp=current_image_time)

					# Process movie image
					self._process_camera_image(timestamp=current_image_time, image=image)

			# Wait
			time.sleep(1 / self.__movie_fps)

	@DataFeatures.PipelineStepExit
	def __exit__(self, exception_type, exception_value, exception_traceback):

		logging.info('OpenCV.Movie context stops...')

		# Exit from Cursor context
		super().__exit__(exception_type, exception_value, exception_traceback)

		# Close data capture
		self.stop()

		# Stop reading thread
		threading.Thread.join(self.__reading_thread)

	def refresh(self):
		"""Refresh current frame."""
		self.__refresh = True

	def previous(self):
		"""Go to previous frame."""
		self.__next_image_index -= 1

		# Clip image index
		if self.__next_image_index < 0:
			self.__next_image_index = 0

	def next(self):
		"""Go to next frame."""

		self.__next_image_index += 1

		# Clip image index
		if self.__next_image_index < 0:
			self.__next_image_index = 0

	@property
	def duration(self) -> int|float:
		"""Get movie duration."""

		return self.__movie_length / self.__movie_fps

	@property
	def progression(self) -> float:
		"""Get movie playback progression between 0 and 1."""

		if self.__current_image_index is not None:

			return self.__current_image_index / self.__movie_length

		else:

			return 0.

class Camera(Cursor, ArFeatures.DataCaptureContext):
	"""Capture camera images and capture cursor position over OpenCV window.

	!!! warning
	    It is assumed that an OpenCV window with the same name than the context is used to display context's pipeline image.
	"""
	@DataFeatures.PipelineStepInit
	def __init__(self, **kwargs):

		# Init Cursor class
		super().__init__()

		# Init private attributes
		self.__camera_id = None
		self.__camera = None
		self.__video_fps = None
		self.__video_width = None
		self.__video_height = None

	@property
	def identifier(self) -> int:
		"""Camera device id."""
		return self.__camera_id

	@identifier.setter
	def identifier(self, camera_id: int):

		self.__camera_id = camera_id

		# Load movie
		self.__camera = cv2.VideoCapture(self.__camera_id)
		self.__video_fps = self.__camera.get(cv2.CAP_PROP_FPS)
		self.__video_width = int(self.__camera.get(cv2.CAP_PROP_FRAME_WIDTH))
		self.__video_height = int(self.__camera.get(cv2.CAP_PROP_FRAME_HEIGHT))

	@DataFeatures.PipelineStepEnter
	def __enter__(self):

		logging.info('OpenCV.Movie context starts...')

		# Enter in Cursor context
		super().__enter__()

		# Open reading thread
		self.__reading_thread = threading.Thread(target=self.__read)

		logging.debug('> starting reading thread...')
		self.__reading_thread.start()
		
		return self

	def __read(self):
		"""Iterate on camera images."""

		while self.is_running():

			# Check pause event (and stop event)
			while self.is_paused() and self.is_running():

				logging.debug('> reading is paused at %i', current_image_time)

				time.sleep(1)

			# Select a new image
			success, image = self.__camera.read()
			image_time = self.__camera.get(cv2.CAP_PROP_POS_MSEC)

			if success:

				# Timestamp image
				image = DataFeatures.TimestampedImage(image, timestamp=image_time)

				# Process movie image
				self._process_camera_image(timestamp=image_time, image=image)

			# Wait
			time.sleep(1 / self.__video_fps)

	@DataFeatures.PipelineStepExit
	def __exit__(self, exception_type, exception_value, exception_traceback):

		logging.info('OpenCV.Movie context stops...')

		# Exit from Cursor context
		super().__exit__(exception_type, exception_value, exception_traceback)

		# Close data capture
		self.stop()

		# Stop reading thread
		threading.Thread.join(self.__reading_thread)