I am exploring google cloud speech api in python.I am following this link. I also referred this stackoverflow link.But I got struck from setting the environment variables.
Things I have done:
1.Installed gcloud python module
2.Installed google-api-python-client module
3.Had Set up a Service account( obtained JSON File)
4.Obtained an API KEY
I got struck in exporting GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT environment variables.
My doubts are:
1.Should they be exported using google cloud sdk?If so,what role does google cloud sdk play here and when should we use this sdk?
2.Since I am not enclosing the API key explicitly in the code, does it mean that my authentication is automatically verified online?In such case what does my get_speech_service() function in the below code do?
Below is the code
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)
I googled many times and I obtained the mentioned stackoverflow link,which clarified some things.Since I am not clear with my above doubts I posted here.
Following steps worked for me. Hope it'll be of some use to you.
clone from github following repo:
navigate to folder:
Install pip (am sure you already have this) and virtualenv . execute following commands:
Then install from requirements.txt
define and export google credentials path (you are already doing this).
start with the quick sample script:
You should be getting following output:
After this you can explore other scripts in the same folder and also try URI samples for long recognition.