【问题标题】:Python google speech recognition module doesn't work after a whilePython 谷歌语音识别模块在一段时间后不起作用
【发布时间】: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


【解决方案1】:

在阅读Speech Recognition Library Reference 之后,我发现与其使用识别器的 listen 方法,不如使用 record 方法 并设置持续时间参数。


代码如下:

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2016-11-04
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多