我正在尝试使用 Python 为名为“Emma”的计算机创建一个类似 Alexa 的应用程序。通过使用语音识别模块,它将使用麦克风作为源来倾听用户的声音。它工作正常,但在回答或做一些类似搜索的事情后,它会冻结并且不再工作。
我认为也许语音识别的使用时间有限,但搜索后我一无所获。现在我只是不知道这是因为语音识别或其他一些模块,如GTTS(谷歌文本到语音)。
如果您需要查看整个代码,这里是我的存储库的链接: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()