C# Kinect语音识别无法工作

C# Kinect语音识别无法工作,c#,sdk,speech-recognition,kinect,C#,Sdk,Speech Recognition,Kinect,我有一个程序,当用户说“开始”或“停止”时,程序会在屏幕上显示一个框架。我使用与相同的代码,它在那里运行良好,但在我的代码中不起作用。我不知道哪部分代码不起作用,因为这是我第一次进行语音识别编程。感谢您的帮助(如果我的代码很乱,很抱歉)识别演讲稿 看起来您从未从SreSpeechRecognized()调用过RecognizeSaidSomething()。创建事件参数: var said=new SaidSomethingEventArgs{动词=0,短语= e、 结果.文本} 但你似乎没有用

我有一个程序,当用户说
“开始”
“停止”
时,程序会在屏幕上显示一个框架。我使用与相同的代码,它在那里运行良好,但在我的代码中不起作用。我不知道哪部分代码不起作用,因为这是我第一次进行语音识别编程。感谢您的帮助(如果我的代码很乱,很抱歉)
识别演讲稿


看起来您从未从
SreSpeechRecognized()
调用过
RecognizeSaidSomething()
。创建事件参数:

var said=new SaidSomethingEventArgs{动词=0,短语= e、 结果.文本}

但你似乎没有用它做任何事情

下面的
foreach
循环似乎没有任何作用,您测试短语,然后就可以跳出循环。在我看到的循环中,你不需要设置任何变量或调用任何函数

然后是一个
for
循环,它似乎做了一些类似于
foreach
循环的事情(只是以不同的方式)。它搜索与已识别短语的匹配项,但对所找到的内容不做任何处理。它刚刚回来


我认为在
SreSpeechRecognized()
事件处理程序中的某个地方,您希望调用
RecognizeSaidSomething()
并将
SaidSomethingEventArgs
传递给它,您应该隔离不起作用的代码,并且比“不起作用”更具体我不是调试器。@EricMickelsen问题是我不知道问题出在哪里,因为这是我第一次用KinectAll进行演讲。必须调用RecognizeSaidSomething()。对吗?这就是您希望执行的代码。它叫什么名字?它是作为函数调用的吗?您是否将其设置为eventhandler或将其作为委托传递到某个位置?我在代码中没有看到任何调用此方法的内容。@MichaelLevy啊,我明白了,如果我调用
InitializeComponent()旁边的方法,它会工作吗
还是只在加载窗口而不在其他地方调用它?您是否了解RecognizeAsync()方法如何向已注册的事件处理程序触发事件,以及在识别完成时如何调用SreSpeechRecognized()?处理SetLabel事件时,您有什么安排?我不熟悉Kinect开发,但我觉得这并不完整。在最后3段中,你在谈论什么?在我的代码中或在
SpeechRecognizer
(类)中,我重写了我的代码,包含了您建议的内容,但仍然不起作用。检查编辑
public class SpeechRecognizer : IDisposable
{
    private KinectAudioSource kinectAudioSource;
    private struct WhatSaid
    {
        public Verbs Verb;
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.Stop();

            if (this.sre != null)
            {
                // NOTE: The SpeechRecognitionEngine can take a long time to dispose
                // so we will dispose it on a background thread
                ThreadPool.QueueUserWorkItem(
                    delegate(object state)
                    {
                        IDisposable toDispose = state as IDisposable;
                        if (toDispose != null)
                        {
                            toDispose.Dispose();
                        }
                    },
                        this.sre);
                this.sre = null;
            }

            this.isDisposed = true;
        }
    }

    public void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }

    public EchoCancellationMode EchoCancellationMode
    {
        get
        {
            this.CheckDisposed();

            return this.kinectAudioSource.EchoCancellationMode;
        }

        set
        {
            this.CheckDisposed();

            this.kinectAudioSource.EchoCancellationMode = value;
        }
    }

    public static SpeechRecognizer Create()
    {
        SpeechRecognizer recognizer = null;

        try
        {
            recognizer = new SpeechRecognizer();
        }
        catch (Exception)
        {
            // speech prereq isn't installed. a null recognizer will be handled properly by the app.
        }

        return recognizer;
    }

    private void CheckDisposed()
    {
        if (this.isDisposed)
        {
            throw new ObjectDisposedException("SpeechRecognizer");
        }
    }

    public void Stop()
    {
        this.CheckDisposed();

        if (this.sre != null)
        {
            this.kinectAudioSource.Stop();
            this.sre.RecognizeAsyncCancel();
            this.sre.RecognizeAsyncStop();

            this.sre.SpeechRecognized -= this.SreSpeechRecognized;
            this.sre.SpeechHypothesized -= this.SreSpeechHypothesized;
            this.sre.SpeechRecognitionRejected -= this.SreSpeechRecognitionRejected;
        }
    }

    public void Start(KinectAudioSource kinectSource)
    {
        this.CheckDisposed();

        this.kinectAudioSource = kinectSource;
        this.kinectAudioSource.AutomaticGainControlEnabled = false;
        this.kinectAudioSource.BeamAngleMode = BeamAngleMode.Adaptive;
        var kinectStream = this.kinectAudioSource.Start();
        this.sre.SetInputToAudioStream(
            kinectStream, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
        this.sre.RecognizeAsync(RecognizeMode.Multiple);
    }

    public enum Verbs
    {
        None = 0,
        Start,
        Stop,
        Resume,
        Pause
    }
    private bool isDisposed;
    private readonly Dictionary<string, WhatSaid> speechCommands = new Dictionary<string, WhatSaid>
    {
        { "Start", new WhatSaid { Verb = Verbs.Start } },
        { "Stop", new WhatSaid { Verb = Verbs.Stop } },
        { "Resume", new WhatSaid { Verb = Verbs.Resume } },
        { "Pause", new WhatSaid { Verb = Verbs.Pause } },
    };
    private SpeechRecognitionEngine sre;

    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 SpeechRecognizer()
    {
        RecognizerInfo ri = GetKinectRecognizer();
        this.sre = new SpeechRecognitionEngine(ri);
        this.LoadGrammar(this.sre);
    }

    private void LoadGrammar(SpeechRecognitionEngine speechRecognitionEngine)
    {
        // Build a simple grammar of shapes, colors, and some simple program control
        var single = new Choices();
        foreach (var phrase in this.speechCommands)
        {
            single.Add(phrase.Key);
        }

        var objectChoices = new Choices();
        objectChoices.Add(single);

        var actionGrammar = new GrammarBuilder();
        actionGrammar.AppendWildcard();
        actionGrammar.Append(objectChoices);

        var allChoices = new Choices();
        allChoices.Add(actionGrammar);
        allChoices.Add(single);

        // This is needed to ensure that it will work on machines with any culture, not just en-us.
        var gb = new GrammarBuilder { Culture = speechRecognitionEngine.RecognizerInfo.Culture };
        gb.Append(allChoices);

        var g = new Grammar(gb);
        speechRecognitionEngine.LoadGrammar(g);
        speechRecognitionEngine.SpeechRecognized += this.SreSpeechRecognized;
        speechRecognitionEngine.SpeechHypothesized += this.SreSpeechHypothesized;
        speechRecognitionEngine.SpeechRecognitionRejected += this.SreSpeechRecognitionRejected;
    }

    private void SreSpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
    {
        var said = new SaidSomethingEventArgs { Verb = Verbs.None, Matched = "?" };

        this.SetLabel("Word not Recognized.... Try 'Start', 'Stop', 'Pause' or 'Resume'");

        if (this.SaidSomething != null)
        {
            this.SaidSomething(new object(), said);
        }
    }

    private void SreSpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
    {
        this.SetLabel("I think you said: " + e.Result.Text);
    }

    public event EventHandler<SaidSomethingEventArgs> SaidSomething;

    private void SreSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        this.SetLabel("\rSpeech Recognized: \t" + e.Result.Text);

        if ((this.SaidSomething == null) || (e.Result.Confidence < 0.3))
        {
            return;
        }

        var said = new SaidSomethingEventArgs { Verb = 0, Phrase = e.Result.Text };

        foreach (var phrase in this.speechCommands)
        {
            if (e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Pause))
            {
                //pause = true;
                break;
            }

            else if ((e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Resume)))
            {
                //resume = true;
                break;
            }

            else if ((e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Start)))
            {
                //start = true;
                break;
            }

            else if ((e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Stop)))
            {
                //stop = true;
                break;
            }
        }

        // Look for a match in the order of the lists below, first match wins.
        List<Dictionary<string, WhatSaid>> allDicts = new List<Dictionary<string, WhatSaid>> { this.speechCommands };

        bool found = false;
        for (int i = 0; i < allDicts.Count && !found; ++i)
        {
            foreach (var phrase in allDicts[i])
            {
                if (e.Result.Text.Contains(phrase.Key))
                {
                    found = true;
                    break;
                }
            }
        }

        if (!found)
        {
            return;
        }
    }

    public class SaidSomethingEventArgs : EventArgs
    {
        public Verbs Verb { get; set; }

        public string Phrase { get; set; }

        public string Matched { get; set; }
    }

    public event Action<string> SetLabel = delegate { };
}
private void RecognizeSaidSomething(object sender, SpeechRecognizer.SpeechRecognizer.SaidSomethingEventArgs e)
    {
        FlyingText.FlyingText.NewFlyingText(this.skeleton.Width / 30, new Point(this.skeleton.Width / 2, this.skeleton.Height / 2), e.Matched);
        switch (e.Verb)
        {
            case SpeechRecognizer.SpeechRecognizer.Verbs.Pause:
                pause = true;
                break;
            case SpeechRecognizer.SpeechRecognizer.Verbs.Resume:
                resume = true;
                break;
            case SpeechRecognizer.SpeechRecognizer.Verbs.Start:
                start = true;
                break;
            case SpeechRecognizer.SpeechRecognizer.Verbs.Stop:
                stop = true;
                break;
        }
    }