我尝试使用Watson Speech to Text服务实现语音识别。我用"MediaStreamRecorder“库用javascript写了一些代码。我通过Websocket发送数据,得到这个问题:如果我使用"content-type":“音频/wav”,Watson只识别第一个blob,并将inactivity_timeout设置为默认值,而我将其设置为2秒。
我使用以下代码打开websocket:
initWebSocket(startRecordingCallback) {
var that = this;
that.websocket = new WebSocket(that.wsURI);
that.websocket.onopen = function (evt) {
console.log("WebSocket: connection OK ");
var message = {
"action": "start",
"content-type": "audio/wav",
"interim_results": true,
"continuous": true,
"inactivity_timeout": 2
};
that.websocket.send(JSON.stringify(message));
};
that.websocket.onclose = function (evt) {
if (event.wasClean) {
console.log("WebSocket: connection closed clearly " + JSON.stringify(evt));
} else {
console.log("WebSocket: disconnect " + JSON.stringify(evt));
}
};
that.websocket.onmessage = function (evt) {
console.log(evt)
};
that.websocket.onerror = function (evt) {
console.log("WebSocket: error " + JSON.stringify(evt));
};
}录制音频的代码如下:
startRecording() {
var that = this;
this.initWebSocket(function () {
var mediaConstraints = {
audio: true
};
function onMediaSuccess(stream) {
that.mediaRecorder = new MediaStreamRecorder(stream);
that.mediaRecorder.mimeType = 'audio/wav';
that.mediaRecorder.ondataavailable = function (blob) {
that.websocket.send(blob);
};
that.mediaRecorder.start(3000);
}
function onMediaError(e) {
console.error('media error', e);
}
navigator.getUserMedia(mediaConstraints, onMediaSuccess, onMediaError);
});
}我需要做实时识别使用网络套接字与套接字自动关闭后2秒不活动。请给我一些建议。
发布于 2018-02-18 08:41:05
正如@Daniel Bolanos所说,如果文字记录为空的时间超过inactivity_timeout秒,则不会触发inactivity_timeout。该服务使用一种不同的方法来检测是否有语音,而不是依赖于转录。
如果服务检测到语音,即使文本为空,它也不会触发
inactivity_timeout。
下面是一段代码,它使用speech-javascript-sdk完成了您想要处理的问题。希望它能帮助未来的StackOverflow用户识别来自麦克风的音频。
document.querySelector('#button').onclick = function () {
// you need to provide this endpoint to fetch a watson token
fetch('/api/speech-to-text/token')
.then(function(response) {
return response.text();
}).then(function (token) {
var stream = WatsonSpeech.SpeechToText.recognizeMicrophone({
token: token,
outputElement: '#output' // CSS selector or DOM Element
});
stream.on('error', function(err) {
console.log(err);
});
document.querySelector('#stop').onclick = function() {
stream.stop();
};
}).catch(function(error) {
console.log(error);
});
};演示:https://watson-speech.mybluemix.net/microphone-streaming.html
致谢@Nathan Friedly,他是这个库的作者。
https://stackoverflow.com/questions/38971899
复制相似问题