0%

语音唤起后-语音部署

之前做的语音部署功能是循环一直在运行,实际使用也不是特别好使,后来查资料准备做语音唤起。
语音唤起,使用开源的snowboy,

环境安装:
安装 sox 软件测试录音与播放功能:
pacman -S sox
安装完成后运行 sox -d -d 命令,对着麦克风说话,确认可以听到自己的声音
PyAudio:
pacman -S python3-pyaudio
安装 SWIG
pacman -S swig
编译源代码
获取源代码:$ git clone https://github.com/Kitt-AI/snowboy.git
编译 Python3 绑定:$ cd snowboy/swig/Python3 && make
进入官方示例目录 snowboy/examples/Python3 并运行以下命令:
$ python3 demo.py resources/models/snowboy.umdl

然后对着麦克风清晰地讲出“snowboy”,如果可以听到“滴”的声音,则安装配置成功。

PS:官方源代码使用 Python3 测试有报错,经测试需修改 snowboy/examples/Python3 目录下的 snowboydecoder.py 文件。
将第 5 行代码 from * import snowboydetect 改为 import snowboydetect 即可直接运行。

需要模型文件 snowboy.pmdl
编译好的 _snowboydetect.so 库
snowboydecoder.py、snowboydetect.py 文件以及 resources 目录
detected_callback 方法对应的是唤醒后执行的方法
最后上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import snowboydecoder
import sys
import signal
import speech_recognition as sr
import logging
import pyttsx3
import paramiko, time

logging.basicConfig(level=logging.DEBUG)
logging.getLogger("paramiko").setLevel(logging.WARNING)
engine = pyttsx3.init()
engine.setProperty('voice', 'zh')
hostname = '10.123.0.197'
port = 22
username = 'mp'
password = '/home/bxw/.ssh/id_rsa'



interrupted = False

def signal_handler(signal, frame):
global interrupted
interrupted = True

def interrupt_callback():
global interrupted
return interrupted

if len(sys.argv) == 1:
print("Error: need to specify model name")
print("Usage: python demo.py your.model")
sys.exit(-1)

def sshclient_execmd(hostname, port, username, ssh_key_path, execmd):
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
pkey = paramiko.RSAKey.from_private_key_file(ssh_key_path, password="username")
s.connect(hostname=hostname, port=port, username=username, pkey=pkey)
stdin, stdout, stderr = s.exec_command (execmd)

line = stdout.readlines()
s.close()
return line

def publishProject():
snowboydecoder.play_audio_file()
r = sr.Recognizer()
# 麦克风
mic = sr.Microphone()

print('录音中...')
with mic as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
print('录音结束,识别中...')
test = r.recognize_google(audio, language='cmn-Hans-CN', show_all=True)
#{'alternative': [{'transcript': '你好', 'confidence': 0.97500253}], 'final': True}
if len(test) >0:
result = test['alternative'][0]['transcript']
print(result)
if "部署" in result:
print(result)
if "后台" in result or "后端" in result:
engine.say("部署后台启动")
engine.runAndWait()
line=sshclient_execmd(hostname,port,username,password,"cd /backup05/mp/eladmin;sh update.sh")
print(line)
engine.say("部署完成")
engine.runAndWait()
elif "前端" in result or "前台" in result :
engine.say("部署前端启动")
engine.runAndWait()
line=sshclient_execmd(hostname,port,username,password,"cd /backup05/mp/web/monitor-web/;sh run-build.sh")
print(line)
engine.say("部署完成")
engine.runAndWait()
print('end')
model = sys.argv[1]

# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)

detector = snowboydecoder.HotwordDetector(model, sensitivity=0.4)
print('Listening... Press Ctrl+C to exit')

# main loop
detector.start(detected_callback=publishProject,
interrupt_check=interrupt_callback,
sleep_time=0.03)

detector.terminate()