Azure Cognitive Services Speech to Text Hello World in C#

1 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.

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

One Response to Azure Cognitive Services Speech to Text Hello World in C#

  1. Hi Carl,
    SpeechFactory is deprecated or not. Because I try your advice and SpeechFactory not exist.
    If deprecated how I must customize your code?

    Thanks in advance.
    Necdet

Leave a Reply

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