1

我正在尝试使用 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()
4

1 回答 1

0

阅读语音识别库参考后,我发现我应该使用记录方法并设置持续时间参数,而不是使用识别器的监听方法。


这是代码:

def record_audio(ask=False, lang="en-US"):
    # Change the sample_rate to 16000 good quality and better recognition
    # higher sample rate means slower app.
    with sr.Microphone(sample_rate=12000) as source:  # microphone as source
        print("Emma: I'm listening")
        audio = r.record(source, duration=5)  # listen for the audio via source
        print("Emma: Microphone turned off, processing...")
        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()
于 2020-02-11T21:15:41.147 回答