Skip to content

RealSense: Capture Single Depth Image

SUMMARY

Connect to a RealSense camera, capture a single depth frame, apply a colour map for visualisation, and inspect the result in Rerun.

Example

Single depth image capture output

Code

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

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

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_Capture_Example", spawn=True)
        camera.connect()

        depth_image = camera.capture_single_depth_frame()

        rr.log(
            "Single_Depth_Image_Capture", 
            rr.DepthImage(
                depth_image,
                meter=1.0, # values are already in metres
                colormap="turbo",
                depth_range=(0.0, 3.0) # 0–3 metres
            )
        )
    except Exception as e:
        logger.error(
            f"Unable to capture image. 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 and makes the device ready to capture frames.

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

capture_single_depth_frame activates the depth stream, retrieves one frame, and returns it as a NumPy array of float values already converted to metres.

python
depth_image = camera.capture_single_depth_frame()

rr.DepthImage renders the raw depth array as a false-colour image in Rerun. meter=1.0 tells Rerun the values are already in metres so no unit conversion is applied. colormap="turbo" maps close distances to blue and far distances to red. depth_range=(0.0, 3.0) clamps the colour scale to the 0–3 metre range, so objects outside that band are clipped rather than compressing the palette.

python
rr.log(
    "Single_Depth_Image_Capture",
    rr.DepthImage(
        depth_image,
        meter=1.0,
        colormap="turbo",
        depth_range=(0.0, 3.0)
    )
)

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_depth_image_example.py

A Rerun viewer opens automatically. The depth frame appears under Single_Depth_Image_Capture with the turbo colour map applied.