私はPythonでgoogle cloud speech apiを探しています。私はこれに続いていますlink。 私はこれもまたstackoverflow linkを参照しました。しかし、私は環境変数の設定に打撃を受けました。pythonでgoogle cloud speech apiを使用する方法
物事私が行っている:
1.Installedのgcloud Pythonモジュール
2.Installedのgoogle-API-のpython-クライアントモジュール3.Hadは、サービスアカウントを設定
(取得したJSONファイルを私はエクスポートGOOGLE_APPLICATION_CREDENTIALSとGCLOUD_PROJECT環境変数に打たれてしまった)
4.Obtained APIキー
。
私の疑問は、以下のとおりです。彼らは、GoogleのクラウドSDKを使用してエクスポートすること
1.Shouldもしそうなら、私たちは、このSDKを使用する必要があるときにどのような役割GoogleのクラウドSDKは、ここでプレーしていませんか?
2. APIキーをコード内に明示的に囲まないので、 は、自分の認証が自動的にオンラインで確認されることを意味しますか?その場合、下のコードのget_speech_service()関数は何をしますか?
以下は、私が何度もGoogleで検索し、私は私がここに掲載私の上記の疑問と明確ではないよ、いくつかのthings.Sinceを明確に言及したStackOverflowのリンクを、取得したコード
import argparse
import base64
import json
import httplib2
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('speech_file',help='This is the path of the audio')
args = parser.parse_args()
print args.speech_file
main(args.speech_file)
def main(speech_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',
'sampleRate':16000,
'languageCode':'en-US',
},
'audio':{
'content':speech_content.decode('UTF-8')
}
})
response = service_request.execute()
print(json.dumps(response))
DISCOVERY_URL = ('https://speech.googleapis.com/$discovery/rest?/version=v1beta1')
def get_speech_service():
credentials = GoogleCredentials.get_application_default().create_scoped(
['https://www.googleapis.com/auth/cloud-platform'])
http = httplib2.Http()
credentials.authorize(http)
return discovery.build('speech','v1beta1',http=http,discoveryServiceUrl=DISCOVERY_URL)
です。