C#程序员干货系列之语音识别
生活随笔
收集整理的這篇文章主要介紹了
C#程序员干货系列之语音识别
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
說實話,這些年來從博客園收獲了不少東西。自從當年注冊以來就想平時分享點簡單的小程序啥的。因為平時比較懶,突然發(fā)現(xiàn)近2年沒更新了。準備陸續(xù)分享些小程序,這些也算是本猿手頭上的一些自制小工具吧。
以后會陸續(xù)分享些WPF的自制按鈕控件。
?
語音識別小程序,調用了windows的識別組件。精簡了一些代碼,算是比較簡單易懂的一個語音識別類。
開發(fā)測試環(huán)境win7,VS2008。如果有其它環(huán)境中的,歡迎補充。
?
SRecognition.cs
1 using System; 2 using System.Speech.Recognition; 3 using System.Globalization; 4 using System.Windows.Forms; 5 6 namespace NingTao 7 { 8 public class SRecognition 9 { 10 public SpeechRecognitionEngine recognizer = null;//語音識別引擎 11 public DictationGrammar dictationGrammar = null; //自然語法 12 public System.Windows.Forms.Control cDisplay; //顯示控件 13 14 public SRecognition(string[] fg) //創(chuàng)建關鍵詞語列表 15 { 16 CultureInfo myCIintl = new CultureInfo("zh-CN"); 17 foreach (RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers())//獲取所有語音引擎 18 { 19 if (config.Culture.Equals(myCIintl) && config.Id == "MS-2052-80-DESK") 20 { 21 recognizer = new SpeechRecognitionEngine(config); 22 break; 23 }//選擇識別引擎 24 } 25 if (recognizer != null) 26 { 27 InitializeSpeechRecognitionEngine(fg);//初始化語音識別引擎 28 dictationGrammar = new DictationGrammar(); 29 } 30 else 31 { 32 MessageBox.Show("創(chuàng)建語音識別失敗"); 33 } 34 } 35 private void InitializeSpeechRecognitionEngine(string[] fg) 36 { 37 recognizer.SetInputToDefaultAudioDevice();//選擇默認的音頻輸入設備 38 Grammar customGrammar = CreateCustomGrammar(fg); 39 //根據(jù)關鍵字數(shù)組建立語法 40 recognizer.UnloadAllGrammars(); 41 recognizer.LoadGrammar(customGrammar); 42 //加載語法 43 recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized); 44 //recognizer.SpeechHypothesized += new EventHandler <SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized); 45 } 46 public void BeginRec(Control tbResult)//關聯(lián)窗口控件 47 { 48 TurnSpeechRecognitionOn(); 49 TurnDictationOn(); 50 cDisplay = tbResult; 51 } 52 public void over()//停止語音識別引擎 53 { 54 TurnSpeechRecognitionOff(); 55 } 56 public virtual Grammar CreateCustomGrammar(string[] fg) //創(chuàng)造自定義語法 57 { 58 GrammarBuilder grammarBuilder = new GrammarBuilder(); 59 grammarBuilder.Append(new Choices(fg)); 60 return new Grammar(grammarBuilder); 61 } 62 private void TurnSpeechRecognitionOn()//啟動語音識別函數(shù) 63 { 64 if (recognizer != null) 65 { 66 recognizer.RecognizeAsync(RecognizeMode.Multiple); 67 //識別模式為連續(xù)識別 68 } 69 else 70 { 71 MessageBox.Show("創(chuàng)建語音識別失敗"); 72 } 73 } 74 private void TurnSpeechRecognitionOff()//關閉語音識別函數(shù) 75 { 76 if (recognizer != null) 77 { 78 recognizer.RecognizeAsyncStop(); 79 TurnDictationOff(); 80 } 81 else 82 { 83 MessageBox.Show("創(chuàng)建語音識別失敗"); 84 } 85 } 86 private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 87 { 88 //識別出結果完成的動作,通常把識別結果傳給某一個控件 89 string text = e.Result.Text; 90 cDisplay.Text += text; 91 } 92 private void TurnDictationOn() 93 { 94 if (recognizer != null) 95 { 96 recognizer.LoadGrammar(dictationGrammar); 97 //加載自然語法 98 } 99 else 100 { 101 MessageBox.Show("創(chuàng)建語音識別失敗"); 102 } 103 } 104 private void TurnDictationOff() 105 { 106 if (dictationGrammar != null) 107 { 108 recognizer.UnloadGrammar(dictationGrammar); 109 //卸載自然語法 110 } 111 else 112 { 113 MessageBox.Show("創(chuàng)建語音識別失敗"); 114 } 115 } 116 } 117 }?
form調用,其中2個按鈕(開始,停止),1個文本框(識別結果)
using System; using System.Windows.Forms;namespace NingTao {public partial class Form1 : Form{private SRecognition sr;public Form1(){InitializeComponent();string[] fg = { "東方", "西方", "南方", "北方" };sr = new SRecognition(fg);button2.Enabled = false;}private void button1_Click(object sender, EventArgs e){sr.BeginRec(textBox1);button1.Enabled = false;button2.Enabled = true;}private void button2_Click(object sender, EventArgs e){sr.over();button1.Enabled = true;button2.Enabled = false;}} }?
然后就可以測試語音識別了。
?
下載地址
轉載于:https://www.cnblogs.com/slowhand/archive/2013/05/07/3065209.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的C#程序员干货系列之语音识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jackson 框架使用说明,轻易转换J
- 下一篇: 打算把我的视频工具整合一下