![](/img/trans.png)
[英]IBM Watson JavaScript SDK for Speech-To-Text WebSocket Issue
[英]IBM Watson Speech-to-Text JavaScript SDK: how to get messages?
我在浏览器或控制台中看到了转录这个词,但我没有看到诸如{'state': 'listening'}
。 更重要的是,我没有看到诸如{"results": [{"alternatives": [{"transcript": "name the mayflower "}],"final": true}],"result_index": 0}
.
我阅读了RecognizeStream 文档并尝试了以下代码:
stream.on('message', function(message) {
console.log(message);
});
但这不起作用。 我在true
和false
中都尝试了object_mode
,但输出是一样的。
这是我正在使用的完整代码:
document.querySelector('#button').onclick = function () {
var stream = WatsonSpeech.SpeechToText.recognizeMicrophone({
token: token,
model: 'en-US_BroadbandModel',
keywords: ["Colorado"],
keywords_threshold: 0.50,
word_confidence: true,
// outputElement: '#output' // send text to browser instead of console
object_mode: false
});
stream.setEncoding('utf8'); // get text instead of Buffers for on data events
stream.on('data', function(data) { // send text to console instead of browser
console.log(data);
});
stream.on('error', function(err) {
console.log(err);
});
document.querySelector('#stop').onclick = function() {
stream.stop();
};
};
recognizeMicrophone()
方法是一个将多个流链接在一起的助手。 message
事件在中间的流之一上触发。 但是,您可以在stream.recognizeStream
访问那个 - 它总是附加到链中的最后一个以支持这样的情况。
因此,在您的代码中,它应该如下所示:
stream.recognizeStream.on('message', function(frame, data) {
console.log('message', frame, data)
});
但是,这主要用于调试。 如果您设置objectMode: true
并且不调用stream.setEncoding('utf8');
,则结果 JSON 应该在data
事件上发出stream.setEncoding('utf8');
.
(这与 Watson Node.js SDK 有点不同,如果您熟悉它的行为。有计划将两者统一,但时间不够...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.