谷歌文本到语音api无法找到json密钥的路径使用anaconda和python在windows上



我正在尝试使用google clould api来使用文本到语音。为此,它需要访问我保存到的json密钥

C:keys*****.json

并将路径设置为

set GOOGLE_APPLICATION_CREDENTIALS="C:keys*****.json"

当我运行示例scrip时,我得到一个错误,即我无法在该路径上找到文件。

C:UsersdsandhuDocumentslinuxgooglecloudpython-docs-samplestexttospeechtesting>python sampletest.py
Traceback (most recent call last):
File "sampletest.py", line 28, in <module>
synthesize_text("hello world")
File "sampletest.py", line 4, in synthesize_text
client = texttospeech.TextToSpeechClient()
File "C:Program Files (x86)Microsoft Visual StudioSharedAnaconda3_64libsite-packagesgooglecloudtexttospeech_v1gapictext_to_speech_client.py", line 84, in __init__
scopes=self._DEFAULT_SCOPES,
File "C:Program Files (x86)Microsoft Visual StudioSharedAnaconda3_64libsite-packagesgoogleapi_coregrpc_helpers.py", line 170, in create_channel
credentials, _ = google.auth.default(scopes=scopes)
File "C:Program Files (x86)Microsoft Visual StudioSharedAnaconda3_64libsite-packagesgoogleauth_default.py", line 294, in default
credentials, project_id = checker()
File "C:Program Files (x86)Microsoft Visual StudioSharedAnaconda3_64libsite-packagesgoogleauth_default.py", line 165, in _get_explicit_environ_credentials
os.environ[environment_vars.CREDENTIALS])
File "C:Program Files (x86)Microsoft Visual StudioSharedAnaconda3_64libsite-packagesgoogleauth_default.py", line 89, in _load_credentials_from_file
'File {} was not found.'.format(filename))
google.auth.exceptions.DefaultCredentialsError: File "C:keys*****.json" was not found.

我使用anaconda(作为管理员(和python来设置anaconda终端中的变量。我也试着在windows cmd中设置它(用相同的命令设置(,但没有成功。

我也张贴了纸条,以防你想测试它:

def synthesize_text(text):
"""Synthesizes speech from the input string of text."""
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
#text = "Hello world"
input_text = texttospeech.types.SynthesisInput(text=text)
#input_text = texttospeech.types.SynthesisInput(text="Hellow world")
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
response = client.synthesize_speech(input_text, voice, audio_config)
# The response's audio_content is binary.
with open('output.mp3', 'wb') as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')

synthesize_text("hello world")

从该链接使用

我只需要删除引号并进行

set GOOGLE_APPLICATION_CREDENTIALS=C:keys******.json

它奏效了。

相关内容

  • 没有找到相关文章