程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 更多關於編程 >> C#中調用SAPI實現語音識別的2種方法

C#中調用SAPI實現語音識別的2種方法

編輯:更多關於編程

       這篇文章主要介紹了C#中調用SAPI實現語音識別的2種方法,本文直接給出實現代碼,需要的朋友可以參考下

      通過微軟的SAPI,不僅僅可以實現語音合成TTS,同樣可以實現語音識別SR。下面我們就介紹並貼出相關代碼。主要有兩種方式:

      1、使用COM組件技術,不管是C++,C#,Delphi都能玩的轉,開發出來的東西在XP和WIN7都能跑。(注意要引入系統組件SpeechLib,XP要安裝識別引擎)

      2、使用WIN7的windows api,其實最終還是調用了SAPI,所以開發出來的東西就只能在WIN7上面跑。

      其實不管是哪一種,都是調用SAPI,可能後一種代碼比較簡單。

      使用第一種方式,需要注意在COM選項卡裡面的Microsoft Speech object library引用

      ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public class SpRecognition { private static SpRecognition _Instance = null; private SpeechLib.ISpeechRecoGrammar isrg; private SpeechLib.SpSharedRecoContextClass ssrContex = null;   public delegate void StringEvent(string str); public StringEvent SetMessage;   private SpRecognition() { ssrContex = new SpSharedRecoContextClass(); isrg = ssrContex.CreateGrammar(1); SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle = new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition); ssrContex.Recognition += recHandle; } public void BeginRec() { isrg.DictationSetState(SpeechRuleState.SGDSActive); } public static SpRecognition instance() { if (_Instance == null) _Instance = new SpRecognition(); return _Instance; } public void CloseRec() { isrg.DictationSetState(SpeechRuleState.SGDSInactive); } private void ContexRecognition(int iIndex, object obj, SpeechLib.SpeechRecognitionType type, SpeechLib.ISpeechRecoResult result) { if (SetMessage != null) { SetMessage(result.PhraseInfo.GetText(0, -1, true)); } } }

      第二種同樣需要引入,不過引入的是Win7中的.NET3.5類庫

      ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Speech; using System.Speech.Recognition; using System.Globalization; using System.Windows.Forms;   namespace StudyBeta { public class SRecognition { public SpeechRecognitionEngine recognizer = null;//語音識別引擎 public DictationGrammar dictationGrammar = null; //自然語法 public System.Windows.Forms.Control cDisplay; //顯示控件   public SRecognition(string[] fg) //創建關鍵詞語列表 { CultureInfo myCIintl = new CultureInfo("en-US"); foreach (RecognizerInfo config in SpeechRecognitionEngine. InstalledRecognizers())//獲取所有語音引擎 { if (config.Culture.Equals(myCIintl) && config.Id == "MS-1033-80-DESK" ) { recognizer = new SpeechRecognitionEngine(config); break; }//選擇美國英語的識別引擎 } if (recognizer != null) { InitializeSpeechRecognitionEngine(fg);//初始化語音識別引擎 dictationGrammar = new DictationGrammar(); } else { MessageBox.Show("創建語音識別失敗"); } } private void InitializeSpeechRecognitionEngine(string[] fg) { recognizer.SetInputToDefaultAudioDevice();//選擇默認的音頻輸入設備 Grammar customGrammar = CreateCustomGrammar(fg); //根據關鍵字數組建立語法 recognizer.UnloadAllGrammars(); recognizer.LoadGrammar(customGrammar); //加載語法 recognizer.SpeechRecognized += new EventHandler <SpeechRecognizedEventArgs>(recognizer_SpeechRecognized); recognizer.SpeechHypothesized += new EventHandler <SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized); } public void BeginRec(Control tbResult)//關聯窗口控件 { TurnSpeechRecognitionOn(); TurnDictationOn(); cDisplay = tbResult; } public void over()//停止語音識別引擎 { TurnSpeechRecognitionOff(); } public virtual Grammar CreateCustomGrammar(string[] fg) //創造自定義語法 { GrammarBuilder grammarBuilder = new GrammarBuilder(); grammarBuilder.Append(new Choices(fg)); return new Grammar(grammarBuilder); } private void TurnSpeechRecognitionOn()//啟動語音識別函數 { if (recognizer != null) { recognizer.RecognizeAsync(RecognizeMode.Multiple); //識別模式為連續識別 } else { MessageBox.Show("創建語音識別失敗"); } } private void TurnSpeechRecognitionOff()//關閉語音識別函數 { if (recognizer != null) { recognizer.RecognizeAsyncStop(); TurnDictationOff(); } else { MessageBox.Show("創建語音識別失敗"); } } private void recognizer_SpeechRecognized(object sender, SpeechRecognized EventArgs e) { //識別出結果完成的動作,通常把識別結果傳給某一個控件 string text = e.Result.Text; cDisplay.Text = text; } private void TurnDictationOn() { if (recognizer != null) { recognizer.LoadGrammar(dictationGrammar); //加載自然語法 } else { MessageBox.Show("創建語音識別失敗"); } } private void TurnDictationOff() { if (dictationGrammar != null) { recognizer.UnloadGrammar(dictationGrammar); //卸載自然語法 } else { MessageBox.Show("創建語音識別失敗"); } } } }
    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved