【问题标题】:Why does my python script not recognize speech from audio file?为什么我的 python 脚本无法识别音频文件中的语音?
【发布时间】:2017-06-29 21:48:01
【问题描述】:

我有以下代码成功识别短(少于 1 分钟)测试音频文件,但识别另一个长音频文件(1.5 小时)失败。

from google.cloud import speech


def run_quickstart():
    speech_client = speech.Client()
    sample = speech_client.sample(source_uri="gs://linear-arena-2109/zoom0070.flac", encoding=speech.Encoding.FLAC)
    alternatives = sample.recognize('uk-UA')
    for alternative in alternatives:
        print(u'Transcript: {}'.format(alternative.transcript))

    with open("Output.txt", "w") as text_file:
        for alternative in alternatives:
            text_file.write(alternative.transcript.encode('utf8'))

if __name__ == '__main__':
    run_quickstart()

这两个文件都上传到 Google Cloud

第一个: https://storage.googleapis.com/linear-arena-2109/sample.flac

第二个: https://storage.googleapis.com/linear-arena-2109/zoom0070.flac

两者都是使用 ffmpeg 实用程序从 mp3 转换而来的:

ffmpeg -i sample.mp3 -ac 1 sample.flac
ffmpeg -i zoom0070.mp3 -ac 1 zoom0070.flac

第一个文件被成功识别,但第二个文件输出如下错误:

google.gax.errors.RetryError: GaxError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Sync input too long. For audio longer than 1 min use LongRunningRecognize with a 'uri' parameter.)>)

但我已经在我的 python 脚本中使用了uri 参数。怎么了?

更新

@NieDzejkob 帮助理解了错误。因此,应该使用方法long_running_recognize 而不是recognize。综合long_running_recognize使用示例可以在对应的document page上找到

【问题讨论】:

  • 阅读错误信息以获得答案。
  • 你是对的。应该改用long_running_recognize 方法。我已经修复了代码。我的音频文件目前正在由google-cloud-speech 处理。

标签: google-cloud-speech


【解决方案1】:

对于任何超过 1 分钟的音频文件,您需要使用异步语音识别并且文件必须上传到 Google Cloud Storage 以便您可以传入gcs_uri

此外,您需要在脚本中使用.long_running_recognize 方法。可以在 here 找到 GCP 文档中的示例。

我意识到 OP 想通了,但认为提供答案并概括一下会很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多