Home > .NET, .NET Core, C#, WPF > Integrating Cognitive Service Speech Recognition in .NET Core WPF applications

Integrating Cognitive Service Speech Recognition in .NET Core WPF applications

Some times ago we talked about how to integrate the Cognitive Service Speech Service in UWP apps. Now, let’s see how to use the same service in a .NET Core WPF application.

In the original article, we shown how to use the UWP MediaCapture APIs to check the availability of the microphone. Thanks to the Microsoft.Windows.SDK.Contracts NuGet package, we can use the same APIs also from .NET Framework 4.6+ and .NET Core 3.0+ platforms:

The Microsoft.Windows.SDK.Contracts NuGet package

The Microsoft.Windows.SDK.Contracts NuGet package

At the time of writing, this package includes all the supported Windows Runtime APIs up to Windows 10 version 2004. So, in order to check whether the microphone is enabled and available, we can use the same exact code of the old article:

private async Task<bool> CheckEnableMicrophoneAsync()
{
    var isMicAvailable = false;

    try
    {
        var mediaCapture = new MediaCapture();
        var settings = new MediaCaptureInitializationSettings
        {
            StreamingCaptureMode = StreamingCaptureMode.Audio
        };

        await mediaCapture.InitializeAsync(settings);
        isMicAvailable = true;
    }
    catch
    {
        await Launcher.LaunchUriAsync
            (new Uri("ms-settings:privacy-microphone"));
    }

    return isMicAvailable;
}

And now we can add the Microsoft.CognitiveServices.Speech NuGet package to the project and use a slight modified version of the code of the original article (because in the meantime the SDK has changed a bit):

private async void RecognitionButton_Click(object sender, RoutedEventArgs e)
{
    const string SpeechSubscriptionKey = "";
    const string SpeechRegion = "";
    const string Culture = "";

    var isMicAvailable = await CheckEnableMicrophoneAsync();
    if (!isMicAvailable)
    {
        return;
    }

    RecognitionButton.Content = "Recognizing...";
    RecognitionButton.IsEnabled = false;
    RecognitionTextBox.Text = string.Empty;

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

    // Starts recognition. It returns when the first utterance has been 
    // recognized.
    using var cognitiveRecognizer = new SpeechRecognizer(config, Culture);

    var result = await cognitiveRecognizer.RecognizeOnceAsync();

    // Checks result.
    if (result.Reason == ResultReason.RecognizedSpeech)
    {
        RecognitionTextBox.Text = result.Text;
    }
    else
    {
        MessageBox.Show(result.Reason.ToString(), "Recognition Result",
            MessageBoxButton.OK, MessageBoxImage.Warning);
    }

    RecognitionButton.Content = "Start recognition";
    RecognitionButton.IsEnabled = true;
}

The lines 3-5 must be completed with the Speech subscription key that we can find on Azure portal, the name of the region in which the service has been created and the language of the speech.

After checking for the microphone availability at lines 7-11, we can start the real recognition process. We instantiate a SpeechConfig at lines 17-18 and use it to create the Cognitive Speech recognizer at line 22. The RecognizeOnceAsync method (line 25) starts speech recognition and stops after the first utterance is recognized.

If the Reason property is equal to RecognizedSpeech (line 27), it means that the recognition succeeded, so we can read the Text property (line 29) to access the recognized text:

The WPF Recognition App

The WPF Recognition App

You can download the sample app using the link below:

Integrating Cognitive Service Speech Recognition in .NET Core WPF applications

As said before, RecognizeOnceAsync returns when the first utterance that has been recognized, so it is suitable only for single shot recognition like command or query. For long-running recognition, we can use the StartContinuousRecognitionAsync method instead. We’ll talk about that in a next article.

Categories: .NET, .NET Core, C#, WPF
  1. No comments yet.
  1. 03/08/2020 at 13:37

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: