📜 ⬆️ ⬇️

Creating a voice assistant

Good day. Recently, I fired up the issue of creating a voice assistant. Faced with a billion problems. But still I decided my questions.

First, download Visual Studio .

Now you need to download 3 microsoft packages.

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

When you download, choose the x86 platform (it's easier with it and it is not buggy).
')
Now. When choosing a language, choose any with the end of TELE (when downloading this Microsoft Speech Platform - Server Runtime Languages), this is a must.



At the end of the language pack installation, nothing will happen, there will be no “end” or “finish” buttons, do not worry, everything is fine.

Now we are passing through Visual Studio, creating a new project (Windows Forms App)



Set 1 label and do not change its name:



Open “Solution explorer” or “Project Explorer”, you can also click on the “Project” or “Project” button and add a link "or" Add reference "in the menu.

This window opens here:



Click on "browser" or "Browse" and go to the folder "C: \ Program Files (x86) \ Microsoft SDKs \ Speech \ v11.0 \ Assembly" and there will be 1 DLL library, select it:



Again we add the link, but already standard. We are looking for "System.Speech" (see below for why). We put a daw and click on the button "OK".



Go to the code! To begin, create an event for the form shown. This can be done by clicking on the tab "Properties" on the zipper:



You do not need to rewrite the name! Just 2 clicks on the entry field.

At the very top, we include the following classes in the project:

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; 

Now immediately under

 public Form1() { InitializeComponent(); } 

We write

 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   . //  .  ,   .    // } 

We have created a variable and a class that will handle speech.

In our event "shown" we push the next piece of code.

 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); 

Here we catch the words. Then you create functions in the function

 static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 

or you can create a function handler.

Now we make our assistant’s voice sound. Making a new function speak. This function will catch the text and voice it.

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

Congratulations, your mini voice assistant is done. We are developing it and it will be no worse than “Okay Google”.

For all questions, write to telegrams @Cp_Troia - I will help as I can.

My source . Before you say something, say “Sudo”, and then a command, for example “Open Google Chrome” or “hello”, there are many commands, you can even open a notebook and calculator, open YouTube, etc.

Source: https://habr.com/ru/post/451718/


All Articles