JS AzureSDK创建自定义功能以捕获语音,显示文本结果和结果的置信度

时间:2019-10-15 02:54:33

标签: javascript azure speech-to-text speech

我需要创建一个简单的javascript函数来捕获输入,然后使用AzureSDK以置信度百分比返回文本。

我最大的问题是我刚接触编码,这是我面临的最困难的问题,所以请对这个谦虚的学生好一点。

我正在使用语音输入构建语言学习网络应用。我已经能够使google服务按我想要的方式工作,但是不幸的是,这些服务在我所在的市场不起作用。我也在使用Phaser 3 API来构建此应用。

我已经能够在git上提供示例代码,以使AzureSDK语音文本javascript正常工作,但是当我尝试使用该代码创建自己的函数时,我得到:  未捕获的TypeError:无法读取未定义的属性'SpeechConfig'

我也不知道如何为语音结果添加置信度。

recordButton.on('pointerdown', function() {
        var SDK =  window.SpeechSDK;
        try {
        AudioContext = window.AudioContext // our preferred impl
            || window.webkitAudioContext   // fallback, mostly for Safari
            || false;                      // could not find.
        if (AudioContext) {
            soundContext = new AudioContext();
          console.log("AudioContext", AudioContext);
        } else {
            alert("Audio context not supported");
        }
        }
        catch (e) {
         console.log("no sound context found, no audio output. " + e);
        }

        console.log("SpeechSDK initialized", SDK);

        speechConfig = 
          SpeechSDK.SpeechConfig.fromSubscription(subscriptionKey, 
           serviceRegion);

        speechConfig.speechRecognitionLanguage = "en-US";
        console.log("speechConfig", SpeechConfig);      

        audioConfig  = SpeechSDK.AudioConfig.fromDefaultMicrophoneInput();
        recognizer = new SpeechSDK.SpeechRecognizer(speechConfig, 
           audioConfig);
        recognizer.recognizeOnceAsync(
        function (result) {
          console.log("result", result); 
          recognizer.close();
          recognizer = undefined;
        },
        function (err) {
          console.log(err);
          recognizer.close();
          recognizer = undefined;
        });
}, this);

我需要捕获语音输入,然后显示学生所说的单词/短语/句子,并根据置信度对它们进行评分。

1 个答案:

答案 0 :(得分:0)

如果您要对从语音转换为文本SDK所获得的文本值获得可靠的评分,请尝试以下代码:

   <html>
    <head>
       <title>Speech SDK JavaScript Quickstart</title>
    </head>
    <script src="microsoft.cognitiveservices.speech.sdk.bundle.js"></script>

<body>
 <div id="warning">
  <h1 style="font-weight:500;">Speech Recognition Speech SDK not found (microsoft.cognitiveservices.speech.sdk.bundle.js missing).</h1>
</div>

<div id="content" style="display:none">
  <table width="100%">
    <tr>
      <td></td>
      <td><h1 style="font-weight:500;">Microsoft Cognitive Services Speech SDK JavaScript Quickstart</h1></td>
    </tr>
    <tr>
      <td align="right"><a href="https://docs.microsoft.com/azure/cognitive-services/speech-service/get-started" target="_blank">Subscription</a>:</td>
      <td><input id="subscriptionKey" type="text" size="40" value="subscription"></td>
    </tr>
    <tr>
      <td align="right">Region</td>
      <td><input id="serviceRegion" type="text" size="40" value="YourServiceRegion"></td>
    </tr>
    <tr>
      <td></td>
      <td><button id="startRecognizeOnceAsyncButton">Start recognition</button></td>
    </tr>
    <tr>
      <td align="right" valign="top">Results</td>
      <td><textarea id="phraseDiv" style="display: inline-block;width:500px;height:200px"></textarea></td>
    </tr>
  </table>
</div>
</body>



<!-- Speech SDK USAGE -->
<script>
  // status fields and start button in UI
  var phraseDiv;
  var startRecognizeOnceAsyncButton;

  // subscription key and region for speech services.
  var subscriptionKey, serviceRegion;
  var authorizationToken;
  var SpeechSDK;
  var recognizer;

  document.addEventListener("DOMContentLoaded", function () {
    startRecognizeOnceAsyncButton = document.getElementById("startRecognizeOnceAsyncButton");
    subscriptionKey = document.getElementById("subscriptionKey");
    serviceRegion = document.getElementById("serviceRegion");
    phraseDiv = document.getElementById("phraseDiv");

    startRecognizeOnceAsyncButton.addEventListener("click", function () {
      startRecognizeOnceAsyncButton.disabled = true;
      phraseDiv.innerHTML = "";

      // if we got an authorization token, use the token. Otherwise use the provided subscription key
      var speechConfig;
      if (authorizationToken) {
        speechConfig = SpeechSDK.SpeechConfig.fromAuthorizationToken(authorizationToken, serviceRegion.value);
      } else {
        if (subscriptionKey.value === "" || subscriptionKey.value === "subscription") {
          alert("Please enter your Microsoft Cognitive Services Speech subscription key!");
          return;
        }
        speechConfig = SpeechSDK.SpeechConfig.fromSubscription(subscriptionKey.value, serviceRegion.value);
      }

      speechConfig.speechRecognitionLanguage = "en-US";
      speechConfig.outputFormat=1;
      var audioConfig  = SpeechSDK.AudioConfig.fromDefaultMicrophoneInput();
      recognizer = new SpeechSDK.SpeechRecognizer(speechConfig, audioConfig);

      recognizer.recognizeOnceAsync(
        function (result) {
          startRecognizeOnceAsyncButton.disabled = false;
          phraseDiv.innerHTML += "Recognize Result:"+ result.text + "\nConfidence Score:" + JSON.parse(result.json).NBest[0].Confidence;

          window.console.log(result);

          recognizer.close();
          recognizer = undefined;
        },
        function (err) {
          startRecognizeOnceAsyncButton.disabled = false;
          phraseDiv.innerHTML += err;
          window.console.log(err);

          recognizer.close();
          recognizer = undefined;
        });
    });

    if (!!window.SpeechSDK) {
      SpeechSDK = window.SpeechSDK;
      startRecognizeOnceAsyncButton.disabled = false;

      document.getElementById('content').style.display = 'block';
      document.getElementById('warning').style.display = 'none';

      // in case we have a function for getting an authorization token, call it.
      if (typeof RequestAuthorizationToken === "function") {
          RequestAuthorizationToken();
      }
    }
  });
</script>
</html>

运行与指示的official doc相同的页面。简而言之,在使用sdk时,您应该配置speechConfig.outputFormat=1,以便获得包含信心分数值的语音服务的详细格式。

结果: enter image description here

在您的代码中,未定义的错误似乎是由于您要打印SpeechConfig而导致,该参数被定义为speechConfig ...

无论如何,为了演示成功获得自信分数,我的代码基于官方演示。希望能帮助到你 。

对于您的代码,请尝试以下html:

<html>

<body>

<button id='recordButton' onclick = 'test()'>test </button>
</body>

<script src="microsoft.cognitiveservices.speech.sdk.bundle.js"></script>
<script>

function test(){
        var SDK =  window.SpeechSDK;
        try {
        AudioContext = window.AudioContext // our preferred impl
            || window.webkitAudioContext   // fallback, mostly for Safari
            || false;                      // could not find.
        if (AudioContext) {
            soundContext = new AudioContext();
          console.log("AudioContext", AudioContext);
        } else {
            alert("Audio context not supported");
        }
        }
        catch (e) {
         console.log("no sound context found, no audio output. " + e);
        }

        console.log("SpeechSDK initialized", SDK);

        var speechConfig = 
          SpeechSDK.SpeechConfig.fromSubscription("<your subscription key>", 
           "<your service region>");

        speechConfig.speechRecognitionLanguage = "en-US";
        console.log("speechConfig", speechConfig);      

        audioConfig  = SpeechSDK.AudioConfig.fromDefaultMicrophoneInput();
        recognizer = new SpeechSDK.SpeechRecognizer(speechConfig, 
           audioConfig);
        recognizer.recognizeOnceAsync(
        function (result) {
          console.log("result", result); 
          recognizer.close();
          recognizer = undefined;
        },
        function (err) {
          console.log(err);
          recognizer.close();
          recognizer = undefined;
        });
}

</script>
</html>

结果:如您所见,结果已记录:

enter image description here

如果我的答案有帮助,请单击答案旁边的复选标记以将其从灰色切换为填充以标记该答案,谢谢!