【问题标题】:IBM Watson Speech to Text Websocket closing (with code 1000) without resultIBM Watson Speech to Text Websocket 关闭(代码 1000)但没有结果
【发布时间】:2016-06-07 14:59:42
【问题描述】:

我尝试使用 ws 包通过 websocket 使用基本的 Speech-To-Text 服务。但是在成功打开连接并发送初始消息后,我再也没有得到listening 状态。 我也尝试发送音频和空二进制文件(表示上传过程已完成),但服务器总是返回close,代码为1000

以下是我的代码

'use strict';

var fs = require('fs');
var request = require('request');
var WS = require('ws');

var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=[TOKEN]&model=en-UK_NarrowbandModell&x-watson-learning-opt-out=1';
var getTokenForm = {
  method: 'GET',
  uri: 'https://[USER_ID]:[PASSWORD]@stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api',
};
var filepath = 'C:/Temp/test1.wav';

request(getTokenForm, function(error, response, body) {
  wsURI = wsURI.replace('[TOKEN]', body);
  var message = {
    'action': 'start',
    'content-type': 'audio/wav',
    'continuous': true,
    'inactivity_timeout': -1
  };
  var ws = new WS(wsURI);

  ['message', 'error', 'close', 'open', 'connection'].forEach(function(eventName) {
    ws.on(eventName, console.log.bind(console, eventName + ' event: '));
  });

  ws.on('open', function(evt) {
    ws.send(JSON.stringify(message));
    setTimeout(function timeout() {
      var readStream = fs.createReadStream(filepath);
      readStream.on('data', function(data) {
        ws.send(data, {
          binary: true,
          mask: false,
        });
      });

      readStream.on('end', function() {
        ws.send(new Buffer(0), {
          binary: true,
          mask: false,
        });
      });
    }, 1000);
  });

  ws.on('close', function(data) {
    console.log(data)
  });
});

也尝试直接发送文件(不带流)。

var sound = fs.readFileSync(filepath);
ws.send(sound, { binary: true, mask: false});

并尝试添加自定义标题Authorization

var authorization = 'Basic ' + new Buffer('USER_ID:PASSWORD').toString('base64');
var ws = new WS(wsURI, {
    headers: {
        'Authorization': authorization,
    }
});

但到目前为止还没有运气。

【问题讨论】:

标签: node.js websocket speech-to-text ibm-watson


【解决方案1】:

这里有几件事。主要问题是查询字符串中的模型有错字 - 最后应该只有一个“l”。 (不过,不响应错误消息是我要向团队报告的服务中的一个错误。)

所以,修复它,你会得到一个应该屏蔽帧的错误。这很容易解决,只需将两个位置的 mask: false 切换为 true

然后,一旦您发送完音频和结束消息,该服务将发送您的最终结果,然后发送另一条 {"state": "listening"} 消息。这第二个state: listening 应该是您关闭连接的触发器。否则它最终会超时并自动关闭(inactivity_timeout 适用于您发送没有语音的音频,而不是当您根本不发送任何数据时。)

【讨论】:

  • 更新:团队已意识到模型名称无效的问题,目前正在努力解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多