Skip to content

RealSense: Stream Live RGB Video

SUMMARY

Connect to a RealSense camera, stream continuous colour frames for 10 seconds, and visualise the live feed with Rerun.

Example

Code

python
"""
A simple example demonstrating how to connect to a RealSense camera, stream a
video and disconnect from the camera when finished.

Please run this example from a terminal to avoid issues with rerun's spawn mode.
"""

import time
from loguru import logger

import rerun as rr

from telekinesis.medulla.cameras import realsense


def main():
    camera = realsense.RealSense(
        name="my_realsense",
    )
    try:
        rr.init("RealSense_Video_Example", spawn=True)
        camera.connect()

        end_time = time.time() + 10
        while time.time() < end_time:
            image = camera.capture_video_color_frame()
            if image is None:
                logger.warning("Image is none")
                continue
            rr.log("Continuous_Image_Capture", rr.Image(image))
    except Exception as e:
        logger.error(
            f"Unable to stream video. Caught exception: {type(e).__name__}: {e}"
        )
    finally:
        camera.disconnect()


if __name__ == "__main__":
    main()

Explanation

Now, let's break down the code piece by piece.

A RealSense camera object is created with just a name. No serial number is required — the class connects to the first available device automatically.

python
camera = realsense.RealSense(
    name="my_realsense",
)

A Rerun viewer is opened with rr.init(..., spawn=True), then camera.connect() initializes the RealSense pipeline with default stream settings and makes the device ready to capture frames.

python
rr.init("RealSense_Video_Example", spawn=True)
camera.connect()

The streaming loop runs for 10 seconds using a wall-clock deadline rather than a fixed frame counter. Each call to capture_video_color_frame keeps the colour stream running continuously between calls and returns one frame as a NumPy array, or None if no frame is ready yet. The None check logs a warning and skips to the next iteration rather than passing invalid data to Rerun.

python
end_time = time.time() + 10
while time.time() < end_time:
    image = camera.capture_video_color_frame()
    if image is None:
        logger.warning("Image is none")
        continue
    rr.log("Continuous_Image_Capture", rr.Image(image))

The finally block guarantees that camera.disconnect() is always called, stopping the RealSense pipeline and releasing all USB resources regardless of whether an exception occurred.

python
finally:
    camera.disconnect()

Run

bash
python capture_rgb_video_example.py

A Rerun viewer opens automatically. Frames stream under Continuous_Image_Capture for 10 seconds, then the camera disconnects cleanly.