【问题标题】:Google cloud speech transcribe 3gp in python谷歌云语音在python中转录3gp
【发布时间】:2017-02-20 17:57:27
【问题描述】:

我无法使用谷歌云语音 api 转录一个简单的 3gp 音频文件。他们的示例在 audio.raw 上运行良好,但是当我将其更改为我的文件时,它出错了。

在我的安卓设备上录制是这样的:

 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
 mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

我是这样翻译的:

import io
import os

# Imports the Google Cloud client library
from google.cloud import speech

# Instantiates a client
speech_client = speech.Client()

# The name of the audio file to transcribe
file_name = os.path.join(
    os.path.dirname(__file__),
    'resources',
    'phone.3gp')

# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
    content = audio_file.read()
    audio_sample = speech_client.sample(
        content,
        source_uri=None,
        encoding='AMR',
        sample_rate=8000) #LINEAR16

# Detects speech in the audio file
alternatives = speech_client.speech_api.sync_recognize(audio_sample)

for alternative in alternatives:
    print('Transcript: {}'.format(alternative.transcript))

编码和采样率是正确的,但我明白了:

Traceback (most recent call last):
  File "transcribe.py", line 26, in <module>
    alternatives = speech_client.speech_api.sync_recognize(audio_sample)
  File "C:\Program Files\Anaconda2\lib\site-packages\google\cloud\speech\_gax.py", line 266, in sync_recognize
    raise ValueError('More than one result or none returned from API.')
ValueError: More than one result or none returned from API.

【问题讨论】:

    标签: android python google-cloud-speech


    【解决方案1】:

    如果有人遇到同样的问题:

    mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    

    应该是

    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    

    文档中的常量有所不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多