问题
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