locked
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()
            {
                InitializeComponent();


                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
                    //Console.ReadKey(true);
                    return;
                }
                sensor.Start();

                // 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.";
                    return;
                }


                using (var sre = new SpeechRecognitionEngine(ri.Id))
                {
                    var colors = new Choices();
                    colors.Add("red");
                    colors.Add("green");
                    colors.Add("blue");

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

                    // Specify the culture to match the recognizer in case we are running in a different culture.                                
                    gb.Append(colors);

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

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

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

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

                        sre.RecognizeAsync(RecognizeMode.Multiple);
                        MessageBox.Show("speech running?");
                        sre.RecognizeAsyncStop();
                    }
                }

                sensor.Stop();

            }

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


            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)
                {
                    DumpRecordedAudio(e.Result.Audio);
                }
            }

            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)
                {
                    return;
                }

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

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

    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.

      sre.RecognizeAsync(RecognizeMode.Multiple);
                        MessageBox.Show("speech running?");
                        sre.RecognizeAsyncStop();
    sensor.stop();

    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.

    Thanks,

    Bharat.


    • 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