2012年12月3日 星期一

Speech SDK 語音辨識範例

1. OS: Windows 7 x64
2. Tools: VS 2010
               Speech SDK x64 (Install Runtime, SDK and then TTS SR zh-TW)
3.

<Grid>
        <Ellipse Name="OO" Margin="0,0,0,0" Stroke="Red" StrokeThickness="20" Visibility="Hidden"/>
        <Grid Name="XX" Margin="0,0,0,0" Visibility="Hidden" >
            <Line Stroke="Blue" StrokeThickness="20" X1="0" Y1="0" X2="525" Y2="350"/>
            <Line Stroke="Blue" StrokeThickness="20" X1="0" Y1="350" X2="525" Y2="0"/>
        </Grid>
  </Grid>
4.

// 1. Add Ref Microsoft Speech Object Library v 11 Not in COM
// C:\Program Files\Microsoft SDKs\Speech\v11.0\Assembly\Microsoft.Speech.dll
using Microsoft.Speech.Recognition;

namespace WPFOX1
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Create a new SpeechRecognitionEngine instance.
            SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("zh-TW"));
            // SpeechRecognitionEngine sre = new SpeechRecognitionEngine();

            // Create a simple grammar that recognizes "red", "green", or "blue".
            Choices colors = new Choices();
            colors.Add("對");
            colors.Add("是");
            colors.Add("圈");
            colors.Add("錯");
            colors.Add("差");
            colors.Add("不對");
           
            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(colors);

            // Create the actual Grammar instance, and then load it into the speech recognizer.
            Grammar g = new Grammar(gb);
            sre.LoadGrammar(g);

            // Register a handler for the SpeechRecognized event.
            sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
            sre.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(sre_SpeechRecognitionRejected);
            sre.SetInputToDefaultAudioDevice();
            sre.RecognizeAsync(RecognizeMode.Multiple);
        }

        void sre_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            this.Title = e.ToString();
        }

        void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            //this.Title = e.ToString();
            switch (e.Result.Text)
            {
                case "對":
                case "是":
                case "圈":
                    OO.Visibility = System.Windows.Visibility.Visible;
                    XX.Visibility = System.Windows.Visibility.Hidden;
                    break;
                case "錯":
                case "差":
                case "不對":
                    OO.Visibility = System.Windows.Visibility.Hidden;
                    XX.Visibility = System.Windows.Visibility.Visible;
                    break;
                default:
                    OO.Visibility = System.Windows.Visibility.Hidden;
                    XX.Visibility = System.Windows.Visibility.Hidden;
                    break;
            }
        }
    }
5 結果




沒有留言:

張貼留言