예제 #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                SpeechFactory speechFactory = SpeechFactory.FromSubscription(luisKey, "");
                recognizer = speechFactory.CreateIntentRecognizer("zh-cn");

                // 创建意图识别器用到的模型
                var model = LanguageUnderstandingModel.FromSubscription(luisKey, luisAppId, luisRegion);

                // 将模型中的意图加入到意图识别器中
                recognizer.AddIntent("None", model, "None");
                recognizer.AddIntent("TurnOn", model, "TurnOn");
                recognizer.AddIntent("TurnOff", model, "TurnOff");

                // 挂载识别中的事件
                // 收到中间结果
                recognizer.IntermediateResultReceived += Recognizer_IntermediateResultReceived;
                // 收到最终结果
                recognizer.FinalResultReceived += Recognizer_FinalResultReceived;
                // 发生错误
                recognizer.RecognitionErrorRaised += Recognizer_RecognitionErrorRaised;

                // 启动语音识别器,开始持续监听音频输入
                recognizer.StartContinuousRecognitionAsync();
            }
            catch (Exception ex)
            {
                Log(ex.Message);
            }
        }
        // Intent recognition using microphone.
        public static async Task RecognitionWithMicrophoneAsync()
        {
            // <intentRecognitionWithMicrophone>
            // 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").
            // The default language is "en-us".
            var config = SpeechConfig.FromSubscription("YourLanguageUnderstandingSubscriptionKey", "YourLanguageUnderstandingServiceRegion");

            // Creates an intent recognizer using microphone as audio input.
            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("YourLanguageUnderstandingAppId");
                recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName1", "id1");
                recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName2", "id2");
                recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName3", "any-IntentId-here");

                // Starts recognizing.
                Console.WriteLine("Say something...");

                // Performs recognition. RecognizeOnceAsync() returns when the first utterance has been recognized,
                // so it is suitable only for single shot recognition like command or query. For long-running
                // recognition, use StartContinuousRecognitionAsync() instead.
                var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                // Checks result.
                if (result.Reason == ResultReason.RecognizedIntent)
                {
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"    Intent Id: {result.IntentId}.");
                    Console.WriteLine($"    Language Understanding JSON: {result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult)}.");
                }
                else if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"    Intent not recognized.");
                }
                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: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
            // </intentRecognitionWithMicrophone>
        }
예제 #3
0
        private async Task <LuisResult> RecognizeSpeechWithIntentRecognizerAsync(string speechFile)
        {
            var speechConfig = SpeechConfig.FromEndpoint(this.LuisConfiguration.SpeechEndpoint, this.LuisConfiguration.EndpointKey);

            using (var audioInput = AudioConfig.FromWavFileInput(speechFile))
                using (var recognizer = new IntentRecognizer(speechConfig, audioInput))
                {
                    // Add intents to intent recognizer
                    var model = LanguageUnderstandingModel.FromAppId(this.LuisConfiguration.AppId);
                    recognizer.AddIntent(model, "None", "None");
                    var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                    // Checks result.
                    // For some reason RecognizeOnceAsync always return ResultReason.RecognizedSpeech
                    // when intent is recognized. It's because we don't add all possible intents (note that this IS intentional)
                    // in code via AddIntent method.
                    if (result.Reason == ResultReason.RecognizedSpeech || result.Reason == ResultReason.RecognizedIntent)
                    {
                        var content = result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult);
                        return(JsonConvert.DeserializeObject <LuisResult>(content));
                    }
                    else if (result.Reason == ResultReason.NoMatch)
                    {
                        Logger.LogWarning("Received 'NoMatch' result from Cognitive Services.");
                        return(null);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Failed to get speech recognition result. Reason = '{result.Reason}'");
                    }
                }
        }
    /// <summary>
    /// Creates and initializes the IntentRecognizer
    /// </summary>
    void CreateIntentRecognizer()
    {
        if (LUISAppKey.Length == 0 || LUISAppKey == "YourLUISAppKey")
        {
            recognizedString = "You forgot to obtain Cognitive Services LUIS credentials and inserting them in this app." + Environment.NewLine +
                               "See the README file and/or the instructions in the Awake() function for more info before proceeding.";
            errorString = "ERROR: Missing service credentials";
            UnityEngine.Debug.LogFormat(errorString);
            return;
        }
        UnityEngine.Debug.LogFormat("Creating Intent Recognizer.");
        recognizedString = "Initializing intent recognition, please wait...";

        if (intentreco == null)
        {
            // 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").
            // The default language is "en-us".
            var config = SpeechConfig.FromSubscription(LUISAppKey, LUISRegion);
            // Creates an intent recognizer using microphone as audio input.
            intentreco = new IntentRecognizer(config);

            // Creates a Language Understanding model using the app id, and adds specific intents from your model
            var model = LanguageUnderstandingModel.FromAppId(LUISAppId);
            intentreco.AddIntent(model, "ChangeColor", "color");
            intentreco.AddIntent(model, "Transform", "transform");
            intentreco.AddIntent(model, "Help", "help");
            intentreco.AddIntent(model, "None", "none");

            // Subscribes to speech events.
            intentreco.Recognizing         += RecognizingHandler;
            intentreco.Recognized          += RecognizedHandler;
            intentreco.SpeechStartDetected += SpeechStartDetectedHandler;
            intentreco.SpeechEndDetected   += SpeechEndDetectedHandler;
            intentreco.Canceled            += CanceledHandler;
            intentreco.SessionStarted      += SessionStartedHandler;
            intentreco.SessionStopped      += SessionStoppedHandler;
        }
        UnityEngine.Debug.LogFormat("CreateIntentRecognizer exit");
    }
예제 #5
0
    private async void RecognizeIntentAsync()
    {
        recognizer = new IntentRecognizer(luisConfig);

        // Creates a Language Understanding model using the app id, and adds specific intents from your model
        var model = LanguageUnderstandingModel.FromAppId(LuisAppId);

        recognizer.AddIntent(model, "changeColor", "changeColor");
        recognizer.AddIntent(model, "moveObject", "moveObject");

        // add event handlers
        recognizer.Recognized  += Recognizer_Recognized;
        recognizer.Recognizing += Recognizer_Recognizing;
        recognizer.Canceled    += Recognizer_Canceled;

        //Start recognizing
        Debug.Log("Say something...");
        await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
    }
        public async Task <Result <LuisResult> > Recognize(string filePath)
        {
            // Credenciais do LUIS
            var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourRegion");

            config.SpeechRecognitionLanguage = "pt-br";

            using (var audioInput = AudioConfig.FromWavFileInput(filePath))
            {
                using (var recognizer = new IntentRecognizer(config, audioInput))
                {
                    var model = LanguageUnderstandingModel.FromAppId("YourLuisAppId");
                    recognizer.AddIntent(model, "intent.iot.device_off", "device_off");
                    recognizer.AddIntent(model, "intent.iot.device_on", "device_on");

                    var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                    if (result.Reason == ResultReason.RecognizedIntent)
                    {
                        var js = new DataContractJsonSerializer(typeof(LuisResult));
                        var ms = new MemoryStream(Encoding.UTF8.GetBytes(result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult)));
                        return(new Result <LuisResult>((js.ReadObject(ms) as LuisResult)));
                    }
                    else if (result.Reason == ResultReason.NoMatch)
                    {
                        return(new Result <LuisResult>(null, false, "Falha no reconhecimento do áudio!"));
                    }
                    else if (result.Reason == ResultReason.Canceled)
                    {
                        var cancellation = CancellationDetails.FromResult(result);
                        if (cancellation.Reason == CancellationReason.Error)
                        {
                            return(new Result <LuisResult>(null, false, $"Motivo: {cancellation.Reason}. Detalhes: {cancellation.ErrorDetails}"));
                        }
                        return(new Result <LuisResult>(null, false, $"Motivo: {cancellation.Reason}."));
                    }
                }
            }
            return(new Result <LuisResult>(null, false, "Erro desconhecido!"));
        }
예제 #7
0
        private async Task <SpeechLuisResult> RecognizeSpeechWithIntentRecognizerAsync(string speechFile)
        {
            if (this.LuisConfiguration.IsStaging)
            {
                throw new NotSupportedException("Testing LUIS from speech with the Speech SDK does not currently support the LUIS staging endpoint.");
            }

            var speechConfig = SpeechConfig.FromSubscription(this.LuisConfiguration.SpeechKey, this.LuisConfiguration.SpeechRegion);

            using (var audioInput = AudioConfig.FromWavFileInput(speechFile))
                using (var recognizer = new IntentRecognizer(speechConfig, audioInput))
                {
                    // Add intents to intent recognizer
                    var model = LanguageUnderstandingModel.FromAppId(this.LuisConfiguration.AppId);
                    recognizer.AddIntent(model, "None", "None");
                    var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                    // Checks result.
                    // For some reason RecognizeOnceAsync always return ResultReason.RecognizedSpeech
                    // when intent is recognized. It's because we don't add all possible intents (note that this IS intentional)
                    // in code via AddIntent method.
                    if (result.Reason == ResultReason.RecognizedSpeech || result.Reason == ResultReason.RecognizedIntent)
                    {
                        var content           = result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult);
                        var luisResult        = JsonConvert.DeserializeObject <LuisResult>(content);
                        var speechContent     = result.Properties.GetProperty(PropertyId.SpeechServiceResponse_JsonResult);
                        var speechContentJson = JObject.Parse(speechContent);
                        var textScore         = speechContentJson["NBest"]?.Max(t => t.Value <double?>("Confidence"));
                        return(new SpeechLuisResult(luisResult, textScore));
                    }
                    else if (result.Reason == ResultReason.NoMatch)
                    {
                        Logger.LogWarning("Received 'NoMatch' result from Cognitive Services.");
                        return(null);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Failed to get speech recognition result. Reason = '{result.Reason}'");
                    }
                }
        }
                    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 void Main(string [] args)
        {
            //CONFIG LUIS
            configLuis = SpeechConfig.FromSubscription("b1ea9c02d3954e9f9cf90592f9062ef6", "westus2");
            configLuis.SpeechRecognitionLanguage = "pt-BR";

            //CONFIG SPEECH
            configSpeech = SpeechConfig.FromSubscription("9184b21db9b743bd83770061b376ee2c", "westus2");
            configSpeech.SpeechSynthesisVoiceName = "Microsoft Server Speech Text to Speech Voice (pt-BR, FranciscaNeural)";

            //ADICIONANDO AS INTENCOES CRIADAS NO LUIS
            recognizer = new IntentRecognizer(configLuis);
            model      = LanguageUnderstandingModel.FromAppId("2940577d-15f2-42bb-a07f-a760cd651c0b");
            recognizer.AddIntent(model, "AvisarChegada", "avisar_chegada");
            recognizer.AddIntent(model, "Cumprimento", "cumprimento");
            recognizer.AddIntent(model, "Despedida", "despedida");
            recognizer.AddIntent(model, "Entregar", "entregar");
            recognizer.AddIntent(model, "Visita", "visitar");
            recognizer.AddIntent(model, "InformarDestino", "destino");
            recognizer.AddIntent(model, "AutorizarVisitante", "autorizado");
            recognizer.AddIntent(model, "NaoAutorizarVisitante", "nao_autorizado");

            synthesizer = new SpeechSynthesizer(configSpeech);

            //LÊ AS REGRAS DEFINIDAS NO ARQUIVO XML
            leitorXML = new Leitor();
            leitorXML.LeArquivoXML();

            //VALIDA AS VARIAVEIS E REGRAS LIDAS NO ARQUIVO ATRAVES DO PROCESSADOR LOGICO
            processador = new Processador();

            while (controle_interacao)
            {
                try
                {
                    if (estado == 2 || estado == 3 || estado == 4)
                    {
                        controle_interacao = false;
                        ligacao_morador();
                    }
                    else if (estado == 6)
                    {
                        despedida();
                    }
                    else
                    {
                        RecognizeIntentAsync().Wait();
                    }
                }
                catch
                {
                    if (timeout == 0)
                    {
                        Resposta("Olá, tem alguém me escutando?").Wait();
                    }

                    else if (timeout == 1)
                    {
                        Resposta("Olá, ainda tem alguém aí?").Wait();
                    }

                    else if (timeout == 2)
                    {
                        Resposta("Encerrando a chamada").Wait();
                        controle_interacao = false;
                    }

                    timeout++;
                }
            }
        }
예제 #11
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);
            }
        }
        static async Task RecognizeIntentAsync()
        {
            // 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").
            // The default language is "en-us".
            var config = SpeechConfig.FromSubscription("YourLanguageUnderstandingSubscriptionKey", "YourLanguageUnderstandingServiceRegion");

            // Creates an intent recognizer using microphone as audio input.
            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("YourLanguageUnderstandingAppId");
                recognizer.AddIntent(model, "HomeAutomation.TurnOff", "off");
                recognizer.AddIntent(model, "HomeAutomation.TurnOn", "on");

                // Starts recognizing.
                Console.WriteLine("Say something...");

                // Starts intent recognition, and returns after a single utterance is recognized. The end of a
                // single utterance is determined by listening for silence at the end or until a maximum of 15
                // seconds of audio is processed.  The task returns the recognition text as result.
                // Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
                // shot recognition like command or query.
                // For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
                var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                // Checks result.
                if (result.Reason == ResultReason.RecognizedIntent)
                {
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"    Intent Id: {result.IntentId}.");
                    Console.WriteLine($"    Language Understanding JSON: {result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult)}.");
                }
                else if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"    Intent not recognized.");
                }
                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?");
                    }
                }
            }
        }
예제 #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
        public static async Task RecognizeIntentAsync()
        {
            // </skeleton_1>
            // 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").
            // The default language is "en-us".
            // <create_speech_configuration>
            var config = SpeechConfig.FromSubscription(
                "YourLanguageUnderstandingSubscriptionKey",
                "YourLanguageUnderstandingServiceRegion");

            // </create_speech_configuration>

            // <create_intent_recognizer_1>
            // Creates an intent recognizer using microphone as audio input.
            using (var recognizer = new IntentRecognizer(config))
            {
                // </create_intent_recognizer_1>

                // <add_intents>
                // 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");
                // </add_intents>

                // To add all of the possible intents from a LUIS model to the recognizer, uncomment the line below:
                // recognizer.AddAllIntents(model);

                // <recognize_intent>
                // Starts recognizing.
                Console.WriteLine("Say something...");

                // Starts intent recognition, and returns after a single utterance is recognized. The end of a
                // single utterance is determined by listening for silence at the end or until a maximum of 15
                // seconds of audio is processed.  The task returns the recognition text as result.
                // Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
                // shot recognition like command or query.
                // For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
                var result = await recognizer.RecognizeOnceAsync();

                // </recognize_intent>

                // <print_results>
                // Checks result.
                switch (result.Reason)
                {
                case ResultReason.RecognizedIntent:
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"    Intent Id: {result.IntentId}.");
                    var json = result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult);
                    Console.WriteLine($"    Language Understanding JSON: {json}.");
                    break;

                case ResultReason.RecognizedSpeech:
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"    Intent not recognized.");
                    break;

                case ResultReason.NoMatch:
                    Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                    break;

                case 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?");
                    }
                    break;
                }
                // </print_results>
                // <create_intent_recognizer_2>
            }
            // </create_intent_recognizer_2>
            // <skeleton_2>
        }
예제 #15
0
        static async Task RecognizeAsync()
        {
            string LUIS_assigned_endpoint_key = "<LUIS-assigned-resource-key>";
            string LUIS_endpoint_key_region   = "westus";
            string LUIS_app_ID = "<LUIS-app-ID>";

            // 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").
            // The default language is "en-us".
            var config = SpeechConfig.FromSubscription(LUIS_assigned_endpoint_key, LUIS_endpoint_key_region);

            // Creates an intent recognizer using microphone as audio input.
            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(LUIS_app_ID);

                // Add intents from HumanResources.json to your intent recognizer
                recognizer.AddIntent(model, "None", "None");
                recognizer.AddIntent(model, "FindForm", "FindForm");
                recognizer.AddIntent(model, "GetEmployeeBenefits", "GetEmployeeBenefits");
                recognizer.AddIntent(model, "GetEmployeeOrgChart", "GetEmployeeOrgChart");
                recognizer.AddIntent(model, "MoveAssetsOrPeople", "MoveAssetsOrPeople");

                // Starts recognizing.
                Console.WriteLine("Say something...");

                // Performs recognition. RecognizeOnceAsync() returns when the first utterance has been recognized,
                // so it is suitable only for single shot recognition like command or query. For long-running
                // recognition, use StartContinuousRecognitionAsync() instead.
                var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                // Checks result.
                if (result.Reason == ResultReason.RecognizedIntent)
                {
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"    Intent Id: {result.IntentId}.");
                    Console.WriteLine($"    Language Understanding JSON: {result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult)}.");
                }
                else if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"    Intent not recognized.");
                }
                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: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
        }
예제 #16
0
        // Intent recognition in the specified language, using microphone.
        public async Task <String> RecognitionWithMicrophoneUsingLanguageAsync()
        {
            log.Info("ENTER - RecognitionWithMicrophoneUsingLanguageAsync");
            // 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(subscriptionKey, serviceRegion);
            var language = "es-mx";

            config.SpeechRecognitionLanguage = language;

            // Creates an intent recognizer in the specified language using microphone as audio input.
            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(appId);
                recognizer.AddIntent(model, "Greetings", "Greetings");
                recognizer.AddIntent(model, "None", "None");
                recognizer.AddIntent(model, "Ubicacion", "Ubicacion");
                recognizer.AddIntent(model, "Despedida", "Despedida");
                recognizer.AddIntent(model, "Compras", "Compras");
                recognizer.AddIntent(model, "Descuento", "Descuento");



                // Starts recognizing.
                log.Debug("Say something in " + language + "...");

                // Starts speech recognition, and returns after a single utterance is recognized. The end of a
                // single utterance is determined by listening for silence at the end or until a maximum of 15
                // seconds of audio is processed.  The task returns the recognition text as result.
                // Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
                // shot recognition like command or query.
                // For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
                var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                // Checks result.
                if (result.Reason == ResultReason.RecognizedIntent)
                {
                    log.Debug($"RECOGNIZED: Text={result.Text}");
                    log.Debug($"    Intent Id: {result.IntentId}.");
                    log.Debug($"    Language Understanding JSON: {result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult)}.");

                    //  Updating MessageArea
                    Demo_ChinaTown.MainWindow.AppWindow.MessageArea.Dispatcher.Invoke((Action) delegate
                    {
                        Demo_ChinaTown.MainWindow.AppWindow.MessageArea.Text += $"\nUsuario: {result.Text}";
                    });

                    return(result.IntentId);
                }
                else if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    log.Debug($"RECOGNIZED: Text={result.Text}");
                    log.Debug($"    Intent not recognized.");
                    return("Intent not recognized.");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    log.Debug($"NOMATCH: Speech could not be recognized.");
                    return("Speech could not be recognized.");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    log.Debug($"CANCELED: Reason={cancellation.Reason}");

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