Speech Recognition with free speech

淺唱寂寞╮ 提交于 2020-01-16 03:30:06

问题


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

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

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

Please help.


回答1:


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

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

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");
    }
//...


来源:https://stackoverflow.com/questions/22510450/speech-recognition-with-free-speech

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!