1.配置声卡
在我们开始在Raspberry Pi上设置Snowboy Hotword检测之前,我们必须首先确保正确配置音频。
- 1.使用以下命令找到USB麦克风。记下卡号和设备号
arecord -l
- 2.查看树莓派板载声卡(树莓派只支持播放,不支持录音)
注意:Raspberry Pi的3.5mm插孔通常标记为模拟或bcm2835 ALSA,HDMI输出标识为bcm2835 IEC958 / HDMI
aplay -l
- 3.我们需要配置音频驱动程序.asoundrc文件,再pi的目录下
nano /home/pi/.asoundrc
添加一下内容:
pcm.!default {
type asym
capture.pcm "mic"
playback.pcm "speaker"
}
pcm.mic {
type plug
slave {
pcm "hw:1,0"
}
}
pcm.speaker {
type plug
slave {
pcm "hw:0,0"
}
}
- 4.安装sox和swig
sudo apt-get install sox swig
- 安装依赖项,大部分是为了添加pyaudio包
sudo apt-get install python-pyaudio libatlas-base-dev portaudio19-dev
pip install pyaudio
- 安装Python库,为了上传录音生成唤醒模型。
sudo pip install requests
- 获取别人已经编译后的文件,只支持python2 。如果想使用python3需要获取源程序,重新编译!
wget -O snowboy.tar.bz2 https://go.pimylifeup.com/napoRs/snowboy
tar xvjf snowboy.tar.bz2
tar xvjf snowboy.tar.bz2
2.获取Snowboy API密钥
首先转到Snowboy网站,然后单击 位于[页面] (https://snowboy.kitt.ai/)右上角的登录按钮,复制如下所示API令牌!
3.制作唤醒词
- 创建python文件
nano /home/pi/training_service.py
- 添加下面代码:请将ENTER_TOKEN替换为您在上一段中检索到的API令牌,并将ENTER_HOTWORD替换为您要记录的热门词,例如“ Pi My Life Up ”
import sys
import base64
import requests
def get_wave(fname):
with open(fname) as infile:
return base64.b64encode(infile.read())
endpoint = "https://snowboy.kitt.ai/api/v1/train/"
token = "ENTER_TOKEN"
hotword_name = "ENTER_HOTWORD"
language = "en"
age_group = "20_29"
gender = "M"
microphone = "usb microphone"
if __name__ == "__main__":
try:
[_, wav1, wav2, wav3, out] = sys.argv
except ValueError:
print "Usage: %s wave_file1 wave_file2 wave_file3 out_model_name" % sys.argv[0]
sys.exit()
data = {
"name": hotword_name,
"language": language,
"age_group": age_group,
"gender": gender,
"microphone": microphone,
"token": token,
"voice_samples": [
{"wave": get_wave(wav1)},
{"wave": get_wave(wav2)},
{"wave": get_wave(wav3)}
]
}
response = requests.post(endpoint, json=data)
if response.ok:
with open(out, "w") as outfile:
outfile.write(response.content)
print "Saved model to '%s'." % out
else:
print "Request failed."
print response.text
- 3 单独运行下面三个命令来语音,执行后说出你的唤醒词,录制三个
arecord --format=S16_LE --duration=5 --rate=16000 --file-type=wav 1.wav
arecord --format=S16_LE --duration=5 --rate=16000 --file-type=wav 2.wav
arecord --format=S16_LE --duration=5 --rate=16000 --file-type=wav 3.wav
- 4 执行python文件,上传唤醒词的录音,生产唤醒词模型为“saved_model”。
python2 training_service.py 1.wav 2.wav 3.wav saved_model.pmdl
4.测试程序
- 1.复制唤醒词模型到snowboy文件下,并进入
cp saved_model.pmdl ./snowboy/saved_model.pmdl
cd snowboy
- 2.执行测试文件
python2 demo.py saved_model.pmdl
- 启动成功后终端将会显示:
Listening... Press Ctrl+C to exit
- 唤醒成功后终端将会显示:
INFO:snowboy:Keyword 1 detected at time: 2019-07-04 05:04:15