Create Voice Assistant

Good day. Recently, I got excited about creating a voice assistant. Faced a billion problems. But still I decided my questions.

To get started, 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’s not buggy).

Now. When choosing a language, choose any one with the TELE ending (when you download this Microsoft Speech Platform - Server Runtime Languages), this is a must.



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

Now we go into Visual Studio, create a new project (Windows Forms App)



Install 1 label and do not change its name:



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

You will see this window:



We 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 add the link, but already standard. We are looking for "System.Speech" (see below for why). We put a daw and click on the "OK" button.



We pass to the code! First, create an event for the “shown” form. You can do this by clicking on the “Properties” tab with a zipper:



You do not need to rewrite the name! Just 2 clicks on the input 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 right 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 created a variable and a class that will handle speech.

In our “shown” event, we stuff 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(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);

Now 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 the voice sound of our assistant. We make 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 finalizing it and it will be no worse than “Ok 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 there, you can even open a notebook and calculator, open YouTube, etc.

Also popular now: