简体   繁体   中英

How to use voice as command in C#?

I am having trouble in writing a code in my speech recognition engine. The task is, when the user says 'circle', the engine should automatically draw a circle on the form:

if(Speech == circle)
{
    DrawCircle();
}

The code I'm using for speech recognition is...

namespace speechexampl
{
    public partial class Form1 : Form
    {

        SpeechRecognizer rec = new SpeechRecognizer();

        public Form1()
        {

            InitializeComponent();

            rec.SpeechRecognized += rec_SpeechRecognized;

        }


        void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            lblLetter.Text = e.Result.Text;
        }

        void Form1_Load(object sender, EventArgs e)
        {

             var c = new Choices();
             for (var i = 0; i <= 100; i++)

             c.Add(i.ToString());

             var gb = new GrammarBuilder(c);

             var g = new Grammar(gb);

             rec.LoadGrammar(g);

             rec.Enabled = true;

         }
     }
}

//**

//> and to draw circle or rectangle:

//**

Pen myPen2 = new Pen(System.Drawing.Color.Red, 3);
Rectangle myRectangle2 = new Rectangle(95, 130, 100, 100);
graphicsObj.DrawEllipse(myPen2, myRectangle2);

I don't know how to merge the above code to execute a circle when said so. Any related answer would be a great help!

e.Result.Text will give you what the person said. So if you want to draw a circle when they say "circle" :

if (e.Result.Text == "circle") {
    //Draw a cricle
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM