azure - 如何让 Microsoft Azure Speech To Text 在程序运行时开始转录? (统一,C#)

标签 azure unity-game-engine speech-recognition azure-cognitive-services azure-language-understanding

我正在尝试使用 Unity3D 中的 Microsoft Azure 认知服务语音转文本 SDK 构建一个简单的应用程序。我关注了this tutorial ,而且效果很好。本教程的唯一问题是语音转文本是通过按钮激活的。当您按下按钮时,它将在整个句子期间进行转录,并且您必须再次按下该按钮才能再次转录。我的问题是,我希望程序在 Unity 中运行后立即开始转录,而不是每次我想转录句子时都必须按一个按钮。

这是代码。

    public async void ButtonClick()
    {
        // Creates an instance of a speech config with specified subscription key and service region.
        // Replace with your own subscription key and service region (e.g., "westus").
        var config = SpeechConfig.FromSubscription("[My API Key]", "westus");

        // Make sure to dispose the recognizer after use!
        using (var recognizer = new SpeechRecognizer(config))
        {
            lock (threadLocker)
            {
                waitingForReco = true;
            }

            // Starts speech recognition, and returns after a single utterance is recognized. The end of a
            // single utterance is determined by listening for silence at the end or until a maximum of 15
            // seconds of audio is processed.  The task returns the recognition text as result.
            // Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
            // shot recognition like command or query.
            // For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
            var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

            // Checks result.
            string newMessage = string.Empty;
            if (result.Reason == ResultReason.RecognizedSpeech)
            {
                newMessage = result.Text;
            }
            else if (result.Reason == ResultReason.NoMatch)
            {
                newMessage = "NOMATCH: Speech could not be recognized.";
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = CancellationDetails.FromResult(result);
                newMessage = $"CANCELED: Reason={cancellation.Reason} ErrorDetails={cancellation.ErrorDetails}";
            }

            lock (threadLocker)
            {
                message = newMessage;
                waitingForReco = false;
            }
        }
    }

    void Start()
    {
        if (outputText == null)
        {
            UnityEngine.Debug.LogError("outputText property is null! Assign a UI Text element to it.");
        }
        else if (startRecoButton == null)
        {
            message = "startRecoButton property is null! Assign a UI Button to it.";
            UnityEngine.Debug.LogError(message);
        }
        else
        {
            // Continue with normal initialization, Text and Button objects are present.
        }
    }

    void Update()
    {
        lock (threadLocker)
        {
            if (startRecoButton != null)
            {
                startRecoButton.interactable = !waitingForReco && micPermissionGranted;
            }
        }
    }

我尝试删除 Button 对象,但语音转文本将无法运行。

任何提示或建议都会很棒。谢谢。

最佳答案

根据您引用的教程脚本中的注释:

// Starts speech recognition, and returns after a single utterance is recognized. The end of a
// single utterance is determined by listening for silence at the end or until a maximum of 15
// seconds of audio is processed.  The task returns the recognition text as result.
// Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
// shot recognition like command or query.
// For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.

但这并不像将“RecognizeOnceAsync”替换为“StartContinouslyRecognitionAsync”那么简单,因为行为不同。 RecognizeOnceAsync 基本上会打开麦克风最多 15 秒,然后停止收听。

相反,将按钮设置为“我是否应该连续收听?”使用 StartContinouslyRecognitionAsyncStopContinouslyRecognitionAsync,然后更改 Start 函数以启动新的识别器并让它等待语音识别器事件到来通过。下面是我用来启用此功能的脚本:

using UnityEngine;
using UnityEngine.UI;
using Microsoft.CognitiveServices.Speech;

public class HelloWorld : MonoBehaviour
{
    public Text outputText;
    public Button startRecordButton;

    // PULLED OUT OF BUTTON CLICK
    SpeechRecognizer recognizer;
    SpeechConfig config;

    private object threadLocker = new object();
    private bool speechStarted = false; //checking to see if you've started listening for speech
    private string message;

    private bool micPermissionGranted = false;

    private void RecognizingHandler(object sender, SpeechRecognitionEventArgs e)
    {
        lock (threadLocker)
        {
            message = e.Result.Text;
        }
    }
    public async void ButtonClick()
    {
        if (speechStarted)
        {
            await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false); // this stops the listening when you click the button, if it's already on
            lock(threadLocker)
            {
                speechStarted = false;
            }
        }
        else
        {
            await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false); // this will start the listening when you click the button, if it's already off
            lock (threadLocker)
            {
                speechStarted = true;
            }
        }

    }

    void Start()
    {
        startRecordButton.onClick.AddListener(ButtonClick);
        config = SpeechConfig.FromSubscription("KEY", "REGION");
        recognizer = new SpeechRecognizer(config);
        recognizer.Recognizing += RecognizingHandler;
    }

    void Update()
    {

        lock (threadLocker)
        {
            if (outputText != null)
            {
                outputText.text = message;
            }
        }
    }
}

下面是我使用此功能的 gif。你不会说我根本没有点击按钮(而且只点击了一次,在录制 gif 之前)(另外,很抱歉这些奇怪的句子,我的同事一直打断问我在和谁说话)

screen recording of continuous speech

关于azure - 如何让 Microsoft Azure Speech To Text 在程序运行时开始转录? (统一,C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57845649/

相关文章:

python - Azure 机器学习请求响应延迟

c# - 从哪里获取 Azure Durable Functions 中 RaiseEventAsync 方法的 InstanceId?

javascript - GetComponent 无法识别 Unity JavaScript 代码中的组件

java - Windows 7 语音识别的 API?

Azure Active Directory 角色管理

azure - 应用服务的 Azure 磁盘空间仅用于与运行应用程序相关的文件,还是也可以存储用户上传的文件?

javascript - Unity3d中的旋转 Angular 约束?

c# - 从父对象调用方法 C# Unity3d

c# - 语音识别程序随机显示语法中不存在的那些口语词的预定义词(在 'GramarBuilder()' 中定义)

c# - 使用特定命令的 C# 语音识别