File size: 3,256 Bytes
f8afc81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/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: "<base64-jpeg>"}}

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=<robot-ip> --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()