【问题标题】: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?当我在 C# 应用程序上执行语音到文本时,我希望能够多次执行此操作。我怎样才能做到这一点?
【发布时间】:2016-02-07 08:08:26
【问题描述】:

我有一个控制台应用程序,我在其中使用 System.Speech.Recognition。我想对着麦克风说话并接受文本。到目前为止,我成功了。但我有更多的句子,比如“你好吗?” “现在是几奌?”等等。应用程序可以识别所有这些,但在我拼出第一句话后它就退出了。我不希望这种情况发生,我希望该应用程序能够识别我在各种时间戳上拼写的所有句子。下面是我的代码:

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 指令,但它仍然没有工作。

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

【问题讨论】:

  • 我忘了说我想被识别的语句是通过 EF 从数据库中获取的,不是那么相关,而是为了让您更好地理解

标签: c# .net winforms console-application speech-to-text


【解决方案1】:

基本上将您的 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() 来避免程序退出,因为识别只会阻止你的 main 中的一个事件。

尽管有Dispose(),它似乎泄漏了内存,所以它可能需要工作。但是你得到了基本的要点。

【讨论】:

    猜你喜欢
    • 2013-09-04
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    相关资源
    最近更新 更多