예제 #1
0
        public async Task StartAsync(string fileName = null)
        {
            var speechConfig = SpeechConfig.FromSubscription(this.settings.SubscriptionKey, this.settings.Region);

            speechConfig.SpeechRecognitionLanguage = "de-de";
            speechConfig.OutputFormat = OutputFormat.Detailed;

            using (var audioInput = fileName == null ? AudioConfig.FromDefaultMicrophoneInput() : AudioConfig.FromWavFileInput(fileName))
            {
                using (var intentRecognizer = new IntentRecognizer(speechConfig, audioInput))
                {
                    stopRecognition = new TaskCompletionSource <int>();

                    var model = LanguageUnderstandingModel.FromAppId(this.settings.LuisAppId);

                    intentRecognizer.AddAllIntents(model);

                    intentRecognizer.SessionStarted      += IntentRecognizer_SessionStarted;
                    intentRecognizer.Recognized          += IntentRecognizer_Recognized;
                    intentRecognizer.Recognizing         += IntentRecognizer_Recognizing;
                    intentRecognizer.SessionStopped      += IntentRecognizer_SessionStopped;
                    intentRecognizer.SpeechEndDetected   += IntentRecognizer_SpeechEndDetected;
                    intentRecognizer.SpeechStartDetected += IntentRecognizer_SpeechStartDetected;
                    intentRecognizer.Canceled            += IntentRecognizer_Canceled;

                    await intentRecognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                    Task.WaitAny(stopRecognition.Task);

                    await intentRecognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
                }
            }
        }
예제 #2
0
        public static async Task <string> ProcessAudioAsync()
        {
            string       key              = "<KEY>";
            string       region           = "<REGION>";
            SpeechConfig configRecognizer = SpeechConfig.FromSubscription(key, region);
            string       processedAudio   = "";

            bool isRecorded = CheckAudioFile();

            if (isRecorded)
            {
                using (AudioConfig audioInput = AudioConfig.FromWavFileInput(_audioFile))
                    using (IntentRecognizer recognizer = new IntentRecognizer(configRecognizer, audioInput))
                    {
                        TaskCompletionSource <int> stopRecognition = new TaskCompletionSource <int>();

                        recognizer.Recognized += (s, e) =>
                        {
                            if (e.Result.Reason == ResultReason.RecognizedSpeech)
                            {
                                processedAudio = e.Result.Text;
                            }
                        };

                        recognizer.Canceled += (s, e) => {
                            if (e.Reason == CancellationReason.Error)
                            {
                                //log
                            }
                            stopRecognition.TrySetResult(0);
                        };

                        recognizer.SessionStarted += (s, e) => {
                            //log
                        };

                        recognizer.SessionStopped += (s, e) => {
                            //log

                            stopRecognition.TrySetResult(0);
                        };

                        await recognizer.StartContinuousRecognitionAsync();

                        Task.WaitAny(new[] { stopRecognition.Task });

                        await recognizer.StopContinuousRecognitionAsync();
                    }

                //log

                return(processedAudio);
            }
            else
            {
                //log

                return(processedAudio);
            }
        }
        public async Task RecognizeSpeech()
        {
            var audioConfig  = AudioConfig.FromDefaultMicrophoneInput();
            var speechConfig = SpeechConfig.FromSubscription(key, "westus2");

            // Creates a speech recognizer.
            using (var recognizer = new IntentRecognizer(speechConfig, audioConfig))
            {
                // Hide user secrets later
                var model = LanguageUnderstandingModel.FromAppId(Environment.GetEnvironmentVariable("LUIS_APP_ID"));
                recognizer.AddAllIntents(model);

                var stopRecognition = new TaskCompletionSource <int>();
                // Can add logic to exit using voice command, "Thanks see you at the window" etc.
                // Subscribe to appropriate events
                recognizer.Recognizing += (s, e) =>
                {
                    // Use this to send partial responses
                    Console.WriteLine($"Partial: {e.Result.Text}");
                };

                recognizer.Recognized += (s, e) =>
                {
                    var exit = ProcessRecognizedText(s, e);
                    if (exit)
                    {
                        recognizer.StopContinuousRecognitionAsync().Wait(); //ConfigureAwait(false);
                    }
                };

                recognizer.SessionStarted += (s, e) =>
                {
                    Console.WriteLine("Session started event.");
                };

                recognizer.SessionStopped += (s, e) =>
                {
                    Console.WriteLine("Session stopped event.");
                    stopRecognition.TrySetResult(0);
                };

                recognizer.Canceled += (s, e) =>
                {
                    Console.WriteLine(e.ErrorDetails);
                    stopRecognition.TrySetResult(0);
                };

                // Instantiate new Order object
                _order = new Order();

                Console.WriteLine("Say something to get started, or \"Exit\" to quit.");
                await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                // Need more understanding about this part
                Task.WaitAny(new[] { stopRecognition.Task });
            }
        }
예제 #4
0
    /// <summary>
    /// IntentRecognizer & event handlers cleanup after use
    /// </summary>
    private async void StopIntentRecognition()
    {
        if (recognizer != null)
        {
            await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);

            recognizer.Recognizing -= Recognizer_Recognizing;
            recognizer.Recognized  -= Recognizer_Recognized;
            recognizer.Canceled    -= Recognizer_Canceled;
            recognizer.Dispose();
            recognizer = null;
        }
    }
    /// <summary>
    /// IntentRecognizer & event handlers cleanup after use
    /// </summary>
    public async void StopIntentRecognition()
    {
        if (intentreco != null)
        {
            await intentreco.StopContinuousRecognitionAsync().ConfigureAwait(false);

            intentreco.Recognizing         -= RecognizingHandler;
            intentreco.Recognized          -= RecognizedHandler;
            intentreco.SpeechStartDetected -= SpeechStartDetectedHandler;
            intentreco.SpeechEndDetected   -= SpeechEndDetectedHandler;
            intentreco.Canceled            -= CanceledHandler;
            intentreco.SessionStarted      -= SessionStartedHandler;
            intentreco.SessionStopped      -= SessionStoppedHandler;
            intentreco.Dispose();
            intentreco       = null;
            recognizedString = "Intent Recognizer is now stopped.";
            UnityEngine.Debug.LogFormat("Intent Recognizer is now stopped.");
        }
    }
예제 #6
0
 private async Task Stop_Button_Click(object sender, RoutedEventArgs e)
 {
     await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
 }
예제 #7
0
        private async void ConfigureIntentRecognizer()
        {
            var config = SpeechConfig.FromSubscription("e4c237841b7043b9b2c73a432a85416c", "westus");

            config.SpeechRecognitionLanguage = "es-es";
            var stopRecognition = new TaskCompletionSource <int>();

            using (recognizer = new IntentRecognizer(config))
            {
                var model = LanguageUnderstandingModel.FromAppId("ce892100-78a7-48b0-8e09-5dc18b16996d");
                recognizer.AddAllIntents(model, "Id1");

                recognizer.Recognized += async(s, e) =>
                {
                    await recognizedText.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        //recognizedText.Text = e.Result.Text;

                        //var prediction = predictionResult.Prediction;
                        //MyMessageBox(prediction.TopIntent);
                    });

                    try
                    {
                        var predictionResult = GetPredictionAsync(e.Result.Text).Result;
                        var prediction       = predictionResult.Prediction;


                        if (prediction.TopIntent == "None")
                        {
                            await recognizedText.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                recognizedText.Text = "No entiendo la acción que me pides.";
                            });

                            muestraHora  = false;
                            muestraTexto = false;
                        }
                        else if (prediction.TopIntent == "MuestraHora")
                        {
                            await recognizedText.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                var now = DateTime.Now;
                                //recognizedText.Text = now.ToString("HH:mm");
                                recognizedText.Text = "Son las " + now.Hour + ":" + now.Minute + ".";
                            });

                            muestraHora  = true;
                            muestraTexto = false;
                        }
                        else if (prediction.TopIntent == "EscribeTexto")
                        {
                            await recognizedText.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                                                                     {;
                                                                      recognizedText.Text = "Esto es un texto."; });

                            muestraTexto = true;
                            muestraHora  = false;
                        }
                        else if (prediction.TopIntent == "OcultaHora")
                        {
                            await recognizedText.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                if (muestraHora)
                                {
                                    recognizedText.Text = "Hora oculta.";
                                    muestraHora         = false;
                                    muestraTexto        = false;
                                }
                            });
                        }
                        else if (prediction.TopIntent == "OcultaTexto")
                        {
                            await recognizedText.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                if (muestraTexto)
                                {
                                    recognizedText.Text = "Texto ocultado.";
                                    muestraHora         = false;
                                    muestraTexto        = false;
                                }
                            });
                        }
                        else if (prediction.TopIntent == "CambioColor")
                        {
                            string color = "";
                            await recognizedText.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                //string item = prediction.Entities.First().Value.ToString();
                                color = prediction.Entities.First().Value.ToString().Replace(System.Environment.NewLine, "");
                                color = color.Replace("[", "");
                                color = color.Replace("]", "");
                                color = color.Replace("\"", "");
                                color = color.Replace(" ", "");
                                recognizedText.Text = "Estableciendo fondo de color " + color + ".";
                                muestraHora         = false;
                                muestraTexto        = false;
                            });

                            try
                            {
                                if (colors.ContainsKey(color))
                                {
                                    await rectangle.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                                    {
                                        rectangle.Fill = new SolidColorBrush(colors[color]);
                                    });
                                }
                            }
                            catch (NullReferenceException) { }
                        }
                    }
                    catch (System.AggregateException)
                    {
                    }

                    /*if (e.Result.Reason == ResultReason.RecognizedIntent)
                     * {
                     *
                     *  var jsonResponse = JObject.Parse(e.Result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult));
                     *  await textJson.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                     *  {
                     *      textJson.Text = jsonResponse.ToString();
                     *  });
                     *  var intent = jsonResponse.SelectToken("topScoringIntent.intent").ToString();
                     *
                     *  if (intent.Equals("CambioColor"))
                     *  {
                     *      try
                     *      {
                     *          string color = jsonResponse.SelectToken("$..entities[?(@.type=='Color')].entity").ToString();
                     *          if (colors.ContainsKey(color))
                     *              await rectangle.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                     *              {
                     *                  rectangle.Fill = new SolidColorBrush(colors[color]);
                     *              });
                     *      }
                     *      catch (NullReferenceException) { }
                     *
                     *
                     *
                     *  }
                     *
                     *
                     * }
                     * else if (e.Result.Reason == ResultReason.RecognizedSpeech)
                     * {
                     *  await recognizedText.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                     *  {
                     *      recognizedText.Text = e.Result.Text;
                     *  });
                     * }
                     * else if (e.Result.Reason == ResultReason.NoMatch)
                     * {
                     *  await recognizedText.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                     *  {
                     *      recognizedText.Text = "Speech could not be recognized.";
                     *  });
                     * }*/
                };

                recognizer.Canceled += (s, e) =>
                {
                    Console.WriteLine($"CANCELED: Reason={e.Reason}");

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

                recognizer.SessionStopped += (s, e) =>
                {
                    Console.WriteLine("\n    Session stopped event.");
                    Console.WriteLine("\nStop recognition.");
                    stopRecognition.TrySetResult(0);
                };

                Console.WriteLine("Say something...");
                await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                // Waits for completion.
                // Use Task.WaitAny to keep the task rooted.
                Task.WaitAny(new[] { stopRecognition.Task });

                // Stops recognition.
                await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
            }
        }
                    public static async Task TextBasedAssistant()
                    {
                        // <intentContinuousRecognitionWithFile>
                        // Creates an instance of a speech config with specified subscription key
                        // and service region. Note that in contrast to other services supported by
                        // the Cognitive Services Speech SDK, the Language Understanding service
                        // requires a specific subscription key from https://www.luis.ai/.
                        // The Language Understanding service calls the required key 'endpoint key'.
                        // Once you've obtained it, replace with below with your own Language Understanding subscription key
                        // and service region (e.g., "westus").
                        var config = SpeechConfig.FromSubscription("", "westus");

                        using (var recognizer = new IntentRecognizer(config))
                        {
                            // Creates a Language Understanding model using the app id, and adds specific intents from your model
                            var model = LanguageUnderstandingModel.FromAppId("");
                            recognizer.AddIntent(model, "DiscoverDealsForTheDay", "id1");
                            recognizer.AddIntent(model, "DiscoverLocation", "id2");
                            recognizer.AddIntent(model, "DiscoverPersonalizedDeals", "id3");
                            recognizer.AddIntent(model, "GoodBye", "id4");
                            recognizer.AddIntent(model, "Greeting", "id5");
                            recognizer.AddIntent(model, "HowToGoToTrialRoom", "id6");
                            recognizer.AddIntent(model, "DiscoverPaymentQueueLength", "id7");

                            // Subscribes to events.
                            recognizer.Recognizing += (s, e) => {
                                //Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
                            };

                            recognizer.Recognized += (s, e) => {
                                if (e.Result.Reason == ResultReason.RecognizedIntent)
                                {
                                    Console.WriteLine($"\nText={e.Result.Text}");
                                    //Console.WriteLine($"    Intent Id: {e.Result.IntentId}.");
                                    string Response = e.Result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult);
                                    //Console.Write(Response);
                                    dynamic IntentResponse = JObject.Parse(Response);
                                    dynamic IntentResult   = JObject.Parse(IntentResponse.topScoringIntent.ToString());
                                    string  Intent         = IntentResult.intent.ToString();
                                    Console.WriteLine("Intent: " + Intent);
                                    String TextResult = "";
                                    if (Intent == "Greeting")
                                    {
                                        TextResult = "Hello Suneetha, Welcome back to Shopper's Heaven. Last time you have visited us on Jan 1st ,2019 for some new year offers. How can I help you today ?";
                                    }
                                    else if (Intent == "DiscoverDealsForTheDay")
                                    {
                                        TextResult = "Sure...Give me 2 mins.. We have got great offers for you today. Women and Kids Casual Wears having 40 % discount offers. Women Foot wear section had just launched 60 % clearance sale offer. And cosmetics section is having personalized offer for you";
                                    }
                                    else if (Intent == "DiscoverLocation")
                                    {
                                        TextResult = "Please go till Gate 2 and take the elevator to second floor. You can find Woman's casual wear section on your righthand side";
                                    }
                                    else if (Intent == "HowToGoToTrialRoom")
                                    {
                                        TextResult = "Trail toom is just 200 metres away from you on your left hand side. But looks like all trial rooms are busy now. Please try after 10 mins";
                                    }
                                    else if (Intent == "DiscoverPersonalizedDeals")
                                    {
                                        TextResult = "Please proceed to cosmetic sections at the entrance of second floor. Swith to scan mode in your app and just scan the product that you are interested to buy. You will get Virtual vouchers for that product which you can redeem when you are buying it";
                                    }
                                    else if (Intent == "DiscoverPaymentQueueLength")
                                    {
                                        TextResult = "Its looks good Suneetha. You will be 3rd person in the queue. Please proceed";
                                    }
                                    else if (Intent == "GoodBye")
                                    {
                                        TextResult = "Thanks Suneetha for visiting us again. Hope you enjoyed shopping with me today. See you soon. Last but not least, please do not forget to give feedback about your experience at Shopper Heaven today at the link..";
                                    }
                                    else
                                    {
                                        TextResult = "No Intent Found";
                                    }
                                    Console.WriteLine(TextResult + "\n");
                                    //{
                                    //    "query": "hello",
                                    //      "topScoringIntent": {
                                    //        "intent": "Greeting",
                                    //        "score": 0.9444705
                                    //      },
                                    //      "entities": []
                                    //    }
                                }
                                else if (e.Result.Reason == ResultReason.RecognizedSpeech)
                                {
                                    //Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
                                    //Console.WriteLine($"    Intent not recognized.");
                                }
                                else if (e.Result.Reason == ResultReason.NoMatch)
                                {
                                    //Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                                }
                            };

                            recognizer.Canceled += (s, e) => {
                                Console.WriteLine($"CANCELED: Reason={e.Reason}");

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

                            recognizer.SessionStarted += (s, e) => {
                                //Console.WriteLine("\nSession started");
                            };

                            recognizer.SessionStopped += (s, e) => {
                                //Console.WriteLine("\nSession stopped");
                                //Console.WriteLine("\nStop recognition.");
                            };


                            // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
                            Console.WriteLine("Say something...");
                            await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                            // Waits for completion.
                            // Use Task.WaitAny to keep the task rooted.

                            do
                            {
                                Console.WriteLine("Press Enter to stop");
                            } while (Console.ReadKey().Key != ConsoleKey.Enter);

                            // Stops recognition.
                            await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
                        }
                    }
        // Continuous intent recognition using file input.
        public static async Task ContinuousRecognitionWithFileAsync()
        {
            // <intentContinuousRecognitionWithFile>
            // Creates an instance of a speech config with specified subscription key
            // and service region. Note that in contrast to other services supported by
            // the Cognitive Services Speech SDK, the Language Understanding service
            // requires a specific subscription key from https://www.luis.ai/.
            // The Language Understanding service calls the required key 'endpoint key'.
            // Once you've obtained it, replace with below with your own Language Understanding subscription key
            // and service region (e.g., "westus").
            var config = SpeechConfig.FromSubscription("YourLanguageUnderstandingSubscriptionKey", "YourLanguageUnderstandingServiceRegion");

            // Creates an intent recognizer using file as audio input.
            // Replace with your own audio file name.
            using (var audioInput = AudioConfig.FromWavFileInput("whatstheweatherlike.wav"))
            {
                using (var recognizer = new IntentRecognizer(config, audioInput))
                {
                    // The TaskCompletionSource to stop recognition.
                    var stopRecognition = new TaskCompletionSource <int>();

                    // Creates a Language Understanding model using the app id, and adds specific intents from your model
                    var model = LanguageUnderstandingModel.FromAppId("YourLanguageUnderstandingAppId");
                    recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName1", "id1");
                    recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName2", "id2");
                    recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName3", "any-IntentId-here");

                    // Subscribes to events.
                    recognizer.Recognizing += (s, e) => {
                        Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
                    };

                    recognizer.Recognized += (s, e) => {
                        if (e.Result.Reason == ResultReason.RecognizedIntent)
                        {
                            Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
                            Console.WriteLine($"    Intent Id: {e.Result.IntentId}.");
                            Console.WriteLine($"    Language Understanding JSON: {e.Result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult)}.");
                        }
                        else if (e.Result.Reason == ResultReason.RecognizedSpeech)
                        {
                            Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
                            Console.WriteLine($"    Intent not recognized.");
                        }
                        else if (e.Result.Reason == ResultReason.NoMatch)
                        {
                            Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                        }
                    };

                    recognizer.Canceled += (s, e) => {
                        Console.WriteLine($"CANCELED: Reason={e.Reason}");

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

                        stopRecognition.TrySetResult(0);
                    };

                    recognizer.SessionStarted += (s, e) => {
                        Console.WriteLine("\n    Session started event.");
                    };

                    recognizer.SessionStopped += (s, e) => {
                        Console.WriteLine("\n    Session stopped event.");
                        Console.WriteLine("\nStop recognition.");
                        stopRecognition.TrySetResult(0);
                    };


                    // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
                    Console.WriteLine("Say something...");
                    await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                    // Waits for completion.
                    // Use Task.WaitAny to keep the task rooted.
                    Task.WaitAny(new[] { stopRecognition.Task });

                    // Stops recognition.
                    await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
                }
            }
            // </intentContinuousRecognitionWithFile>
        }
예제 #10
0
        public static async Task RecognizeSpeechAsync()
        {
            // Create an instance of a speech config with the specified app key and region.
            var config = SpeechConfig.FromSubscription(YourAppKey, YourRegion);

            // Use the microphone as input.
            var audioConfig = AudioConfig.FromDefaultMicrophoneInput();

            var stopRecognition = new TaskCompletionSource <int>();

            // Create a new IntentRecognizer, which uses the config and the audioConfig.
            using (var recognizer = new IntentRecognizer(config, audioConfig))
            {
                // Create a Language Understanding model using the app id.
                var model = LanguageUnderstandingModel.FromAppId(YourAppId);

                // Add the intents which are specified by the LUIS app.
                recognizer.AddIntent(model, "HomeAutomation.TurnOff", "off");
                recognizer.AddIntent(model, "HomeAutomation.TurnOn", "on");

                // Subscribe to events.
                recognizer.Recognizing += (s, e) =>
                {
                    Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
                };

                recognizer.Recognized += (s, e) =>
                {
                    // The LUIS app recognized an intent.
                    if (e.Result.Reason == ResultReason.RecognizedIntent)
                    {
                        // Get the result from the event arguments and print it into the console.
                        var responseJson = e.Result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult);

                        Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
                        Console.WriteLine($"    Intent Id: {e.Result.IntentId}.");
                        Console.WriteLine($"    Language Understanding JSON: {responseJson}.");

                        // Deserialize the JSON result into an object.
                        var responseObject = JsonConvert.DeserializeObject <SpeechResponse>(responseJson);

                        // Get the intent out of the result. This gives us the command.
                        var intent = responseObject.topScoringIntent.intent;
                        if (intent == "HomeAutomation.TurnOn")
                        {
                            intent = "on";
                        }
                        else if (intent == "HomeAutomation.TurnOff")
                        {
                            intent = "off";
                        }

                        // Get the colour entity out of the result.
                        var colourEntity = responseObject.entities.FirstOrDefault(x => x.type == "Colour");
                        var colour       = colourEntity.entity;

                        // Create the request we will send to the web API.
                        var request = new SpeechRequest
                        {
                            Colour  = colour,
                            Command = intent
                        };

                        // Create a new HttpClient and send the request.
                        var client  = new HttpClient();
                        var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");

                        client.PostAsync("http://<your-local-raspberrypi-ip>/api/Speech", content);
                    }

                    // The speech service recognized speech.
                    else if (e.Result.Reason == ResultReason.RecognizedSpeech)
                    {
                        Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
                        Console.WriteLine($"    Intent not recognized.");
                    }

                    // The input has not been recognized.
                    else if (e.Result.Reason == ResultReason.NoMatch)
                    {
                        Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                    }
                };

                recognizer.Canceled += (s, e) =>
                {
                    Console.WriteLine($"CANCELED: Reason={e.Reason}");

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

                    stopRecognition.TrySetResult(0);
                };

                recognizer.SessionStarted += (s, e) =>
                {
                    Console.WriteLine("\n    Session started event.");
                };

                recognizer.SessionStopped += (s, e) =>
                {
                    Console.WriteLine("\n    Session stopped event.");
                    Console.WriteLine("\nStop recognition.");
                    stopRecognition.TrySetResult(0);
                };

                // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
                await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                // Waits for completion.
                // Use Task.WaitAny to keep the task rooted.
                Task.WaitAny(new[] { stopRecognition.Task });

                // Stops recognition.
                await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
            }
        }
예제 #11
0
        /// <summary>
        /// processing audio file with Microsoft.CognitiveServices and returns recognised text string or empty string
        /// </summary>
        /// <returns></returns>
        public async Task <string> ProcessAudioSampleAsync()
        {
            _log.Called();

            string       key              = _config.AudioApiKey;
            string       region           = _config.AudioApiRegion;
            SpeechConfig configRecognizer = SpeechConfig.FromSubscription(key, region);
            string       processedAudio   = "";
            string       audioFile        = _fileService.GetAudioFilePath();
            bool         isRecorded       = _fileService.CheckAudioFile();

            if (!isRecorded)
            {
                return(processedAudio); //if file empty
            }
            try
            {
                using (AudioConfig audioInput = AudioConfig.FromWavFileInput(audioFile))
                    using (IntentRecognizer recognizer = new IntentRecognizer(configRecognizer, audioInput))
                    {
                        TaskCompletionSource <int> stopRecognition = new TaskCompletionSource <int>();

                        recognizer.Recognized += (s, e) =>
                        {
                            if (e.Result.Reason == ResultReason.RecognizedSpeech)
                            {
                                processedAudio = e.Result.Text;
                            }
                        };

                        recognizer.Canceled += (s, e) => {
                            if (e.Reason == CancellationReason.Error)
                            {
                                //log
                            }
                            stopRecognition.TrySetResult(0);
                        };

                        recognizer.SessionStarted += (s, e) => {
                            _log.Info("Start recording.");
                        };

                        recognizer.SessionStopped += (s, e) => {
                            _log.Info("Stop recording.");

                            stopRecognition.TrySetResult(0);
                        };

                        await recognizer.StartContinuousRecognitionAsync();

                        Task.WaitAny(new[] { stopRecognition.Task });

                        await recognizer.StopContinuousRecognitionAsync();
                    }

                return(processedAudio);
            }
            catch (Exception e)
            {
                _log.Error(e.Message);

                return(string.Empty);
            }
        }
        public static async Task RecognizeSpeechAsync()
        {
            initMqttClient(mqttBrokerAddress);
            // Creates an instance of a speech config with specified subscription key and service region.
            // Replace with your own subscription key // and service region (e.g., "westus").
            var intentConfig = SpeechConfig.FromSubscription("", "westus2");

            // Creates a speech recognizer.
            using (var intentRecognizer = new IntentRecognizer(intentConfig))
            {
                // The TaskCompletionSource to stop recognition.
                var stopRecognition = new TaskCompletionSource <int>();

                var model = LanguageUnderstandingModel.FromAppId("");
                intentRecognizer.AddAllIntents(model);

                // Subscribes to events.
                intentRecognizer.Recognizing += (s, e) => {
                    Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
                };

                intentRecognizer.Recognized += (s, e) => {
                    if (e.Result.Reason == ResultReason.RecognizedIntent)
                    {
                        Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
                        Console.WriteLine($"    Intent Id: {e.Result.IntentId}.");
                        Console.WriteLine($"    Language Understanding JSON: {e.Result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult)}.");
                        if (e.Result.IntentId == "FollowPerson")
                        {
                            var     jsonResult = e.Result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult);
                            dynamic stuff      = JObject.Parse(jsonResult);
                            try
                            {
                                string name = stuff.entities[0].entity;
                                Console.WriteLine(name);
                                int id = nameToIdDict.GetValueOrDefault(name);
                                mqttClient.Publish("bcx19-seek-the-geek/tag/control", Encoding.UTF8.GetBytes($"target.{name}"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                                Console.WriteLine("MQTT Message sent");
                            }
                            catch
                            {
                                Console.WriteLine("Error");
                            }
                        }
                        else if (e.Result.IntentId == "Rover.Stop")
                        {
                            mqttClient.Publish("bcx19-seek-the-geek/tag/control", Encoding.UTF8.GetBytes("rover.stop"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            Console.WriteLine("MQTT Message sent");
                        }
                        else if (e.Result.IntentId == "Rover.Start")
                        {
                            mqttClient.Publish("bcx19-seek-the-geek/tag/control", Encoding.UTF8.GetBytes("rover.start"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            Console.WriteLine("MQTT Message sent");
                        }
                        else if (e.Result.IntentId == "Rover.Left")
                        {
                            mqttClient.Publish("bcx19-seek-the-geek/tag/control", Encoding.UTF8.GetBytes("rover.left"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            Console.WriteLine("MQTT Message sent");
                        }
                        else if (e.Result.IntentId == "Rover.Right")
                        {
                            mqttClient.Publish("bcx19-seek-the-geek/tag/control", Encoding.UTF8.GetBytes("rover.right"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            Console.WriteLine("MQTT Message sent");
                        }
                        else if (e.Result.IntentId == "Rover.Exit")
                        {
                            mqttClient.Publish("bcx19-seek-the-geek/tag/control", Encoding.UTF8.GetBytes("rover.exit"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            Console.WriteLine("MQTT Message sent");
                        }
                        else if (e.Result.IntentId == "Rover.Back")
                        {
                            mqttClient.Publish("bcx19-seek-the-geek/tag/control", Encoding.UTF8.GetBytes("rover.back"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            Console.WriteLine("MQTT Message sent");
                        }
                    }
                    else if (e.Result.Reason == ResultReason.RecognizedSpeech)
                    {
                        Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
                        Console.WriteLine($"    Intent not recognized.");
                    }
                    else if (e.Result.Reason == ResultReason.NoMatch)
                    {
                        Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                    }
                };

                intentRecognizer.Canceled += (s, e) => {
                    Console.WriteLine($"CANCELED: Reason={e.Reason}");

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

                    stopRecognition.TrySetResult(0);
                };

                intentRecognizer.SessionStarted += (s, e) => {
                    Console.WriteLine("\n    Session started event.");
                };

                intentRecognizer.SessionStopped += (s, e) => {
                    Console.WriteLine("\n    Session stopped event.");
                    Console.WriteLine("\nStop recognition.");
                    stopRecognition.TrySetResult(0);
                };


                // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
                Console.WriteLine("Say something...");
                await intentRecognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                // Waits for completion.
                // Use Task.WaitAny to keep the task rooted.
                Task.WaitAny(new[] { stopRecognition.Task });

                // Stops recognition.
                await intentRecognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
            }
        }
예제 #13
0
        private async static Task RecognizeSpeechAndIntentAsync()
        {
            var config = SpeechConfig.FromEndpoint(
                new Uri("https://eastus2.api.cognitive.microsoft.com/sts/v1.0/issuetoken"),
                "MySuscriptionKey");

            config.SpeechRecognitionLanguage = "es-ES";

            using var speechRecognition = new IntentRecognizer(config);

            var luisModel = LanguageUnderstandingModel.FromAppId("ba417c40-bb51-4704-966a-f9c58afaf1c8");

            speechRecognition.AddAllIntents(luisModel);
            speechRecognition.AddIntent("chao");

            var endRecognition = new TaskCompletionSource <int>();

            speechRecognition.Recognized += (s, e) =>
            {
                switch (e.Result.Reason)
                {
                case ResultReason.NoMatch:
                    if (!endRecognition.Task.IsCompleted)
                    {
                        Console.WriteLine($"No entendí na':{e.Result.Text}");
                    }
                    break;

                case ResultReason.Canceled:
                    Console.WriteLine($"Se canceló la escucha:{e.Result.Text}");
                    break;

                case ResultReason.RecognizingSpeech:
                    Console.WriteLine($"Escuchando:{e.Result.Text}");
                    break;

                case ResultReason.RecognizedSpeech:
                    Console.WriteLine($"Entendí esto:{e.Result.Text}");
                    break;

                case ResultReason.RecognizedIntent:
                    Console.WriteLine($"Detectado comando de voz:{e.Result.Text}");
                    Console.WriteLine($"Saliendo ....");
                    endRecognition.TrySetResult(0);
                    break;

                default:
                    Console.WriteLine($"LLegué aquí porque:{e.Result.Reason}");
                    break;
                }
            };

            speechRecognition.Canceled += (s, e) =>
            {
                if (e.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"ocurrió un error:{e.ErrorCode} => {e.ErrorDetails}");
                }

                endRecognition.TrySetResult(0);
            };

            speechRecognition.SessionStopped += (s, e) =>
            {
                Console.WriteLine("Deteniendo");
                endRecognition.TrySetResult(0);
            };

            Console.WriteLine("Ahora empieza a hablar...");
            await speechRecognition.StartContinuousRecognitionAsync().ConfigureAwait(false);

            Task.WaitAny(new[] { endRecognition.Task });
            await speechRecognition.StopContinuousRecognitionAsync().ConfigureAwait(false);
        }
예제 #14
0
        static async Task ContinuousRecognizeIntentAsync()
        {
            //continuous speech recognition and detect intent
            var config = SpeechConfig.FromSubscription(luisKey, luisRegion);

            using (var recognizer = new IntentRecognizer(config))
            {
                var stopRecognition = new TaskCompletionSource <int>();

                var model = LanguageUnderstandingModel.FromAppId(luisAppId);
                recognizer.AddAllIntents(model);

                recognizer.Recognizing += (s, e) =>
                {
                    Console.WriteLine($"Recognizing: Text = {e.Result.Text}");
                };

                recognizer.Recognized += (s, e) =>
                {
                    if (e.Result.Reason == ResultReason.RecognizedIntent)
                    {
                        Console.WriteLine($"Recognized: Text = {e.Result.Text}");
                        LUIS.GetLuisResult(e.Result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult));
                        string intent = LUIS.GetLuisIntent(e.Result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult));
                        LUIS.ResponseToLuisIntent(intent, e.Result.Text);
                    }

                    else if (e.Result.Reason == ResultReason.RecognizedSpeech)
                    {
                        Console.WriteLine($"Recognized: Text = {e.Result.Text}");
                        Console.WriteLine("Intent not recognized.");
                    }
                };

                recognizer.Canceled += (s, e) =>
                {
                    Console.WriteLine($"Canceled: Reason={e.Reason}");
                    if (e.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                    stopRecognition.TrySetResult(0);
                };

                recognizer.SessionStarted += (s, e) =>
                {
                    Console.WriteLine("\n Session started event.");
                };

                recognizer.SessionStopped += (s, e) =>
                {
                    Console.WriteLine("\n Session stopped.");
                    stopRecognition.TrySetResult(0);
                };

                Console.WriteLine("[Continous Recognition] Say something...");
                await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                do
                {
                    Console.WriteLine("Press Enter to stop");
                } while (Console.ReadKey().Key != ConsoleKey.Enter);

                await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
            }
        }