Reachy Mini documentation

カスタムメディアマネージャー

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

カスタムメディアマネージャー

この例では、組み込みのメディアマネージャーを無効にし、OpenCVとsounddeviceを使用してカメラとマイクに直接アクセスする方法を示しています。

なぜですか? デーモンは通常、カメラとオーディオのハードウェアを所有しています。生のアクセス(例:カスタムのOpenCVパイプライン、sounddeviceによる録音、サードパーティのビジョンライブラリなど)が必要な場合は、まずデーモンにハードウェアの解放を指示する必要があります。詳細はMedia Architecture - Disabling Mediaを参照してください。

仕組み:

  1. media_backend="no_media" と接続します。これにより、デーモンにカメラやオーディオハードウェアを解放するよう自動的に指示されます。
  2. OpenCVを使用して、カメラから直接フレームをキャプチャします
  3. sounddeviceを使用して、マイクから音声を録音します
  4. 終了時、デーモンは自動的にハードウェアを再取得します

💡 ヒント: メディアが解放されている間も、ロボットの制御(ヘッド、アンテナ、ボディ)は通常通り動作します。影響を受けるのはカメラとオーディオのみです。

要件:

pip install opencv-python sounddevice soundfile

使用方法:

python custom_media_manager.py

import sys
import time

try:
    import cv2
except ImportError:
    print("Error: OpenCV is required. Install with: pip install opencv-python")
    sys.exit(1)

try:
    import sounddevice as sd
    import soundfile as sf
except ImportError:
    print("Error: sounddevice + soundfile are required.")
    print("Install with: pip install sounddevice soundfile")
    sys.exit(1)

import numpy as np

from reachy_mini import ReachyMini


def main() -> None:
    """Capture a frame with OpenCV and record audio with sounddevice."""
    # media_backend="no_media" automatically releases daemon media hardware
    with ReachyMini(media_backend="no_media") as mini:
        print(f"Connected. media_released={mini.media_released}")

        # --- OpenCV camera capture ---
        print("\nOpening camera with OpenCV...")
        cap = cv2.VideoCapture(0)
        if not cap.isOpened():
            print("Could not open camera — is it plugged in?")
        else:
            # Let the camera auto-adjust
            for _ in range(10):
                cap.read()

            ret, frame = cap.read()
            if ret:
                cv2.imwrite("release_media_frame.jpg", frame)
                print(f"Saved frame: release_media_frame.jpg ({frame.shape})")
            else:
                print("Failed to capture frame.")
            cap.release()

        # --- sounddevice audio recording ---
        duration = 2.0  # seconds
        samplerate = 44100
        print(f"\nRecording {duration}s of audio at {samplerate} Hz...")
        try:
            audio = sd.rec(
                int(duration * samplerate),
                samplerate=samplerate,
                channels=1,
                dtype=np.int16,
            )
            sd.wait()
            sf.write("release_media_audio.wav", audio, samplerate)
            print("Saved audio: release_media_audio.wav")
        except Exception as e:
            print(f"Audio recording failed: {e}")

        # The robot is still controllable while media is released
        print("\nWiggling antennas to prove robot control still works...")
        mini.goto_target(antennas=[0.3, -0.3], duration=0.5)
        time.sleep(0.2)
        mini.goto_target(antennas=[0.0, 0.0], duration=0.5)

    # __exit__ automatically calls acquire_media() → daemon reclaims hardware
    print("\nDone. Daemon media re-acquired on exit.")


if __name__ == "__main__":
    main()
Update on GitHub