From f29ceb8d715bba120e0698f277df3bd4a47ab323 Mon Sep 17 00:00:00 2001 From: Théo de la Hogue Date: Mon, 3 Jul 2023 11:00:59 +0200 Subject: Adding thread lock feature into ArScreen class. Removing thereading lock from demo script. --- src/argaze/ArFeatures.py | 20 ++++++++++++++++++- src/argaze/utils/demo_gaze_features_run.py | 32 +----------------------------- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/argaze/ArFeatures.py b/src/argaze/ArFeatures.py index d3ce874..b4d7c11 100644 --- a/src/argaze/ArFeatures.py +++ b/src/argaze/ArFeatures.py @@ -688,13 +688,17 @@ class ArScreen(): # Define scene attribute: it will be setup by parent scene later self._scene = None - # Init gaze data + # Init current gaze position self.__gaze_position = GazeFeatures.UnvalidGazePosition() + # Init heatmap if required if self.heatmap: self.heatmap.init() + # Init lock to share looked data wit hmultiples threads + self.__looking_lock = threading.Lock() + @classmethod def from_scene(self, aoi_3d_scene, aoi_name, size, background, gaze_movement_identifier, scan_path_analyzers: list = [], aoi_scan_path_analyzers: list = [], heatmap: bool = False) -> ArScreenType: @@ -716,12 +720,20 @@ class ArScreen(): def current_gaze_position(self): """Get current gaze position on screen.""" + # Wait for screen to be unlocked + while self.__looking_lock.locked(): + pass + return self.__gaze_position @property def current_gaze_movement(self): """Get current gaze movement on screen.""" + # Wait for screen to be unlocked + while self.__looking_lock.locked(): + pass + # Check current screen fixation current_fixation = self.gaze_movement_identifier.current_fixation @@ -749,6 +761,9 @@ class ArScreen(): aoi_scan_step: new scan step (if aoi_scan_path is intanciated) """ + # Lock screen exploitation + self.__looking_lock.acquire() + self.__gaze_position = inner_gaze_position # Prepare return @@ -818,5 +833,8 @@ class ArScreen(): self.heatmap.update(self.__gaze_position.value, sigma=0.05) + # Unlock screen exploitation + self.__looking_lock.release() + # Return return gaze_movement, look_at, scan_step, aoi_scan_step diff --git a/src/argaze/utils/demo_gaze_features_run.py b/src/argaze/utils/demo_gaze_features_run.py index 03467e9..6ec4c97 100644 --- a/src/argaze/utils/demo_gaze_features_run.py +++ b/src/argaze/utils/demo_gaze_features_run.py @@ -54,13 +54,6 @@ def main(): # Update pointer position def on_mouse_event(event, x, y, flags, param): - # Don't look at screen while screen is exploited into video loop - if ar_screen_lock.locked(): - return - - # Lock screen exploitation - ar_screen_lock.acquire() - try: # Edit millisecond timestamp @@ -73,11 +66,6 @@ def main(): print(f'Error on {e.aoi} step:', e) - # Unlock screen exploitation - ar_screen_lock.release() - - return - # Attach mouse callback to window cv2.setMouseCallback(ar_screen.name, on_mouse_event) @@ -87,21 +75,12 @@ def main(): # Analyse mouse positions while True: - # Lock screen exploitation - ar_screen_lock.acquire() - # Draw screen image = ar_screen.background.copy() # Draw heatmap if ar_screen.heatmap: - # Clear heatmap - if clear_heatmap: - - ar_screen.heatmap.init(10 if enable_heatmap_buffer else 0) - clear_heatmap = False - image = cv2.addWeighted(ar_screen.heatmap.image, 0.5, image, 1., 0) # Write heatmap buffer manual @@ -259,9 +238,6 @@ def main(): pass - # Unlock screen exploitation - ar_screen_lock.release() - # Draw image cv2.imshow(ar_screen.name, image) @@ -273,21 +249,15 @@ def main(): # Reload environment with 'h' key if key_pressed == 114: - # Lock screen exploitation - ar_screen_lock.acquire() - ar_environment = ArFeatures.ArEnvironment.from_json(args.environment) ar_screen = ar_environment.scenes["AR Scene Demo"].screens["GrayRectangle"] - # Unlock screen exploitation - ar_screen_lock.release() - # Enable heatmap buffer with 'b' key if key_pressed == 98: enable_heatmap_buffer = not enable_heatmap_buffer - clear_heatmap = True + ar_screen.heatmap.init(10 if enable_heatmap_buffer else 0) # Stop calibration by pressing 'Esc' key if key_pressed == 27: -- cgit v1.1