【问题标题】:How do I give the time duration of listening to python speech recognition library?如何给出听python语音识别库的持续时间?
【发布时间】:2022-02-14 20:09:26
【问题描述】:

我正在研究 RPi 4 并且代码可以正常工作,但是从我的麦克风听我的语音识别对象的时间真的很长,几乎像 10 秒。我想减少这个时间。我查看了speech recognition library documentation,但它没有在任何地方提及该功能。

Python 编辑器给了我listen() 函数的以下提示;

self:识别器,来源,超时 = 无,phrase_time_limit = 无 snowboy_configuration =无

所以我尝试像这样调用函数:

audio = r.listen(source,None,3)

或者

 audio = r.listen(source,3,3)

希望它会听 3 秒钟,但它不是那样工作的。

以下是我的代码:

import speech_recognition as sr

r = sr.Recognizer()

speech = sr.Microphone(2)

#print(sr.Microphone.list_microphone_names())

while 1:

             with speech as source:
                 print("say something!…")
                 audio = r.adjust_for_ambient_noise(source)
                 audio = r.listen(source,None,3)
                 print("the audio has been recorded")
             # Speech recognition using Google Speech Recognition
             try:
                 print("api is enabled")
                 recog = r.recognize_google(audio, language = 'en-US')
                 # for testing purposes, we're just using the default API key
                 # to use another API key, use r.recognize_google(audio)
                 # instead of r.recognize_google(audio)

                 print("You said: " + recog)
             except sr.UnknownValueError:
                 print("Google Speech Recognition could not understand audio")
             except sr.RequestError as e:
                 print("Could not request results from Google Speech Recognition service; {0}".format(e))

如何缩短收听时间?

【问题讨论】:

    标签: python speech-recognition google-speech-api


    【解决方案1】:

    试试这个audio = r.listen(source, 10, 3)

    【讨论】:

    • 欢迎来到 SO。虽然您的代码可能会解决问题,但对它如何解决问题的一些解释总是有用的。
    【解决方案2】:
    audio = r.listen(source, phrase_time_limit=3)
    

    您可以设置phrase_time_limit,而忽略其他可选参数。

    文档说:

    phrase_time_limit 参数是在停止和返回达到时间限制之前处理的部分短语之前允许短语继续的最大秒数。生成的音频将是在时间限制内截断的短语。如果phrase_timeout为None,则没有phrase时间限制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多