2
votes

I am trying to convert audio to text using Google Cloud Speech API, but I am getting the following error:

Traceback (most recent call last):
  File "/home/prateek/Google Drive/projects/linuxAI/src/linuxAI.py", line 6, in <module>
    client = speech.SpeechClient()
  File "/usr/lib/python3.6/site-packages/google/cloud/gapic/speech/v1/speech_client.py", line 146, in __init__
    ssl_credentials=ssl_credentials)
  File "/usr/lib/python3.6/site-packages/google/gax/grpc.py", line 106, in create_stub
    credentials = _grpc_google_auth.get_default_credentials(scopes)
  File "/usr/lib/python3.6/site-packages/google/gax/_grpc_google_auth.py", line 62, in get_default_credentials
    credentials, _ = google.auth.default(scopes=scopes)
  File "/usr/lib/python3.6/site-packages/google/auth/_default.py", line 283, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
explicitly create credential and re-run the application. For more
information, please see
https://developers.google.com/accounts/docs/application-default-credentials.

I am using this code:

"""Transcribe the given audio file."""
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types

client = speech.SpeechClient()
speech_file = "output.wav"
with open(speech_file, 'rb') as audio_file:
    content = audio_file.read()

audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=16000,
    language_code='en-US')

response = client.recognize(config, audio)
# Each result is for a consecutive portion of the audio. Iterate through
# them to get the transcripts for the entire audio file.
for result in response.results:
    # The first alternative is the most likely one for this portion.
    print('Transcript: {}'.format(result.alternatives[0].transcript))

I found tutorial from here.

1

1 Answers

1
votes

Did you authenticated using credentials?

First you need to create api keys. https://support.google.com/cloud/answer/6158862?hl=en

Read this tutorial to authenticate. https://cloud.google.com/speech/docs/auth

You can see some sample applications provided by Google. https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/speech/cloud-client