问题描述
我正在研究 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)