Home > .NET, .NET Core, C#, WPF > Continuous Speech Recognition in .NET Core WPF applications

Continuous Speech Recognition in .NET Core WPF applications

In the last article we talked about how to integrate Speech Recognition in .NET Core WPF applications with the Cognitive Services Speech SDK. In that occasion, we shown how to use the RecognizeOnceAsync method to recognize a single utterance. But the Speech SDK supports also continuous recognition, so today we’ll see how to leverage this feature.

Basically, instead of RecognizeOnceAsync, we need to use StartContinuousRecognitionAsync method and subscribe to the appropriate events.

Starting from the previous sample, as usual we have to define some constants and a reference to a SpeechRecognizer object:

private const string SpeechSubscriptionKey = "";
private const string SpeechRegion = "";
private const string Culture = "";

private readonly SpeechRecognizer speechRecognizer;
private bool isRecognizing;

Then, in the constructor of the Window we can initialize the recognizer:

public MainWindow()
{
    InitializeComponent();

    var config = SpeechConfig.FromSubscription
        (SpeechSubscriptionKey, SpeechRegion);

    speechRecognizer = new SpeechRecognizer(config, Culture);
    speechRecognizer.Recognized += SpeechRecognizer_Recognized;
}

At line 9 we register to the RecognizedEvent evebt, that is raised every time a final recognition result is received:

private async void SpeechRecognizer_Recognized(object sender, 
    SpeechRecognitionEventArgs e)
{
    if (e.Result.Reason == ResultReason.RecognizedSpeech)
    {
        await Dispatcher.InvokeAsync(() =>
            RecognitionTextBox.Text += $"{e.Result.Text} ");
    }
}

If the recognition succeeds (line 4), we use the Dispatcher (line 6, because recognition is performed in a background task) to add the recognized text to the TextBox.

Now we only need to start and stop recognition when the user press the corresponding button:

private async void RecognitionButton_Click(object sender, RoutedEventArgs e)
{
    if (!isRecognizing)
    {
        var isMicAvailable = await CheckEnableMicrophoneAsync();
        if (!isMicAvailable)
        {
            return;
        }

        await speechRecognizer.StartContinuousRecognitionAsync();

        RecognitionTextBox.Text = string.Empty;
        isRecognizing = true;
        RecognitionButton.Content = "Stop Recognition";
    }
    else
    {
        await speechRecognizer.StopContinuousRecognitionAsync();

        isRecognizing = false;
        RecognitionButton.Content = "Start Recognition";
    }
}

The CheckEnableMicrophoneAsync method is the same you can find in the previous article. After we call the StartContinuousRecognitionAsync method at lines 11, the SDK begins to raise the RecognizedEvent event for each recognized utterance. To stop this behavior, we need to invoke the StopContinuousRecognitionAsync (lines 19).

So, here it is our application running:

The WPF Continuous Recognition App

The WPF Continuous Recognition App

You can download the sample app using the link below:

Continuous Speech Recognition in .NET Core WPF applications

Categories: .NET, .NET Core, C#, WPF

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: