From a65321017358b2a94840ea985239ac2abdf7d0b6 Mon Sep 17 00:00:00 2001 From: Steven Palma Date: Tue, 20 May 2025 18:31:55 +0200 Subject: [PATCH] chore(cameras): move _stop_thread under _start_thread methods --- .../common/cameras/opencv/camera_opencv.py | 34 ++++++++-------- .../cameras/realsense/camera_realsense.py | 40 +++++++++---------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lerobot/common/cameras/opencv/camera_opencv.py b/lerobot/common/cameras/opencv/camera_opencv.py index 9951cfb1..101940c0 100644 --- a/lerobot/common/cameras/opencv/camera_opencv.py +++ b/lerobot/common/cameras/opencv/camera_opencv.py @@ -444,6 +444,23 @@ class OpenCVCamera(Camera): self.thread.start() logger.debug(f"Read thread started for {self}.") + def _stop_read_thread(self) -> None: + """Signals the background read thread to stop and waits for it to join.""" + if self.stop_event is not None: + logger.debug(f"Signaling stop event for read thread of {self}.") + self.stop_event.set() + + if self.thread is not None and self.thread.is_alive(): + logger.debug(f"Waiting for read thread of {self} to join...") + self.thread.join(timeout=2.0) + if self.thread.is_alive(): + logger.warning(f"Read thread for {self} did not terminate gracefully after 2 seconds.") + else: + logger.debug(f"Read thread for {self} joined successfully.") + + self.thread = None + self.stop_event = None + def async_read(self, timeout_ms: float = 2000) -> np.ndarray: """ Reads the latest available frame asynchronously. @@ -488,23 +505,6 @@ class OpenCVCamera(Camera): logger.exception(f"Unexpected error getting frame from queue for {self}: {e}") raise RuntimeError(f"Error getting frame from queue for camera {self.index_or_path}: {e}") from e - def _stop_read_thread(self) -> None: - """Signals the background read thread to stop and waits for it to join.""" - if self.stop_event is not None: - logger.debug(f"Signaling stop event for read thread of {self}.") - self.stop_event.set() - - if self.thread is not None and self.thread.is_alive(): - logger.debug(f"Waiting for read thread of {self} to join...") - self.thread.join(timeout=2.0) - if self.thread.is_alive(): - logger.warning(f"Read thread for {self} did not terminate gracefully after 2 seconds.") - else: - logger.debug(f"Read thread for {self} joined successfully.") - - self.thread = None - self.stop_event = None - def disconnect(self): """ Disconnects from the camera and cleans up resources. diff --git a/lerobot/common/cameras/realsense/camera_realsense.py b/lerobot/common/cameras/realsense/camera_realsense.py index e97dd561..d791a7e6 100644 --- a/lerobot/common/cameras/realsense/camera_realsense.py +++ b/lerobot/common/cameras/realsense/camera_realsense.py @@ -308,15 +308,15 @@ class RealSenseCamera(Camera): raise DeviceNotConnectedError(f"Cannot validate settings for {self} as it is not connected.") stream = self.rs_profile.get_stream(rs.stream.color).as_video_stream_profile() + if self.fps is None: self.fps = stream.fps() else: self._validate_fps(stream) - actual_width = int(round(stream.width())) - actual_height = int(round(stream.height())) - if self.width is None or self.height is None: + actual_width = int(round(stream.width())) + actual_height = int(round(stream.height())) if self.rotation in [cv2.ROTATE_90_CLOCKWISE, cv2.ROTATE_90_COUNTERCLOCKWISE]: self.width, self.height = actual_height, actual_width self.capture_width, self.capture_height = actual_width, actual_height @@ -540,6 +540,23 @@ class RealSenseCamera(Camera): self.thread.start() logger.debug(f"Read thread started for {self}.") + def _stop_read_thread(self): + """Signals the background read thread to stop and waits for it to join.""" + if self.stop_event is not None: + logger.debug(f"Signaling stop event for read thread of {self}.") + self.stop_event.set() + + if self.thread is not None and self.thread.is_alive(): + logger.debug(f"Waiting for read thread of {self} to join...") + self.thread.join(timeout=2.0) + if self.thread.is_alive(): + logger.warning(f"Read thread for {self} did not terminate gracefully after 2 seconds.") + else: + logger.debug(f"Read thread for {self} joined successfully.") + + self.thread = None + self.stop_event = None + # NOTE(Steven): Missing implementation for depth for now def async_read(self, timeout_ms: float = 100) -> np.ndarray: """ @@ -587,23 +604,6 @@ class RealSenseCamera(Camera): f"Error getting frame data from queue for camera {self.serial_number}: {e}" ) from e - def _stop_read_thread(self): - """Signals the background read thread to stop and waits for it to join.""" - if self.stop_event is not None: - logger.debug(f"Signaling stop event for read thread of {self}.") - self.stop_event.set() - - if self.thread is not None and self.thread.is_alive(): - logger.debug(f"Waiting for read thread of {self} to join...") - self.thread.join(timeout=2.0) - if self.thread.is_alive(): - logger.warning(f"Read thread for {self} did not terminate gracefully after 2 seconds.") - else: - logger.debug(f"Read thread for {self} joined successfully.") - - self.thread = None - self.stop_event = None - def disconnect(self): """ Disconnects from the camera, stops the pipeline, and cleans up resources.