Sunday, 23 June 2019

Voice Bot Assistance Part 1 - Microsoft.CognitiveServices.Speech configuration on Azure

As Siri and Google assistance is popular now these days, I thought of exploring developing one for mine. SO I started with System.Speech.dll 1.0.4 nuget package which comes out to be  not compatible with netcoreapp2.1 (.NETCoreApp,Version=v2.1).
Hence started looking for Microsoft.CognitiveServices.Speech Speech Services Documentation.

For this service to use first we have to create a Azure account and then we need to create a Speech Resource. Follow below steps to create the resource :

1. Login in to Azure portal using this Link .click on Create a resource and search for "Speech". Select Speech and click on create.



2. Fill in the details here, Resource Name , Subscription , Location(it is important. Respective region code will be used while coding), Pricing tier and Resource Group and click on create. Mine is free trial and below are the entries :

3. Once it is created, look for the subscription key. for this click on All resource , search for your resource.

4. Click on your resource and click on "Grab your Keys". Copy one of your key.



Hurray!! the configuration is done. Now we will consume it and convert our microphone speech to text.

Its very easy to convert what do you speak to text and vice versa. for this what I have done is to create a Visual studio solution and added a class library solution. I added nuget package of Microsoft.CognitiveServices.Speech. and write code as per the guidelines by Microsoft.

For Speech to Text, I wrote below code:

public async Task<string> ConvertSpeechtoTextFromMicrophone()
        {            
            SpeechConfig config = SpeechConfig.FromSubscription(Subscription.key, Subscription.region);
            string result = string.Empty;
            using (var recognizer = new SpeechRecognizer(config))
            {                
                var recognizerAsync = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);
                if (recognizerAsync.Reason == ResultReason.RecognizedSpeech) result += recognizerAsync.Text;
                else if (recognizerAsync.Reason == ResultReason.NoMatch) result += ("Error");
                else if (recognizerAsync.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(recognizerAsync);
                    result +=  ($"CANCELED: Reason={cancellation.Reason}");
                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        result += ($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        result += ($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        result += ($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
            return result;
        }


Here are the links to understand how it works :

And for Text to Speech , I wrote below code :

 
public async Task CovertTextToSpeechFromMicrophone(string TextToSpeek)
        {
            SpeechConfig config = SpeechConfig.FromSubscription(Subscription.key, Subscription.region);
            // Creates a speech synthesizer using the default speaker as audio output.
            using (var synthesizer = new SpeechSynthesizer(config))
            {
                using (var result = await synthesizer.SpeakTextAsync(TextToSpeek))
                {
                    if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                    {
                        Console.WriteLine($"Voice Bot Assistance said :  [{TextToSpeek}]");
                    }
                    else if (result.Reason == ResultReason.Canceled)
                    {
                        var cancellation = SpeechSynthesisCancellationDetails.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?");
                        }
                    }
                }
            }
        }

Here are the links :

It was easy :-).  You can find my code here on github :


Now Next is to train my program to answer my questions. For this read my next blog on Voice Bot Assistance Part 2 - Microsoft QandA Maker Service on Azure



3 comments: