简体   繁体   English

是否可以使用 Python 中的语音识别模块设置语音记录时间的最大持续时间?

[英]Is it possible to set the max duration of voice record time using speech_recognition module in Python?

I'm using the speech_recognition module in Python.我在 Python 中使用了语音识别模块。 I have it use adjust_for_ambient_noise() before recording audio with listen() .我让它在使用adjust_for_ambient_noise()录制音频之前使用adjust_for_ambient_noise() listen() The problem is sometimes it just tries to record seemingly forever, and then when it finally finishes it eats bandwidth transfering the large audio file it recorded to Google for transcription.问题是有时它只是试图似乎永远地记录,然后当它最终完成时,它会消耗带宽将它记录的大型音频文件传输到谷歌进行转录。

Is there a way for me to specify a maximum record time of like 5 seconds?有没有办法让我指定大约 5 秒的最大记录时间?

Setting timeout=5 is only the amount of time it will wait to detect a starting audio signal.设置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)

您可以使用(source, phrase_time_limit=5)而不是(source, timeout=5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM