我使用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客户机库的Python中与Watson一起运行实时ASR的其他人身上。
发布于 2018-12-06 16:03:02
@zedavid --一个多月前,我们转而使用IAM,因此username和password被IAM apikey取代。您应该将Cloud演讲迁移到文本实例到IAM。有一个迁移页面将帮助您了解更多有关这方面的信息。您还可以创建一个新的语音到文本实例,这将是一个资源控制的实例在默认情况下。
一旦您拥有了新实例,您将需要获得一个类似于Cloud中的access_token的token。access_token将用于授权您的请求。
最后,我们最近在Python中发布了对语音到文本和文本到语音的支持。我鼓励您使用它,而不是为令牌交换和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()发布于 2018-12-04 12:26:28
谢谢你的标题信息。对我来说是这样的。
我正在使用WebSocket-Client0.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交换协议升级: websocket连接:升级Sec-WebSocket-接受: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-协议: chat
此外,当我使用ws://而不是wss://时,我将面临操作超时问题。
更新:带有实时语音识别的示例- https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/microphone-speech-to-text.py
https://stackoverflow.com/questions/53593587
复制相似问题