我正在使用Python中的speech_recognition模块。在使用adjust_for_ambient_noise()
录制音频之前,我使用了listen()
。问题是有时它似乎只是试图永久地记录,然后在它最终完成时吃了带宽,将记录下来的大音频文件传输给Google进行转录。
我是否可以指定最长录制时间为5秒?
设置timeout=5
只是等待检测到开始音频信号的时间。
import speech_recognition
def listen(rec, mic):
with mic as source:
print('adjusting mic for ambient noise..')
rec.adjust_for_ambient_noise(source)
print('listening..')
# timeout is not max record time but max time it will wait for audio to start
audio = rec.listen(source, timeout=5)
try:
print('sending audio to google..')
outputString = rec.recognize_google(audio).lower()
except speech_recognition.UnknownValueError:
print('google was not able to transcribe the audio')
return None
except speech_recognition.RequestError:
print('google API unreachable')
return None
except speech_recognition.WaitTimeoutError:
print('timout expired while waiting for audio command to start')
return None
print('transcription \'' + outputString + '\'')
return outputString
rec = sr.Recognizer()
mic = sr.Microphone()
outputString = listen(rec, mic)
答案 0 :(得分:2)
您可以使用(source, phrase_time_limit=5)
代替(source, timeout=5)