简体   繁体   English

如何将连续的 Speech To Text 服务(使用 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?

I am trying to include a continuous Speech to Text service in an ASP.net application.我正在尝试在 ASP.net 应用程序中包含一个连续的 Speech to Text 服务。 The user uses a microphone on the client side and the speech is captured in a text box.用户在客户端使用麦克风,并在文本框中捕获语音。 The server side will use Microsoft's Cognitive service on Azure.服务器端将在 Azure 上使用微软的认知服务。 I found this article https://codez.deedx.cz/posts/continuous-speech-to-text/ .我发现这篇文章https://codez.deedx.cz/posts/continuous-speech-to-text/ I am not sure how the client side will talk to this API.我不确定客户端将如何与此 API 对话。 Any help or sample code which captures both client and server side will be appreciated.任何捕获客户端和服务器端的帮助或示例代码将不胜感激。

Thanks!谢谢!

If you want to integrate Speech to text(stt) service with your asp.net application , maybe using stt service as an HTML page and integrate it as a view will be the simplest way.如果您想将 Speech to text(stt) 服务与您的 asp.net 应用程序集成,也许将 stt 服务用作 HTML 页面并将其集成为视图将是最简单的方法。

I write a continuous speech to text HTML demo for you,just try the code below :我为你写了一个连续的语音到文本 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>

How to run it :如何运行它:

  1. Save the code as a .html file.将代码另存为 .html 文件。
  2. Replace subscriptionKey and serviceRegion with your own service values , you can find them here :subscriptionKeyserviceRegion替换为您自己的服务值,您可以在此处找到它们:

在此处输入图片说明

3.From the Speech SDK for JavaScript .zip package extract the file microsoft.cognitiveservices.speech.sdk.bundle.js and place it into the folder that contains this sample. 3.从Speech SDK for JavaScript .zip 包中提取文件microsoft.cognitiveservices.speech.sdk.bundle.js并将其放入包含此示例的文件夹中。 Just as below :如下: 在此处输入图片说明

Test Result :测试结果 :

在此处输入图片说明

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM