我使用通过websocket-client库实现的python Websockets,以便使用Watson ASR执行实时语音识别。该解决方案一直有效直到最近,但是大约一个月前它停止工作。甚至没有握手。奇怪的是,我没有更改代码(如下)。另一个使用其他帐户的同事也有同样的问题,因此我们认为我们的帐户没有任何问题。我已经就此事与IBM联系,但是由于没有握手,因此他们无法跟踪自己是否有问题。 Websocket的代码如下所示。
import websocket
(...)
ws = websocket.WebSocketApp(
self.api_url,
header=headers,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close,
on_open=self.on_open
)
其中url为“ wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize”时,标头是授权令牌,而其他函数和方法则用于处理回调。目前发生的情况是该方法运行并等待直到连接超时。我想知道是否其他人在运行此websocket-client库的Python中使用Watson运行实时ASR的其他人是否会遇到此问题。
答案 0 :(得分:2)
@zedavid一个月前,我们切换为使用IAM,因此username
和password
被替换为IAM apikey
。您应该将Cloud Foundry Speech到Text实例迁移到IAM。有一个Migration页可以帮助您了解更多信息。您还可以创建一个新的“语音转文本”实例,默认情况下该实例将是资源控制的实例。
拥有新实例后,您将需要获得一个access_token
,它与Cloud Foundry中的token
类似。 access_token
将用于授权您的请求。
最后,我们最近在Python SDK中发布了对语音转文字和文字转语音的支持。我鼓励您使用它,而不是为令牌交换和WebSocket连接管理编写代码。
service = SpeechToTextV1(
iam_apikey='YOUR APIKEY',
url='https://stream.watsonplatform.net/speech-to-text/api')
# Example using websockets
class MyRecognizeCallback(RecognizeCallback):
def __init__(self):
RecognizeCallback.__init__(self)
def on_transcription(self, transcript):
print(transcript)
def on_connected(self):
print('Connection was successful')
def on_error(self, error):
print('Error received: {}'.format(error))
def on_inactivity_timeout(self, error):
print('Inactivity timeout: {}'.format(error))
def on_listening(self):
print('Service is listening')
def on_hypothesis(self, hypothesis):
print(hypothesis)
def on_data(self, data):
print(data)
# Example using threads in a non-blocking way
mycallback = MyRecognizeCallback()
audio_file = open(join(dirname(__file__), '../resources/speech.wav'), 'rb')
audio_source = AudioSource(audio_file)
recognize_thread = threading.Thread(
target=service.recognize_using_websocket,
args=(audio_source, "audio/l16; rate=44100", mycallback))
recognize_thread.start()
答案 1 :(得分:0)
感谢标题信息。这就是它的工作方式。
我正在使用WebSocket-client 0.54.0,它是当前的最新版本。我使用
生成了令牌curl -u <USERNAME>:<PASSWORD> "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"
在下面的代码中使用返回的令牌,我能够进行握手
import websocket
try:
import thread
except ImportError:
import _thread as thread
import time
import json
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
def run(*args):
for i in range(3):
time.sleep(1)
ws.send("Hello %d" % i)
time.sleep(1)
ws.close()
print("thread terminating...")
thread.start_new_thread(run, ())
if __name__ == "__main__":
# headers["Authorization"] = "Basic " + base64.b64encode(auth.encode()).decode('utf-8')
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize",
on_message=on_message,
on_error=on_error,
on_close=on_close,
header={
"X-Watson-Authorization-Token": <TOKEN>"})
ws.on_open = on_open
ws.run_forever()
响应:
--- request header ---
GET /speech-to-text/api/v1/recognize HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: stream.watsonplatform.net
Origin: http://stream.watsonplatform.net
Sec-WebSocket-Key: Yuack3TM04/MPePJzvH8bA==
Sec-WebSocket-Version: 13
X-Watson-Authorization-Token: <TOKEN>
-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Date: Tue, 04 Dec 2018 12:13:57 GMT
Content-Type: application/octet-stream
Connection: upgrade
Upgrade: websocket
Sec-Websocket-Accept: 4te/E4t9+T8pBtxabmxrvPZfPfI=
x-global-transaction-id: a83c91fd1d100ff0cb2a6f50a7690694
X-DP-Watson-Tran-ID: a83c91fd1d100ff0cb2a6f50a7690694
-----------------------
send: b'\x81\x87\x9fd\xd9\xae\xd7\x01\xb5\xc2\xf0D\xe9'
Connection is already closed.
### closed ###
Process finished with exit code 0
根据RFC 6455,服务器应使用101交换协议进行响应,
与服务器的握手如下:
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat
另外,当我使用ws://
而不是wss://
时,我遇到了操作超时问题。
更新:具有实时语音识别功能的示例-https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/microphone-speech-to-text.py