如何将连续语音转换为文本服务(使用MS Cognitive Service)纳入ASP.NET Web应用程序中,以将其写入页面中的文本框?

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

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

谢谢!

c# asp.net speech-to-text microsoft-cognitive
1个回答
0
投票

[如果您想通过asp.net应用程序将语音集成到文本(stt)服务,也许将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,您可以在这里找到它们:

enter image description here

3。从Speech SDK for JavaScript .zip package中提取文件microsoft.cognitiveservices.speech.sdk.bundle.js,并将其放入包含此样本的文件夹中。如下所示:enter image description here

测试结果:

enter image description here

希望有帮助!

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