首页 > 解决方案 > 使用 IBM Watson 进行文本转语音的简单 Python 代码

问题描述

我一直在寻找可以帮助我在 pycharm 中使用 ibm watson 进行文本转语音的代码。请尽快帮助我

标签: pythonpycharmibm-watson

解决方案


您可以在IBM Watson 文档中找到大部分代码

首先,您需要安装 ibmwatson 软件包,然后您需要通过API 密钥和 url 进行身份验证。只有建立连接后,才能调用该synthesize函数。

pip install --upgrade "ibm-watson>=4.4.0"

from ibm_watson import TextToSpeechV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator('{apikey}')
text_to_speech = TextToSpeechV1(
    authenticator=authenticator
)

text_to_speech.set_service_url('{url}')

with open('filename.wav','wb') as audio_file:
    audio_file.write(text_to_speech.synthesize('hello world',voice='en-US_Henry3Voice',accept='audio/mp3').get_result().content)

推荐阅读