Here we describe how to set up and use a camera with LeRobot. We support different ways of capturing videos in LeRobot, such as using a phone camera, an integrated laptop camera, an external webcam, or an Intel realsense camera.
There are three ways to connect and use a camera with LeRobot:
In LeRobot, you can efficiently record frames from most cameras using either the OpenCVCamera class or the RealSenseCamera class. For more details on compatibility for the OpenCVCamera class, see Video I/O with OpenCV Overview.
To instantiate a camera, you need a camera index. When you only have one camera like a webcam of a laptop, the camera index is usually 0 but it might differ, and the camera index might change if you reboot your computer or re-plug your camera. This behavior depends on your operating system.
To find the camera indices, run the following script:
python lerobot/find_cameras.py list-cameras
The output will look something like this if you have two cameras connected:
--- Detected Cameras ---
Camera #0:
Name: OpenCV Camera @ 0
Type: OpenCV
Id: 0
Backend api: AVFOUNDATION
Default stream profile:
Format: 16.0
Width: 1920
Height: 1080
Fps: 15.0
--------------------
Camera #1:
Name: OpenCV Camera @ 1
Type: OpenCV
Id: 1
Backend api: AVFOUNDATION
Default stream profile:
Format: 16.0
Width: 1920
Height: 1080
Fps: 1.0
--------------------On , you could get this error: Error finding RealSense cameras: failed to set power state, this can be solved by running the same command with sudo permissions.
To use your iPhone as a camera on macOS, enable the Continuity Camera feature:
For more details, visit Apple support.
Your iPhone should be detected automatically when running the camera setup script in the next section.
Below are two examples, demonstrating how to work with the API.
This snippet shows how to:
OpenCVCameraConfig with your desired FPS, resolution, color mode, and rotation.OpenCVCamera, performing a warm-up read.async_read(timeout_ms).from lerobot.common.cameras.opencv.configuration_opencv import OpenCVCameraConfig
from lerobot.common.cameras.opencv.camera_opencv import OpenCVCamera
from lerobot.common.cameras.configs import ColorMode, Cv2Rotation
config = OpenCVCameraConfig(
index_or_path=0,
fps=15,
width=1920,
height=1080,
color_mode=ColorMode.RGB,
rotation=Cv2Rotation.NO_ROTATION
)
camera = OpenCVCamera(config)
camera.connect(do_warmup_read=True)
try:
for i in range(10):
frame = camera.async_read(timeout_ms=1000)
print(f"Async frame {i} shape:", frame.shape)
finally:
camera.disconnect()This snippet shows how to:
RealSenseCameraConfig specifying your camera’s serial number and enabling depth.RealSenseCamera with warm-up.read() and a depth map via read_depth().from lerobot.common.cameras.intel.configuration_realsense import RealSenseCameraConfig
from lerobot.common.cameras.intel.camera_realsense import RealSenseCamera
from lerobot.common.cameras.configs import ColorMode, Cv2Rotation
config = RealSenseCameraConfig(
serial_number="233522074606",
fps=15,
width=640,
height=480,
color_mode=ColorMode.RGB,
use_depth=True,
rotation=Cv2Rotation.NO_ROTATION
)
camera = RealSenseCamera(config)
camera.connect(do_warmup_read=True)
try:
color_frame = camera.read()
depth_map = camera.read_depth()
print("Color frame shape:", color_frame.shape)
print("Depth map shape:", depth_map.shape)
finally:
camera.disconnect()