Kinect 语音识别 可以借鉴

using Microsoft.Kinect;
using Microsoft.Speech.AudioFormat;
using Microsoft.Speech.Recognition;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace KinectTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建KinectSensor对象,并获取对象赋值,启动设备
            KinectSensor KinectSensor = (from k in KinectSensor.KinectSensors
                                         where k.Status == KinectStatus.Connected
                                         select k).FirstOrDefault();
            if (KinectSensor == null)
            {
                Console.WriteLine("No Kinect Connected\n" + "Press any key to continue.\n");
                Console.ReadKey(true);
                return;
            }
            KinectSensor.Start();



            //audioSource保存获取到的音频流
            KinectAudioSource audioSource = KinectSensor.AudioSource;
            //不采用回拨消除和抑制
            audioSource.EchoCancellationMode = EchoCancellationMode.None;
            //KienctAudioDMO不执行自动增益控制
            audioSource.AutomaticGainControlEnabled = false;

            //在Speech中创建语音识别引擎
            RecognizerInfo recognizerInfo= GetKinectRecognizer();
            
            //指定语音命令,创建并加载一个包含要识别词语的语法器
            using (var speechRecognitionEngine = new SpeechRecognitionEngine(recognizerInfo.Id))
            {
                //Choices对象
                var colors = new Choices();
                colors.Add("red");
                colors.Add("green");
                colors.Add("blue");

                //构造语法,语义与识别器的语义匹配
                var grammatBuilder = new GrammarBuilder { Culture = recognizerInfo.Culture };
                grammatBuilder.Append(colors);
                var g = new Grammar(grammatBuilder);

                //把定义好的语法元素加载到语音识别引擎中
                speechRecognitionEngine.LoadGrammar(g);

                //识别成功
                speechRecognitionEngine.SpeechRecognized += SreSpeechRecognized;
                speechRecognitionEngine.SpeechHypothesized += SreSpeechHypothesized;
                //识别未成功
                speechRecognitionEngine.SpeechRecognitionRejected += SreSpeechRecognitionRejected;

                /*
                 * 识别命令:
                 * 在配置成功后,Speech将启动处理流程,引擎会自动识别出语法中的单词,
                 * 并根据识别的情况触发相应的事件
                 * 
                 * */
                using (Stream s = audioSource.Start())
                {
                    //指定音频源集齐特征
                    speechRecognitionEngine.SetInputToAudioStream(
                        s, new SpeechAudioFormatInfo(EncodingFormat.Pcm,
                        16000, 16, 1, 32000, 2, null));

                    Console.WriteLine(
                        "Recognizing speech. Say: 'red', 'green' or 'blue'. Press ENTER to stop");

                    //RecognizeAsync:指定异步识别,语音识别引擎会一直在后台运行,直至用户通过按键终止该进程
                    speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
                    Console.ReadLine();
                    Console.WriteLine("Stopping recognizer ...");
                    //RecognizeAsyncStop:停止处理并关闭引擎
                    speechRecognitionEngine.RecognizeAsyncStop();
                }
            }
        }


        private static RecognizerInfo GetKinectRecognizer()
        {
            
            /*
             * SpeechRecognitionEngine提供一系列获取和管理语音识别引擎的方法
             * InstalledRecognizers静态方法,返回一个语音识别器列表,包括当前系统上安装的所有语音识别器         
             * RecognizerInfo类:表示语音识别引擎对象的相关信息:Name,Id,Culture          
             * */
            foreach (RecognizerInfo recognizer in SpeechRecognitionEngine.InstalledRecognizers())
            {
                string value;
                recognizer.AdditionalInfo.TryGetValue("Kinect", out value);
                if ("True".Equals(value, StringComparison.OrdinalIgnoreCase) && "en-US".Equals(recognizer.Culture.Name, StringComparison.OrdinalIgnoreCase))
                {
                    return recognizer;
                }
            }
            return null;                   
        }

        private static void SreSpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            Console.WriteLine("\nSpeech Rejected");
            if (e.Result != null)
            {
                DumpRecordedAudio(e.Result.Audio);
            }
        }


        private static void SreSpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
        {
            Console.Write("\rSpeech Hypothesized: \t{0}", e.Result.Text);
        }


        private static void SreSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Confidence >= 0.7)
            {
                Console.WriteLine("\nSpeech Recognized: \t{0}\tConfidence:\t{1}",
                    e.Result.Text, e.Result.Confidence);
            }
            else
            {
                Console.WriteLine("\nSpeech Recognized but confidence was too low: \t{0}",
                    e.Result.Confidence);
                DumpRecordedAudio(e.Result.Audio);
            }
        }

        
        private static void DumpRecordedAudio(RecognizedAudio audio)
        {
            //将记录的单词写入wav文件
            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);
        }
    }
}


你可能感兴趣的:(Kinect)