【发布时间】:2014-12-24 13:19:22
【问题描述】:
我尝试在 C# 中识别语音,但开始很糟糕。我在 YouTube 上遵循了一些教程,但每次都会出现这个错误。所以我得到了微软的 MSDN 代码,并试图在谷歌中找到一些解决方案。这是我正在使用的代码:
public Form1()
{
InitializeComponent();
Initialize();
}
private void Initialize()
{
// Create a new SpeechRecognitionEngine instance.
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
// Configure the input to the recognizer.
//sre.SetInputToWaveFile(@"c:\Test\Colors.wav");
sre.SetInputToDefaultAudioDevice();
// Create a simple grammar that recognizes "red", "green", or "blue".
Choices colors = new Choices();
colors.Add(new string[] { "red", "green", "blue" });
// Create a GrammarBuilder object and append the Choices object.
GrammarBuilder gb = new GrammarBuilder();
gb.Append(colors);
// Create the Grammar instance and load it into the speech recognition engine.
Grammar g = new Grammar(gb);
sre.LoadGrammar(g);
// Register a handler for the SpeechRecognized event.
sre.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
// Start recognition.
sre.Recognize();
}
// Create a simple handler for the SpeechRecognized event.
void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
MessageBox.Show("Speech recognized: " + e.Result.Text);
}
我已经下载了Microsoft Speech Platform SDK 和Runtime Languages(美国)。错误仍然存在。
我也已经使用过这段代码,正如我在 StackOverflow 的一个主题中看到的那样:
sre = new SpeechRecognitionEngine(new CultureInfo("en-GB"));
它不起作用,所以我尝试使用它(正如我在 MSDN 中看到的那样):
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
当我尝试使用这些代码时,错误发生了变化并说:“找不到所需 ID 的识别器”。 我使用 Windows 7 (Home Basic, x64), Microsoft Visual C# 2010 Express。我的默认语言(系统语言也是)是葡萄牙语(巴西),也许是错误原因? 好了,就是这样,希望我写的都详细,让你明白。对不起我的英语我正在训练这个,哈哈:P
【问题讨论】:
-
@Saruman 我已切换,错误更改为“未找到所需 ID 的识别器”。在 CultureInfo 中(当我创建 sre 时)。我试图下载 pt-BR 文件,但它也发生了。如果我不理会它,错误会更改为“此系统上无法使用语音识别。找不到 SAPI 和语音识别引擎。”
标签: c# .net speech-recognition