Control websocket with snowboy (offline voice commands recognition)
Snowboy is used as an Hotword Detection Engine, but it works pretty well with predefined voice commands (and really fast).
You will need to record the voice commands on the snowboy website, you can login with a github account, once you have downloaded the model files, it will works without a network connection.
Install snowboy on a Raspberry Pi (check documentation if you have issues)
#!/bin/bash
mkdir /opt/snowboy
cd /opt/snowboy
wget https://s3-us-west-2.amazonaws.com/snowboy/snowboy-releases/rpi-arm-raspbian-8.0-1.2.0.tar.bz2
tar -xvf rpi-arm-raspbian-8.0-1.2.0.tar.bz2
apt-get install python-pyaudio python3-pyaudio sox libatlas-base-dev
pip install pyaudio
# Generate alsa configuration for root
cat > ~/.asoundrc << EOF
pcm.!default {
type asym
playback.pcm {
type plug
slave.pcm "hw:0,0"
}
capture.pcm {
type plug
slave.pcm "hw:1,0"
}
}
EOF
rec test.wav
Python Demo with test (first model file with turn on the led, second model file will turn off the led)import snowboydecoder
import sys
import signal
import websocket
# Demo code for listening two hotwords at the same time
interrupted = False
ws = websocket.WebSocket()
ws.connect("ws://localhost:42000");
def signal_handler(signal, frame):
global interrupted
interrupted = True
def interrupt_callback():
global interrupted
return interrupted
if len(sys.argv) != 3:
print("Error: need to specify 2 model names")
print("Usage: python demo.py 1st.model 2nd.model")
sys.exit(-1)
def turn_on():
print("ON")
ws.send("ON")
def turn_off():
print("OFF")
ws.send("OFF")
models = sys.argv[1:]
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
sensitivity = [0.5]*len(models)
detector = snowboydecoder.HotwordDetector(models, sensitivity=sensitivity)
callbacks = [lambda: turn_on(),
lambda: turn_off()]
print('Listening... Press Ctrl+C to exit')
# main loop
# make sure you have the same numbers of callbacks and models
detector.start(detected_callback=callbacks,
interrupt_check=interrupt_callback,
sleep_time=0.03)
detector.terminate()
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.