python中的语音识别持续时间设置问题

2024-05-03

我有一个 Wav 格式的音频文件,我想转录:

我的代码是:

import speech_recognition as sr
harvard = sr.AudioFile('speech_file.wav')
with harvard as source:
    try:
        audio = r.listen(source)
        #print("Done")
    except sr.UnknownValueError:
        exec()

r.recognize_google(audio)

我确实收到了输出:

Out[20]: 'thank you for calling my name is Denise who I have a pleasure speaking with hi my name is Mary Jane. Good afternoon Mary Jane I do have your account open with your email'

不过,这之后还有很多话要说。我认为它只捕获了这部分语音,因为在音频文件中说出“电子邮件”一词后有一个短暂的停顿。我尝试设置持续时间,但收到错误:

import speech_recognition as sr
harvard = sr.AudioFile('speech_file.wav')
with harvard as source:
    try:
        audio = r.listen(source,duration = 200)
        #print("Done")
    except sr.UnknownValueError:
        exec()


r.recognize_google(audio)
Traceback (most recent call last):

  File "<ipython-input-24-30fb65edc627>", line 5, in <module>
    audio = r.listen(source,duration = 200)

TypeError: listen() got an unexpected keyword argument 'duration'

我该怎么做才能让我的代码转录整个音频文件并且在出现暂停时不会停止打印文本?


您可以使用timeout代替duration像这样:

audio = r.listen(source, timeout=2)

这意味着模型将等待最多两秒在放弃和投掷之前开始的一个短语speech_recognition.WaitTimeoutError例外。如果timeout=None,您的情况将无需等待。

EDIT

所有功能recognize_google()所做的就是调用 google Speech API 并返回结果。当我使用提供的音频文件时,我得到了前 30 秒的转录。这是由于免费版 Google 语音 API 的限制,与代码无关。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python中的语音识别持续时间设置问题 的相关文章

随机推荐