我在 IBM Bluemix 平台上使用 python。如何调用文本转语音 Watson 服务?我的 python 代码中有字符串,我需要传递此文本以供阅读。
最佳答案
假设您已经拥有一个 Bluemix 帐户并将文本转语音 Watson API 添加到您的 Bluemix 工作区,您拥有访问该 API 的凭据(restful)。
如果您请求使用 CURL linux 应用程序,它将是这样的
curl -u "xxxxx729-b03f-4403-8adf-c5418ee4ea05":"xxxxxiWtmVoG" "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?text=Hello+World" -H "accept: audio/flac" > sound.flac
使用Python,可以
import requests
headers = {'accept': 'audio/flac'}
r = requests.get('https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?text=Hello+World', auth=('xxxxx729-b03f-4403-8adf-c5418ee4ea05', 'xxxxxiWtmVoG'), headers=headers)
with open('/home/leo/sound.flac', 'wb') as fd:
for chunk in r.iter_content(1024):
fd.write(chunk)
参见 http://docs.python-requests.org/en/master/user/quickstart/有关请求包的详细信息。
参见 https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/text-to-speech/api/v1/用于 text2Speech 文档。
关于python - 在 bluemix 上使用 python 调用服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35820579/