Search code examples
javascriptnode.jsazureapispeech-to-text

REST API azure speech to text (RECOGNIZED: Text=undefined)


I am trying to use the azure api (speech to text), but when I execute the code it does not give me the audio result. The audio is in the format requested (.WAV).

code example documentation

const fs = require('fs');
const sdk = require("microsoft-cognitiveservices-speech-sdk");
const speechConfig = sdk.SpeechConfig.fromSubscription("---", "eastus2");

function fromFile() {
    let pushStream = sdk.AudioInputStream.createPushStream();

    fs.createReadStream("audio/aboutSpeechSdk.wav").on('data', function (arrayBuffer) {
        pushStream.write(arrayBuffer.slice());
    }).on('end', function () {
        pushStream.close();
    });

    let audioConfig = sdk.AudioConfig.fromStreamInput(pushStream);
    let recognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);
    recognizer.recognizeOnceAsync(result => {
        console.log(`RECOGNIZED: Text=${result.text}`);
        recognizer.close();
    });
}

fromFile();


Solution

  • According to the code you provide, it seems that you do not configure Speech Recognition Language. Please add the code speechConfig.speechRecognitionLanguage = "" into you sample. For more details about language, please refer to here

    For example. You can download the video to do a test.

    var sdk = require("microsoft-cognitiveservices-speech-sdk");
    var fs = require("fs");
    var subscriptionKey = "";
    var serviceRegion = "";
    var language = "en-US";
    
    function openPushStream(filename) {
      // create the push stream we need for the speech sdk.
      var pushStream = sdk.AudioInputStream.createPushStream();
    
      // open the file and push it to the push stream.
      fs.createReadStream(filename)
        .on("data", function (arrayBuffer) {
          pushStream.write(arrayBuffer.slice());
        })
        .on("end", function () {
          pushStream.close();
        });
    
      return pushStream;
    }
    
    var audioConfig = sdk.AudioConfig.fromStreamInput(
      openPushStream("aboutSpeechSdk.wav")
    );
    var speechConfig = sdk.SpeechConfig.fromSubscription(
      subscriptionKey,
      serviceRegion
    );
    speechConfig.speechRecognitionLanguage = language;
    var recognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);
    recognizer.recognizeOnceAsync(
      function (result) {
        console.log(result.text);
        recognizer.close();
        recognizer = undefined;
      },
      function (err) {
        console.log(err);
        recognizer.close();
        recognizer = undefined;
      }
    

    enter image description here

    For more details, please refer to the blog