#!/usr/bin/env python """Stream a single camera over ZMQ. This is meant for the common "robot PC has the camera" setup: - Run this script on the robot PC (near the camera). - Run `lerobot-view-camera` on your remote PC to view the feed. Protocol matches `lerobot.cameras.zmq.ZMQCamera`: {"timestamps": {name: float}, "images": {name: ""}} Examples -------- 1) Find your RealSense serial on the robot PC: lerobot-find-cameras realsense 2) Stream RealSense from robot PC: lerobot-stream-camera --camera.type=intelrealsense --camera.serial_number_or_name=0123456789 --port=5555 3) View from remote PC: lerobot-view-camera --server-address= --port=5555 --camera-name=camera Notes ----- - This tool streams *color* images (RGB) as JPEG. - If you need depth, you can extend the protocol, but ZMQCamera currently expects only RGB JPEG. """ import logging from dataclasses import dataclass, field from pprint import pformat from lerobot.cameras.zmq.image_server import ImageServer from lerobot.configs import parser from lerobot.utils.utils import init_logging logger = logging.getLogger(__name__) @dataclass class CameraConfig: """Single camera configuration.""" type: str = "intelrealsense" """Camera type: 'intelrealsense' or 'opencv'.""" # Intel RealSense serial_number_or_name: str | None = None # OpenCV index_or_path: int | str | None = None # Common fps: int | None = None width: int | None = None height: int | None = None warmup_s: int = 1 @dataclass class StreamCameraConfig: port: int = 5555 fps: int = 30 camera_name: str = "camera" camera: CameraConfig = field(default_factory=CameraConfig) @parser.wrap() def stream_camera(cfg: StreamCameraConfig) -> None: init_logging() logger.info(pformat(cfg)) cam_type = cfg.camera.type.lower() camera_dict: dict = {"type": cam_type, "fps": cfg.camera.fps or cfg.fps} if cam_type in {"intelrealsense", "realsense"}: if not cfg.camera.serial_number_or_name: raise ValueError( "Missing --camera.serial_number_or_name. Run `lerobot-find-cameras realsense` on the robot PC." ) camera_dict.update( { "serial_number_or_name": cfg.camera.serial_number_or_name, "width": cfg.camera.width, "height": cfg.camera.height, "warmup_s": cfg.camera.warmup_s, } ) elif cam_type == "opencv": if cfg.camera.index_or_path is None: raise ValueError("Missing --camera.index_or_path (e.g. 0 or /dev/video0)") camera_dict.update( { "index_or_path": cfg.camera.index_or_path, "width": cfg.camera.width or 640, "height": cfg.camera.height or 480, "warmup_s": cfg.camera.warmup_s, } ) else: raise ValueError("Unsupported --camera.type. Use 'intelrealsense' or 'opencv'.") config = {"fps": cfg.fps, "cameras": {cfg.camera_name: camera_dict}} server = ImageServer(config=config, port=cfg.port) server.run() def main() -> None: stream_camera() if __name__ == "__main__": main()