How to realize speech recognition function based on C#
Today Xiaobian to share with you how to achieve speech recognition function based on C#related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after some harvest, let's learn about it together.
In. NET 4.0, I was able to get the computer to recognize our voice with the System.Speech component.
Above, when I say "name," it says "Darren," and I say "age," it says "forever 21." How?
First of all, you need to turn on the voice recognition function of the computer.
Right-click the speaker at the bottom right of your computer and select Recording Devices.
Click the default "microphone" and then click the "configure" button in the lower left corner.
Create a form application in VS with a RichTextBox and 2 Buttons on the interface.
Add a reference to System.Speech.
(PS: Emphasize that if you do not turn on speech recognition, the startup program will report an error: speech recognition is not available on this system. SAPI and speech recognition engine not found.)
Write as follows:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Speech.Recognition;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsForms{ public partial class Form1 : Form { SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(); public Form1() { InitializeComponent(); } private void btnEnable_Click(object sender, EventArgs e) { recEngine.RecognizeAsync(RecognizeMode.Multiple); btnDisable.Enabled = true; } private void Form1_Load(object sender, EventArgs e) { Choices preCmd = new Choices(); preCmd.Add(new string[] { "name", "age" }); GrammarBuilder gb = new GrammarBuilder(); gb.Append(preCmd); Grammar gr = new Grammar(gb); recEngine.LoadGrammarAsync(gr); recEngine.SetInputToDefaultAudioDevice(); recEngine.SpeechHypothesized += RecEngine_SpeechHypothesized; } private void RecEngine_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e) { switch (e.Result.Text) { case "name": txtList.Text += "\nDarren"; break; case "age": txtList.Text += "\n Forever 21"; break; default: break; } } private void btnDisable_Click(object sender, EventArgs e) { recEngine.RecognizeAsyncStop(); btnDisable.Enabled = false; } }} The above is all the content of this article "How to realize speech recognition function based on C#", thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.