语音识别和自由语音 [英] Speech Recognition with free speech

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

问题描述

我需要帮助.我想制作一个能够识别我在说什么并做我所说的事情的应用程序.例如:

I need help. I want to make an application that will recognize what I am saying and do stuff that I say. For example:

如果我说open [notepad],其中[notepad]可以是任何应用程序名称,则需要打开记事本.

If I say open [notepad], where [notepad] can be any application name, it needs to open notepad.

我认为我需要同时使用 语法 DictationGrammar ,但是我不知道如何使用.请帮我.谢谢.

I think I need to use both Grammar and DictationGrammar, but I don't know how. Please help me. Thanks.

我的代码现在看起来像这样:

My code now looks like this:

    string WelcomeSentence = "Hello sir, how are you today";
    SpeechSynthesizer sSynth = new SpeechSynthesizer();
    PromptBuilder pBuilder = new PromptBuilder();
    SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine();

    private void frmMain_Load(object sender, EventArgs e)
    {
        sSynth.SelectVoice("IVONA Amy");
        sSynth.SetOutputToDefaultAudioDevice();
        pBuilder.ClearContent();
        pBuilder.AppendText(WelcomeSentence);
        sSynth.Speak(pBuilder);

        Choices sList = new Choices();
        sList.Add(File.ReadAllLines(@"Commands.ekd"));
        Grammar gr = new Grammar(new GrammarBuilder(sList));
        DictationGrammar dgr = new DictationGrammar();
        try
        {
            sRecognize.RequestRecognizerUpdate();
            sRecognize.LoadGrammar(gr);
            sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
            sRecognize.SetInputToDefaultAudioDevice();
            sRecognize.RecognizeAsync(RecognizeMode.Multiple);
            sRecognize.Recognize(); 
        }
        catch { return; }
    }
    private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        if (e.Result.Text == "open notepad")
        {
            System.Diagnostics.Process.Start(@"C:\Windows\System32\Notepad.exe");
        }
        else
        {
            pBuilder.ClearContent();
            pBuilder.AppendText(e.Result.Text);
            sSynth.Speak(pBuilder);
        }
    }

请帮助.

推荐答案

下面附有答案我几个月前发布的信息,我提供了这个建议.

Following along with an answer I posted several months ago, I offer this suggestion.

意识到我将省略SpeechFactory类和许多MySpeechMethods类,请从其他答案中复制它.另外,如另一个答案中所述,您将必须执行一些错误处理.有了这一警告,您将以这种方式修改自己的代码.

Realize that I'm leaving out the SpeechFactory class and much of the MySpeechMethods class, please copy it from the other answer. Also, as noted in the other answer, you'll have to do some error handling. With that caveat, you would modify your own code this way.

private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    var methods = new MySpeechMethods();
    MethodInfo myMethod;
    myMethod = SpeechFactory.GetSpeechMethod(e.Result.Text);

    if(myMethod != null) return;        
    pBuilder.ClearContent();
    pBuilder.AppendText(e.Result.Text);
    sSynth.Speak(pBuilder);
}

然后在MySpeechMethods中,您将拥有命令.

Then in the MySpeechMethods you would have your commands.

public class MySpeechMethods
{
    [Speech("Open Notepad")]
    public void OpenNotepad()
    {
       System.Diagnostics.Process.Start(@"C:\Windows\System32\Notepad.exe");
    }
//...

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

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