[英]IBM Watson Speech-to-Text JavaScript SDK: how to get messages?
I'm seeing the word transcriptions, either in the browser or in the console, but I'm not seeing the messages such as {'state': 'listening'}
.我在浏览器或控制台中看到了转录这个词,但我没有看到诸如
{'state': 'listening'}
。 More importantly, I'm not seeing the results such as {"results": [{"alternatives": [{"transcript": "name the mayflower "}],"final": true}],"result_index": 0}
.更重要的是,我没有看到诸如
{"results": [{"alternatives": [{"transcript": "name the mayflower "}],"final": true}],"result_index": 0}
.
I read the RecognizeStream documentation and tried this code:我阅读了RecognizeStream 文档并尝试了以下代码:
stream.on('message', function(message) {
console.log(message);
});
but that doesn't work.但这不起作用。 I tried
object_mode
in both true
and false
but the output was the same.我在
true
和false
中都尝试了object_mode
,但输出是一样的。
Here's the full code that I'm using:这是我正在使用的完整代码:
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();
};
};
The recognizeMicrophone()
method is a helper that chains together a number of streams. recognizeMicrophone()
方法是一个将多个流链接在一起的助手。 The message
event is fired on one of the streams in the middle. message
事件在中间的流之一上触发。 But, you can get access to that one at stream.recognizeStream
- it's always attached to the last one in the chain in order to support cases like this.但是,您可以在
stream.recognizeStream
访问那个 - 它总是附加到链中的最后一个以支持这样的情况。
So, in your code, it should look something like this:因此,在您的代码中,它应该如下所示:
stream.recognizeStream.on('message', function(frame, data) {
console.log('message', frame, data)
});
However, that is mainly there for debugging.但是,这主要用于调试。 The results JSON should be emitted on the
data
event, if you set objectMode: true
and don't call stream.setEncoding('utf8');
如果您设置
objectMode: true
并且不调用stream.setEncoding('utf8');
,则结果 JSON 应该在data
事件上发出stream.setEncoding('utf8');
. .
(This is somewhat different from Watson Node.js SDK, if you're familiar with it's behavior. There are plans to unify the two, but never enough time...) (这与 Watson Node.js SDK 有点不同,如果您熟悉它的行为。有计划将两者统一,但时间不够...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.