Alpha Webdesign

From the Blog

Voice recognition VB Project

Visual Basic – Voice commands & Speech Recognition

Visual Basic – Voice commands & Speech Recognition

‘Please visit: https://www.youtube.com/channel/UCGBJFrVjfsTRu-C_Eh2BgVQ?view_as=subscriber
‘for instructions on how to complete this Visual Basic project
‘Created by Ioannis Ntizoglou 05/03/2019
‘Import the Speech recognition on the form
Imports System.Speech

Public Class Form1

‘Create the first global variable MyVoice to recognise the new voice each time a person speaks
Dim WithEvents MyVoice As New Recognition.SpeechRecognitionEngine

‘The first Private Sub – SetColor (it will allow you to action the command and set the background colour to the colour the user has said.
Private Sub SetColor(ByVal color As System.Drawing.Color)
Me.BackColor = color
End Sub

‘Form load – if you have renamed your form, you will need to change the “Form1_Load” to the “NameOfYourForm_Loud
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘activate the default audio device “Mic”
MyVoice.SetInputToDefaultAudioDevice()

‘Create a var MyGrammar
Dim MyGrammar As New Recognition.SrgsGrammar.SrgsDocument

‘Create a var MyWordsRule
Dim MyWordsRule As New Recognition.SrgsGrammar.SrgsRule(“words”)

‘Create a var MyWordsRule
Dim MyWords As New Recognition.SrgsGrammar.SrgsOneOf(“green”, “blue”, “Lee”, “Ioannis”)

‘Add the words I speak onto the system
MyWordsRule.Add(MyWords)

‘Add the MyWordRule onto the system
MyGrammar.Rules.Add(MyWordsRule)

‘The location to MyWordRule
MyGrammar.Root = MyWordsRule

‘When you hear my voice, LoadGrammar
MyVoice.LoadGrammar(New Recognition.Grammar(MyGrammar))

‘recognise my voice on form load
MyVoice.RecognizeAsync()

End Sub

‘recognise my voice every time I speak
Private Sub reco_RecognizeCompleted(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognizeCompletedEventArgs) Handles MyVoice.RecognizeCompleted

MyVoice.RecognizeAsync()

End Sub
Dim SAPI

‘recognise my voice and if the case exists, execute the procedure
Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles MyVoice.SpeechRecognized
SAPI = CreateObject(“SAPI.spvoice”)
‘Select Case
Select Case e.Result.Text
‘if the user speaks “Blue”
Case “blue”
‘The Background of the Form will change to Blue
SetColor(Color.Blue)
PictureBox1.Visible = False
PictureBox2.Visible = False

‘if the user speaks “Blue”
Case “green”
‘The Background of the Form will change to Green
SetColor(Color.Green)
PictureBox1.Visible = False
PictureBox2.Visible = False

Case “Lee”
‘The Background of the Form will change to Green
SetColor(Color.Green)
PictureBox1.Visible = True
PictureBox2.Visible = False
SAPI.Speak(“Lee is not as good looking as Ioannis”)

Case “Ioannis”
‘The Background of the Form will change to Green
SetColor(Color.Blue)
PictureBox2.Visible = True
PictureBox1.Visible = False
SAPI.Speak(“Ioannis is the True Greek God and he is very very very good looking”)

End Select

End Sub

End Class

2 comments

  1. maverick - October 10, 2019 6:02 am

    why i cant copy the codes?

    Reply

Have your say