-5
如何在c#中创建语音识别程序,学习并记住先前所说的最后一个单词。我应该使用数据库来保存我说的每一个字。 下面的代码工作正常,但语法有限。 抱歉或我的英语不好。具有学习能力的语音识别
下面的代码:
public Form1()
{
InitializeComponent();
add();
remove();
}
[DllImport("user32")]
public static extern void LockWorkStation();
private SpeechRecognitionEngine recognizer;
public SpeechSynthesizer reader;
void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
recognize(e.Result);
}
void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
{
recognize(e.Result);
}
public void recognize(RecognitionResult e)
{
foreach(RecognizedPhrase r in e.Alternates)
{
if(r.Text=="system shutdown")
{
textBox1.Text = "Very Well";
Process.Start("shutdown", "/s/t");
}
else if(r.Text=="system restart")
{
textBox1.Text = "See You In Short";
Process.Start("restart", "/r/t 0");
}
else if (r.Text == "system open explorer")
{
textBox1.Text = "explorer open";
Process.Start("explorer.exe");
}
else if (r.Text == "system open my document")
{
textBox1.Text = "document open";
Process.Start("explorer.exe","/a");
}
else if (r.Text == "system open download")
{
textBox1.Text = "download open";
Process.Start("C:/Users/Donz/Downloads");
}
else if (r.Text == "system open my personal drive")
{
textBox1.Text = "your personal drive is open";
Process.Start("E:/");
}
else if (r.Text == "system what time now")
{
textBox1.Text ="time is" + DateTime.Now.ToString(" h mm ss tt");
}
else if (r.Text == "system date today")
{
textBox1.Text = "date is" + DateTime.Now.ToString(" dd mm yyyy");
}
//else if (r.Text == "system lock")
//{
// textBox1.Text = "Go find some one to do a work for you";
// //LockWorkStation();
//}
else if (r.Text == "system what is your name")
{
textBox1.Text = "i don't have a name, am just a piece of code compress of if and else";
}
else if (r.Text == "system what is this")
{
textBox1.Text = "let me see it";
}
else if (r.Text == "system very good")
{
textBox1.Text = "thank you";
}
else if (r.Text == "system good bye see you next time")
{
textBox1.Text = "Good bye i will see you soon";
}
else if (r.Text == "system close")
{
pictureBox1.Visible = true;
textBox1.Text = "System will close in.";
timer1.Start();
}
else
{
textBox1.Text = "i don't get you i will try to learn";
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Visible = false;
GrammarBuilder grammerbuilder = new GrammarBuilder();
grammerbuilder.Append(new Choices("system"));
grammerbuilder.Append(new Choices("shutdown", "restart", "lock", "close", "what is your name", "what is this","very good","good bye see you next time","open explorer","open my document","open download","open my computer","open my personal drive","what time now","date today"));
recognizer = new SpeechRecognitionEngine();
recognizer.RequestRecognizerUpdate();
recognizer.LoadGrammar(new Grammar(grammerbuilder));
//recognizer.LoadGrammar(new DictationGrammar());
recognizer.SpeechRecognized += recognizer_SpeechRecognized;
recognizer.SpeechRecognitionRejected += recognizer_SpeechRecognitionRejected;
recognizer.SetInputToDefaultAudioDevice();
recognizer.RecognizeAsync(RecognizeMode.Multiple);
reader = new SpeechSynthesizer();
textBox1.Text = "Greting, How may i help you";
}
void reader_sp(object sender, SpeakCompletedEventArgs e)
{
label1.Text = "IDLE";
}
private void text_check(object sender, EventArgs e)
{
reader = new SpeechSynthesizer();
reader.Dispose();
if (textBox1.Text != "")
{
reader = new SpeechSynthesizer();
reader.SpeakAsync(textBox1.Text);
label1.Text = "Speaking";
reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(reader_sp);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.Value = progressBar1.Value + 1;
if (progressBar1.Value == 40)
{
textBox1.Text = "4";
}
if (progressBar1.Value == 60)
{
textBox1.Text = "3";
}
if (progressBar1.Value == 80)
{
textBox1.Text = "2";
}
if (progressBar1.Value == 90)
{
textBox1.Text = "1";
}
if (progressBar1.Value == 98)
{
textBox1.Text = "1";
timer1.Stop();
Application.Exit();
}
}
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
static ManagementEventWatcher w = null;
public void remove()
{
WqlEventQuery q;
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;
try
{
q = new WqlEventQuery();
q.EventClassName = "__InstanceDeletionEvent";
q.WithinInterval = new TimeSpan(0, 0, 3);
q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
w = new ManagementEventWatcher(scope, q);
w.EventArrived += USBRemoved;
w.Start();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
if (w != null)
{
w.Stop();
}
}
}
public void add()
{
WqlEventQuery q;
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;
try
{
q = new WqlEventQuery();
q.EventClassName = "__InstanceCreationEvent";
q.WithinInterval = new TimeSpan(0, 0, 3);
q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
w = new ManagementEventWatcher(scope, q);
w.EventArrived += USBInsert;
w.Start();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
if (w != null)
{
w.Stop();
}
}
}
public void USBInsert(object sender, EventArgs e)
{
player.SoundLocation = "C:/Users/Donz/Desktop/Today/Voice Recognition/lockPc/lockPc/Resources/tone/insert.wav";
player.Play();
}
public void USBRemoved(object sender, EventArgs e)
{
player.SoundLocation = "C:/Users/Donz/Desktop/Today/Voice Recognition/lockPc/lockPc/Resources/tone/remove.wav";
player.Play();
}
}
示例代码将不胜感激 Donz 印度梅加拉亚邦
你是什么意思“学习”和“记住”?如果你只需要返回你说的最后一个单词,你不能只使用一个变量吗?如果“内存”需要在运行代码的时候持续存在,那么是的,你应该把最后一个单词存储在数据库中。 –
是虚构的,但这只能通过语法来完成,我的意思是,当我说我想说我的名字是donz时,所以当我再次运行时,它会记住这个单词“donz”。我知道代码还没有满。我尝试切换到DictationGrammar();但它没有认出。像这样: recognitionizer.LoadGrammar(new DictationGrammar()); – donbok