在 运行 代码之后,我将命令告诉我的语音助手,但他没有响应,没有错误
After running the code, I tell the command to my voice assistant, but he does not respond to it, there are no errors
在运行代码后,我把命令告诉我的语音助手,但他没有回应,也没有错误。
我告诉我的机器人“现在几点了”,但遗憾的是,在那之后它什么也没有给出。
我尝试重新安装 python
没有错误,只是没反应
我认为问题出在我的 Python 库中
import datetime
import time
import pyttsx3
import speech_recognition as sr
from fuzzywuzzy import fuzz
opts = {
"alias": ("Андрей", "Эндрю", "Andrew"),
"tbr": ("скажи", "расскажи", "покажи", "сколько", "произнеси"),
"cmds": {
"time": ("текущее время", "сейчас времени", "который час"),
}
}
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)
except sr.UnknownValueError:
print("[log] Голос не распознан!")
except sr.RequestError as e:
print("[log] Неизвестная ошибка, проверьте интернет!")
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"])
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 == "time":
now = datetime.datetime.now()
speak("Сейчас " + str(now.hour) + ":" + str(now.minute))
r = sr.Recognizer()
m = sr.Microphone(device_index=1)
with m as source:
r.adjust_for_ambient_noise(source)
speak_engine = pyttsx3.init()
speak("Добрый день, создатель")
speak("Слушаю вас ...")
stop_listening = r.listen_in_background(m, callback)
while True: time.sleep(0.1)
缩进错误:在 callback
函数中,当且仅当 recognizer.recognize_google
引发 sr.RequestError
异常,所以你应该将所述逻辑向上移动一级缩进。
编辑:
此外,由于lower()
在voice = recognizer.recognize_google(audio, language="ru-RU").lower()
中的使用,voice
总是小写,所以voice.startswith(opts["alias"])
将始终为假,因为 opts["alias"]
的每个元素都包含一个大写字符:即 А
、Э
和 A
。
在运行代码后,我把命令告诉我的语音助手,但他没有回应,也没有错误。
我告诉我的机器人“现在几点了”,但遗憾的是,在那之后它什么也没有给出。
我尝试重新安装 python
没有错误,只是没反应
我认为问题出在我的 Python 库中
import datetime
import time
import pyttsx3
import speech_recognition as sr
from fuzzywuzzy import fuzz
opts = {
"alias": ("Андрей", "Эндрю", "Andrew"),
"tbr": ("скажи", "расскажи", "покажи", "сколько", "произнеси"),
"cmds": {
"time": ("текущее время", "сейчас времени", "который час"),
}
}
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)
except sr.UnknownValueError:
print("[log] Голос не распознан!")
except sr.RequestError as e:
print("[log] Неизвестная ошибка, проверьте интернет!")
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"])
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 == "time":
now = datetime.datetime.now()
speak("Сейчас " + str(now.hour) + ":" + str(now.minute))
r = sr.Recognizer()
m = sr.Microphone(device_index=1)
with m as source:
r.adjust_for_ambient_noise(source)
speak_engine = pyttsx3.init()
speak("Добрый день, создатель")
speak("Слушаю вас ...")
stop_listening = r.listen_in_background(m, callback)
while True: time.sleep(0.1)
缩进错误:在 callback
函数中,当且仅当 recognizer.recognize_google
引发 sr.RequestError
异常,所以你应该将所述逻辑向上移动一级缩进。
编辑:
此外,由于lower()
在voice = recognizer.recognize_google(audio, language="ru-RU").lower()
中的使用,voice
总是小写,所以voice.startswith(opts["alias"])
将始终为假,因为 opts["alias"]
的每个元素都包含一个大写字符:即 А
、Э
和 A
。