Mar 31, 2019

Microsoft Cognitive Services – Speech recognize

Microsoft Cognitive services allow you using many advance Machine Learning driven services easily. This offering consists of Visual identification, Speech identification etc. These services typically should be gained as an Azure service. Anyway, here we try Speech recognition API using a trial subscription (no Azure needed).

1. Get the trial subscription

a. Browse to Cognitive Service trial link here.

b. Select Speech API tab

c. Click Get API Key and click Next while region is selected (Default: United States)

d. Resulting page would show relevant API endpoint and keys.

2. Sample Console Application to test

a. Start Visual Studio 2017

b. It is required to have .NET cross-platform development workload is available.
To check go to Tools > Get Tools and Features



c. Open a New Console Application


d. Now install Speech NuGet Package


     Search for Microsoft.CognitiveServices.Speech and install the resulting Package.


e. Add the code as below;

using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;

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

        public static async Task RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("<Subscription Key>", "<Region>");
            // Creates a speech recognizer.
            using (var recognizer = new SpeechRecognizer(config))
            {
                Console.WriteLine("Please say the text you need to convert...");
                var result = await recognizer.RecognizeOnceAsync();
                // Checks result.
                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"We recognized: {result.Text}");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
        }
    }
}

Make sure you replace the <Subscription Key> and <Region> with the values you obtained in 1.d step. Region is westus, if you left default United States region when getting the trial subscription.

f. Run the application and see how what you say is converted to text. Enjoy!


Ref: https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/quickstart-csharp-dotnetcore-windows