Python Snippets

Set speaker volume

import navel

with navel.Robot() as robot:
    print(f"Previous volume: {robot.volume}%")
    robot.volume += 3
    print(f"New volume: {robot.volume}%")

Stream audio to Google ASR

In your virtual environment, pip install pyaudio.

Follow examples on Github.

You can configure what microphone to use by adding an input_device_index in self._audio_interface.open().

To list the available devices with indices, use the following code.

import pyaudio
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
    print(p.get_device_info_by_index(i))

Disable automatic facial expressions

Facial expressions are in auto-mode by default, which weakens your manual control. You can disable it until restart with the following code.

import navel

with navel.Robot() as robot:
    robot.config_set("cns_fer_cont_t1", 0, navel.DataType.U32)
    robot.config_set("cns_fer_cont_t2", 0, navel.DataType.U32)
    robot.config_set("cns_fer_peak_t1", 0, navel.DataType.U32)
    robot.config_set("cns_fer_peak_t2", 0, navel.DataType.U32)
    robot.config_set("cns_fer_overlay_inc", 0, navel.DataType.F32)
    robot.config_set("cns_fer_overlay_max", 0, navel.DataType.F32)
    robot.config_set("cns_fer_overlay_peak_min", 0, navel.DataType.F32)
    robot.config_set("cns_fer_peak_max", 0, navel.DataType.F32)

Read head camera frame

Use HeadCamera to fetch a single RGB frame on demand. Frames are returned as numpy.ndarray in HWC format (height x width x 3) with uint8 values. The frame timestamp is in microseconds.

import os
import time
from datetime import datetime

from navel import HeadCamera


SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
OUT_DIR = os.path.join(SCRIPT_DIR, "head_frames")

def _write_ppm(path, data, width, height):
    header = f"P6\n{width} {height}\n255\n".encode("ascii")
    with open(path, "wb") as f:
        f.write(header)
        f.write(data.tobytes())


if __name__ == "__main__":
    os.makedirs(OUT_DIR, exist_ok=True)
    try:
        with HeadCamera() as cam:
            while True:
                frame = cam.get_frame(timeout_ms=500)
                ts = datetime.fromtimestamp(
                    frame.timestamp_us / 1_000_000
                )
                stamp = ts.strftime("%Y%m%d_%H%M%S")
                path = os.path.join(
                    OUT_DIR, f"head_{stamp}.ppm"
                )
                _write_ppm(path, frame.data, frame.width, frame.height)
                print(f"wrote {path}")
                time.sleep(1.0)
    except KeyboardInterrupt:
        pass