issue with vs2010 C# windows voice recognition - c#

I have ran into an interesting issue with my voice recognition code for C#. I have had this code work before, but I migrated it to another project and it just wont work. I must be missing something, because there are no errors or warnings about the speech recognition and I do have the reference for speech. Here is the main function:
static void Main(string[] args)
{
Program prgm = new Program();
string[] argument = prgm.readConfigFile();
if(argument[2].ToLower().Contains("true"))
{
recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
recognizer.LoadGrammar(new DictationGrammar());
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
recognizer.SetInputToDefaultAudioDevice();
recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
prgm._con.updateConsole(argument, prgm._list);
}
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(e.Result.Text);
}
along with the recognizer:
recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
I did add the using System.Speech at the top of my code. When ever I start talking the event handler should start, but it never gets hit (checked with breakpoint). What am I doing wrong?

Related

RecognizeAsync() doesn't work but Recognize() does, why?

Im trying to have my windows forms program to continuously listen to my microphone to detect speech and then display that information on the gui.
Here is my SpeechListener class:
{
public class SpeechListener
{
GUI gui;
public SpeechListener(GUI gui) { this.gui = gui; }
public void StartListening() {
gui.setLabel("speech activated");
// Create an in-process speech recognizer for the en-US locale.
using (
SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("en-US")))
{
// Create and load a dictation grammar.
recognizer.LoadGrammar(new DictationGrammar());
// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(Recognizer_SpeechRecognized);
// Configure input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice();
// Start asynchronous, continuous speech recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
}
// Handle the SpeechRecognized event.
public void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//this is where i want to get
gui.setLabel("Recognized text: " + e.Result.Text);
}
}
}
This class is instantiated and StartListening() is called from a form class (my gui).
I never reach the method that handles the SpeechRecognized event. However when i change
recognizer.RecognizeAsync(RecognizeMode.Multiple);
to
recognizer.Recognize();
the speech detection works (but only once, and it freezes my gui). Why doesn't the async method work?
I've used this same code on a console program and it works perfectly.

Microsoft Speech Platform speech to text

I'd like to write the speech a user says to text. Can I do this with the Microsoft Speech Platform? Perhaps I'm just misunderstanding how it's supposed to work and what its intended use case is.
I've got this console application now:
static void Main(string[] args)
{
Choices words = new Choices();
words.Add(new string[] { "test", "hello" ,"blah"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(words);
Grammar g = new Grammar(gb);
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
sre.LoadGrammar(g);
sre.SetInputToDefaultAudioDevice();
//add listeners
sre.Recognize();
Console.ReadLine();
}
And it only seems to output the words that I specify in Choices.
Would I have to add an entire dictionary of words if I wanted to match (most) of what a user will say?
Furthermore it stops right after it matches a single word. What if I wanted to capture entire sentences?
I'm looking for solutions for A) Capturing a wide array of words, and B) capturing more than one word at once.
Edit:
I found this: http://www.codeproject.com/Articles/483347/Speech-recognition-speech-to-text-text-to-speech-a#torecognizeallspeech
As seen in this page, the DictationGrammar class has a basic library of common words.
To capture more than one word at once I did
sre.RecognizeAsync(RecognizeMode.Multiple);
So my code is now this:
public static SpeechRecognitionEngine sre;
static void Main(string[] args)
{
sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
sre.LoadGrammar(new Grammar(new GrammarBuilder("exit")));
sre.LoadGrammar(new DictationGrammar());
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
Console.ReadLine();
}
private static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text == "exit")
{
sre.RecognizeAsyncStop();
}
Console.WriteLine("You said: " + e.Result.Text);
}

Stop responding to windows commands

Please have a look at the following code
private void button2_Click(object sender, EventArgs e)
{
SpeechRecognizer sr = new SpeechRecognizer();
Choices colors = new Choices();
colors.Add(new string[] { "red arrow", "green", "blue" });
GrammarBuilder gb = new GrammarBuilder();
gb.Append(colors);
Grammar g = new Grammar(gb);
sr.LoadGrammar(g);
// SpeechSynthesizer s = new SpeechSynthesizer();
// s.SpeakAsync("start speaking");
sr.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sr_SpeechRecognized);
}
void sr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
MessageBox.Show(e.Result.Text);
}
This is normal speech recognition code which uses the MS speech engine. You can see here that I have loaded some grammar. But, there is an issue as well. That is, this is not responding only to the given grammar but also to the MS Built-In speech commands! Like speech command to minimize a window, open start menu etc!
I really don't need that. My application should only respond to my grammar and not to MS built-in commands. Is there is a way I can achieve this?
The SpeechRecognizer object builds on top of the existing Windows Speech system. From MSDN:
Applications use the shared recognizer to access Windows Speech
Recognition. Use the SpeechRecognizer object to add to the Windows
speech user experience.
Consider using a SpeechRecognitionEngine object instead as this runs in-process rather than system-wide.

Speech to Text C# Train For Better Translation

I need to way to make the speech to text smarter as many of the words it is just getting incorrect in the translation. I cannot find much help on adding a list of words, not commands or grammar but words to help better translate audio recording.
Here is the code I found on the web, and this works, but I need to way to train, or make the engine smarter. Any ideas?
Thanks.
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("en-US")))
{
// Create and load a dictation grammar.
recognizer.LoadGrammar(new DictationGrammar());
// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// Configure input to the speech recognizer.
recognizer.SetInputToWaveFile(#"c:\test2.wav");
// 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);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"C:\WriteLines2.txt", true))
{
file.WriteLine("");
}
}

Programmatically turn off the automation features of windows speech recognition?

I'm making a program that uses the system.speech namespace (it's a simple program that will launch movies). I load all of the filenames from a folder and add them to the grammars I want to use. It's working remarkably well, however there is a hitch: I DON'T want the windows speech recognition to interact with windows at all (ie. when I say start, I don't want the start menu to open... I don't want anything to happen).
Likewise, I have a listbox for the moment that lists all of the movies found in the directory. When I say the show/movie that I want to open, the program isn't recognizing that the name was said because windows speech recognition is selecting the listboxitem from the list instead of passing that to my program.
The recognition is working otherwise, because I have words like "stop", "play", "rewind" in the grammar, and when I catch listener_SpeechRecognized, it will correctly know the word(s)/phrase that I'm saying (and currently just type it in a textbox).
Any idea how I might be able to do this?
I'd use the SpeechRecognitionEngine class rather than the SpeechRecognizer class. This creates a speech recognizer that is completely disconnected from Windows Speech Recognition.
private bool Status = false;
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
Choices dic = new Choices(new String[] {
"word1",
"word2",
});
public Form1()
{
InitializeComponent();
Grammar gmr = new Grammar(new GrammarBuilder(dic));
gmr.Name = "myGMR";
// My Dic
sre.LoadGrammar(gmr);
sre.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
sre.SetInputToDefaultAudioDevice();
sre.RecognizeAsync(RecognizeMode.Multiple);
}
private void button1_Click(object sender, EventArgs e)
{
if (Status)
{
button1.Text = "START";
Status = false;
stslable.Text = "Stopped";
}
else {
button1.Text = "STOP";
Status = true;
stslable.Text = "Started";
}
}
public void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs ev)
{
String theText = ev.Result.Text;
MessageBox.Show(theText);
}

Categories