使用Google语音时,从Google Cloud存储中访问音频文件



我在下面使用了这一点代码来成功解析一个.wav文件,其中包含语音,文本,使用Google语音。

,但我想访问我已将其放置在Google Cloud Storage(公开(上的其他.WAV文件,而不是在本地硬盘驱动器上。为什么不简单地更改

speech_file = 'my/local/system/sample.wav'


speech_file = 'https://console.cloud.google.com/storage/browser/speech_proj_files/sample.wav'

工作可接受?

这是我的代码:

speech_file = 'https://console.cloud.google.com/storage/browser/speech_proj_files/sample.wav'
DISCOVERY_URL = ('https://{api}.googleapis.com/$discovery/rest?'
                 'version={apiVersion}')

def get_speech_service():
    credentials = GoogleCredentials.get_application_default().create_scoped(
        ['https://www.googleapis.com/auth/cloud-platform'])
    http = htt|plib2.Http()
    credentials.authorize(http)
    return discovery.build(
        'speech', 'v1beta1', http=http, discoveryServiceUrl=DISCOVERY_URL)
def main(speech_file):
    """Transcribe the given audio file.
    Args:
        speech_file: the name of the audio file.
    """
    with open(speech_file, 'rb') as speech:
        speech_content = base64.b64encode(speech.read())
    service = get_speech_service()
    service_request = service.speech().syncrecognize(
        body={
            'config': {
                'encoding': 'LINEAR16',  # raw 16-bit signed LE samples
                'sampleRate': 44100,  # 16 khz
                'languageCode': 'en-US',  # a BCP-47 language tag
            },
            'audio': {
                'content': speech_content.decode('UTF-8')
                }
            })
    response = service_request.execute()
    return response

我不确定为什么您的方法不起作用,但我想提供一个快速建议。

Google Cloud语音API本身支持Google Cloud Storage对象。而不是仅下载整个对象以将其上传回云语音API,而只需通过交换以下行来指定对象:

        'audio': {
            # Remove this: 'content': speech_content.decode('UTF-8')
            'uri': 'gs://speech_proj_files/sample.wav'  # Do this!
            }

另一个建议。您可能会发现Google-Cloud Python库更易于使用。尝试以下操作:

from google.cloud import speech
speech_client = speech.Client()
audio_sample = speech_client.sample(
    content=None,
    source_uri='gs://speech_proj_files/sample.wav',
    encoding='LINEAR16',
    sample_rate_hertz= 44100)
results_list = audio_sample.sync_recognize(language_code='en-US')

这里有一些很棒的示例:https://github.com/googleclecloudplatform/python-docs-samples/tree/master/master/speech/cloud-client

最新更新