2010-11-18 111 views
2

我試圖用Windows 7識別語音,但它始終將語音識別爲命令或只是說「那是什麼?」。SAPI和Windows 7問題

我如何獲得所有演講?

CODE:

SpeechRecognizer _speechRecognizer; 

    public Window1() 
    { 
     InitializeComponent(); 

     // set up the recognizer 
     _speechRecognizer = new SpeechRecognizer(); 
     _speechRecognizer.Enabled = false; 
     _speechRecognizer.SpeechRecognized += 
     new EventHandler<SpeechRecognizedEventArgs>(_speechRecognizer_SpeechRecognized); } 

回答

5

也許你想使用.NET System.Speech命名空間,而不是SAPI?有一篇很好的文章在幾年前發佈在http://msdn.microsoft.com/en-us/magazine/cc163663.aspx。這可能是迄今爲止我發現的最好的介紹性文章。這是有點過時,但非常helfpul。 (AppendResultKeyValue方法在測試版後被刪除。)

您是否在嘗試使用共享識別器?這可能是你看到命令的原因。你有具體的任務來表彰嗎?在這種情況下,您最好使用特定於任務的語法和inproc識別器。

如果您需要處理任何單詞,請使用System.Speech附帶的DictationGrammar。請參閱http://msdn.microsoft.com/en-us/library/system.speech.recognition.dictationgrammar%28VS.85%29.aspx

爲了好玩,我將最簡單的.NET窗體窗體應用程序放在一起,以便使用我能想到的聽寫語法。我創建了一個表單。丟下一個按鈕,並使按鈕大。加入System.Speech參考和行:

using System.Speech.Recognition; 

然後添加以下事件處理程序按鈕1:

private void button1_Click(object sender, EventArgs e) 
{   
    SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); 
    Grammar dictationGrammar = new DictationGrammar(); 
    recognizer.LoadGrammar(dictationGrammar); 
    try 
    { 
     button1.Text = "Speak Now"; 
     recognizer.SetInputToDefaultAudioDevice(); 
     RecognitionResult result = recognizer.Recognize(); 
     button1.Text = result.Text; 
    } 
    catch (InvalidOperationException exception) 
    { 
     button1.Text = String.Format("Could not recognize input from default aduio device. Is a microphone or sound card available?\r\n{0} - {1}.", exception.Source, exception.Message); 
    } 
    finally 
    { 
     recognizer.UnloadAllGrammars(); 
    }       
} 
+0

感謝邁克爾。我需要認識每一個字。您提供的鏈接中的所有示例都是關於實際構建命令的。我如何獲得全部? – Kaan 2010-11-18 23:19:47

+0

如果您使用桌面識別器(Windows Vista和7中附帶),則它帶有內置的聽寫語法。請參閱http://msdn.microsoft.com/en-us/library/system.speech.recognition.dictationgrammar%28VS.85%29.aspx – 2010-11-19 03:26:19

+0

我更新了答案以包含使用DictationGrammar的示例。 – 2010-11-24 20:35:03