【问题标题】:Failing to open websocket connection to IBM Watson speech to text无法打开与 IBM Watson 语音转文本的 websocket 连接
【发布时间】:2018-02-04 15:52:50
【问题描述】:

下面的代码只是返回输出:“Failed to open WebSocket”请帮我改正。我怀疑 ws_url 可能有问题,但仍然不确定。

from ws4py.client.threadedclient import WebSocketClient
import base64, time

class SpeechToTextClient(WebSocketClient):
    def __init__(self):
    ws_url = "wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize"

    username = "your username"
    password = "your password"
    auth_string = "%s:%s" % (username, password)
    base64string = base64.b64encode(auth_string.encode())

    try:
        WebSocketClient.__init__(self, ws_url,
            headers=[("Authorization", "Basic %s" % base64string)])
        self.connect()
    except: print("Failed to open WebSocket.")

    def opened(self):
        self.send('{"action": "start", "content-type": "audio/l16;rate=16000"}')

def received_message(self, message):
    print(message)

stt_client = SpeechToTextClient()
time.sleep(3)
stt_client.close()

错误信息如下:¨

这是完整的错误信息:

Traceback(最近一次调用最后一次): 文件“C:\Users\Vetle\Desktop\Python projects\problem.py”,第 27 行,在 stt_client.close() 文件“C:\Users\Vetle\AppData\Local\Programs\Python\Python35\lib\site-packages\ws4py\client__init__.py”,第 205 行,关闭 self._write(self.stream.close(code=code, reason=reason).single(mask=True)) 文件“C:\Users\Vetle\AppData\Local\Programs\Python\Python35\lib\site-packages\ws4py\websocket.py”,第 283 行,在 _write raise RuntimeError("无法在终止的 websocket 上发送") RuntimeError: 无法在终止的 websocket 上发送

【问题讨论】:

  • 您最好提供完整的异常消息和堆栈跟踪。
  • 现在添加异常消息

标签: python websocket speech-recognition ibm-watson


【解决方案1】:

根据 API 文档,您需要传入身份验证令牌,而不是基本服务凭据。该文档有示例 Web 套接字代码 - https://www.ibm.com/watson/developercloud/speech-to-text/api/v1/#recognize_audio_websockets

【讨论】:

    猜你喜欢
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2019-06-30
    • 2019-04-15
    • 1970-01-01
    相关资源
    最近更新 更多