如何将连续的语音转文本服务(使用 MS 认知服务)包含到写入页面文本框的 ASP.NET Web 应用程序中?

How to include a continuous Speech To Text service(using MS Cognitive Service) into an ASP.NET web application that writes to text boxes in pages?

我正在尝试在 ASP.net 应用程序中包含一个连续的 Speech to Text 服务。用户在客户端使用麦克风,语音在文本框中被捕获。服务器端将使用 Microsoft 在 Azure 上的 Cognitive 服务。我找到这篇文章 https://codez.deedx.cz/posts/continuous-speech-to-text/ 。我不确定客户端将如何与此对话 API。捕获客户端和服务器端的任何帮助或示例代码将不胜感激。

谢谢!

如果您想将 Speech to text(stt) 服务与您的 asp.net 应用程序集成,也许使用 stt 服务作为 HTML 页面并将其作为视图集成是最简单的方法。

我写了一个连续的语音转文本 HTML 演示给你,试试下面的代码:

<!DOCTYPE html>
<html>
<head>
  <title>Microsoft Cognitive Services Speech SDK JavaScript Quickstart</title>
  <meta charset="utf-8" />
</head>
<body style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:13px;">
  <!-- <uidiv> -->

  <div id="content" style="display:none">
    <table width="100%">
      <tr>
        <td></td>
        <td><h1 style="font-weight:500;">Continuous speech to text demo </h1></td>
      </tr>
        <td></td>
        <td><button id="startRecognizeAsyncButton">Start recognition</button>
        <button id="stopRecognizeAsyncButton">Stop 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>

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

  <script>
    // status fields and start button in UI
    var subscriptionKey = "<your subscription key>";
    var serviceRegion= "<your region>";
    var phraseDiv;
    var startRecognizeAsyncButton;
    var stopRecognizeAsyncButton;
    var SpeechSDK;
    var recognizer;
    document.addEventListener("DOMContentLoaded", function () {
      startRecognizeAsyncButton = document.getElementById("startRecognizeAsyncButton");
      stopRecognizeAsyncButton = document.getElementById("stopRecognizeAsyncButton");
      stopRecognizeAsyncButton.disabled=true;
      phraseDiv = document.getElementById("phraseDiv");
      var speechConfig;
      speechConfig = SpeechSDK.SpeechConfig.fromSubscription(subscriptionKey, serviceRegion);
      speechConfig.speechRecognitionLanguage = "en-US";
      var audioConfig  = SpeechSDK.AudioConfig.fromDefaultMicrophoneInput();
      recognizer = new SpeechSDK.SpeechRecognizer(speechConfig, audioConfig);

      startRecognizeAsyncButton.addEventListener("click", function () {
        startRecognizeAsyncButton.disabled = true;
        stopRecognizeAsyncButton.disabled=false;
        phraseDiv.innerHTML = "";

        recognizer.startContinuousRecognitionAsync();
        recognizer.recognized = function(s, e){
          phraseDiv.innerHTML += e.result.text;
        };
      });

      stopRecognizeAsyncButton.addEventListener("click",function(){
        startRecognizeAsyncButton.disabled = false ; 
        stopRecognizeAsyncButton.disabled = true ; 
        recognizer.stopContinuousRecognitionAsync();

      });
      if (!!window.SpeechSDK) {
        SpeechSDK = window.SpeechSDK;
        startRecognizeAsyncButton.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>
  <!-- </quickstartcode> -->
</body>
</html>

如何 运行 :

  1. 将代码保存为 .html 文件。
  2. subscriptionKeyserviceRegion 替换为您自己的服务值,您可以在此处找到它们:

3.From Speech SDK for JavaScript .zip package 提取文件 microsoft.cognitiveservices.speech.sdk.bundle.js 并将其放入包含此示例的文件夹中。如下所示:

测试结果:

希望对您有所帮助!