创建语音助手

大家好 最近,我为创建语音助手而感到兴奋。 面临十亿个问题。 但是我还是决定了我的问题。

首先,下载Visual Studio

现在您需要下载3个Microsoft软件包

  1. https://www.microsoft.com/zh-cn/download/details.aspx?id=3971
  2. https://www.microsoft.com/zh-cn/download/details.aspx?id=24003
  3. https://www.microsoft.com/zh-cn/download/details.aspx?id=24974

下载后,请选择x86平台(使用起来更简单,而且没有漏洞)。

现在 选择一种语言时,请选择任何一个TELE结尾的语言(下载此Microsoft Speech Platform-服务器运行时语言时),这是必须的。



语言包安装结束时,什么都不会发生,不会有“完成”或“完成”按钮,请不要惊慌,一切都很好。

现在通过Visual Studio,创建一个新项目(Windows Forms App)



设置1个标签,并且不要更改其名称:



打开“解决方案资源管理器”或“项目浏览器”,也可以单击菜单中的“项目”或“项目”按钮,并添加2链接“或”“添加引用”。

这里有这个窗口:



单击“浏览器”或“浏览”,然后转到文件夹“ C:\ Program Files(x86)\ Microsoft SDKs \ Speech \ v11.0 \ Assembly”,将有1个DLL库,选择它:



同样,我们添加了链接,但已经是标准链接。 我们正在寻找“ System.Speech”(为什么原因见下文)。 我们放了一个DAW,然后单击“确定”按钮。



我们传递给代码! 首先,为“显示”表单创建一个事件。 您可以通过使用拉链单击“属性”选项卡来执行此操作:



标题不需要重写! 在输入字段上单击2次即可。

在最顶层,我们在项目中包括以下类:

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; using System.IO; using System.Speech.Synthesis; using System.Diagnostics; using System.Threading; 

现在就在

 public Form1() { InitializeComponent(); } 

我们写

 static Label l; static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { if (e.Result.Confidence > 0.7) { l.Text = e.Result.Text; if(e.Result.Text == "  "){ //  .     ( //      speak,   !  ,  // speak("  ."); } }//  0,7   . //  .  ,   .    // } 

我们创建了一个变量和一个将处理语音的类。

在“显示”事件中,我们填充下一段代码。

 l = label1; System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("ru-ru"); // ru-ru   ,   en-us  ..       //   new System.Globalization.CultureInfo("ru-ru")  // System.Globalization.CultureInfo.CurrentCulture SpeechRecognitionEngine sre = new SpeechRecognitionEngine(ci); sre.SetInputToDefaultAudioDevice(); sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized); Choices numbers = new Choices(); numbers.Add(new string[] { "", "", "", "", "", "  " }); //   // GrammarBuilder gb = new GrammarBuilder(); gb.Culture = ci; gb.Append(numbers); Grammar g = new Grammar(gb); sre.LoadGrammar(g); sre.RecognizeAsync(RecognizeMode.Multiple); 

现在我们抓住了这句话。 然后在函数中创建函数

 static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 

或者您可以创建一个函数处理程序。

现在,我们向助手发出声音。 我们说一个新的功能。 此功能将捕获文字并发出声音。

 private void speak(string text) { SpeechSynthesizer speaker = new SpeechSynthesizer(); speaker.Rate = 1; speaker.Volume = 100; speaker.Speak(text); } 

恭喜,您的迷你语音助手已完成。 我们正在完成它,它不会比“ Ok Google”更糟糕。

对于所有问题,请写电报@Cp_Troia-我会尽力而为。

我的消息来源 。 在说一句话之前,先说“ Sudo”,然后说一个命令,例如“ Open Google Chrome”或“ hello”,那里有很多命令,甚至可以打开笔记本和计算器,打开YouTube等。

Source: https://habr.com/ru/post/zh-CN451718/


All Articles