C#中的语音识别 [英] Speech recognition in c#

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

问题描述

我正在编写一个c#代码来控制具有语音识别功能的鼠标.代码显示波纹管.

I am writing a c# code to control mouse with speech recognition. The code is showing bellow.

using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using System.Speech.Recognition;

namespace my
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine recognitionEngine;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            recognitionEngine = new SpeechRecognitionEngine();
            recognitionEngine.SetInputToDefaultAudioDevice();
            recognitionEngine.SpeechRecognized += (s, args) =>
            {
                string line1 = "";
                foreach (RecognizedWordUnit word in args.Result.Words)
                {
                    if (word.Confidence > 0.5f)
                        line1 += word.Text + " ";
                }

                string command1 = line1.Trim();
                System.Console.WriteLine(command1);
                if (command1 == "start")
                {
                    System.Console.WriteLine("!!!!TEST-1!!!!");

                    recognitionEngine = new SpeechRecognitionEngine();
                    recognitionEngine.SetInputToDefaultAudioDevice();
                    recognitionEngine.SpeechRecognized += (c, arg) =>
                    {
                        string line = "";
                        foreach (RecognizedWordUnit word in args.Result.Words)
                        {
                            if (word.Confidence > 0.5f)
                                line += word.Text + " ";
                        }

                        string command = line.Trim();
                        System.Console.WriteLine(command);
                        System.Console.WriteLine("!!!!TEST-2!!!!");
                        switch (command)
                        {
                            case "left":
                                MoveMouse(Cursor.Position.X - 50, Cursor.Position.Y);
                                break;
                            case "right":
                                MoveMouse(Cursor.Position.X + 50, Cursor.Position.Y);
                                break;
                            case "up":
                                MoveMouse(Cursor.Position.X, Cursor.Position.Y - 50);
                                break;
                            case "down":
                                MoveMouse(Cursor.Position.X, Cursor.Position.Y + 50);
                                break;
                        }

                        txtOutput.Text += line;
                        txtOutput.Text += Environment.NewLine;
                    };

                    recognitionEngine.UnloadAllGrammars();
                    recognitionEngine.LoadGrammar(CreateGrammars());
                    recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

                }
                else
                {
                    System.Console.WriteLine("!!!!TEST-3!!!!");
                }

               
                
                txtOutput.Text += line1;
                txtOutput.Text += Environment.NewLine;
            };

            recognitionEngine.UnloadAllGrammars();
            recognitionEngine.LoadGrammar(CreateGrammars());
            recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        }
        

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            recognitionEngine.RecognizeAsyncStop();
        }

       
        private Grammar CreateGrammars()
        {
            Choices commandChoices = new Choices("left", "right", "up", "down","start");
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            grammarBuilder.Append(commandChoices);
            return new Grammar(grammarBuilder);
        }

        private void MoveMouse(int x, int y)
        {
            this.Cursor = new Cursor(Cursor.Current.Handle);
            Cursor.Position = new Point(x, y);
            Cursor.Clip = new Rectangle(this.Location, this.Size);
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


当用户运行代码时,用户可以说命令上",下",左",右"和鼠标指针将分别以50为增量跳跃.但是首先我想当用户说开始"时运行当用户说上",下",左",右"命令和鼠标指针时,程序和秒将分别以50为增量跳跃.当用户说停止"时,则停止程序.在这里,我说的是"start"字符串command1 get就是"start",它进入System.Console.WriteLine("!!!! TEST-1 !!!!");此命令.但是第二,当我说"up"字符串命令时,将其作为"start"而不是"up".因此,请不要转到switch语句.如果您可以运行此代码,您就可以理解问题.自两个星期以来,我一直试图解决此问题.但是我仍然无法解决.如果您可以解决此问题,对我会大有帮助.因为我要完成将近1个月的项目.谢谢u


When user run the code, user can say the commands "up", "down", "left", "right" and the mouse pointer will jump respectively in increments of 50. But first i want when user say "start" run the program and second when user say the commands "up", "down", "left", "right" and the mouse pointer will jump respectively in increments of 50. When user say "stop" then stop the program. In here I said as "start" string command1 get is as "start" and it goes into System.Console.WriteLine("!!!!TEST-1!!!!"); this command. But secondly when i said as "up" string command get it as "start" not "up".So never go to the switch statement. If u can run this code u can understand the problem.I tried to solve this problem since a two week.But i still couldn''t slove.If u can solve this problem it will be big help to me. Because i want to finish this project nearly 1 month .Thank u

推荐答案



我想了解您的问题是什么,我还没有在我的环境中尝试过.
但是,每当一个单词被识别时,就会调用RecognitionEngine.SpeechRecognized事件,因此在您的情况下,您总是必须说开始",然后说上".但是我认为这行不通,因为您的开关位于if-"start"语句中.
因此,您需要的是一个bool变量,例如,如果识别出开始"(true),它将可以处理您的切换,否则bool仍然为false.

希望对您有所帮助!

此致
Hi,

I''m trying to understand what''s your problem is, I didn''t tried it in my enviroment yet.
But recognitionEngine.SpeechRecognized event is called everytime a word is recognized, so in your case you would always have to say "start" and then "up" or something. But I think this can''t work because your switch is IN the if-"start" statement.
So what you need is something like a bool variable, like if "start" was recognized (true), it can handle your switch, otherwise bool is still false.

Hope that helps!

Best Regards


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

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