Rachel Smith
Rachel Smith

Reputation: 1

Commands using speech recognition

I created a C# project using speech recognition where I have a form that has a next and last button What I am trying to do is when I say next the button will take me to the next file or if I say back it will go to the previous file. But When debug the project it only shows me what I say instead of doing it. Does anyone know how can I Fix it?

This is the code I made:

    private void Form1_Load(object sender, EventArgs e)
    {
        SpeechRecognizer recognizer = new SpeechRecognizer();
        Choices command = new Choices();
        command.Add(new string[] { "next", "last", "first" });

        GrammarBuilder gb = new GrammarBuilder();
        gb.Append(command);

        Grammar g = new Grammar(gb);
        recognizer.LoadGrammar(g);

        recognizer.SpeechRecognized +=
              new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized); 
    }


    void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        MessageBox.Show("Speech recognized: " + e.Result.Text);
    }
}

Upvotes: 0

Views: 3181

Answers (1)

zkanoca
zkanoca

Reputation: 9918

A couple of years ago I had a case study for this subject. If you compare my codes with yours you can find out something. The code below changes a light bulb's status.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Threading;


namespace SesTanima
{
    public partial class Form1 : Form
    {
        private SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadGrammars();
            StartRecognition();
        }


        private void LoadGrammars() 
        {
            Choices choices = new Choices( new string[] {"Lights on", "Exit", "Zoom out", "Zoom in", "Reset", "Lights off" } );
            GrammarBuilder grammarBuilder = new GrammarBuilder(choices);
            Grammar grammar = new Grammar(grammarBuilder);
            recognizer.LoadGrammar(grammar);
        }

        private void StartRecognition() 
        {
            recognizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);
            recognizer.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);
            recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
            recognizer.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);


            Thread t1 = new Thread(delegate()
            {            
                recognizer.SetInputToDefaultAudioDevice();
                recognizer.RecognizeAsync(RecognizeMode.Single);
            });
            t1.Start();
        }

        private void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e) 
        {
            textBox1.Text = "Recognizing voice command...";     
        }

        private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
        {
            if (e.Result.Text == "Lights on")
            {
                pictureBox1.Image = Properties.Resources.lightsOn;
            }
            else if (e.Result.Text == "Lights off")
            {
                pictureBox1.Image = Properties.Resources.lightsOff;
            }
            else if ( e.Result.Text == "Exit" )
            {
                recognizer.Dispose();
                Application.Exit();

            }
            else if ( e.Result.Text == "Zoom out" )
            {
                pictureBox1.Size = new System.Drawing.Size( 135, 107 );
            }
            else if ( e.Result.Text == "Zoom in" )
            {
                pictureBox1.Size = new System.Drawing.Size( 538, 426 );
            }
            else if ( e.Result.Text == "Reset" )
            {
                pictureBox1.Size = new System.Drawing.Size( 269, 213 );
            }
            textBox1.Text = e.Result.Text;
        }

        private void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e) 
        {
            textBox1.Text = "Failure.";
        }

        private void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e) 
        {
            recognizer.RecognizeAsync();
        }
    }
}

Upvotes: 2

Related Questions