[GCP] Python3 x google-cloud-speechで音声認識を実装する

GCPでCloud Speech-to-Text APIを有効化します。

GCPユーザの認証データの*.jsonを取得します。

audio.rawはこちらからお借りします。
https://github.com/googleapis/java-speech/tree/master/samples/install-without-bom/resources

$ pip3 install google-cloud-speech

main.py

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "hpscript.json"
from google.cloud import speech
import io

def transcribe_file(speech_file):
	
    client = speech.SpeechClient()

    with io.open(speech_file, "rb") as audio_file:
        content = audio_file.read()

    audio = speech.RecognitionAudio(content=content)
    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=16000,
        language_code="en-US",
    )
    response = client.recognize(config=config, audio=audio)

    for result in response.results:
        print(u"Transcript: {}".format(result.alternatives[0].transcript))

if __name__ == '__main__':
	transcribe_file('audio.raw')

$ python3 main.py
Transcript: how old is the Brooklyn Bridge

うーむ、なんだこれは