语音识别程序不捕获语音/命令

时间:2018-01-08 22:47:36

标签: c# system.speech.recognition

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Recognition;

namespace ConsoleApp2
{
  class Program
  {
     static SpeechRecognitionEngine recEngine = new 
     SpeechRecognitionEngine();
     bool keyHold = false;

     static void Main(string[] args)
     {
        Choices commands = new Choices();
        commands.Add(new string[] { "Current dollar value", "Current euro value" });
        GrammarBuilder gBuilder = new GrammarBuilder();
        gBuilder.Append(commands);
        Grammar grammar = new Grammar(gBuilder);

        recEngine.LoadGrammarAsync(grammar);
        recEngine.SetInputToDefaultAudioDevice();
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
     }

     void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
     {
         switch (e.Result.Text)
         {
            case "Current dollar value":
                Console.WriteLine("10kr");
                break;

            case "Current euro value":
                Console.WriteLine();
                break;
        }
     }
  }
}

该程序刚刚启动时关闭,并且没有录制我的声音,我尝试在代码console.readkey();中的recEngine.RecognizeAsync(RecognizeMode.Multiple);下放置此行。它保持程序并且没有自动关闭,但程序仍然不记录我的传入语音命令 这是为什么?

1 个答案:

答案 0 :(得分:1)

语音识别是事件驱动的 您需要定义必要的事件处理程序 必须订阅SpeechRecognitionEngine.SpeechRecognized Even才能收到认可结果 另见SpeechRecognitionEngine.AudioStateChanged Event

此外,在开始识别之前,必须使用新设置更新引擎:
SpeechRecognitionEngine.RequestRecognizerUpdate Method

  

注意:
我只使用" 美元"和" 欧元"作为语法项目的提示:   尝试在你的语法中使用可能较少的关键词,否则他们   变得非常难以处理。您可以发出整个"当前美元价值"短语,然而,公认的关键词是"美元"。   

using System.Speech.Recognition;

  static void Main(string[] args)
  {
    using (SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine())
    {
       recEngine.SpeechRecognized += 
             new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);
       recEngine.AudioStateChanged += 
             new EventHandler<AudioStateChangedEventArgs>(recEngine_AudioStateChange);

       Choices commands = new Choices();
       commands.Add(new string[] { "dollar", "euro" });
       GrammarBuilder gBuilder = new GrammarBuilder();
       gBuilder.Append(commands);
       Grammar grammar = new Grammar(gBuilder);

       recEngine.SetInputToDefaultAudioDevice();
       recEngine.LoadGrammarAsync(grammar);
       recEngine.RequestRecognizerUpdate();

       recEngine.RecognizeAsync(RecognizeMode.Multiple);

       Console.ReadKey();
       recEngine.SpeechRecognized -= recEngine_SpeechRecognized;
       recEngine.AudioStateChanged -= recEngine_AudioStateChange;
    }
  }

  internal static void recEngine_AudioStateChange(object sender, AudioStateChangedEventArgs e)
  {
     Console.WriteLine("Current AudioLevel: {0}", e.AudioState);
  }

  internal static void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  {
     switch (e.Result.Text)
     {
        case "dollar":
           Console.WriteLine("10kr");
           break;
        case "euro":
           Console.WriteLine("A lot more");
           break;
     }
  }
  

注2:
在Windows 7上,System.Speech.Recognition无效。
可以使用Microsoft Speech Platform的免费开发工具启用语音识别和TTS(文本到语音)。下载并安装SDK,Runtime和Desidered语言。这是服务器语音识别实现,开发用于通过电话线支持语音识别。因此它具有非常好的原生滤波器,可以发出噪音,并且对发音怪癖具有良好的容忍度。