想用Python做一个Watson IBM助手。如何能把我的代码固定在IDE Pycharm里面说出答案,不需要音频文件。使用VLC

问题描述 投票:0回答:1

这是从另一个基于Watson的问题中抓取的代码。我试图做一个系统,在这个系统中,我把我的语音作为一个问题或命令输入,它在IDE中说一个答案,这是我之前问题的链接 我的上一个问题 但我如何能修复下面的代码。

这是代码...

    import vlc
    from ibm_watson import TextToSpeechV1
    from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator("API Key")
text_to_speech = TextToSpeechV1(
    authenticator=authenticator
)

text_to_speech.set_service_url(
'https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/113cd664-f07b-44fe-a11d-a46cc50caf84')

# define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')

# Define VLC player
player = instance.media_player_new()

# Define VLC media
media = instance.media_new(
    text_to_speech.synthesize(
        'Hello world',
        voice='en-US_AllisonVoice',
        accept='audio/wav').get_result().content)

# Set player media
player.set_media(media)

# Play the media
player.play()][1]

登录后,果然有了改善。我修改了密钥和URL,只得到2个小代码错误...

Traceback (most recent call last):
  File "C:/Users/PycharmProjects/IBM Test/iBM tEST.py", line 24, in <module>
    accept='audio/wav').get_result().content)
  File "C:\Users\PycharmProjects\IBM Test\venv\lib\site-packages\vlc.py", line 1947, in media_new
    if ':' in mrl and mrl.index(':') > 1:
TypeError: a bytes-like object is required, not 'str'
python ibm-cloud vlc watson-text-to-speech
1个回答
1
投票

这个错误是告诉你,python运行时找不到vlc模块。你需要运行

pip install python-vlc

sudo pip install python-vlc

对第二个问题的编辑

你需要调试你的代码,找出服务返回的错误。要做到这一点,你就要把你的代码拆开。


try:
  result = text_to_speech.synthesize(
        'Hello world',
        voice='en-US_AllisonVoice',
        accept='audio/wav').get_result()
  print(result)

except Exception as e:
  print(e.message)

编辑第三个问题

http 404找不到意味着你有错误的网址

编辑第四个问题

http 403是禁止的,这意味着url、key和method的组合是无效的。这主要说明你使用的url和key是完全不同的服务。

© www.soinside.com 2019 - 2024. All rights reserved.