Kinect Speech Recognition in wpf RRS feed

  • Question

  • I don't know why it doesn't work

    plz anybody help me T_T

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Kinect;
    using Microsoft.Speech.Recognition;
    using System.IO;
    using Microsoft.Speech.AudioFormat;

    namespace wpqkf
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
            public MainWindow()

                KinectSensor sensor = (from sensorToCheck in KinectSensor.KinectSensors where sensorToCheck.Status == KinectStatus.Connected select sensorToCheck).FirstOrDefault();
                if (sensor == null)
                    textBox1.Text ="No Kinect sensors are attached to this computer or none of the ones that are\n" +"attached are \"Connected\".\n" +
                            "Attach the KinectSensor and restart this application.\n" +
                            "If that doesn't work run SkeletonViewer-WPF to better understand the Status of\n" +
                            "the Kinect sensors.\n\n" +
                            "Press any key to continue.\n";

                    // Give a chance for user to see console output before it is dismissed

                // Obtain the KinectAudioSource to do audio capture
                KinectAudioSource source = sensor.AudioSource;
                source.EchoCancellationMode = EchoCancellationMode.None; // No AEC for this sample
                source.AutomaticGainControlEnabled = false; // Important to turn this off for speech recognition

                RecognizerInfo ri = GetKinectRecognizer();

                if (ri == null)
                    textBox1.Text="Could not find Kinect speech recognizer. Please refer to the sample requirements.";

                using (var sre = new SpeechRecognitionEngine(ri.Id))
                    var colors = new Choices();

                    var gb = new GrammarBuilder { Culture = ri.Culture };

                    // Specify the culture to match the recognizer in case we are running in a different culture.                                

                    // Create the actual Grammar instance, and then load it into the speech recognizer.
                    var g = new Grammar(gb);

                    sre.SpeechRecognized+=new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
                    sre.SpeechHypothesized += SreSpeechHypothesized;
                    sre.SpeechRecognitionRejected += SreSpeechRecognitionRejected;

                    using (Stream s = source.Start())
                            s, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));

                        textBox1.Text = "Recognizing speech. Say: 'red', 'green' or 'blue'. Press ENTER to stop";

                        MessageBox.Show("speech running?");



            void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
                if (e.Result.Confidence >= 0.3)
                    MessageBox.Show("\nSpeech Recognized: {0}", e.Result.Text);

            private static RecognizerInfo GetKinectRecognizer()
                Func<RecognizerInfo, bool> matchingFunc = r =>
                    string value;
                    r.AdditionalInfo.TryGetValue("Kinect", out value);
                    return "True".Equals(value, StringComparison.InvariantCultureIgnoreCase) && "en-US".Equals(r.Culture.Name, StringComparison.InvariantCultureIgnoreCase);
                return SpeechRecognitionEngine.InstalledRecognizers().Where(matchingFunc).FirstOrDefault();

            private static void SreSpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
                MessageBox.Show("Speech Rejected");
                if (e.Result != null)

            private static void SreSpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
                MessageBox.Show("Speech Hypothesized: \t{0}", e.Result.Text);

            private static void DumpRecordedAudio(RecognizedAudio audio)
                if (audio == null)

                int fileId = 0;
                string filename;
                while (File.Exists((filename = "RetainedAudio_" + fileId + ".wav")))

                //Console.WriteLine("\nWriting file: {0}", filename);
                using (var file = new FileStream(filename, System.IO.FileMode.CreateNew))

    Saturday, June 30, 2012 11:19 AM

All replies

  • I try to programm speech and doesn't work too...
    Wednesday, July 4, 2012 9:30 AM
  •  I use Kinect for windows and it was perfect with me in 1.5 and 1.6 SDK

    try to remove speechRecognitionEngine initialization from the using block and declare it at class scope

    • Proposed as answer by Noha Bakry Friday, December 7, 2012 8:30 PM
    Friday, December 7, 2012 8:27 PM
  • Hi,

    I believe you are stopping the Speech Engine in the next line immediately after starting it.

                        MessageBox.Show("speech running?");

    Please place the sre.RecognizeAsyncStop(); & sensor.stop(); methods in Window Close method and check if it works. sre.RecognizeAsyncStop(); method stops the speech recognition process.



    • Edited by ykbharat Monday, December 17, 2012 4:57 AM modified
    • Proposed as answer by ykbharat Monday, December 17, 2012 4:57 AM
    Friday, December 14, 2012 12:32 PM
  • You are missing , i have written a clean way of doing this, also with a Grammar File that will allow you to specify your rules in an Xml file  

    How to do voice recgnition

    and also you must consider this book which has a lot of examples and info 

    Kinect for Windows SDK Programming Guide

    Vuyiswa Maseko

    Wednesday, January 16, 2013 4:45 PM