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

问题描述 投票:0回答:1

我需要创建一个简单的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);

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

javascript azure speech-to-text speech
1个回答
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 ...

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

© www.soinside.com 2019 - 2024. All rights reserved.