#!/usr/bin/env python """View a camera stream. This viewer is designed to subscribe to a ZMQ camera server that uses the `lerobot.cameras.zmq.ZMQCamera` JSON protocol. Typical workflow (camera is on robot PC): - On robot PC: run `lerobot-stream-camera ...` - On remote PC: run `lerobot-view-camera --server-address --port 5555` If you are on a headless machine (no display), this script will still run but cannot show a window; use Rerun or save frames instead. """ import logging import os import time from dataclasses import dataclass from pprint import pformat import cv2 from lerobot.cameras.zmq import ZMQCamera, ZMQCameraConfig from lerobot.configs import parser from lerobot.utils.utils import init_logging logger = logging.getLogger(__name__) @dataclass class ViewCameraConfig: server_address: str port: int = 5555 camera_name: str = "camera" fps: float = 30.0 timeout_ms: int = 1000 @parser.wrap() def view_camera(cfg: ViewCameraConfig) -> None: init_logging() logger.info(pformat(cfg)) camera_cfg = ZMQCameraConfig( server_address=cfg.server_address, port=cfg.port, camera_name=cfg.camera_name, timeout_ms=cfg.timeout_ms, ) cam = ZMQCamera(camera_cfg) cam.connect(warmup=True) has_display = bool(os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")) gui_available = has_display if gui_available: try: cv2.namedWindow("lerobot-view-camera", cv2.WINDOW_NORMAL) cv2.destroyWindow("lerobot-view-camera") except Exception as e: gui_available = False logger.warning( "OpenCV GUI not available (likely opencv-python-headless). " "Falling back to headless mode (printing frame info). Error: %s", e, ) if not gui_available: logger.warning("Viewer running without a window; printing frame info instead.") try: while True: t0 = time.time() frame = cam.read() # BGR if not gui_available: h, w = frame.shape[:2] logger.info(f"Frame: {w}x{h}") else: cv2.imshow(f"ZMQCamera {cfg.camera_name}@{cfg.server_address}:{cfg.port}", frame) if cv2.waitKey(1) & 0xFF == ord("q"): break sleep = (1.0 / cfg.fps) - (time.time() - t0) if sleep > 0: time.sleep(sleep) except KeyboardInterrupt: pass finally: cam.disconnect() if gui_available: cv2.destroyAllWindows() def main() -> None: view_camera() if __name__ == "__main__": main()