多级语音命令?

时间:2015-05-31 01:45:01

标签: c# speech-recognition

我正在尝试进行多级命令演讲,但我遇到了一些问题...

我希望拥有以下主要命令:" TV"," Light"," water"等

例如,当我说" TV"时,我希望为所需的操作设置子命令,例如:

当我说" TV":子命令 - > "音量增大","音量减小","关机","开机"

我只会识别电视的命令,直到我说"电视完成"。然后我回到主命令列表。

我该怎么做?

我的实际代码是:

 class Program
    {
        static Dictionary<string, string> listaCanais = new Dictionary<string, string>()
            {
                { "Fox News", "0 6 0" },
                { "The Weather Channel", "0 3 1"},
                { "Spike", "0 3 8"}
            };
        static void Main(string[] args)
        {
            using (var sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("pt-BR")))
            {
                sre.SetInputToDefaultAudioDevice();

                sre.LoadGrammarAsync(Comandos());

                sre.RequestRecognizerUpdate();
                sre.SpeechRecognitionRejected += sre_SpeechRecognitionRejected;
                sre.SpeechRecognized += sre_SpeechRecognized;

                sre.RecognizeAsync(RecognizeMode.Multiple);

                Console.ReadLine();
            }
        }

        public static Grammar Comandos()
        {
            Choices numerosTV = new Choices("zero", "um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove", "10", "11", "12");

            GrammarBuilder fraseNumeroTV = new GrammarBuilder(numerosTV);

            GrammarBuilder fraseMudarCanal = new GrammarBuilder("TV, canal");
            fraseMudarCanal.Append(numerosTV);
            //fraseMudarCanal.Append(numerosTV);
            //fraseMudarCanal.Append(numerosTV);

            Choices nomeCanal = new Choices();

                      foreach (string key in listaCanais.Keys)
            {
                nomeCanal.Add(key);
            }

            GrammarBuilder fraseNomeCanal = new GrammarBuilder("TV, canal");
            fraseNomeCanal.Append(nomeCanal);

            GrammarBuilder fraseMudo = new GrammarBuilder("TV, silencioso");
            GrammarBuilder fraseLigar = new GrammarBuilder("TV, ligar");
            GrammarBuilder fraseFecharApp = new GrammarBuilder("Controle, fechar aplicativo");
            GrammarBuilder frasePauseComandoVoz = new GrammarBuilder("Controle, pausar comando de voz");
            GrammarBuilder fraseIniciarComandoVoz = new GrammarBuilder("Controle, ativar comando de voz");

            Choices opcoesPrincipais = new Choices(new GrammarBuilder[] {fraseMudarCanal,
                                                                         fraseNomeCanal,
                                                                         fraseMudo,
                                                                         fraseLigar,
                                                                         fraseFecharApp,
                                                                         frasePauseComandoVoz,
                                                                         fraseIniciarComandoVoz});

            Grammar resultado = new Grammar((GrammarBuilder)opcoesPrincipais);
            return resultado;

        }

        static void sre_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            Console.WriteLine("Ignorado");

        }

        // Create a simple handler for the SpeechRecognized event.
        static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Console.WriteLine("Ouvi");

            if (e.Result == null)
                return;

            Console.WriteLine("[ " + e.Result.Confidence + " ]" + "Reconhecido: " + e.Result.Text);
        }
    }

提前致谢。

1 个答案:

答案 0 :(得分:0)

这些事情很容易在state machine framework内实施。您可以定义识别器的状态并根据它进行操作,主要工作在onRecognitionResult方法中完成:

states = {INPUT, TV, WATER};

inputGrammar = createInputGrammar();
tvGrammar = createTvGrammar();
waterGrammar = createWaterGrammar();

state = INPUT;

vodi onRecognitionResult() {

   if (state == INPUT) {
        if (result == "TV") {
            state = TV;
            recognizer.loadGrammar(tvGrammar);
        }
   }
   if (state == TV) {
        if (result == "Volume UP") {
            raiseVolume();
        }
        if (result == "TV done") {
            state = INPUT;
            recognizer.loadGrammar(inputGrammar);
        }
    }
    // Restart recognition
    recognizer.recognizeAsync();
}

您可以阅读Simple state machine example in C#?了解详情。