Running a basic speech recognition program with windows 7 in c# VS 2010, it compiles, but it does not run
I recently tried writing a basic speech recognition using C# (WinFormsApp) in VS 2010 on a Windows 7 Dell computer. I was able to make it work using the basic examples from microsoft (http://msdn.microsoft.com/en-us/magazine/cc163663.aspx#S5) and from forums like this one. However, I needed to change computers and now I am trying to replicate this on a Lenovo computer with the same specs. It does not work, in fact, the speech recognition keeps interfering with the program running and when I changed to the SpeechRecognitionEngine
it still doesn't run.
I am able to compile with no error, but I do not see a result, that is, the MessageBox showing the e.Result.Text
.
My code is below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Threading;
namespace SpeechRecogTest
{
public partial class Form1 : Form
{
SpeechRecognitionEngine sr = new SpeechRecognitionEngine();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Create grammar
Choices words = new Choices();
words.Add("Hi");
words.Add("No");
words.Add("Yes");
Grammar wordsList = new Grammar(new G开发者_Python百科rammarBuilder(words));
wordsList.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(rec_SpeechRecognized);
sr.LoadGrammar(wordsList);
}
void rec_SpeechRecognized(object sender, RecognitionEventArgs e)
{
MessageBox.Show(e.Result.Text);
}
}
}
I would really appreciate all your help. I am pretty sure I have installed all the SDKs, including SAPI 5.1, Windows SDK v7.0 and v7.1, I have also included the speech libraries for both COM and NET, and I built a synthesizer that works.
You loaded the grammar, but did you ever call sr.RecognizeAsync(); ?
You have to call either Recognize() for synchronous recognition or RecognizeAsync() to perform a recognition. You have done neither.
Assuming you are capturing audio from the soundcard, after the grammar is loaded, try:
sr.SetInputToDefaultAudioDevice();
sr.RecognizeAsync();
To get started with .NET speech, there is a very good article that was published a few years ago at http://msdn.microsoft.com/en-us/magazine/cc163663.aspx. It is probably the best introductory article I’ve found so far. It is a little out of date, but very helfpul. (The AppendResultKeyValue method was dropped after the beta.)
精彩评论