【发布时间】:2020-03-04 02:49:30
【问题描述】:
我正在尝试使用 Python 为名为“Emma”的计算机创建一个类似 Alexa 的应用程序。 通过使用Speech Recognition 模块,它将使用麦克风作为源来聆听用户的声音。 它可以正常工作,但是在回答或执行一些诸如搜索之类的操作后,它会冻结并且不再起作用。
我认为可能语音识别的使用时间有限,但搜索后我一无所获。现在我只是不知道这是因为语音识别或其他一些模块,如 GTTS (Google Text To Speech)。
如果您需要查看整个代码,这里是我的存储库的链接:https://github.com/sina1mhi/emma_virtual_assistant
请告诉我你解决问题的方法。
这里是部分语音识别代码:
def record_audio(ask=False, lang="en-US"):
with sr.Microphone() as source: # microphone as source
print("Emma: I'm listening")
if ask:
speak(ask)
time.sleep(1)
audio = r.listen(source) # listen for the audio via source
voice_data = ''
try:
voice_data = r.recognize_google(
audio, language=lang) # convert audio to text
except sr.UnknownValueError: # error: recognizer does not understand
speak("I did'nt get that")
exit()
except sr.RequestError:
# error: recognizer is not connected
speak('Sorry, the service is down')
exit()
print(f">> {voice_data.lower()}") # print what user said
return voice_data.lower()
【问题讨论】:
-
到目前为止你有什么尝试?
-
@Jamie 实际上我根本不知道该怎么做。我是编程 BTW 的新手。我试图通过将它放在一个while循环中并使用 continue 语句来提高响应速度,但没有任何效果。
-
对不起,我应该更清楚。你怎么知道它结冰了?在代码的哪一行停止工作?你有任何错误吗?
-
@Jamie 不,我没有收到任何错误,当我在终端中运行应用程序后,它会停止,就像你知道的无限循环一样,但根本没有无限循环。我百分百确定。
-
您是否尝试过在代码的检查点添加打印语句以查看它停止的位置?或者您可以尝试使用大多数 IDE 附带的调试工具。
标签: python speech-recognition gtts