0

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 GrammarBuilder(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.

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
Ami
  • 11
  • 1
  • 2

1 Answers1

2

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.)

Michael Levy
  • 13,097
  • 15
  • 66
  • 100
  • Thank you for your response. I just tried the two lines I added, and it doesn't either. My computer is able to pick it up as dictation and validate that it hears me, but my program isn't able to show this result in my text box – Ami Apr 29 '11 at 22:57
  • Why don't you try the small sample program I posted in http://stackoverflow.com/questions/4730318/what-is-the-best-option-for-transcribing-speech-to-text-in-a-asp-net-web-app/4737003#4737003 and see if it works. See if that works. – Michael Levy Apr 30 '11 at 21:34
  • Thank you, I will. I think it has something to do with the installations I did on this new computer vs the old one, because otherwise the program is verbatim. What components do you think would be important to install on a x64 windows 7 machine? I have already removed the devenv.exe from regedit. – Ami May 02 '11 at 17:31
  • The recognizer engine is core to Windows 7. The System.Speech support is built into .net 3.0 and later. I can't really think of something that would be an installation dependent issue. Could it be something like setting the input volume on the microphone or selecting the correct audio input device as the default? – Michael Levy May 02 '11 at 17:43
  • I actually got it to work on my machine finally. I had to reinstall my windows sdk v7.1 and it ended up working. What I found is that the old file I had made and was working prior was running well, but any new file i tried to make it VS 2010 did not work the same. In debugging I realized that it is somehow skipping the Form1_Load, so that is not getting executed. I have no clue why – Ami May 05 '11 at 21:17
  • You sure you just didn't owverwrite your event binding for Load? anyway, it doesn't matter. You've got it now. – Michael Levy May 05 '11 at 21:24
  • Thanks Michael...I think my problem had something to do with the Program.cs being different formats on different machines, so it executed the code differently. I would appreciate your input on another issue though. – Ami Aug 18 '11 at 20:35
  • I am having issues with speech recognition (used to control windows) interrupting the execution of my code. I found that there was no way to run the code without setting it up, but now it starts trying to insert text when my code is running. Is there any way to suppress the background speech recognition so that it is only concentrated in running my code and not turning other things on/off? – Ami Aug 18 '11 at 20:38
  • You should probably be using an inproc recognizer for your application. You do this by instantiating a SpeechRecognitionEngine() in your application. See SpeechRecognitionEngine Class [http://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine%28v=VS.90%29.aspx]. I suspect you are instantiating a shared recognizer - SpeechRecognizer Class [http://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognizer%28v=VS.90%29.aspx] – Michael Levy Aug 18 '11 at 21:23
  • I think the link is broken. If I do use the SpeechRecognitionEngine class, do i still have to enable speech recognition on my desktop? – Ami Aug 18 '11 at 21:26
  • see http://stackoverflow.com/questions/6633521/using-system-speech-recognition-opens-windows-speech-recognition – Michael Levy Aug 19 '11 at 13:00