Speech Recognition in C#.Net

Sunday, April 26, 2009

I’ve now started playing around with speech recognition using C# in Visual Studio 2008. The last time I demonstrated Speech Synthesis to read the contents out loud from a text file. However this implementation is command based and not dictation based. This is very very basic and it recognizes only one word and then displays the results from database containing that word. For basics on getting started with speech utilities in C#, refer to my last post here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech;
using System.Speech.Recognition;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace speechrecog
{
    class Program
    {
        //connection initialization to the database
        static string connectionString = "Data Source=MEOW-PC;Initial Catalog=Shopping_eye;Integrated Security=SSPI";
        static SqlConnection cn = new SqlConnection(connectionString);        
        static void Main(string[] args)
        {
            new Program();            
        }
        private Program()
        {
            //access to the default shared speech recognition engine used by the desktop
            SpeechRecognizer rec = new SpeechRecognizer();            
            rec.SpeechRecognized += rec_SpeechRecognized;                        
            //add the words in choices that you want to be recognized     
            Choices c = new Choices();            
            c.Add("cheese");
            c.Add("pizza");
            c.Add("olives");
            c.Add("exit");
            //Populate the grammar builder with the choices and load it as a new grammar
            GrammarBuilder pizzadetails = new GrammarBuilder(c);            
            Grammar g = new Grammar(pizzadetails);
            rec.LoadGrammar(g);
            rec.Enabled = true;           
            Console.Read();            
        }
        void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Text == "exit")
                System.Environment.Exit(0);//Closes the console when you say exit
            string cassy =e.Result.Text;
            Console.Write(cassy);       
            //This query will display the results containing the word that was recognized                 
            string squery = "select * from speechrecogtest where name like '%"+cassy+"%'";
            SqlDataAdapter da = new SqlDataAdapter(squery, cn);
            DataTable dt = new DataTable();
            cn.Open();
            // Fill the data table with select statement's query results:
            int recordsAffected = da.Fill(dt);            
            System.Console.WriteLine();
            if (recordsAffected > 0)
            {             
                foreach (DataRow dr in dt.Rows)
                {
                    System.Console.Write(dr[0]);
                    System.Console.Write(" ");
                    System.Console.Write(dr[1]);
                    System.Console.Write(" ");
                    System.Console.Write(dr[2]);
                    System.Console.WriteLine("");
                }
            }
        }
    }
}
 



image



So, when I say “cheese”, the following entries containing the word are retrieved from the database and displayed on the console. I am still figuring how to loop the program to recognize my commands back to back, as per the choices mentioned in the code. Until then, keep trying and experimenting…Au Revoire!



4 comments:

Vinay S, Associate Profesor and Head, Dept of CSE, PESITM, Shivamogga said...

most of the application can be enhanced by making use of speech synthesis and recognition. It adds a new dimension to the application. I was not aware of C# supporting these things. Very good article... I hope students make use of this in their projects...

swaroop said...

hey its nice !!! but hav u used ur brain or else u surfed net ..!
anyways nice one ..!!

Ni said...

both surfed net and used brain...MSDN provides fairly good documentation for C# which you can refer..however this is the most simple and probably a crude way of doing it (don't know if the my program adheres to any standards)...Moreover this just command based and not dictation based..still learning about the latter part!

Ganesh Kumar K said...

Hi Blogger,
Nice post it is. But in your example you just chosen only 4-5 words as grammar. Now my requirement is i want to use all the English words. Is there any alternative for adding grammar or this is the only way to add words.

Thanks for your support in advance.

 
Design by Pocket