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) 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]
signal.signal(signal.SIGINT, signal_handler)
detector = snowboydecoder.HotwordDetector(model, sensitivity=0.4) print('Listening... Press Ctrl+C to exit')
detector.start(detected_callback=publishProject, interrupt_check=interrupt_callback, sleep_time=0.03)
detector.terminate()
|