'The voice assistant executes the command only 1 time, how to fix it?
The fact is that it works, but only once you can say something to it, then it freezes, does not stop, but simply freezes. And it is necessary that after the execution of the first request, he continued to listen and execute commands, how to do this? This is not the whole code, but it seems to be the only one related to this. There is also such that, at the moment when I recognize the type of command and then the actions that should be performed for it go, it freezes only if there is a speak ('') command, and if print or something else, then the software does not freeze and works as it should
def speak(what):
print(what)
speak_engine.say(what)
speak_engine.runAndWait()
speak_engine.stop()
def callback(recognizer, audio):
try:
voice = recognizer.recognize_google(audio, language = 'ru-RU').lower()
print('[log] Распознано: ' + voice)
if voice.startswith (opts['alias']): #Обращение к помощнику
cmd=voice
for x in opts['alias']:
cmd=cmd.replace(x,'').strip()
for x in opts['tbr']: #Убираем вводное слово
cmd=cmd.replace(x,'').strip()
#распознаем и выполняем команду
cmd = recognize_cmd(cmd)
execute_cmd(cmd['cmd'])
except sr.UnknownValueError:
print('[log] Голос не распознан ')
except sr.RequestError:
print('[log] Неизвестная ошибка с отправкой запроса')
def recognize_cmd(cmd):
RC = {'cmd': '', 'percent': 0}
for c, v in opts['cmds'].items():
for x in v:
vrt = fuzz.ratio(cmd,x)
if vrt > RC['percent']:
RC['cmd'] = c
RC['percent'] = vrt
return RC
def execute_cmd(cmd): # Определяет вид команды
if cmd == 'ctime': #Текущее время
now = datetime.datetime.now()
speak('Сейчас ' + str(now.hour)+':' + str(now.minute))
elif cmd == 'telega':
os.startfile(r'C://Users//......')
speak('Открыл сэр')
elif cmd == 'osadki':
rain="w.rain"
if rain=="{}":
speak ("Сэр, не беспокойтесь, дождя нет")
else:
speak("Сейчас идет дождь сэр")
elif cmd == 'pogoda':
owm = pyowm.OWM('........')
owm.config["language"] = "ru"
observation = owm.weather_manager().weather_at_place("Саранск")
w = observation.weather
temp = w.temperature('celsius')["temp"]
speak('Сейчас '+str(temp) + ' по цельсию сэр')
elif cmd == 'brauzer':
os.startfile(r'C://Users//.......')
speak('Открыл сэр')
else:
speak('Я не смог распознать команду, повторите еще раз')
print('Я не смог распознать команду, повторите еще раз')
r=sr.Recognizer()
m=sr.Microphone(device_index=1)
with m as source:
r.adjust_for_ambient_noise(source)
speak_engine=pyttsx3.init('sapi5')
#Только, когда используется другой голос для синтеза речи
voices=speak_engine.getProperty('voices')
speak_engine.setProperty('voice', voices[5].id)
speak('Добрый день сэр' + ', я вас слушаю')
stop_listening=r.listen_in_background(m, callback)
while True: time.sleep(0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
