LeRobot 文件

相機

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

攝影機

LeRobot 為影片擷取提供了多種選擇

類別 支援的攝影機
OpenCVCamera 手機、筆記型電腦內建相機、USB 網路攝影機
ZMQCamera 網路連接的攝影機
RealSenseCamera Intel RealSense(具備深度感測)
Reachy2Camera Reachy 2 機器人專用攝影機

有關 OpenCVCamera 的相容性詳細資訊,請參閱 OpenCV 影片 I/O 總覽

尋找您的攝影機

每部攝影機都需要一個唯一的識別碼來進行實例化,讓您能區分多個已連接的裝置。

OpenCVCameraRealSenseCamera 支援自動偵測。執行下方指令以列出可用裝置及其識別碼。請注意,這些識別碼可能會根據您的作業系統,在重啟電腦或重新插拔攝影機後發生變更。

lerobot-find-cameras opencv # or realsense for Intel Realsense cameras

若您已連接兩部攝影機,輸出看起來會像這樣

--- 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
--------------------
(more cameras ...)

macOS 上使用 Intel RealSense 攝影機時,您可能會遇到此 錯誤Error finding RealSense cameras: failed to set power state,這可以透過以 sudo 權限執行相同指令來解決。請注意,在 macOS 上使用 RealSense 攝影機並不穩定。

ZMQCameraReachy2Camera 不支援自動偵測。它們必須手動設定,並提供網路位址、連接埠或機器人 SDK 設定。

使用攝影機

影格存取模式

所有攝影機類別皆實作了三種用來擷取影格的存取模式

方法 行為 是否阻塞? 最適合用途
read() 等待攝影機硬體傳回影格。根據攝影機和 SDK 的不同,可能會阻塞較長的時間。 簡單腳本、順序擷取
async_read(timeout_ms) 從背景執行緒傳回最新未消耗的影格。僅在緩衝區為空時阻塞,最長阻塞時間為 timeout_ms。若無影格傳入,則拋出 TimeoutError 帶有逾時設定 與攝影機 FPS 同步的控制迴圈
read_latest(max_age_ms) 查看緩衝區中最新的影格(可能已過期)。若影格超過 max_age_ms 則拋出 TimeoutError UI 視覺化、記錄、監控

使用範例

下列範例展示如何使用攝影機 API 來設定及從不同類型的攝影機擷取影格。

  • 使用基於 OpenCV 的攝影機進行阻塞與非阻塞影格擷取
  • 使用 Intel RealSense 攝影機進行彩色與深度影像擷取

未能乾淨地中斷連接攝影機可能會導致資源洩漏。請使用內容管理員 (context manager) 協定以確保自動清理

with OpenCVCamera(config) as camera:
    ...

您也可以手動呼叫 connect()disconnect(),但請務必為後者使用 finally 區塊。

Open CV 攝影機
Intel Realsense 攝影機
from lerobot.cameras.opencv.configuration_opencv import OpenCVCameraConfig
from lerobot.cameras.opencv.camera_opencv import OpenCVCamera
from lerobot.cameras.configs import ColorMode, Cv2Rotation

# Construct an `OpenCVCameraConfig` with your desired FPS, resolution, color mode, and rotation.
config = OpenCVCameraConfig(
    index_or_path=0,
    fps=15,
    width=1920,
    height=1080,
    color_mode=ColorMode.RGB,
    rotation=Cv2Rotation.NO_ROTATION
)

# Instantiate and connect an `OpenCVCamera`, performing a warm-up read (default).
with OpenCVCamera(config) as camera:

    # Read a frame synchronously — blocks until hardware delivers a new frame
    frame = camera.read()
    print(f"read() call returned frame with shape:", frame.shape)

    # Read a frame asynchronously with a timeout — returns the latest unconsumed frame or waits up to timeout_ms for a new one
    try:
        for i in range(10):
            frame = camera.async_read(timeout_ms=200)
            print(f"async_read call returned frame {i} with shape:", frame.shape)
    except TimeoutError as e:
        print(f"No frame received within timeout: {e}")

    # Instantly return a frame - returns the most recent frame captured by the camera
    try:
        initial_frame = camera.read_latest(max_age_ms=1000)
        for i in range(10):
            frame = camera.read_latest(max_age_ms=1000)
            print(f"read_latest call returned frame {i} with shape:", frame.shape)
            print(f"Was a new frame received by the camera? {not (initial_frame == frame).any()}")
    except TimeoutError as e:
        print(f"Frame too old: {e}")

使用您的手機攝影機

iPhone 與 macOS
OBS 虛擬攝影機

若要在 macOS 上將 iPhone 用作攝影機,請啟用「接續互通相機」功能

  • 確保您的 Mac 執行 macOS 13 或更新版本,且 iPhone 執行 iOS 16 或更新版本。
  • 兩個裝置均使用相同的 Apple ID 登入。
  • 使用 USB 連接線連接您的裝置,或開啟 Wi-Fi 和藍牙以進行無線連接。

如需更多詳細資訊,請造訪 Apple 支援

如果一切設定正確,您的手機將會顯示為標準的 OpenCV 攝影機,並可與 OpenCVCamera 一起使用。

在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.