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 :
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():
Choices myChoices = new Choices(grammerList.ToArray());
GrammarBuilder builder = new GrammarBuilder(myChoices);
Grammar gram = new Grammar(builder);
speechReco.Enabled = true;
speechReco.SpeechRecognized += new
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.