我正在尝试使用Google Cloud Speech API将音频转换为文本,但收到以下错误:
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.我使用的是以下代码:
"""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))我在here上找到了教程。
发布于 2018-01-29 05:42:28
您是否使用凭据进行了身份验证?
首先,您需要创建api密钥。https://support.google.com/cloud/answer/6158862?hl=en
请阅读本教程进行身份验证。https://cloud.google.com/speech/docs/auth
您可以看到Google提供的一些示例应用程序。https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/speech/cloud-client
https://stackoverflow.com/questions/48491601
复制相似问题