没有命令的C#系统、语音、识别?

没有命令的C#系统、语音、识别?,c#,winforms,speech-to-text,C#,Winforms,Speech To Text,我认为这是同一个问题,但是答案不起作用。Microsoft API要求使用命令。如果我没有输入命令,它会显示一条消息,说明它是必需的。如果我添加一个命令,这是唯一遇到的文本。我想写点东西,把我说的每一个字都听写下来。就像特工小姐当年做的那样。任何人都有一些方向,因为我失败了,不想使用谷歌云API。我想让它在本地运行 using System; using System.Speech.Recognition; using System.Speech.Synthesis; using System.

我认为这是同一个问题,但是答案不起作用。Microsoft API要求使用命令。如果我没有输入命令,它会显示一条消息,说明它是必需的。如果我添加一个命令,这是唯一遇到的文本。我想写点东西,把我说的每一个字都听写下来。就像特工小姐当年做的那样。任何人都有一些方向,因为我失败了,不想使用谷歌云API。我想让它在本地运行

using System;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;

namespace S2TextDemo
{
    class Program
    {
        static SpeechSynthesizer ss = new SpeechSynthesizer();
        static SpeechRecognitionEngine sre;
        static bool speechOn = true;
        static private AutoResetEvent _quitEvent;

        static void Main(string[] args)
        {
            try
            {
                 _quitEvent = new AutoResetEvent(false);

                ss.SetOutputToDefaultAudioDevice();
                CultureInfo ci = new CultureInfo("en-us");
                sre = new SpeechRecognitionEngine(ci);
                sre.SetInputToDefaultAudioDevice();
                sre.SpeechRecognized += sre_SpeechRecognized;
                //sre.SpeechRecognized += SpeechRecognizedHandler;
                Choices ch_StartStopCommands = new Choices();

                ch_StartStopCommands.Add("quit");

                GrammarBuilder gb_StartStop = new GrammarBuilder();
                gb_StartStop.Append(ch_StartStopCommands);
                Grammar g_StartStop = new Grammar(gb_StartStop);
                sre.LoadGrammarAsync(g_StartStop);

                sre.RecognizeAsync(RecognizeMode.Multiple);
                Console.WriteLine("Listening...\n");
                ss.SpeakAsync("I'm now listening.");

                 _quitEvent.WaitOne();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        } // Main

        static void SpeechRecognizedHandler(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result == null) return;
            string txt = e.Result.Text;

            // Add event handler code here.

            // The following code illustrates some of the information available
            // in the recognition result.
            Console.WriteLine("Grammar({0}), {1}: {2}",
              e.Result.Grammar.Name, e.Result.Audio.Duration, e.Result.Text);

            // Display the semantic values in the recognition result.
            foreach (KeyValuePair<String, SemanticValue> child in e.Result.Semantics)
            {
                Console.WriteLine(" {0} key: {1}",
                  child.Key, child.Value.Value ?? "null");
            }
            Console.WriteLine();

            // Display information about the words in the recognition result.
            foreach (RecognizedWordUnit word in e.Result.Words)
            {
                RecognizedAudio audio = e.Result.GetAudioForWordRange(word, word);
                Console.WriteLine(" {0,-10} {1,-10} {2,-10} {3} ({4})",
                  word.Text, word.LexicalForm, word.Pronunciation,
                  audio.Duration, word.DisplayAttributes);
            }

            // Display the recognition alternates for the result.
            foreach (RecognizedPhrase phrase in e.Result.Alternates)
            {
                Console.WriteLine(" alt({0}) {1}", phrase.Confidence, phrase.Text);
            }
        }

        static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            double minConfidence = 0.90;
            string txt = e.Result.Text;
            float confidence = e.Result.Confidence;

            Console.WriteLine("\nRecognized: " + txt);
            if (confidence < minConfidence)
            {
                Console.WriteLine($"Failed confidence: {minConfidence} with {confidence}" );
                return;
            }

            if (txt.IndexOf("quit") >= 0)
            {
                if(speechOn)
                    ss.SpeakAsync("Shutting down.");
                else
                    Console.WriteLine("Shutting down.");

                Thread.Sleep(1000);
                _quitEvent.Set();
            }
        } // sre_SpeechRecognized
    } // Program
} // ns
使用系统;
使用系统语音识别;
使用系统、语音、合成;
利用制度全球化;
使用系统线程;
使用System.Collections.Generic;
命名空间S2TextDemo
{
班级计划
{
静态语音合成器ss=新的语音合成器();
静态语音识别引擎sre;
静态bool-speechOn=true;
静态私有自动重置事件;
静态void Main(字符串[]参数)
{
尝试
{
_quitEvent=新的自动恢复事件(false);
ss.setOutputOdeFaultAudioDevice();
CultureInfo ci=新的CultureInfo(“美国”);
sre=新的语音识别引擎(ci);
sre.setInputOdeFaultAudioDevice();
sre.speechrecogned+=sre_speechrecogned;
//sre.SpeechRecognized+=SpeechRecognizedHandler;
Choices ch_StartStopCommands=新选项();
chu StartStopCommands.Add(“quit”);
GrammarBuilder gb_StartStop=新的GrammarBuilder();
gb_StartStop.Append(ch_StartStop命令);
语法g_StartStop=新语法(gb_StartStop);
sre.LoadGrammarAsync(g_StartStop);
sre.RecognizeAsync(RecognizeMode.Multiple);
Console.WriteLine(“侦听…\n”);
ss.SpeakAsync(“我正在听。”);
_quitEvent.WaitOne();
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
Console.ReadLine();
}
}//主要
静态无效SpeechRecognitizedHandler(对象发送方,SpeechRecognitizedEventArgs e)
{
如果(e.Result==null)返回;
字符串txt=e.Result.Text;
//在此处添加事件处理程序代码。
//以下代码说明了一些可用信息
//在识别结果中。
WriteLine(“语法({0}),{1}:{2}”,
e、 Result.Grammar.Name,e.Result.Audio.Duration,e.Result.Text);
//显示识别结果中的语义值。
foreach(e.Result.Semantics中的KeyValuePair子级)
{
Console.WriteLine(“{0}键:{1}”,
child.Key,child.Value.Value??“null”);
}
Console.WriteLine();
//显示有关识别结果中单词的信息。
foreach(在e.Result.Words中识别出的单词)
{
识别Daudio audio=e.Result.GetAudioForWordRange(单词,单词);
Console.WriteLine(“{0,-10}{1,-10}{2,-10}{3}({4})”,
单词。文本,单词。词汇形式,单词。发音,
音频、持续时间、单词、显示属性);
}
//显示结果的识别备选方案。
foreach(e.Result.Alternates中的可识别短语)
{
Console.WriteLine(“alt({0}){1}”,phrase.Confidence,phrase.Text);
}
}
静态无效sre_SpeechRecognized(对象发送方,SpeechRecognizedEventArgs e)
{
双最小置信度=0.90;
字符串txt=e.Result.Text;
浮动置信度=e.结果置信度;
Console.WriteLine(“\n识别:+txt”);
if(置信度<最小置信度)
{
WriteLine($“失败的置信度:{minConfidence}与{confidence}”);
返回;
}
如果(txt.IndexOf(“quit”)>=0)
{
如果(speechOn)
ss.SpeakAsync(“关闭”);
其他的
控制台。WriteLine(“关闭”);
睡眠(1000);
_quitEvent.Set();
}
}//sre_演讲稿
}//程序
}//ns

创建新语法时,请使用以下命令:

sre.LoadGrammarAsync(new DictationGrammar());
来自原始文档:

我真的很讨厌这有多糟糕。我买了一个高质量的麦克风,看看是否有帮助,但这是非常敏感的,有接近我说的话,但很少有我说的实际话。我同意这是不准确的