Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/source/cameras.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ Every camera requires a unique identifier to be instantiated, allowing you to di
lerobot-find-cameras opencv # or realsense for Intel Realsense cameras
```

The output will look something like this if you have two cameras connected:
To view a **live preview** of each camera in separate windows:

```bash
lerobot-preview-cameras # preview all detected OpenCV cameras
lerobot-preview-cameras 0 1 2 # preview cameras at indices 0, 1, 2
```

Press `q` in any preview window to quit.

The output of `lerobot-find-cameras` will look something like this if you have two cameras connected:

```bash
--- Detected Cameras ---
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ intelrealsense = [
"pyrealsense2>=2.55.1.6486,<2.57.0 ; sys_platform != 'darwin'",
"pyrealsense2-macosx>=2.54,<2.55.0 ; sys_platform == 'darwin'",
]
realsense-mesh = ["lerobot[intelrealsense]", "open3d>=0.18.0,<0.20.0"]
phone = ["hebi-py>=2.8.0,<2.12.0", "teleop>=0.1.0,<0.2.0", "fastapi<1.0"]

# Policies
Expand Down Expand Up @@ -198,6 +199,8 @@ all = [
[project.scripts]
lerobot-calibrate="lerobot.scripts.lerobot_calibrate:main"
lerobot-find-cameras="lerobot.scripts.lerobot_find_cameras:main"
lerobot-preview-cameras="lerobot.scripts.lerobot_preview_cameras:main"
lerobot-realsense-mesh="lerobot.scripts.lerobot_realsense_mesh:main"
lerobot-find-port="lerobot.scripts.lerobot_find_port:main"
lerobot-record="lerobot.scripts.lerobot_record:main"
lerobot-replay="lerobot.scripts.lerobot_replay:main"
Expand Down
109 changes: 109 additions & 0 deletions src/lerobot/scripts/lerobot_preview_cameras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env python

# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Live preview of camera feeds using OpenCV windows.

Example:

```shell
# Preview all detected OpenCV cameras
lerobot-preview-cameras

# Preview specific camera indices
lerobot-preview-cameras 0 1 2
```
"""

import argparse
import sys

import cv2

from lerobot.cameras.configs import ColorMode
from lerobot.cameras.opencv.camera_opencv import OpenCVCamera
from lerobot.cameras.opencv.configuration_opencv import OpenCVCameraConfig


def preview_cameras(indices_or_paths: list[int | str]) -> None:
"""Connect to each camera index/path and show live feed in separate windows."""
cameras: list[OpenCVCamera] = []
window_names: list[str] = []

try:
for idx in indices_or_paths:
config = OpenCVCameraConfig(
index_or_path=idx,
color_mode=ColorMode.RGB,
)
cam = OpenCVCamera(config)
cam.connect(warmup=True)
cameras.append(cam)
name = f"Camera {idx}"
window_names.append(name)
cv2.namedWindow(name, cv2.WINDOW_NORMAL)

print("Showing camera preview. Press 'q' in any window to quit.")
while True:
for i, (cam, name) in enumerate(zip(cameras, window_names)):
frame = cam.read()
if frame is None:
continue
# OpenCV imshow expects BGR
bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
cv2.imshow(name, bgr)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
finally:
for name in window_names:
cv2.destroyWindow(name)
for cam in cameras:
if cam.is_connected:
cam.disconnect()


def main() -> None:
parser = argparse.ArgumentParser(
description="Live preview of OpenCV camera feeds in separate windows.",
)
parser.add_argument(
"indices",
type=int,
nargs="*",
help="Camera indices to preview (e.g. 0 1 2). If omitted, discovers and previews all OpenCV cameras.",
)
args = parser.parse_args()

if args.indices:
indices = args.indices
else:
# Discover all OpenCV cameras
try:
found = OpenCVCamera.find_cameras()
except Exception as e:
print(f"Error discovering cameras: {e}", file=sys.stderr)
sys.exit(1)
if not found:
print("No OpenCV cameras detected. Try: lerobot-find-cameras opencv", file=sys.stderr)
sys.exit(1)
indices = [c["id"] for c in found]
print(f"Previewing cameras: {indices}")

preview_cameras(indices)


if __name__ == "__main__":
main()