From 976fc38dfa024cf149129883490f420a29b213a6 Mon Sep 17 00:00:00 2001 From: Mathieu De Coster Date: Wed, 14 Feb 2024 13:08:25 +0100 Subject: [PATCH] Selectable resolution --- .../opencv_videocapture.py | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/airo-camera-toolkit/airo_camera_toolkit/cameras/opencv_videocapture/opencv_videocapture.py b/airo-camera-toolkit/airo_camera_toolkit/cameras/opencv_videocapture/opencv_videocapture.py index abe7d13..7d30840 100644 --- a/airo-camera-toolkit/airo_camera_toolkit/cameras/opencv_videocapture/opencv_videocapture.py +++ b/airo-camera-toolkit/airo_camera_toolkit/cameras/opencv_videocapture/opencv_videocapture.py @@ -13,8 +13,20 @@ class OpenCVVideoCapture(RGBCamera): """Wrapper around OpenCV's VideoCapture so we can test the camera interface without external cameras.""" + # Some standard resolutions that are likely to be supported by webcams. + # 16:9 + RESOLUTION_1080 = (1920, 1080) + RESOLUTION_720 = (1280, 720) + # 4:3 + RESOLUTION_768 = (1024, 768) + RESOLUTION_480 = (640, 480) + def __init__( - self, video_capture_args: Tuple[Any] = (0,), intrinsics_matrix: Optional[CameraIntrinsicsMatrixType] = None + self, + video_capture_args: Tuple[Any] = (0,), + intrinsics_matrix: Optional[CameraIntrinsicsMatrixType] = None, + resolution: CameraResolutionType = RESOLUTION_480, + fps: int = 30, ) -> None: self.video_capture = cv2.VideoCapture(*video_capture_args) @@ -26,9 +38,15 @@ def __init__( if not self.video_capture.isOpened(): raise RuntimeError(f"Cannot open camera {video_capture_args[0]}. Is it connected?") - self.fps = self.video_capture.get(cv2.CAP_PROP_FPS) + # Note that the following will not forcibly set the resolution. If the user's webcam + # does not support the desired resolution, OpenCV will silently select a close match. + self.video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, resolution[0]) + self.video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, resolution[1]) + self.video_capture.set(cv2.CAP_PROP_FPS, fps) + self._intrinsics_matrix = intrinsics_matrix + self.fps = self.video_capture.get(cv2.CAP_PROP_FPS) self._resolution = ( math.floor(self.video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)), math.floor(self.video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)),