Thursday, 7 November 2013

Speech Recognition

In today’s world everything is being automated using voice commands and hand gestures. In this topic I would like to explain speech recognition. The System.Speech namespaces are available in Microsoft .NET Framework 3.0 and later versions.


Text To Speech

using System;
using System.Speech.Synthesis;

namespace SampleSynthesis
{
  class Program
  {
    static void Main(string[] args)
    {

      // Initialize a new instance of the SpeechSynthesizer.
      SpeechSynthesizer synth = new SpeechSynthesizer();

      // Configure the audio output.
      synth.SetOutputToDefaultAudioDevice();

      // Speak a string.
      synth.Speak("This example demonstrates a basic use of Speech Synthesizer");

      Console.WriteLine();
      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
    }
  }
}


Speech to Text

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            using (
SpeechRecognitionEngine recognizer =
  new SpeechRecognitionEngine(
    new System.Globalization.CultureInfo("en-US")))
            {

                // Create and load a dictation grammar.
                recognizer.LoadGrammar(new DictationGrammar());

                // Add a handler for the speech recognized event.
                recognizer.SpeechRecognized +=
                  new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

                // Configure input to the speech recognizer.
                recognizer.SetInputToDefaultAudioDevice();

                // Start asynchronous, continuous speech recognition.
                recognizer.RecognizeAsync(RecognizeMode.Multiple);

                // Keep the console window open.

                Console.ReadLine();

            }

        }
        static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Confidence > 3)
            {
                Console.WriteLine("Recognized text: " + e.Result.Text);
            }
        }

    }
}

You can also use the
recognizer.LoadGrammar(new Grammar(new GrammarBuilder("test")) Name = { "testGrammar" });
to create your own words


SpeechRecognizer vs. SpeechRecognitionEngine


In this article, I used the SpeechRecognitionEngine class. There's also a SpeechRecognizer class. So, what's the difference between the SpeechRecognizer class and the SpeechRecognitionEngine class? If you use the SpeechRecognizer class, you'll see the Windows Speech Recognizer:



If you use the SpeechRecognitionEngine class, you'll not see the Windows Speech Recognizer, the SpeechRecognitionEngine is the engine of a SpeechRecognizer. Also, the SpeechRecognizer class doesn't contain the methods SetInputToDefaultAudioDevice and RecognizeAsync.

About Author:
Steven Pinto is technology geek and loves to write on technology. He works in Systems Plus Pvt. Ltd. and actively contributes to technology. To more of interesting topics written by Steven, follow http://mad4teck.blogspot.in/

No comments:

Post a Comment