C#程序員干貨系列之語音識別


說實話,這些年來從博客園收獲了不少東西。自從當年注冊以來就想平時分享點簡單的小程序啥的。因為平時比較懶,突然發現近2年沒更新了。准備陸續分享些小程序,這些也算是本猿手頭上的一些自制小工具吧。

以后會陸續分享些WPF的自制按鈕控件。

 

語音識別小程序,調用了windows的識別組件。精簡了一些代碼,算是比較簡單易懂的一個語音識別類。

開發測試環境win7,VS2008。如果有其它環境中的,歡迎補充。

 

SRecognition.cs

  1 using System;
  2 using System.Speech.Recognition;
  3 using System.Globalization;
  4 using System.Windows.Forms;
  5 
  6 namespace NingTao
  7 {
  8   public class SRecognition
  9   {
 10     public SpeechRecognitionEngine recognizer = null;//語音識別引擎  
 11     public DictationGrammar dictationGrammar = null; //自然語法  
 12     public System.Windows.Forms.Control cDisplay; //顯示控件  
 13 
 14     public SRecognition(string[] fg) //創建關鍵詞語列表  
 15     {
 16       CultureInfo myCIintl = new CultureInfo("zh-CN");
 17       foreach (RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers())//獲取所有語音引擎  
 18       {
 19         if (config.Culture.Equals(myCIintl) && config.Id == "MS-2052-80-DESK")
 20         {
 21           recognizer = new SpeechRecognitionEngine(config);
 22           break;
 23         }//選擇識別引擎
 24       }
 25       if (recognizer != null)
 26       {
 27         InitializeSpeechRecognitionEngine(fg);//初始化語音識別引擎  
 28         dictationGrammar = new DictationGrammar();
 29       }
 30       else
 31       {
 32         MessageBox.Show("創建語音識別失敗");
 33       }
 34     }
 35     private void InitializeSpeechRecognitionEngine(string[] fg)
 36     {
 37       recognizer.SetInputToDefaultAudioDevice();//選擇默認的音頻輸入設備  
 38       Grammar customGrammar = CreateCustomGrammar(fg);
 39       //根據關鍵字數組建立語法  
 40       recognizer.UnloadAllGrammars();
 41       recognizer.LoadGrammar(customGrammar);
 42       //加載語法  
 43       recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
 44       //recognizer.SpeechHypothesized += new EventHandler <SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);  
 45     }
 46     public void BeginRec(Control tbResult)//關聯窗口控件  
 47     {
 48       TurnSpeechRecognitionOn();
 49       TurnDictationOn();
 50       cDisplay = tbResult;
 51     }
 52     public void over()//停止語音識別引擎  
 53     {
 54       TurnSpeechRecognitionOff();
 55     }
 56     public virtual Grammar CreateCustomGrammar(string[] fg) //創造自定義語法  
 57     {
 58       GrammarBuilder grammarBuilder = new GrammarBuilder();
 59       grammarBuilder.Append(new Choices(fg));
 60       return new Grammar(grammarBuilder);
 61     }
 62     private void TurnSpeechRecognitionOn()//啟動語音識別函數  
 63     {
 64       if (recognizer != null)
 65       {
 66         recognizer.RecognizeAsync(RecognizeMode.Multiple);
 67         //識別模式為連續識別  
 68       }
 69       else
 70       {
 71         MessageBox.Show("創建語音識別失敗");
 72       }
 73     }
 74     private void TurnSpeechRecognitionOff()//關閉語音識別函數  
 75     {
 76       if (recognizer != null)
 77       {
 78         recognizer.RecognizeAsyncStop();
 79         TurnDictationOff();
 80       }
 81       else
 82       {
 83         MessageBox.Show("創建語音識別失敗");
 84       }
 85     }
 86     private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
 87     {
 88       //識別出結果完成的動作,通常把識別結果傳給某一個控件  
 89       string text = e.Result.Text;
 90       cDisplay.Text += text;
 91     }
 92     private void TurnDictationOn()
 93     {
 94       if (recognizer != null)
 95       {
 96         recognizer.LoadGrammar(dictationGrammar);
 97         //加載自然語法  
 98       }
 99       else
100       {
101         MessageBox.Show("創建語音識別失敗");
102       }
103     }
104     private void TurnDictationOff()
105     {
106       if (dictationGrammar != null)
107       {
108         recognizer.UnloadGrammar(dictationGrammar);
109         //卸載自然語法  
110       }
111       else
112       {
113         MessageBox.Show("創建語音識別失敗");
114       }
115     }
116   }
117 }

 

form調用,其中2個按鈕(開始,停止),1個文本框(識別結果)

using System;
using System.Windows.Forms;

namespace NingTao
{
  public partial class Form1 : Form
  {
    private SRecognition sr;

    public Form1()
    {
      InitializeComponent();

      string[] fg = { "東方", "西方", "南方", "北方" };
      sr = new SRecognition(fg);
      button2.Enabled = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      sr.BeginRec(textBox1);
      button1.Enabled = false;
      button2.Enabled = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
      sr.over();
      button1.Enabled = true;
      button2.Enabled = false;
    }
  }
}

 

然后就可以測試語音識別了。

 

下載地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM