如何给出听python语音识别库的持续时间?

问题描述

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

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

self:Recogniser,source,timeout = None,phrase_time_limit = None 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,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))

如何缩短收听时间?

解决方法

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...