2017-04-21 1 views
2

私は以下のコードを使用して、音声を含む.wavファイルをGoogle Speechを使用してテキストに解析しました。Google Speechを使用しているときにGoogle Cloud Storageからオーディオファイルにアクセスする

しかし、ローカルのハードドライブではなく、Google Cloud Storage(一般公開)に配置した別の.wavファイルにアクセスしたいとします。なぜ単純に許容可能
speech_file = 'https://console.cloud.google.com/storage/browser/speech_proj_files/sample.wav'

仕事に

speech_file = 'my/local/system/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 

答えて

3

は、ここに私のコードです。

Google Cloud Speech APIは、Google Cloud Storageオブジェクトをネイティブサポートしています。

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

もう一つの提案:その代わりにのみバッククラウドスピーチAPIにそれをアップロードするには、オブジェクト全体をダウンロードする、ちょうどこのラインを交換してオブジェクトを指定します。 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/GoogleCloudPlatform/python-docs-samples/tree/master/speech/cloud-client

+0

コードの2番目のブロックがうまく動作しているようですが、私はちょうど 'results_list'に保存されているものにアクセスする問題を抱えています。それはリスト以外の何かのオブジェクトです、それは確かに... jsonのようには見えません...それは何ですか、そしてそれに侵入する方法ですか? 'results_list.response'が空になります。おそらく実際には機能しなかったでしょう。 –

+0

私はこの一日中混乱しました - 別の質問をしました。http://stackoverflow.com/questions/43555694/audio-file-isnt-being-parsed-with-google-speechそれはたくさんのことを意味します。 –

+0

@BrandonYarbroughモニカが知りたいのは、オブジェクトを公開せずに彼女のバケツで彼女のオブジェクトにアクセスする方法です。 –

関連する問題