Azure Cognitive Services Speech to Text Hello World in C#

Leave a comment

In this post we will create a Windows console app that integrates with Azure Cognitive Services Speech Service to convert to text what we are speaking.

In Visual Studio, create a new console app:

You will see:

Add Microsoft.CognitiveServices.Speech through NuGet:

Click on the Solution->Configuration Manager:

Change the platform to your current platform:

Add the code. This is based on this sample code here. For yourapikey, use the key created here. For the region, use your region such as westus.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;

namespace Carl.SpeechRecognitionHelloWorld
{
    class Program
    {
        public static async Task RecognizeSpeechAsync()
        {
            var factory = SpeechFactory.FromSubscription(yourapikey, yourregion);

            using (var recognizer = factory.CreateSpeechRecognizer())
            {
                Console.WriteLine("Say something...");

                var result = await recognizer.RecognizeAsync();
                if (result.RecognitionStatus != RecognitionStatus.Recognized)
                {
                    Console.WriteLine($"Recognition status: {result.RecognitionStatus.ToString()}");
                    if (result.RecognitionStatus == RecognitionStatus.Canceled)
                        Console.WriteLine($"There was an error, reason: {result.RecognitionFailureReason}");
                    else
                        Console.WriteLine("No speech could be recognized.\n");
                }
                else
                    Console.WriteLine($"We recognized: {result.Text}");
            }
        }

        static void Main()
        {
            RecognizeSpeechAsync().Wait();
            Console.WriteLine("Please press a key to continue.");
            Console.ReadLine();
        }
    }
}

Press F5 to run the application. You will see “Say something”. After saying something, it will appear in text, i.e. below I said “Testing 123” and it converted it to text and displayed it on the screen:

Once we get a response from the Speech API, we can use this to look perform any number of actions.

In our next post we will use voice input to look up the weather.

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *