日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#程序员干货系列之语音识别

發(fā)布時(shí)間:2025/6/17 C# 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#程序员干货系列之语音识别 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

說實(shí)話,這些年來從博客園收獲了不少東西。自從當(dāng)年注冊以來就想平時(shí)分享點(diǎn)簡單的小程序啥的。因?yàn)槠綍r(shí)比較懶,突然發(fā)現(xiàn)近2年沒更新了。準(zhǔn)備陸續(xù)分享些小程序,這些也算是本猿手頭上的一些自制小工具吧。

以后會(huì)陸續(xù)分享些WPF的自制按鈕控件。

?

語音識別小程序,調(diào)用了windows的識別組件。精簡了一些代碼,算是比較簡單易懂的一個(gè)語音識別類。

開發(fā)測試環(huán)境win7,VS2008。如果有其它環(huán)境中的,歡迎補(bǔ)充。

?

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)建關(guān)鍵詞語列表 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();//選擇默認(rèn)的音頻輸入設(shè)備 38 Grammar customGrammar = CreateCustomGrammar(fg); 39 //根據(jù)關(guān)鍵字?jǐn)?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)//關(guān)聯(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()//啟動(dòng)語音識別函數(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()//關(guān)閉語音識別函數(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 //識別出結(jié)果完成的動(dòng)作,通常把識別結(jié)果傳給某一個(gè)控件 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調(diào)用,其中2個(gè)按鈕(開始,停止),1個(gè)文本框(識別結(jié)果)

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;}} }

?

然后就可以測試語音識別了。

?

下載地址

轉(zhuǎn)載于:https://www.cnblogs.com/slowhand/archive/2013/05/07/3065209.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的C#程序员干货系列之语音识别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。