使用Google Speech时从Google Cloud Storage访问音频文件 [英] accessing audio files from Google Cloud Storage when using Google Speech

查看:121
本文介绍了使用Google Speech时从Google Cloud Storage访问音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用下面的这段代码通过Google Speech成功地将包含语音的.wav文件解析为文本.

I have used this bit of code below to successfully parse a .wav file which contains speech, to text, using Google Speech.

但是我想访问一个不同的.wav文件,该文件已放置在Google Cloud Storage(公开)上,而不是本地硬盘上.为什么不简单地更改

But I want to access a different .wav file, which I have placed on Google Cloud Storage (publicly), instead of on my local hard drive. Why doesn't simply changing

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

推荐答案

我不确定您的方法为何行不通,但我想提一个快速的建议.

I'm not sure why your approach isn't working, but I want to offer a quick suggestion.

Google Cloud Speech API本机支持Google Cloud Storage对象.不必下载整个对象只是将其上传回Cloud Speech API,而只需换掉这一行即可指定对象:

Google Cloud Speech API natively supports Google Cloud Storage objects. Instead of downloading the whole object only to upload it back to the Cloud Speech API, just specify the object by swapping out this line:

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

另一个建议.您可能会发现 google-cloud Python库更易于使用.试试这个:

One other suggestion. You may find the google-cloud Python library easier to use. Try this:

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/GoogleCloudPlatform/python-docs-samples/tree/master/speech/cloud-client

There are some great examples here: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/speech/cloud-client

这篇关于使用Google Speech时从Google Cloud Storage访问音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆