第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 Python 中使用 Google 的 Text-to-Speech API

如何在 Python 中使用 Google 的 Text-to-Speech API

ibeautiful 2021-11-09 15:43:20
我的鑰匙已準(zhǔn)備好提出請(qǐng)求并從 Google 的文本中獲取語(yǔ)音。我嘗試了這些命令以及更多命令。這些文檔沒(méi)有提供我發(fā)現(xiàn)的 Python 入門(mén)的直接解決方案。我不知道我的 API 密鑰與 JSON 和 URL 一起去哪里了他們的文檔中的一種解決方案是針對(duì) CURL。. 但是涉及在必須將請(qǐng)求發(fā)送回他們以獲取文件之后下載txt。有沒(méi)有辦法在 Python 中做到這一點(diǎn)而不涉及我必須返回的 txt?我只希望我的字符串列表作為音頻文件返回。(我把我的實(shí)際密鑰放在上面的塊中。我不打算在這里分享它。)
查看完整描述

3 回答

?
達(dá)令說(shuō)

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊

為 JSON 文件配置 Python 應(yīng)用程序并安裝客戶端庫(kù)

  1. 創(chuàng)建服務(wù)帳戶

  2. 使用此處的服務(wù)帳戶創(chuàng)建服務(wù)帳戶密鑰

  3. JSON 文件下載并安全保存

  4. 在您的 Python 應(yīng)用程序中包含 Google 應(yīng)用程序憑據(jù)

  5. 安裝庫(kù): pip install --upgrade google-cloud-texttospeech

使用 Google 的 Python 示例找到:https : //cloud.google.com/text-to-speech/docs/reference/libraries 注意:在 Google 的示例中,它沒(méi)有正確包含 name 參數(shù)。和 https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/texttospeech/cloud-client/quickstart.py

以下是使用谷歌應(yīng)用程序憑據(jù)和女性的 wavenet 語(yǔ)音從示例中修改的內(nèi)容。

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/yourproject-12345.json"


from google.cloud import texttospeech


# Instantiates a client

client = texttospeech.TextToSpeechClient()


# Set the text input to be synthesized

synthesis_input = texttospeech.types.SynthesisInput(text="Do no evil!")


# Build the voice request, select the language code ("en-US") 

# ****** the NAME

# and the ssml voice gender ("neutral")

voice = texttospeech.types.VoiceSelectionParams(

    language_code='en-US',

    name='en-US-Wavenet-C',

    ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)


# Select the type of audio file you want returned

audio_config = texttospeech.types.AudioConfig(

    audio_encoding=texttospeech.enums.AudioEncoding.MP3)


# Perform the text-to-speech request on the text input with the selected

# voice parameters and audio file type

response = client.synthesize_speech(synthesis_input, voice, audio_config)


# The response's audio_content is binary.

with open('output.mp3', 'wb') as out:

    # Write the response to the output file.

    out.write(response.audio_content)

    print('Audio content written to file "output.mp3"')

語(yǔ)音、姓名、語(yǔ)言代碼、SSML 性別等

語(yǔ)音列表:https : //cloud.google.com/text-to-speech/docs/voices


在上面的代碼示例中,我將 Google 示例代碼中的語(yǔ)音更改為包含名稱參數(shù)并使用 Wavenet 語(yǔ)音(大大改進(jìn)但更貴 16 美元/百萬(wàn)個(gè)字符)和 SSML 性別為 FEMALE。


voice = texttospeech.types.VoiceSelectionParams(

        language_code='en-US',

        name='en-US-Wavenet-C',

        ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)


查看完整回答
反對(duì) 回復(fù) 2021-11-09
?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊

如果您想避免使用 google Python API,您可以簡(jiǎn)單地執(zhí)行以下操作:


import requests 

import json


url = "https://texttospeech.googleapis.com/v1beta1/text:synthesize"


text = "This is a text"


data = {

        "input": {"text": text},

        "voice": {"name":  "fr-FR-Wavenet-A", "languageCode": "fr-FR"},

        "audioConfig": {"audioEncoding": "MP3"}

      };


headers = {"content-type": "application/json", "X-Goog-Api-Key": "YOUR_API_KEY" }


r = requests.post(url=url, json=data, headers=headers)

content = json.loads(r.content)

它與您所做的類似,但您需要包含您的 API 密鑰。


查看完整回答
反對(duì) 回復(fù) 2021-11-09
?
拉丁的傳說(shuō)

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊

找到了答案并丟失了我打開(kāi)的 150 個(gè) Google 文檔頁(yè)面之間的鏈接。


#(Since I'm using a Jupyter Notebook)

import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/Path/to/JSON/file/jsonfile.json"

from google.cloud import texttospeech


# Instantiates a client

client = texttospeech.TextToSpeechClient()


# Set the text input to be synthesized

synthesis_input = texttospeech.types.SynthesisInput(text="Hello, World!")


# Build the voice request, select the language code ("en-US") and the ssml

# voice gender ("neutral")

voice = texttospeech.types.VoiceSelectionParams(

    language_code='en-US',

    ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)


# Select the type of audio file you want returned

audio_config = texttospeech.types.AudioConfig(

    audio_encoding=texttospeech.enums.AudioEncoding.MP3)


# Perform the text-to-speech request on the text input with the selected

# voice parameters and audio file type

response = client.synthesize_speech(synthesis_input, voice, audio_config)


# The response's audio_content is binary.

with open('output.mp3', 'wb') as out:

    # Write the response to the output file.

    out.write(response.audio_content)

    print('Audio content written to file "output.mp3"')

我耗時(shí)的追求是嘗試使用 Python 通過(guò) JSON 發(fā)送請(qǐng)求,但這似乎是通過(guò)自己的模塊,工作正常。請(qǐng)注意,默認(rèn)語(yǔ)音性別為“中性”。


查看完整回答
反對(duì) 回復(fù) 2021-11-09
  • 3 回答
  • 0 關(guān)注
  • 466 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)