【问题标题】:jarvis takes too long to respondjarvis 响应时间过长
【发布时间】:2018-11-19 21:26:38
【问题描述】:

我正在使用 c# 制作 jarvis。我对它进行了编码,一旦我们说了什么,jarvis 就需要很长时间才能回复。回复需要 10-15 分钟,有时甚至没有回复。我希望 jarvis 在我发出语音命令后立即做出响应。

 using System;
    using System.IO;
    using System.Speech.Recognition;
    using System.Speech.Synthesis;
    using System.Windows.Forms;

    namespace Jarvis
    {
        public partial class Form1 : Form
        {
            SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
            SpeechSynthesizer JARVIS = new SpeechSynthesizer();
            string QEvent;
            string ProcWindow;
            double timer = 10;
            int count = 1;
            Random rnd = new Random();
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                _recognizer.SetInputToDefaultAudioDevice();
                _recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Ashwini\Desktop\Jarvis\Jarvis\bin\Debug\commands.txt")))));
                _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
                _recognizer.RecognizeAsync(RecognizeMode.Multiple);
            }
            void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
            {
                int ranNum = rnd.Next(1, 10);
                string speech = e.Result.Text;
                switch (speech)
                {

                    case "hello":
                    case "hello jarvis":
                        if (ranNum < 6) { JARVIS.Speak("Hello sir"); }
                        else if (ranNum > 5) { JARVIS.Speak("Hello"); }
                        break;
                    case "goodbye":
                    case "goodbye jarvis":
                    case "close":
                    case "close jarvis":
                        JARVIS.Speak("Until next time");
                        Close();
                        break;
                    case "jarvis":
                        if (ranNum < 5) { QEvent = ""; JARVIS.Speak("Yes sir"); }
                        else if (ranNum > 4) { QEvent = ""; JARVIS.Speak("Yes?"); }
                        break;

                    //WEBSITES
                    case "open google":
                        System.Diagnostics.Process.Start("https://www.google.com/");
                        break;
                    case "open my facebook":
                        System.Diagnostics.Process.Start("https://www.facebook.com/");
                        break;
                    case "open gmail":
                        System.Diagnostics.Process.Start("https://www.google.com/gmail/");
                        break;

                    //SHELL COMMANDS
                    case "open this pc":
                        System.Diagnostics.Process.Start("This PC");
                        JARVIS.Speak("Loading");
                        break;

                    case "open my folder":
                        System.Diagnostics.Process.Start("F:");
                        JARVIS.Speak("Loading");
                        break;

                    //CLOSE PROGRAMS
                    case "close this pc":
                        ProcWindow = "This PC";
                        StopWindow();
                        break;

                    //CONDITION OF DAY
                    case "what time is it":
                        DateTime now = DateTime.Now;
                        string time = now.GetDateTimeFormats('t')[0];
                        JARVIS.Speak(time);
                        break;
                    case "what day is it":
                        JARVIS.Speak(DateTime.Today.ToString("dddd"));
                        break;
                    case "whats the date":
                    case "whats todays date":
                        JARVIS.Speak(DateTime.Today.ToString("dd-MM-yyyy"));
                        break;

                    //OTHER COMMANDS
                    case "go fullscreen":
                        FormBorderStyle = FormBorderStyle.None;
                        WindowState = FormWindowState.Maximized;
                        TopMost = true;
                        JARVIS.Speak("expanding");
                        break;
                    case "exit fullscreen":
                        FormBorderStyle = FormBorderStyle.Sizable;
                        WindowState = FormWindowState.Normal;
                        TopMost = false;
                        break;
                    case "switch window":
                        SendKeys.Send("%{TAB " + count + "}");
                        count += 1;
                        break;

                    case "out of the way":
                        if (WindowState == FormWindowState.Normal || WindowState == FormWindowState.Maximized)
                        {
                            WindowState = FormWindowState.Minimized;
                            JARVIS.Speak("My apologies");
                        }
                        break;
                    case "come back":
                        if (WindowState == FormWindowState.Minimized)
                        {
                            JARVIS.Speak("Alright?");
                            WindowState = FormWindowState.Normal;
                        }
                        break;
                    case "show commands":
                        string[] commands = (File.ReadAllLines(@"C:\Users\Ashwini\Desktop\Jarvis\Jarvis\bin\Debug\commands.txt"));
                        JARVIS.Speak("Very well");
                        lstCommands.Items.Clear();
                        lstCommands.SelectionMode = SelectionMode.None;
                        lstCommands.Visible = true;
                        foreach (string command in commands)
                        {
                            lstCommands.Items.Add(command);
                        }
                        break;
                    case "hide listbox":
                        lstCommands.Visible = false;
                        break;

                }
            }

            private void StopWindow()
            {

            }
        }
    }

我不知道这段代码有什么问题。请帮帮我。

【问题讨论】:

  • 我已经构建了很多次,最好使用if statements,如果你愿意,我可以给你一个更好的方法。
  • @Halonic Yaa 我想要更好的方法来做到这一点,感谢您的提示
  • 我今天在 YouTube 上做一个教程,这将是一个简短的教程,介绍设置语音的基础知识以及有关命令和类似内容的一些内容。我的频道也是 www.youtube.com/c/campnerd 我会在这里发布但有很多代码。我在程序中所做的大约 100 多行。此外,我的响应时间约为 0.849 秒,现在我的完整程序中最长的等待时间是 3 秒。上传后我会在这里发帖。不过,我的频道上也有 C# 教程。
  • 好的,我订阅了你的频道,所以每当你上传有关语音设置和命令的视频时,我都会知道。谢谢哥们!@Halonic
  • 这里是教程。 youtu.be/BJkymbvxlJs

标签: c# speech-recognition


【解决方案1】:

有关语音识别的深入教程,请参阅我的教程https://youtu.be/BJkymbvxlJs

使用语音识别时,您需要使用 Async 方法。 Jarvis.SpeakAsync Async 方法将给出持续的命令和指示。

仅使用 Jarvis.Speak 您必须等待引擎完成。示例:

  Jarvis.Speak("this is what Jarvis will say after a  command is spoken");

使用Jarvis.Speak 合成时不能执行任何命令。

   Jarvis.SpeakAsync("this is what Jarvis will say after a command is spoken");

使用Jarvis.SpeakAsync 时,您可以在合成过程中暂停、停止和/或中断语音引擎。

上面的教程链接显示了SpeakAsync 的基本格式以及如何在应用程序中使用它进行语音识别。

【讨论】:

    【解决方案2】:

    问题是,即使我们说完,它仍然在听背景噪音,所以只需提高能量阈值,我的 300 我将其更改为 1000,现在我只需大声一点,但语音识别非常完美。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2016-10-24
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 2021-10-16
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多