微软.net framework自带离线语音识别引擎,可以支持多种语言的离线识别。该引擎具有免费、无需网络、针对性强、运算量小、兼容windows的特点,对于固定语境的语音识别具有不可比拟的优势。
在使用时注意添加对system.speech的引用,默认是不引用的,如果没有请注意.net版本,最好在4.5以上。另外添加using命名空间。以下为简单的控制台例子。
using System;
using System.Speech.Recognition;
namespace 语音识别
{
class Program
{
static void Main(string[] args)
{
// Create an in-process speech recognizer for the en-US locale.
using (
SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("zh-CN")))
{
Choices choices = new Choices(new string[] { "你好", "123","木头人","我是"});
GrammarBuilder gb = new GrammarBuilder(choices);
// Create and load a dictation grammar.
recognizer.LoadGrammar(new Grammar(gb));
// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler(recognizer_SpeechRecognized);
// Configure input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice();
// Start asynchronous, continuous speech recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple);
// Keep the console window open.
while (true)
{
Console.ReadLine();
}
}
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Recognized text: " + e.Result.Text);
}
}
}
可以直接下载我的例程https://download.csdn.net/download/u010622265/12649885
需要注意的是CultureInfo,默认情况下中文的操作系统只有"Zh-CN",使用其他的会报错,除非系统安装了其他的语音识别包,win10可在设置里安装:
语法可以有多种形式,具体参考.net文档。
下面列举一种以SRGS(Speech Recognition Grammar Specification)规范定义的语法文件(格式遵循xml)。
该文件中root是main规则,而main规则中又可以包含其他规则(part1-5)定义的语法,最终包含的句式有:
我/你/他+是+学生/老师/工人
我/你/他+喜欢/讨厌+打球/读书/音乐
我/你/他+今年+12/24/36/50+岁
是 今年 岁 - 我
- 你
- 他
- 学生
- 老师
- 工人
- 喜欢
- 讨厌
- 打球
- 读书
- 音乐
- 12
- 24
- 36
- 50
调用这种SRGS文件定义的语法:
recognizer.LoadGrammar(new Grammar("Grammar.xml"));
SRGS格式详细参考https://www.w3.org/TR/speech-grammar/



