簡體   English   中英

在Javascript / Node.Js中設置Watson Speech to Text API時出現問題

[英]Problems setting up Watson Speech to Text API in Javascript / Node.Js

我正在嘗試將Watson Speech to Text設置為可在Node.Js應用程序中使用。

首先,我在app.js文件中設置服務器端以獲取令牌(這是在設置express服務器(我省略了)的基礎上進行的):

const AuthorizationV1 = require('watson-developer-cloud/authorization/v1');
const SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
const TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
const vcapServices = require('vcap_services');

// speech to text token endpoint
var sttAuthService = new AuthorizationV1(
  Object.assign(
    {
      username: MY_IBM_APP_USERNAME, // or hard-code credentials here
      password: MY_IBM_APP_PASSWORD
    },
    vcapServices.getCredentials('speech_to_text') // pulls credentials from environment in bluemix, otherwise returns {}
  )
);
app.use('/api/speech-to-text/token', function(req, res) {
  sttAuthService.getToken(
    {
      url: SpeechToTextV1.URL
    },
    function(err, token) {
      if (err) {
        console.log('Error retrieving token: ', err);
        res.status(500).send('Error retrieving token');
        return;
      }
      res.send(token);
    }
  );
});

(以上是IBM Speech Javascript SDK的示例)

然后,在實際的服務器端文件中,我具有以下代碼來首先獲取令牌並激活語音輸入(以下示例 ):

              fetch('/api/speech-to-text/token')
.then(function(response) {
    return response.text();
}).then(function (token) {

  var stream = WatsonSpeech.SpeechToText.recognizeMicrophone({
      token: token,
      outputElement: '#statement' // CSS selector or DOM Element to put the text in
  });

  stream.on('error', function(err) {
      console.log(err);
  });

  document.querySelector('#stop').onclick = function() {
    stream.stop();
  };

}).catch(function(error) {
    console.log(error);
});

但是,在Chrome上,我收到錯誤消息“無法構建Web套接字”,而在Safari上,我只得到/api/speech-to-text/token 404錯誤,該錯誤應該得到令牌。

我究竟做錯了什么? 哪里出錯?

謝謝!

我能夠重現您的問題-該示例似乎正在使用包含錯誤的較早版本的watson-developer-cloud

更新到最新版本的watson-developer-cloud (v3.12.0)為我解決了此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM