Based on. NET speech synthesis or speech recognition applications are relatively simple, the base Class library has helped us do a lot of work. We just need to focus on our needs. So here is also a simple case to demonstrate based on. NET of speech recognition. The speech recognition engine is integrated in the Windows system. The engine does not have a standalone installation package, which is included in the system language pack. In general, the Simplified Chinese system includes only Chinese recognition, and if additional language packs are installed, there should also be an additional speech recognition engine, so we can identify the speech recognition engine we are using as much as possible in our development.
First create a form program, add System.Speech.Recognition Reference, the program interface layout is as follows:
The program code is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Speech.Recognition;usingSystem.Globalization;namespacerecognition{ Public Partial classFrmmain:form {PrivateSpeechrecognitionengine recognizer;//Speech recognition engine Private stringEngineid ="Ms-2052-80-desk";//Win7 speech recognition engine number Private stringCurrentCulture ="ZH-CN";//language PublicFrmmain () {InitializeComponent (); } Private voidFrmmain_load (Objectsender, EventArgs e) { /** If there are multiple recognition engines on the system, initialize the engine with the specified number and language * The number of other engines can be obtained from speechrecognitionengine.installedrecognizers () */CultureInfo CultureInfo=NewCultureInfo (currentCulture);//Current Regional Culture foreach(Recognizerinfo Recognizerinfoinchspeechrecognitionengine.installedrecognizers ()) { if(RecognizerInfo.Culture.Equals (cultureInfo) && recognizerinfo.id = =Engineid) {Recognizer=NewSpeechrecognitionengine (Recognizerinfo); Recognizer. SpeechRecognized+=NewEventhandler<speechrecognizedeventargs>(recognizer_speechrecognized); Break; } } //Uninstall all Syntaxrecognizer. Unloadallgrammars (); //Load Natural GrammarDictationgrammar Dictationgrammar =NewDictationgrammar (); Recognizer. Loadgrammar (Dictationgrammar); } /// <summary> ///Start listening/// </summary> Private voidbtnStart_Click (Objectsender, EventArgs e) { //set up a voice input device (using the system default input device)recognizer. Setinputtodefaultaudiodevice (); //Enable continuous speech recognition moderecognizer. Recognizeasync (recognizemode.multiple); This. Btnstart.text ="Listening in ..."; This. btnstart.enabled =false; This. btnstop.enabled =true; } /// <summary> ///Stop listening/// </summary> Private voidBtnstop_click (Objectsender, EventArgs e) {recognizer. Recognizeasyncstop (); This. Btnstart.text ="Start listening"; This. btnstart.enabled =true; This. btnstop.enabled =false; } /// <summary> ///raised when continuous recognition is complete/// </summary> Private voidrecognizer_speechrecognized (Object sender, Speechrecognizedeventargs e) { This. Rtxtresult.text =E.result.text; } }}
When initializing the engine, you need to indicate which recognition engine is currently in use. How do I see the number of the speech recognition engine installed on the system? You can use the static method of Speechrecognitionengine.installedrecognizers () to get all the engines installed on the system. Use this method to pre-understand the number and language of the engine identified by the system.
Based on the information you already know, you can pre-define the following recognition engines to use:
Private Speechrecognitionengine recognizer; // Speech recognition engine Private string " Ms-2052-80-desk " // Win7 speech recognition engine number Private string " ZH-CN " // language
The
Then initializes the recognition engine based on the above information:
CultureInfo CultureInfo = new CultureInfo (currentCulture); // current region culture foreach ( Recognizerinfo recognizerinfo in Speechrecognitionengine.installedrecognizers ()) { if ( RecognizerInfo.Culture.Equals (cultureInfo) && recognizerinfo.id == Engineid) { Recognizer = new Speechrecognitionengine ( Recognizerinfo); Recognizer. speechrecognized + = new eventhandler<speechrecognizedeventargs>< Span style= "COLOR: #000000" > (recognizer_speechrecognized); break ; }}
The speech recognition engine is more important than the recognition syntax. There are two kinds of general: one is natural grammar and one is custom grammar. Natural grammar is the basic syntax for the speech recognition engine to work. If you do not load the natural grammar recognition accuracy will be very low. The custom syntax, however, is that we are programmed to improve recognition accuracy for specific requirements or projects. The above case is only used to identify the natural grammar.
// Uninstall all Syntax recognizer. Unloadallgrammars (); // Load Natural Grammar New Dictationgrammar (); recognizer. Loadgrammar (Dictationgrammar);
This first unloads all the syntax, in order to avoid loading the duplicate syntax.
Open and test the microphone available, run the above code, and try to speak to the microphone, the result is as follows:
TIPS: You can combine the previous speech synthesis to make a simple question and answer robot program.
The above case source code: Source Download
(iv) Speech recognition test case