首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Watson在识别“音频/wav”时忽略不活动超时

Watson在识别“音频/wav”时忽略不活动超时
EN

Stack Overflow用户
提问于 2016-08-16 18:00:15
回答 1查看 565关注 0票数 2

我尝试使用Watson Speech to Text服务实现语音识别。我用"MediaStreamRecorder“库用javascript写了一些代码。我通过Websocket发送数据,得到这个问题:如果我使用"content-type":“音频/wav”,Watson只识别第一个blob,并将inactivity_timeout设置为默认值,而我将其设置为2秒。

我使用以下代码打开websocket:

代码语言:javascript
复制
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));
    };
}

录制音频的代码如下:

代码语言:javascript
复制
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秒不活动。请给我一些建议。

EN

回答 1

Stack Overflow用户

发布于 2018-02-18 08:41:05

正如@Daniel Bolanos所说,如果文字记录为空的时间超过inactivity_timeout秒,则不会触发inactivity_timeout。该服务使用一种不同的方法来检测是否有语音,而不是依赖于转录。

如果服务检测到语音,即使文本为空,它也不会触发inactivity_timeout

下面是一段代码,它使用speech-javascript-sdk完成了您想要处理的问题。希望它能帮助未来的StackOverflow用户识别来自麦克风的音频。

代码语言:javascript
复制
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,他是这个库的作者。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38971899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档