2014-02-22 172 views
1

我尝试实施Microsoft语音识别教程。我没有得到任何错误,但仍然没有认出声音。该代码就像Microsoft语音无法识别语音

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using Microsoft.Speech.Recognition; 

namespace WindowsFormsApplication3 
{ 
public partial class Form1 : Form 
{ 
    //SpeechRecognizer recognizer; 
    SpeechRecognitionEngine sre; 

    public Form1() 
    { 
     InitializeComponent(); 
     sre = new SpeechRecognitionEngine(); 
     sre.SetInputToWaveFile(@"c:\Test\Colors.wav"); 

     Console.WriteLine("here"); 


     // Create a simple grammar that recognizes "red", "green", or "blue". 
     Choices colors = new Choices(); 
     colors.Add(new string[] { "red", "green", "blue" }); 
     Console.WriteLine("here"); 

     // Create a GrammarBuilder object and append the Choices object. 
     GrammarBuilder gb = new GrammarBuilder(); 
     gb.Append(colors); 

     // Create the Grammar instance and load it into the speech recognition engine. 
     Grammar g = new Grammar(gb); 
     sre.LoadGrammar(g); 

     // Register a handler for the SpeechRecognized event. 

     // Start recognition. 
     sre.SpeechRecognized += 
      new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized); 


     sre.Recognize(); 
     Console.WriteLine("here"); 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 


    } 

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

     MessageBox.Show("Speech recognized: " + e.Result.Text); 
    } 

} 
} 

请帮我理清!我不知道为什么它不工作,是新来的C#和Visual Studio

PS:我也得在输出窗口消息像下面这样在运行程序

The thread '<No Name>' (0x674) has exited with code 0 (0x0). 
The thread '<No Name>' (0x1ee0) has exited with code 0 (0x0). 
The thread '<No Name>' (0xf8) has exited with code 0 (0x0). 
The thread '<No Name>' (0x760) has exited with code 0 (0x0). 
The thread 'vshost.RunParkingWindow' (0x1184) has exited with code 0 (0x0). 
+0

你在使用什么语音库?我添加了对system.speech的引用,并且您的代码识别了我的wav文件。 –

+0

我正在使用Microsoft.speech库...编辑:我检查了错误的文件对不起, – practice2perfect

+1

请不要包括关于在问题标题中使用的语言的信息,除非它没有它没有意义。标签用于此目的。另外,如果你的问题不是直接关于它并且你只是写代码,那么不要在标签中包含IDE标签(Visual Studio)。代码问题很少连接到代码编辑器。 –

回答

2

试试这个代码适用于我

 public partial class MainWindow : Window 
     { 

      SpeechRecognitionEngine _recognizer; 
      SpeechSynthesizer sre = new SpeechSynthesizer(); 
      int count = 1; 

      public MainWindow() 
     { 
      InitializeComponent(); 
      Initialize(); 
     } 

     private void Initialize() 
     { 
      try 
      { 
       var culture = new CultureInfo("en-US"); 
       _recognizer = new SpeechRecognitionEngine(culture); 
       _recognizer.SetInputToDefaultAudioDevice(); 
       _recognizer.LoadGrammar(GetGrammer()); 
       _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized); 

       _recognizer.RecognizeAsync(RecognizeMode.Multiple); 

       sre.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Child); 
       sre.Rate = -2; 

      } 
      catch (Exception ex) 
      { 
       System.Windows.MessageBox.Show(ex.InnerException.Message); 
      } 
     } 

     private static Grammar GetGrammer() 
     { 

      var choices = new Choices(); 
      //add custom commands 
      choices.Add(File.ReadAllLines(@"Commands.txt")); 
      //to add the letters to the dictionary 
      choices.Add(Enum.GetNames(typeof(Keys)).ToArray()); 

      var grammer = new Grammar(new GrammarBuilder(choices)); 

      return grammer; 
     } 

     void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     { 

      string speech = e.Result.Text; 

     //to type letters in open application like notepad 
     if (Enum.GetNames(typeof(Keys)).Contains(speech)) 
     { 
      try 
      { //send the string to the application 
       SendKeys.SendWait("{" + speech + "}"); 
      } 
      catch (ArgumentException) 
      { 

      }    
     } 

     //handle custom commands 
      switch (speech) 
      { 

       case "Hello": 
        sre.Speak("Goodmorning "); 
        break; 

       case "Notepad": 
        System.Diagnostics.Process.Start("Notepad"); 
        break; 
       case "Maximize": 
        this.WindowState = System.Windows.WindowState.Maximized; 
        break; 
       case "Minimize": 
        this.WindowState = System.Windows.WindowState.Minimized; 
        break; 
       case "Restore": 
        this.WindowState = System.Windows.WindowState.Normal; 
        break; 
       case "Close": 
        Close(); 
        break; 
      } 
     }  
    } 

您还需要创建一个.txt文件使用以下命令加载每个语法像一条线下方

Notepad 
Close 
Minimize 
Maximize 
Open 
Hello 
+0

感谢它的工作!我改变了一些东西,比如在代码本身中添加语法..也可以建议我..是否有可能将字典语法添加到现有语法..即如果语音接近..应用程序应该关闭..(as每个代码),如果声音退出,那么它应该输入退出..某事就像..如打开记事本后,如果我说非用户定义的语法..它应该开始在记事本中键入 – practice2perfect

+0

@ user3260179编辑答案,添加了自定义逻辑,在打开的应用程序中键入字母,如记事本。我添加了逻辑,以便在添加Commands.txt后将所有英文字母添加到字典中,然后将字母发送到事件中的字典中。这将仅键入单个字母,添加自定义单词,你将需要将它们添加到commands.txt – Tinu

+0

thnks为你的答复...而不是字母如何使用预建的字典语法..我试过但代码doesn工作..我cudn上传代码因为我现在没有它.. – practice2perfect