【问题标题】:Google Speech API Python Exception: Specify FLAC encoding to match file header?Google Speech API Python 异常:指定 FLAC 编码以匹配文件头?
【发布时间】:2017-08-27 02:17:06
【问题描述】:

我正在运行此处发布的 Google Speech API Python 示例代码:https://googlecloudplatform.github.io/google-cloud-python/stable/speech-usage.html

我要使用异步识别方法(只允许使用 LINEAR16 编码):

导入 Google Cloud 客户端库

  from google.cloud import speech

  client = speech.Client() 
  sample = client.sample(source_uri='gs://my-bucket/example.flac',
                    encoding=speech.Encoding.LINEAR16,
                    sample_rate=44100)
  operation = sample.async_recognize(language_code='es-CL',max_alternatives=2)

   retry_count = 100
   while retry_count > 0 and not operation.complete:
   retry_count -= 1
   time.sleep(10)
   operation.poll()  # API call

   operation.complete

   for result in operation.results:
        for alternative in result.alternatives:
        print('=' * 20)
        print(alternative.transcript)
        print(alternative.confidence)

这是我得到的错误: google.gax.errors.RetryError:GaxError(重试方法发生异常,未归类为瞬态,由 <_rendezvous of rpc flac>)

我该如何解决这个问题?使用同步方法时我没有遇到这个问题。

【问题讨论】:

    标签: python google-speech-api


    【解决方案1】:

    从您的代码(以及您链接到的 Google 代码)看来,您将编码指定为 LINEAR16,但使用的是 FLAC 文件。如果您需要使用异步 API,则必须将 .flac 文件转换为原始 LINEAR PCM 文件。所以第二行应该看起来更像这样:

    sample = client.sample(source_uri='gs://my-bucket/example.raw',
                    encoding=speech.Encoding.LINEAR16,
                    sample_rate=44100)
    

    要将 FLAC 转换为 LINEAR16,您需要使用其他工具,例如 sox。 有关转换文件格式的更多信息,请参阅this page。该命令可能类似于:

    sox example.flac example.raw
    

    【讨论】:

    • 效果很好。谢谢@blambert
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多