C# 开发微软Speech 语音识别

.net 4.0 以上

第一步 引用System.Speech

代码如下

 

using System.Speech.Recognition;
using System.Speech.Synthesis;

using System.Globalization;
using System.IO;

 

private void button1_Click(object sender, EventArgs e)
{
try
{

//查看系统中都安装了哪些语言包

//foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers())
//{
// string s = ri.Culture.Name;
//}


SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new CultureInfo("zh-CN"));//en-US

//SpeechRecognitionEngine sre = new SpeechRecognitionEngine();

//命中关键字------------------------------------------------
var words = new Choices();
//words.Add("你好");
words.Add("院领导");
words.Add("办公室");
words.Add("办公区");
words.Add("客户端咨询");
words.Add("外网客户端咨询");
words.Add("检品受理");
words.Add("综合");
words.Add("人事");
words.Add("计划");
words.Add("财务");

words.Add("科研管理");
words.Add("科研");
words.Add("仪器");
words.Add("设备");
words.Add("网站");
words.Add("网络安全");
words.Add("信息");
words.Add("档案");

 

var gb = new GrammarBuilder();
gb.Culture = new System.Globalization.CultureInfo("zh-CN");
gb.Append(words);
Grammar g = new Grammar(gb);

sre.LoadGrammar(g);
//------------------------------------------------------------------------

//sre.LoadGrammar(new DictationGrammar());

sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognitionCallback);
sre.SetInputToDefaultAudioDevice();
sre.RecognizeAsync(RecognizeMode.Multiple);
}
catch (Exception ex)
{
MessageBox.Show("init RS Error : " + ex.ToString());
}
}

 

// 识别的语音命令回调函数
private static void SpeechRecognitionCallback(object sender, SpeechRecognizedEventArgs e)
{
string result = e.Result.Text.ToLower();

MessageBox.Show(result);

}

posted @ 2023-08-18 16:15  海乐学习  阅读(446)  评论(0编辑  收藏  举报