Skip to content

Cameras and OpenCV

Cameras are global scene resources. Discover a camera by its mount metadata, then subscribe using its exact scene ID.

Capture an overhead camera frame

Install the optional decoder first:

pip install "sim2bot[cv2]"
from pathlib import Path

import cv2

from sim2bot import Robot


def main() -> None:
    with Robot(auto_bridge=True, wait_for_sim=True) as sim:
        overhead = next(
            (
                camera
                for camera in sim.cameras()
                if camera["mount"]["kind"] == "world"
            ),
            None,
        )
        if overhead is None:
            raise RuntimeError("The scene has no world-mounted camera")

        with sim.camera(
            overhead["id"],
            fps=20,
            width=640,
            height=480,
            quality=0.8,
        ) as stream:
            frame = stream.read(timeout=3.0)
            if frame is None:
                raise TimeoutError("No camera frame arrived")

            image = frame.image()
            if image is None:
                raise RuntimeError("The received frame could not be decoded")
            cv2.imwrite(str(Path("overhead.jpg")), image)


if __name__ == "__main__":
    main()

CameraStream.read() returns None when no newer frame arrives before the timeout. That is different from receiving an empty or undecodable image.

Relevant APIs