Microsoft Speech无法识别语音

时间:2014-02-22 12:17:55

标签: c# speech-recognition speech-to-text

我尝试实施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).

1 个答案:

答案 0 :(得分:3)

试试这段代码适合我

     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