From e42485c8371351c95a5dd392441e61ec256228b4 Mon Sep 17 00:00:00 2001 From: Steven Palma Date: Thu, 17 Apr 2025 00:51:24 +0200 Subject: [PATCH] refactor(cameras): remove tmp video capture in connect --- .../common/cameras/opencv/camera_opencv.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/lerobot/common/cameras/opencv/camera_opencv.py b/lerobot/common/cameras/opencv/camera_opencv.py index 4ed4a17c..6eb1b2bd 100644 --- a/lerobot/common/cameras/opencv/camera_opencv.py +++ b/lerobot/common/cameras/opencv/camera_opencv.py @@ -308,17 +308,11 @@ class OpenCVCamera(Camera): ) camera_idx = f"/dev/video{self.camera_index}" if platform.system() == "Linux" else self.camera_index - # First create a temporary camera trying to access `camera_index`, - # and verify it is a valid camera by calling `isOpened`. - tmp_camera = cv2_sdk.VideoCapture(camera_idx, backend) - is_camera_open = tmp_camera.isOpened() - # Release camera to make it accessible for `find_camera_indices` - tmp_camera.release() - del tmp_camera - # If the camera doesn't work, display the camera indices corresponding to - # valid cameras. - if not is_camera_open: + self.camera = cv2_sdk.VideoCapture(camera_idx, backend) + + if not self.camera.isOpened(): + self.camera.release() # Release the failed attempt # Verify that the provided `camera_index` is valid before printing the traceback cameras_info = find_cameras(cv2_sdk=cv2_sdk) available_cam_ids = [cam["index"] for cam in cameras_info] @@ -330,11 +324,6 @@ class OpenCVCamera(Camera): raise OSError(f"Can't access OpenCVCamera({camera_idx}).") - # Secondly, create the camera that will be used downstream. - # Note: For some unknown reason, calling `isOpened` blocks the camera which then - # needs to be re-created. - self.camera = cv2_sdk.VideoCapture(camera_idx, backend) - if self.fps is not None: self.camera.set(cv2_sdk.CAP_PROP_FPS, self.fps) if self.capture_width is not None: