as199581tg
丶小柒灬
采纳率50%
2015-07-31 07:07 阅读 4.6k
已采纳

怎样利用Kinect的麦克风序列进行中文语音识别,只需要识别几个中文单词,英文识别已经做出来了。

5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Kinect;
using Microsoft.Speech.AudioFormat;
using Microsoft.Speech.Recognition;

namespace KinectSpeech
{
class Program
{
static void Main(string[] args)
{
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();

        KinectAudioSource audioSource = KinectSensor.AudioSource;
        audioSource.EchoCancellationMode = EchoCancellationMode.None;
        audioSource.AutomaticGainControlEnabled = false;

        RecognizerInfo recognizerInfo = GetKinectRecognizer();
        using (var speechRecognitionEngine = new SpeechRecognitionEngine(recognizerInfo.Id))
        {
            var colors = new Choices();
            colors.Add("help");
            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;

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

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

                speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
                Console.ReadLine();
                Console.WriteLine("Stopping recognizer ...");
                speechRecognitionEngine.RecognizeAsyncStop();
            }
        }
    }


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

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    devmiao devmiao 2015-07-31 14:16

    目前不支持中文,用发音类似的英文看看

    点赞 3 评论 复制链接分享
  • SteelTeng SteelTeng 2016-03-11 14:32

    安装中文语音包就可以实现,我在Kinect1.0上用过没问题的, V2的还没时间试

    点赞 评论 复制链接分享

相关推荐