разница между вызовом API и почтальоном - PullRequest
0 голосов
/ 13 января 2019

Я использовал API-интерфейс Azure Speech rest. И я попробовал это на человеке почты с файлом .wav, и это успешно возвращает результат. Тем не менее, когда я вызываю API из моего кода node.js. Он всегда возвращает неподдерживаемый аудиоформат, хотя я даю один и тот же аудиофайл. Может кто-нибудь сказать мне, в чем их разница? Или что почтальон сделал, чтобы все заработало?

Ниже показано, как я называю речевой API от node.js.

'use strict';

const request = require('request');

const subscriptionKey = 'MYSUBSCRIPTIONKEY';

const uriBase = 'https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US';

const options = {
    uri: uriBase,
    body: 'speech.wav',
    headers: {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key' : subscriptionKey,
        'Transfer-Encoding': 'chunked',
        'Expect': '100-continue',
        'Content-type':'audio/wav; codec=audio/pcm; samplerate=16000'
    }
};

request.post(options, (error, response, body) => {
  if (error) {
    console.log('Error: ', error);
    return;
  }
  let jsonResponse = JSON.stringify(JSON.parse(body), null, '  ');
  console.log('JSON Response\n');
  console.log(jsonResponse);
});

1 Ответ

0 голосов
/ 14 января 2019

Вы можете попробовать это

fs.readFile('/path/to/my/audiofile.wav', function (err, data) {
  if (err) throw err;
  var options = {
    host: 'https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US',
    method: 'POST',
    headers: { 'Content-Type': 'audio/wav' }
  };
  var req = http.request(options, function(res) {
    // Handle a successful response here...
  });
  req.on('error', function(e) {
    // Handle an error response here...
  });
  // Write the audio data in the request body.
  req.write(data);
  req.end();
});
...