aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/ArFeatures.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/argaze/ArFeatures.py')
-rw-r--r--src/argaze/ArFeatures.py202
1 files changed, 183 insertions, 19 deletions
diff --git a/src/argaze/ArFeatures.py b/src/argaze/ArFeatures.py
index baa26a1..a3ce1e3 100644
--- a/src/argaze/ArFeatures.py
+++ b/src/argaze/ArFeatures.py
@@ -19,6 +19,7 @@ __license__ = "GPLv3"
import logging
import math
import os
+import ast
from typing import Iterator, Union
import cv2
@@ -1032,25 +1033,11 @@ class ArScene(DataFeatures.PipelineStepObject):
for name, layer in self._layers.items():
- # Clip AOI out of the visual horizontal field of view (optional)
- # TODO: use HFOV and VFOV and don't use vision_cone method
- if visual_hfov > 0:
-
- # Transform layer aoi scene into camera referential
- aoi_scene_camera_ref = layer.aoi_scene.transform(tvec, rvec)
-
- # Get aoi inside vision cone field
- cone_vision_height_cm = 200 # cm
- cone_vision_radius_cm = numpy.tan(numpy.deg2rad(visual_hfov / 2)) * cone_vision_height_cm
-
- _, aoi_outside = aoi_scene_camera_ref.vision_cone(cone_vision_radius_cm, cone_vision_height_cm)
-
- # Keep only aoi inside vision cone field
- aoi_scene_copy = layer.aoi_scene.copy(exclude=aoi_outside.keys())
-
- else:
-
- aoi_scene_copy = layer.aoi_scene.copy()
+ # TODO: if greater than 0., use HFOV and VFOV
+ # to clip AOI out of the visual horizontal field of view
+
+ # Copy aoi scene before projection
+ aoi_scene_copy = layer.aoi_scene.copy()
# Project layer aoi scene
# noinspection PyUnresolvedReferences
@@ -1072,6 +1059,10 @@ class ArCamera(ArFrame):
# Init private attributes
self.__visual_hfov = 0.
self.__visual_vfov = 0.
+ self.__projection_cache = None
+ self.__projection_cache_writer = None
+ self.__projection_cache_reader = None
+ self.__projection_cache_data = None
# Init protected attributes
self._scenes = {}
@@ -1133,6 +1124,132 @@ class ArCamera(ArFrame):
"""Set camera's visual vertical field of view."""
self.__visual_vfov = value
+ @property
+ def projection_cache(self) -> str:
+ """file path to store/read layers projections into/from a cache."""
+ return self.__projection_cache
+
+ @projection_cache.setter
+ def projection_cache(self, projection_cache: str):
+
+ self.__projection_cache = projection_cache
+
+ # The file doesn't exist yet: store projections into the cache
+ if not os.path.exists(os.path.join( DataFeatures.get_working_directory(), self.__projection_cache) ):
+
+ self.__projection_cache_writer = UtilsFeatures.FileWriter(path=self.__projection_cache)
+ self.__projection_cache_reader = None
+
+ logging.info('ArCamera %s writes projection into %s', self.name, self.__projection_cache)
+
+ # The file exist: read projection from the cache
+ else:
+
+ self.__projection_cache_writer = None
+ self.__projection_cache_reader = UtilsFeatures.FileReader(path=self.__projection_cache)
+
+ logging.info('ArCamera %s reads projection from %s', self.name, self.__projection_cache)
+
+ def _clear_projection(self):
+ """Clear layers projection."""
+
+ logging.debug('ArCamera._clear_projection %s', self.name)
+
+ for layer_name, layer in self.layers.items():
+
+ # Initialize layer if needed
+ if layer.aoi_scene is None:
+
+ layer.aoi_scene = AOI2DScene.AOI2DScene()
+
+ else:
+
+ layer.aoi_scene.clear()
+
+ def _write_projection_cache(self, timestamp: int|float, exception = None):
+ """Write layers aoi scene into the projection cache.
+
+ Parameters:
+ timestamp: cache time
+ """
+
+ if self.__projection_cache_writer is not None:
+
+ logging.debug('ArCamera._write_projection_cache %s %f', self.name, timestamp)
+
+ if exception is None:
+
+ projection = {}
+
+ for layer_name, layer in self.layers.items():
+
+ projection[layer_name] = layer.aoi_scene
+
+ self.__projection_cache_writer.write( (timestamp, projection) )
+
+ else:
+
+ self.__projection_cache_writer.write( (timestamp, exception) )
+
+ def _read_projection_cache(self, timestamp: int|float):
+ """Read layers aoi scene from the projection cache.
+
+ Parameters:
+ timestamp: cache time.
+
+ Returns:
+ success: False if there is no projection cache, True otherwise.
+ """
+
+ if self.__projection_cache_reader is None:
+
+ return False
+
+ logging.debug('ArCamera._read_projection_cache %s %f', self.name, timestamp)
+
+ # Clear former projection
+ self._clear_projection()
+
+ try:
+
+ # Read first data if not done yet
+ if self.__projection_cache_data is None:
+
+ self.__projection_cache_data = self.__projection_cache_reader.read()
+
+ # Continue reading cache until correct timestamped projection
+ while float(self.__projection_cache_data[0]) < timestamp:
+
+ self.__projection_cache_data = self.__projection_cache_reader.read()
+
+ # No more projection in the cache
+ except EOFError:
+
+ raise DataFeatures.TimestampedException("Projection cache is empty", timestamp=timestamp)
+
+ # Correct timestamped projection is found
+ if float(self.__projection_cache_data[0]) == timestamp:
+
+ # When correct timestamped projection is found
+ projection = {}
+
+ try:
+
+ projection = ast.literal_eval(self.__projection_cache_data[1])
+
+ for layer_name, aoi_scene in projection.items():
+
+ self._layers[layer_name].aoi_scene = AOI2DScene.AOI2DScene(aoi_scene)
+ self._layers[layer_name].timestamp = timestamp
+
+ logging.debug('> reading %s projection from cache', layer_name)
+
+ except SyntaxError as e:
+
+ raise DataFeatures.TimestampedException(self.__projection_cache_data[1], timestamp=timestamp)
+
+ return True
+
def scene_frames(self) -> Iterator[ArFrame]:
"""Iterate over all scenes frames"""
@@ -1153,6 +1270,28 @@ class ArCamera(ArFrame):
"visual_vfov": self.__visual_vfov
}
+ @DataFeatures.PipelineStepEnter
+ def __enter__(self):
+
+ if self.__projection_cache_writer is not None:
+
+ self.__projection_cache_writer.__enter__()
+
+ if self.__projection_cache_reader is not None:
+
+ self.__projection_cache_reader.__enter__()
+
+ @DataFeatures.PipelineStepExit
+ def __exit__(self, exception_type, exception_value, exception_traceback):
+
+ if self.__projection_cache_writer is not None:
+
+ self.__projection_cache_writer.__exit__(exception_type, exception_value, exception_traceback)
+
+ if self.__projection_cache_reader is not None:
+
+ self.__projection_cache_reader.__exit__(exception_type, exception_value, exception_traceback)
+
def _update_expected_and_excluded_aoi(self):
"""Edit expected aoi of each layer aoi scan path with the aoi of corresponding scene layer.
Edit excluded aoi to ignore frame aoi from aoi matching.
@@ -1468,11 +1607,14 @@ class ArContext(DataFeatures.PipelineStepObject):
logging.debug('\t> get image (%i x %i)', width, height)
+ last_position = self.__pipeline.last_gaze_position()
+
info_stack = 0
if draw_times:
if image.is_timestamped():
+
info_stack += 1
cv2.putText(image, f'Frame at {image.timestamp}ms', (20, info_stack * 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA)
@@ -1489,6 +1631,11 @@ class ArContext(DataFeatures.PipelineStepObject):
info_stack += 1
cv2.putText(image, f'Watch {watch_time}ms at {self.__process_camera_image_frequency}Hz', (20, info_stack * 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA)
+ if last_position is not None:
+
+ info_stack += 1
+ cv2.putText(image, f'Position at {last_position.timestamp}ms', (20, info_stack * 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA)
+
if issubclass(type(self.__pipeline), ArFrame):
try:
@@ -1513,3 +1660,20 @@ class ArContext(DataFeatures.PipelineStepObject):
cv2.putText(image, f'error: {e}', (20, height - (i + 1) * 50 + 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA)
return image
+
+ @DataFeatures.PipelineStepMethod
+ def pause(self):
+ """Pause pipeline processing."""
+
+ raise NotImplementedError('pause() method not implemented')
+
+ def is_paused(self) -> bool:
+ """Is pipeline processing paused?"""
+
+ raise NotImplementedError('is_paused() method not implemented')
+
+ @DataFeatures.PipelineStepMethod
+ def resume(self):
+ """Resume pipeline processing."""
+
+ raise NotImplementedError('resume() method not implemented')