【问题标题】:Console Speech Recognition exits right after start控制台语音识别在启动后立即退出
【发布时间】:2017-01-23 14:51:26
【问题描述】:

我尝试按照 this 教程构建语音识别 C# 应用程序,唯一的区别是我想要一个 Console 应用程序,而不是 Win Form 应用程序,所以我写了这段代码:

using System;
using System.Speech.Recognition;
//using System.Speech.Synthesis;

namespace Voice_Recognation
{
    class Program
    {
        static void Main(string[] args)
        {
            SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
            recEngine.SetInputToDefaultAudioDevice();  

            Choices commands = new Choices();
            commands.Add(new string[] { "say Hi", "say Hello"});
            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(commands);
            Grammar g = new Grammar(gb);

            recEngine.LoadGrammarAsync(g);
            recEngine.RecognizeAsync(RecognizeMode.Multiple);

            recEngine.SpeechRecognized += recEngine_SpeechRecognized;

        }

                // Create a simple handler for the SpeechRecognized event
        static void recEngine_SpeechRecognized (object sender, SpeechRecognizedEventArgs e)
        {
            Console.WriteLine("Speech recognized: {0}", e.Result.Text);
            switch(e.Result.Text){
                case "Red":
                    Console.WriteLine("you said hi"); 
                break;
                default:
                break;
            }
        }

    }
}

并使用mono项目编译如下:

c:\mcs /reference:System.Speech.dll Program.cs

System.Speech.dll添加到文件夹项目后,生成Program.exe文件。

一旦我在终端运行program,它就直接结束了,没有给我任何机会说什么!!

我有 2 个问题:

我在这里遗漏了什么,我做错了什么?

有没有办法以更好的方式添加“.dll”文件,我尝试将其添加到Project.json 文件中,如下所示,但没有奏效,虽然我在运行@987654332 时没有遇到任何错误@:

  "frameworks": {
    "netcoreapp1.0": {
                  "bin": {
            "assembly": "D:/2016/Speech/CORE/System.Speech.dll"
        },
     }
   }

【问题讨论】:

标签: c# mono .net-core voice-recognition


【解决方案1】:

我通过添加Thread.Sleep 解决了第一部分,这样它就有足够的时间让线程继续侦听,另一种选择是使用while(true); 使其无限循环

我仍然无法解决第二部分,即如何使 VS 代码识别存在的程序集文件。

新的完整代码,有兴趣的在下面,更全面的代码可以找到here

using System;
using System.Speech.Recognition;
using System.Threading;
//using System.Speech.Synthesis;

namespace Voice_Recognation
{
    class Program
    {
        static void Main(string[] args)
        {
            SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
            recEngine.SetInputToDefaultAudioDevice();  

            Choices commands = new Choices();
            commands.Add(new string[] { "say Hi", "say Hello"});
            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(commands);
            Grammar g = new Grammar(gb);

            recEngine.LoadGrammarAsync(g);


            recEngine.SpeechRecognized += recEngine_SpeechRecognized;
            Console.WriteLine("Starting asynchronous recognition...");
            recEngine.RecognizeAsync(RecognizeMode.Multiple);

            // Wait 30 seconds, and then cancel asynchronous recognition.
            Thread.Sleep(TimeSpan.FromSeconds(30));

            // or 
            // while(true);
        }

        // Create a simple handler for the SpeechRecognized event
        static void recEngine_SpeechRecognized (object sender, SpeechRecognizedEventArgs e)
        {
            Console.WriteLine("Speech recognized: {0}", e.Result.Text);
            switch(e.Result.Text){
                case "say Hello":
                    Console.WriteLine("you said hi"); 
                break;
                default:
                break;
            }
        }

    }
}

【讨论】:

    猜你喜欢
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    相关资源
    最近更新 更多