Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
2.80/5 (5 votes)
See more:
I am writing a c# code to control mouse with speech recognition. The code is showing bellow.
C#
using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using System.Speech.Recognition;

namespace my
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine recognitionEngine;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            recognitionEngine = new SpeechRecognitionEngine();
            recognitionEngine.SetInputToDefaultAudioDevice();
            recognitionEngine.SpeechRecognized += (s, args) =>
            {
                string line1 = "";
                foreach (RecognizedWordUnit word in args.Result.Words)
                {
                    if (word.Confidence > 0.5f)
                        line1 += word.Text + " ";
                }

                string command1 = line1.Trim();
                System.Console.WriteLine(command1);
                if (command1 == "start")
                {
                    System.Console.WriteLine("!!!!TEST-1!!!!");

                    recognitionEngine = new SpeechRecognitionEngine();
                    recognitionEngine.SetInputToDefaultAudioDevice();
                    recognitionEngine.SpeechRecognized += (c, arg) =>
                    {
                        string line = "";
                        foreach (RecognizedWordUnit word in args.Result.Words)
                        {
                            if (word.Confidence > 0.5f)
                                line += word.Text + " ";
                        }

                        string command = line.Trim();
                        System.Console.WriteLine(command);
                        System.Console.WriteLine("!!!!TEST-2!!!!");
                        switch (command)
                        {
                            case "left":
                                MoveMouse(Cursor.Position.X - 50, Cursor.Position.Y);
                                break;
                            case "right":
                                MoveMouse(Cursor.Position.X + 50, Cursor.Position.Y);
                                break;
                            case "up":
                                MoveMouse(Cursor.Position.X, Cursor.Position.Y - 50);
                                break;
                            case "down":
                                MoveMouse(Cursor.Position.X, Cursor.Position.Y + 50);
                                break;
                        }

                        txtOutput.Text += line;
                        txtOutput.Text += Environment.NewLine;
                    };

                    recognitionEngine.UnloadAllGrammars();
                    recognitionEngine.LoadGrammar(CreateGrammars());
                    recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

                }
                else
                {
                    System.Console.WriteLine("!!!!TEST-3!!!!");
                }

               
                
                txtOutput.Text += line1;
                txtOutput.Text += Environment.NewLine;
            };

            recognitionEngine.UnloadAllGrammars();
            recognitionEngine.LoadGrammar(CreateGrammars());
            recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        }
        

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            recognitionEngine.RecognizeAsyncStop();
        }

       
        private Grammar CreateGrammars()
        {
            Choices commandChoices = new Choices("left", "right", "up", "down","start");
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            grammarBuilder.Append(commandChoices);
            return new Grammar(grammarBuilder);
        }

        private void MoveMouse(int x, int y)
        {
            this.Cursor = new Cursor(Cursor.Current.Handle);
            Cursor.Position = new Point(x, y);
            Cursor.Clip = new Rectangle(this.Location, this.Size);
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

When user run the code, user can say the commands “up”, “down”, “left”, “right” and the mouse pointer will jump respectively in increments of 50. But first i want when user say "start" run the program and second when user say the commands “up”, “down”, “left”, “right” and the mouse pointer will jump respectively in increments of 50. When user say "stop" then stop the program. In here I said as "start" string command1 get is as "start" and it goes into System.Console.WriteLine("!!!!TEST-1!!!!"); this command. But secondly when i said as "up" string command get it as "start" not "up".So never go to the switch statement. If u can run this code u can understand the problem.I tried to solve this problem since a two week.But i still couldn't slove.If u can solve this problem it will be big help to me. Because i want to finish this project nearly 1 month .Thank u
Posted
Updated 6-Jul-12 4:41am
v5
Comments
Sergey Alexandrovich Kryukov 5-Jul-12 2:42am    
"I could not" is not descriptive. What part of your code cause a problem? What is that problem? Are there any exceptions? How about comprehensive exception report if you have them?
--SA
sampathbc123 6-Jul-12 10:44am    
I updated my question now. I hope now u can understand my problem.Thank u very much for tried to solve problem.
Sergey Alexandrovich Kryukov 6-Jul-12 11:53am    
Sorry, but the code is too bad to make it worth dealing with. You need to re-write it in an supportable way. Main problem is using hard-coded immediate constant. You use "left" in Choices and another "left" in case. How can you support it? What if you change one of the strings? A compiler will not warn you about the problem. You need to do switch by some enumeration type. Never hard-code any strings (and not almost anything else).
--SA
[no name] 5-Jul-12 10:01am    
"I tried to write a code do it. but i could't"... I could not see anywhere in your code where you tried "start" or "stop" so my guess is that you have some other sort of problem.
sampathbc123 6-Jul-12 10:26am    
using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using System.Speech.Recognition;

namespace my
{
public partial class Form1 : Form
{
SpeechRecognitionEngine recognitionEngine;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (s, args) =>
{
string line1 = "";
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.5f)
line1 += word.Text + " ";
}

string command1 = line1.Trim();
System.Console.WriteLine(command1);
if (command1 == "start")
{
System.Console.WriteLine("!!!!TEST-1!!!!");

recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (c, arg) =>
{
string line = "";
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.5f)
line += word.Text + " ";
}

string command = line.Trim();
System.Console.WriteLine(command);
System.Console.WriteLine("!!!!TEST-2!!!!");
switch (command)
{
case "left":
MoveMouse(Cursor.Position.X - 50, Cursor.Position.Y);
break;
case "right":
MoveMouse(Cursor.Position.X + 50, Cursor.Position.Y);
break;
case "up":
MoveMouse(Cursor.Position.X, Cursor.Position.Y - 50);
break;
case "down":
MoveMouse(Cursor.Position.X, Cursor.Position.Y + 50);
break;
}

txtOutput.Text += line;
txtOutput.Text += Environment.NewLine;
};

recognitionEngine.UnloadAllGrammars();
recognitionEngine.LoadGrammar(CreateGrammars());
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

}
else
{
System.Console.WriteLine("!!!!TEST-3!!!!");
}



txtOutput.Text += line1;
txtOutput.Text += Environment.NewLine;
};

recognitionEngine.UnloadAllGrammars();
recognitionEngine.LoadGrammar(CreateGrammars());
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}


private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
recognitionEngine.RecognizeAsyncStop();
}


private Grammar CreateGrammars()
{
Choices commandChoices = new Choices("left", "right", "up", "down","start");
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(commandChoices);
return new Grammar(grammarBuilder);
}

private void MoveMouse(int x, int y)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(x, y);

1 solution

Hi,

I'm trying to understand what's your problem is, I didn't tried it in my enviroment yet.
But recognitionEngine.SpeechRecognized event is called everytime a word is recognized, so in your case you would always have to say "start" and then "up" or something. But I think this can't work because your switch is IN the if-"start" statement.
So what you need is something like a bool variable, like if "start" was recognized (true), it can handle your switch, otherwise bool is still false.

Hope that helps!

Best Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900