Buckets:

hf-doc-build/doc-dev / reachy_mini /pr_1069 /en /examples /custom_media_manager.html
download
raw
17.2 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Custom Media Manager&quot;,&quot;local&quot;:&quot;custom-media-manager&quot;,&quot;sections&quot;:[],&quot;depth&quot;:1}">
<link href="/docs/reachy_mini/pr_1069/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/entry/start.6cd5df39.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/scheduler.f05771bc.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/singletons.8ab55633.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/index.2528d289.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/paths.a8f2e6ae.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/entry/app.405911a4.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/preload-helper.e957381b.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/index.b379be46.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/nodes/0.b229c383.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/nodes/20.f5b58a2c.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.c489a506.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/CodeBlock.f56fd9c8.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Custom Media Manager&quot;,&quot;local&quot;:&quot;custom-media-manager&quot;,&quot;sections&quot;:[],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="custom-media-manager" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#custom-media-manager"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Custom Media Manager</span></h1> <p data-svelte-h="svelte-bazaap">This example demonstrates how to deactivate the built-in media manager and access the camera and microphone directly using OpenCV and sounddevice.</p> <p data-svelte-h="svelte-12kd74v"><strong>Why?</strong> The daemon normally owns the camera and audio hardware. If you need raw access (e.g. custom OpenCV pipelines, sounddevice recording, or a third-party vision library), you must first tell the daemon to release the hardware. See <a href="../SDK/media-architecture#disabling-media--direct-hardware-access">Media Architecture - Disabling Media</a> for details.</p> <p data-svelte-h="svelte-1k5i1so"><strong>How it works:</strong></p> <ol data-svelte-h="svelte-18gqvpz"><li>Connects with <code>media_backend=&quot;no_media&quot;</code> — this automatically tells the daemon to release camera and audio hardware</li> <li>Uses OpenCV to capture a frame directly from the camera</li> <li>Uses sounddevice to record audio from the microphone</li> <li>On exit, the daemon automatically re-acquires the hardware</li></ol> <blockquote data-svelte-h="svelte-1xfobgu"><p><strong>💡 Tip:</strong> Robot control (head, antennas, body) keeps working normally while media is released. Only camera and audio are affected.</p></blockquote> <p data-svelte-h="svelte-11lzvnb"><strong>Requirements:</strong></p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-bash "><!-- HTML_TAG_START -->pip install opencv-python sounddevice soundfile<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-qbjyqk"><strong>Usage:</strong></p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-bash "><!-- HTML_TAG_START -->python custom_media_manager.py<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START -->
<span class="hljs-keyword">import</span> sys
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">try</span>:
<span class="hljs-keyword">import</span> cv2
<span class="hljs-keyword">except</span> ImportError:
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Error: OpenCV is required. Install with: pip install opencv-python&quot;</span>)
sys.exit(<span class="hljs-number">1</span>)
<span class="hljs-keyword">try</span>:
<span class="hljs-keyword">import</span> sounddevice <span class="hljs-keyword">as</span> sd
<span class="hljs-keyword">import</span> soundfile <span class="hljs-keyword">as</span> sf
<span class="hljs-keyword">except</span> ImportError:
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Error: sounddevice + soundfile are required.&quot;</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Install with: pip install sounddevice soundfile&quot;</span>)
sys.exit(<span class="hljs-number">1</span>)
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> reachy_mini <span class="hljs-keyword">import</span> ReachyMini
<span class="hljs-keyword">def</span> <span class="hljs-title function_">main</span>() -&gt; <span class="hljs-literal">None</span>:
<span class="hljs-string">&quot;&quot;&quot;Capture a frame with OpenCV and record audio with sounddevice.&quot;&quot;&quot;</span>
<span class="hljs-comment"># media_backend=&quot;no_media&quot; automatically releases daemon media hardware</span>
<span class="hljs-keyword">with</span> ReachyMini(media_backend=<span class="hljs-string">&quot;no_media&quot;</span>) <span class="hljs-keyword">as</span> mini:
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Connected. media_released=<span class="hljs-subst">{mini.media_released}</span>&quot;</span>)
<span class="hljs-comment"># --- OpenCV camera capture ---</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;\nOpening camera with OpenCV...&quot;</span>)
cap = cv2.VideoCapture(<span class="hljs-number">0</span>)
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> cap.isOpened():
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Could not open camera — is it plugged in?&quot;</span>)
<span class="hljs-keyword">else</span>:
<span class="hljs-comment"># Let the camera auto-adjust</span>
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>):
cap.read()
ret, frame = cap.read()
<span class="hljs-keyword">if</span> ret:
cv2.imwrite(<span class="hljs-string">&quot;release_media_frame.jpg&quot;</span>, frame)
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Saved frame: release_media_frame.jpg (<span class="hljs-subst">{frame.shape}</span>)&quot;</span>)
<span class="hljs-keyword">else</span>:
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Failed to capture frame.&quot;</span>)
cap.release()
<span class="hljs-comment"># --- sounddevice audio recording ---</span>
duration = <span class="hljs-number">2.0</span> <span class="hljs-comment"># seconds</span>
samplerate = <span class="hljs-number">44100</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;\nRecording <span class="hljs-subst">{duration}</span>s of audio at <span class="hljs-subst">{samplerate}</span> Hz...&quot;</span>)
<span class="hljs-keyword">try</span>:
audio = sd.rec(
<span class="hljs-built_in">int</span>(duration * samplerate),
samplerate=samplerate,
channels=<span class="hljs-number">1</span>,
dtype=np.int16,
)
sd.wait()
sf.write(<span class="hljs-string">&quot;release_media_audio.wav&quot;</span>, audio, samplerate)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Saved audio: release_media_audio.wav&quot;</span>)
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Audio recording failed: <span class="hljs-subst">{e}</span>&quot;</span>)
<span class="hljs-comment"># The robot is still controllable while media is released</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;\nWiggling antennas to prove robot control still works...&quot;</span>)
mini.goto_target(antennas=[<span class="hljs-number">0.3</span>, -<span class="hljs-number">0.3</span>], duration=<span class="hljs-number">0.5</span>)
time.sleep(<span class="hljs-number">0.2</span>)
mini.goto_target(antennas=[<span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>], duration=<span class="hljs-number">0.5</span>)
<span class="hljs-comment"># __exit__ automatically calls acquire_media() → daemon reclaims hardware</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;\nDone. Daemon media re-acquired on exit.&quot;</span>)
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
main()<!-- HTML_TAG_END --></pre></div> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/pollen-robotics/reachy_mini/blob/main/docs/source/examples/custom_media_manager.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_14cjvgd = {
assets: "/docs/reachy_mini/pr_1069/en",
base: "/docs/reachy_mini/pr_1069/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/reachy_mini/pr_1069/en/_app/immutable/entry/start.6cd5df39.js"),
import("/docs/reachy_mini/pr_1069/en/_app/immutable/entry/app.405911a4.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 20],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
17.2 kB
·
Xet hash:
fdd9471d41e56d357de25be2e67d1ebec7fb5ea3ad92660ffd5196c36299d7f0

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.