如何使用语音识别(Microsoft.Speech)获取我所说的所有文本

时间:2014-02-02 17:47:01

标签: c# webforms speech-recognition speech-to-text

我使用Microsoft System.Speech识别用户说出的声音并将其转换为文字。但问题是当用户说出需要为其定义语法的东西时。但实际上我想得到所有错误的词(也没有定义语法)。

例如,用户说'gru gru',我希望文本应该是'gru gru'。有人可以告诉我,我可以这样做吗?我在互联网上搜索了很多地方,但遗憾的是它并没有像我预期的那样工作。

下面的代码

        private delegate void DoStuff(); //delegate for the action

        SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRecog_Click(object sender, EventArgs e)
        {
            DoStuff myAction = SomeVeryLongAction;
            myAction.BeginInvoke(null, null);
        }

        protected void btnStop_Click(object sender, EventArgs e)
        {
        }

        private void SomeVeryLongAction()
        {

            recognizer.SpeechRecognized += recognizer_SpeechRecognized;                

            // Grammar dictationGrammar = new DictationGrammar();
            recognizer.RequestRecognizerUpdate();
            Choices colors = new Choices();
            colors.Add(new string[] { "red", "green", "blue", "hello" });

            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(colors);

            // Create the Grammar instance.
            Grammar g = new Grammar(gb);

            recognizer.LoadGrammar(g);


            recognizer.BabbleTimeout = TimeSpan.FromSeconds(10.0);
            recognizer.EndSilenceTimeout = TimeSpan.FromSeconds(10.0);
            recognizer.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(10.0);
            recognizer.InitialSilenceTimeout = TimeSpan.FromSeconds(10.0);

            try
            {
                recognizer.SetInputToDefaultAudioDevice();
                var result = recognizer.Recognize();                    

                lblInfo.Text = result.Text;
            }
            catch (InvalidOperationException exception)
            {

            }
            finally
            {
                recognizer.UnloadAllGrammars();
            } 
        }   


        void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            var tmp = e.Result.Text;
        }

此刻,当我说出一些未在语法规则中定义的内容,如'love'时,结果总是返回null。我希望有办法得到我说的任何东西。

1 个答案:

答案 0 :(得分:2)

你不能用Microsoft.Speech.Recognition做到这一点。 SR引擎只支持语法,不支持自由格式的听写。

如果你想这样做,你需要切换到支持听写的System.Speech.Recognition,但需要更高质量的音频输入和训练。