使用语音转换为文本的结果将命令发送到端口 [英] using result from speech converted to text to send command to port

查看:72
本文介绍了使用语音转换为文本的结果将命令发送到端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,其中使用语音SDK处理来自用户的语音,并将文本结果与特定字符串进行比较,并且输出用于发送命令以打开或关闭灯。我坚持如何比较语音的输出和字符串,以便如果它们相同,我将必要的命令发送到输出。我是编码的新手,我尝试的一切都给出了错误。请帮助。

i am writing an app where speech from a user is processed using the speech SDK and the text result is compared with a certain string and the output used to send a command to turn a light on or off. i am stuck at how to compare the output of the speech with the string so that if they are the same, i send the necessary command to the output. i am a novice at coding and everything i try just gives errors. please help.

        void Main(string[] args)
        {
           
            // Create a SpeechRecognitionEngine object for the default recognizer in the en-US locale since we have chosen
            // american english as our language 
            using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))
            {

                // Create a grammar.
                Choices appliances = new Choices(new string[] { "fan", "lights" });
                Choices Commands = new Choices(new string[] { "on", "off" });

                GrammarBuilder gb = new GrammarBuilder();
                gb.Append("Please turn the");
                gb.Append(appliances);
                gb.Append(Commands);

                // Create a Grammar object and load it to the recognizer.
                Grammar g = new Grammar(gb);
                recognizer.LoadGrammarAsync(g);

                // Attach event handlers.
                recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
                recognizer.SpeechRecognitionRejected +=
                new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);

                // Set the input to the recognizer i.e to the audio file that was stored
                recognizer.SetInputToWaveFile(@"D:\speechtestfiles.wav");


                // Start asynchronous, continuous recognition.
                recognizer.RecognizeAsync(RecognizeMode.Multiple);


                // Keep the console window open.
                Console.ReadLine();

            }

string _recognised1 = " Speech recognized: Please turn the lights on";
            string _recognised2 = " Speech recognized: Please turn the fan on";
            string _recognised3 = " Speech recognized: Please turn the lights off";
            string _recognised4 = " Speech recognized: Please turn the fan off";

string _detected2 = new string(Result.Text);
         
//sends the "turn the light on" command to the appliances
            if (String.Compare(_recognised1, _detected2)==0)
            {
               
                PortAccess.Output(888,1);
            }

}

// Handle the SpeechRecognitionRejected event.
        public static void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            foreach (RecognizedPhrase phrase in e.Result.Alternates)
            {
                Console.WriteLine("  Rejected phrase: " + phrase.Text);

            }
        }

        // Handle the SpeechRecognized event.
        public static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
          Console.WriteLine("  Speech recognized: " + e.Result.Text);

        }

推荐答案

我自己从未使用过语音识别API,但是如果你让它工作的话(只是识别),意思是如果你得到
I have myself never used the Speech Recognition API, but if you get it to work (Just the recognition), meaning if you get the
Quote:

Console.WriteLine(Speech recognition:+ e.Result.Text);

Console.WriteLine(" Speech recognized: " + e.Result.Text);

执行,右边所说的文本在屏幕上打印,然后你应该把分析结果的代码(e.Result.Text)放在那一行之后,因为Recognizer_SpeechRecognized是演讲时运行的方法已成功识别。

现在,您应该尝试让识别器在屏幕上打印文本,完成后,进行简单的字符串比较(e.Result.Text.ToLowerCase()。Trim ()==请打开风扇)看它是否运行,一旦有效,你可以使它成为一种更复杂的语音分析方法。

我希望这有帮助。

to execute, and the right said text does print on the screen, then you should put the code that analyses the result (e.Result.Text) right after that line, since recognizer_SpeechRecognized is the method that runs if the speech is successfully recognized.
For now, you should just try to get the recognizer to print the text on screen, once done, do a simple string comparison (e.Result.Text.ToLowerCase().Trim() == "Please turn the fan on") to see if it runs, once that works, you can make it a more sophisticated speech analysis method.
I hope this helps.


这篇关于使用语音转换为文本的结果将命令发送到端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆