Speech Recognition in a C# WPF Application

Last week, I posted a tutorial on how to save and retrieve images using SQL Server and WPF. This is an extension to that article. In my previous article, the images were displayed in the image box as soon as its name was selected from the list box. In this tutorial the picture will be displayed from the database as soon as you the speak the name of the picture.

Yes.. we will be playing with Speech Recognition technology! (Please refer to this article for Speech to Text)

The database schema used for this tutorial is similar to the one I used in my previous article.

Lets get started! Create a new WPF Application in Visual Studio 2008 and follow the steps below:

1. Add Reference to System.Speech and make Global Declarations

Add a reference to the System.Speech assembly:

First declare an object of SpeechRecognizer and a list of string globally.

We use a list data structure because it lets us add items to it dynamically.

SpeechRecognizer speechReco = new SpeechRecognizer();

List<string> grammerList = new List<string>();

Add the namespace on the top :

using System.Speech.Recognition;

2. Build the Grammar

Get all the names of the pictures from the database in a dataset.

Add all the names through a loop in the ‘grammerlist’.

Create an object of Choices which hold the list in an array.

Create an object of Grammer Builder to hold ‘mychoices’.

Create an object of Grammer with the help of the Grammer Builder object.

Load the Grammer object into the Speech Recognition object.

Enable the speech recognition object.

Add the event handler to the SpeechRecognized property of SpeechRecognizer-as soon as it hears something from the grammar list, the event will be fired.

Add the following code in the constructor of your Window():

public Window2()

{

InitializeComponent();

sqlCon.Open();

Dataset ds = new DataSet();

SqlDataAdapter sqa = new SqlDataAdapter

("select name from picture", sqlCon);

sqa.Fill(ds);

sqlCon.Close();

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

{

grammerList.Add(ds.Tables[0].Rows[i][0].ToString());

}

Choices myChoices = new Choices(grammerList.ToArray());

GrammarBuilder builder = new GrammarBuilder(myChoices);

Grammar gram = new Grammar(builder);

speechReco.LoadGrammar(gram);

speechReco.Enabled = true;

speechReco.SpeechRecognized += new

EventHandler<SpeechRecognizedEventArgs>

(speechReco_SpeechRecognized);

}

Grammar is added to narrow down the number of possibilities the speech recognizer has. Limiting the recognition pool increases the accuracy. Otherwise it takes a lot of time for it to recognize the words you speak precisely.

3. Display the Picture

Select the picture in bytes corresponding to what the user has said from the database in the dataset.

If the dataset is not null then display that picture in the image box.

This is a really useful tutorial/sample, so thank you I have pretty much copied it all so far!

Although my application is different it is very similar. I would like to be able to read in a .wav file and do voice recognition on that, could anybody point me to a suitable example or advise as to how to go about doing this,