python谷歌语音识别模块过一会不起作用

问题描述 投票:0回答:1

我正在尝试使用Python为名为“ Emma”的计算机创建一个类似Alexa的应用程序。通过使用Speech Recognition模块,它将使用麦克风作为收听用户的信号源。它可以正常工作,但是在回答或进行诸如搜索之类的操作后,它将冻结并且不再起作用。

我以为,语音识别的使用时间可能有限,但是在搜索后我什么都没找到。现在,我只是不知道是因为语音识别或其他一些模块,例如GTTS(Google文本到语音)。

如果您需要查看完整的代码,这是指向我的存储库的链接:https://github.com/ebraz/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()
python speech-recognition gtts
1个回答
0
投票

阅读Speech Recognition Library Reference之后,我发现与其使用识别器的listen方法,还应该使用record method并设置duration参数。


这是代码:

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()
© www.soinside.com 2019 - 2024. All rights reserved.