当我在 C# 应用程序上执行语音到文本时,我希望能够尽可能多地执行此操作。我怎样才能做到这一点?

When I perform a speech-to-text on C# app I want to be able to do that as many time as I want. How can I do that?

我有一个控制台应用程序,我在其中使用 System.Speech.Recognition。我想对着麦克风说话并接听文字。到目前为止我成功了。但是我有更多的句子像"How are you?" "What time is it?"等等。应用程序可以识别所有这些应用程序,但在我拼出第一句话后它就会退出。我不希望这种情况发生,我希望该应用能够识别我在不同时间戳上拼写的所有句子。下面是我的代码:

class Program
    {
        static void Main(string[] args)
        {
            NaoSpeechRecognitionEntities db = new NaoSpeechRecognitionEntities();
            var userInput = db.UserInputs.ToList();
            //List<string> userReplies = new List<string>();
            Choices userReplies = new Choices();
            foreach (var reply in userInput)
            {
                userReplies.Add(reply.Reply);
            }

        SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
        GrammarBuilder gb = new GrammarBuilder();
        gb.Append(userReplies);
        Grammar g = new Grammar(gb);
        sre.LoadGrammar(g);
        sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
        sre.SetInputToDefaultAudioDevice();

        // Start recognition.
        sre.Recognize();
    }

    static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        //MessageBox.Show("Speech recognized: " + e.Result.Text);
        CurrentReply.currentReply = e.Result.Text;
        //NaoSpeechRecognitionEntities db = new NaoSpeechRecognitionEntities();
        //var userInput = db.UserInputs.Where(ui=>ui.Reply==e.Result.Text).SingleOrDefault();
        //var robotReply = db.RobotOutputs.Where(rp=>rp.UserInputId==userInput.UserInputId).SingleOrDefault();
        //CurrentReply.currentReply = robotReply.Reply;
    }

如您所见,它是一个控制台应用程序,我尝试了 windows 个表单,但到目前为止没有成功。我也试着把 sre.Recognize();在无限循环中,或者使用 goto 指令,但它仍然没有工作。

非常感谢任何帮助。 提前致谢!

基本上将您的 SpeechRecognitionEngine 对象 sre 存储在私有字段中,然后从事件处理程序中再次调用 sre.Recognize()。您可能 运行 遇到了无法直接调用 Recognize() 命令的问题。那么最简单的方法可能是重新创建 RecognitionEngine,同时处理以前的。

class Program
{
    private static SpeechRecognitionEngine sre;
    static void Main(string[] args)
    {
        RestartRec();
        Console.ReadKey();
    }
    static void RestartRec()
    {
        if (sre != null) sre.Dispose();

        //Preload DB entities once, store in private field and add them again here
        Choices userReplies = new Choices();
        userReplies.Add("hello");

        sre = new SpeechRecognitionEngine();
        GrammarBuilder gb = new GrammarBuilder();
        gb.Append(userReplies);
        Grammar g = new Grammar(gb);
        sre.LoadGrammar(g);
        sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
        sre.SetInputToDefaultAudioDevice();

        // Start recognition.
        sre.Recognize();
    }
    static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        Console.WriteLine(e.Result.Text);
        RestartRec();
    }
}

这样,引擎识别出某些内容后,它就会再次开始收听。您可能需要一个 ReadKey() 来避免程序退出,因为 Recognize 只会阻止您的主程序中的一个事件。

尽管有 Dispose(),它似乎仍会泄漏内存,因此它可能需要改进。但是你得到了基本的要点。