From ca8c60a0ed0778fc6af7bc511f0b63e28a3a4612 Mon Sep 17 00:00:00 2001 From: von Neumann 101 <2961978672@qq.com> Date: Tue, 19 May 2026 20:06:41 +0800 Subject: [PATCH] Set OpenCV fourcc after size and fps (#3620) * Set OpenCV fourcc after size and fps * Set OpenCV fourcc last on Windows * Add comment explaining DSHOW fourcc ordering --- src/lerobot/cameras/opencv/camera_opencv.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lerobot/cameras/opencv/camera_opencv.py b/src/lerobot/cameras/opencv/camera_opencv.py index f3289ddc7..3e92eaf06 100644 --- a/src/lerobot/cameras/opencv/camera_opencv.py +++ b/src/lerobot/cameras/opencv/camera_opencv.py @@ -199,12 +199,13 @@ class OpenCVCamera(Camera): DeviceNotConnectedError: If the camera is not connected. """ - # Set FOURCC first (if specified) as it can affect available FPS/resolution options - if self.config.fourcc is not None: - self._validate_fourcc() if self.videocapture is None: raise DeviceNotConnectedError(f"{self} videocapture is not initialized") + set_fourcc_after_size_and_fps = platform.system() == "Windows" + if self.config.fourcc is not None and not set_fourcc_after_size_and_fps: + self._validate_fourcc() + default_width = int(round(self.videocapture.get(cv2.CAP_PROP_FRAME_WIDTH))) default_height = int(round(self.videocapture.get(cv2.CAP_PROP_FRAME_HEIGHT))) @@ -222,6 +223,11 @@ class OpenCVCamera(Camera): else: self._validate_fps() + if self.config.fourcc is not None and set_fourcc_after_size_and_fps: + # On Windows with DSHOW, changing the resolution can silently override the FOURCC setting. + # Set FOURCC last to make sure the requested pixel format is actually enforced. + self._validate_fourcc() + def _validate_fps(self) -> None: """Validates and sets the camera's frames per second (FPS)."""