How to make speech recognition app in C# using Window Forms?

How to make speech recognition app in C# using Window Forms?

Hello friends! In this article we will leaner that how to build a speech recognizer that will recognize the words spoken by user and write that words in text box and at the same time it can also speak the words that has already been written in text box. So this article will really make you interested in C# development.
This application has also concept little bit related to machine learning algorithm. These algorithms are used through libraries provided .Net  framework like SpeechSynthesizer, PromptBuilder, GrammerBuilder etc.
Here is the screenshot of our application including the toolbox that I have used in this application:

User Interface:


In above I have used a textbox with multiline is checked and two buttons (i) speak text to speak already written words in text box and start to write words that are spoken. So lets code this Speech recognizer.
Source Code in C#:
______________________________________________________________________________
using System;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;

namespace TR_Speech_Recoginition
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //provide the access to already installed speech synthesis engine
        SpeechSynthesizer sSynth = new SpeechSynthesizer();
        //done the basics work about speaking and pronocication
        PromptBuilder pBuild = new PromptBuilder();
        //manage in process speech recognition engine
        SpeechRecognitionEngine sRecog = new SpeechRecognitionEngine();

        //speak text that has already been written
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {        
                pBuild.ClearContent();
                pBuild.AppendText(textBox1.Text);
                sSynth.Speak(pBuild);              
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Strat button to start recognizing the words from speaking
        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button2.Enabled = true;
            Choices sList = new Choices();
            sList.Add(new string[] { "hello", "test", "it", "works", });
            Grammar gr=new Grammar(new GrammarBuilder(sList));
            try{
                sRecog.RequestRecognizerUpdate();
                sRecog.LoadGrammar(gr);
                //now use the SpeechRecognized event that is written below
                sRecog.SpeechRecognized+=sRecog_SpeechRecognized;
                sRecog.SetInputToDefaultAudioDevice();
                sRecog.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch
            {
                return;
            }
        }

        private void sRecog_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {        
            if(e.Result.Text=="exit")
            {
            Application.Exit();
            }
            else
            {
            textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString();
            }
        }
    }
}
______________________________________________________________________________

Above is the code for our speech recognizer so run it and enjoy your new app. Here below video description is also provided.
Video Tutorial:





Please comment your suggestions and follow this blog to be updated with technology.

Comments

Popular posts from this blog

How to build a CGPA Calculator in C# using Window Forms ?

How to change back color of window form using Track Bars in C#?

How to Build/Make a Calculator in C# using window form ?