Skip to content

Commit

Permalink
Selectable resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
m-decoster committed Feb 14, 2024
1 parent b27c304 commit 976fc38
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)),
Expand Down

0 comments on commit 976fc38

Please sign in to comment.