Wpf中的Kinect语音识别 [英] Kinect Speech Recognition in wpf

查看:83
本文介绍了Wpf中的Kinect语音识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么它不起作用

I don't know why it doesn't work

plz任何人帮助我T_T

plz anybody help me T_T

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Windows;

使用System.Windows.Controls;

使用System.Windows .Data;

使用System.Windows.Documents;

使用System.Windows.Input;

使用System.Windows.Media;

使用System.Windows.Media.Imaging;

使用System.Windows.Navigation;

使用System.Windows.Shapes;

使用Microsoft.Kinect;

使用Microsoft.Speech.Recognition;

使用System.IO;

使用Microsoft.Speech.AudioFormat ;

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;

命名空间wpqkf

{

    ///< summary>

    /// MainWindow.xaml的交互逻辑

    ///< / summary>

    public partial class MainWindow:Window

    {

        public MainWindow()

        {

            InitializeComponent();

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



            KinectSensor传感器=(从sensorToCheck在KinectSensor.KinectSensors其中sensorToCheck.Status == KinectStatus.Connected选择sensorToCheck).FirstOrDefault();

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP; if(sensor == null)

            {

                textBox1.Text ="没有Kinect传感器连接到这台计算机,或者没有任何一个传感器连接到这台计算机上。 +"attach are \"Connected \""。\ n" +

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; "附加KinectSensor并重新启动此应用程序。\ n" +

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; "如果这不起作用,请运行SkeletonViewer-WPF以更好地理解\ n的状态"。 +

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; "Kinect sensors.\\\
.n" +

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; "按任何键continue.\\\
英寸;


            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";

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP; //允许用户在解除控制台输出之前查看控制台输出

              ;&NBSP; //Console.ReadKey(true);

               返回;

            }
            sensor.Start();

                // Give a chance for user to see console output before it is dismissed
                //Console.ReadKey(true);
                return;
            }
            sensor.Start();

            //获取KinectAudioSource进行音频捕获

            KinectAudioSource source = sensor.AudioSource;

            source.EchoCancellationMode = EchoCancellationMode.None; //此样本没有AEC

            source.AutomaticGainControlEnabled = false; //重要的是关闭语音识别

            // 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();

            RecognizerInfo ri = GetKinectRecognizer();



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; if(ri == null)

            {

                textBox1.Text =&"无法找到Kinect语音识别器。请参阅样本要求。";

               返回;

            }


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



           使用(var sre = new SpeechRecognitionEngine(ri.Id))

            {

                var colors = new Choices();

                colors.Add(" red");&
                colors.Add(" green");&
                colors.Add(QUOT;蓝色");


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

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; VAR GB =新GrammarBuilder {文化= ri.Culture};

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

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP; //指定培养以匹配的情况下,识别器,我们在不同的培养物运行&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;


                gb.Append(颜色);

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

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //创建实际的语法实例,然后将其加载到语音识别器中。

              ;&NBSP;&NBSP; VAR克=新语法(GB);

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

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; sre.LoadGrammar(g);

                sre.SpeechRecognized + =新的EventHandler< SpeechRecognizedEventArgs>(sre_SpeechRecognized);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; sre.SpeechHypothesized + = SreSpeechHypothesized;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; sre.SpeechRecognitionRejected + = SreSpeechRecognitionRejected;

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

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;使用(Stream s = source.Start())

                {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; sre.SetInputToAudioStream(

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; S,新SpeechAudioFormatInfo(EncodingFormat.Pcm,16000,16,1,32000,2,NULL));

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

&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; textBox1.Text ="识别语音。说:'红色','绿色'或'蓝色'。按ENTER键停止英寸;

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

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; sre.RecognizeAsync(RecognizeMode.Multiple);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; MessageBox.Show(QUOT;语音运行"?);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP; sre.RecognizeAsyncStop();

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
            }

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

            sensor.Stop();

            sensor.Stop();

        }

        }

        void sre_SpeechRecognized(object sender,SpeechRecognizedEventArgs e)

        {

            if(e.Result.Confidence> = 0.3)

            {

                MessageBox.Show(QUOT; \\\
Speech识别:{0}" ;, e.Result.Text);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP; }
           否则为
            {

                DumpRecordedAudio(e.Result.Audio);

            }
        }

        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 =>

            {

               字符串值;

                r.AdditionalInfo.TryGetValue(QUOT;超高动力学" ;,超时值);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; return" True" .Equals(value,StringComparison.InvariantCultureIgnoreCase)&& "的en-US" .Equals(r.Culture.Name,StringComparison.InvariantCultureIgnoreCase);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }; b $ b            return SpeechRecognitionEngine.InstalledRecognizers()。Where(matchingFunc).FirstOrDefault();

        }


        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 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 SreSpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
        {
            MessageBox.Show("Speech Hypothesized: \t{0}", e.Result.Text);
        }

        private static void DumpRecordedAudio(RecognizedAudio audio)

        {

            if(audio == null)

            {

               返回;

            }

        private static void DumpRecordedAudio(RecognizedAudio audio)
        {
            if (audio == null)
            {
                return;
            }

            int fileId = 0;

            string filename;

           而(File.Exists((文件名= QUOT; RetainedAudio_" + FILEID + QUOT;的.wav")))

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP; {

                fileId ++;

            }

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

            //Console.WriteLine("\\\
Writing文件:{0}" ;,文件名);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;使用(var文件=新的FileStream(文件名,System.IO.FileMode.CreateNew))

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

                audio.WriteToWaveStream(file);

            }
        }
    }
}

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

推荐答案

我尝试编程语音并且也不起作用......
I try to programm speech and doesn't work too...


这篇关于Wpf中的Kinect语音识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆