Hack Forums
[.NET] Voice Recognition - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Voice Recognition (/showthread.php?tid=3275428)

Pages: Pages 1 2


Voice Recognition - SIM-1456245 - Feb 21, 2013

This tutorial is about Voice Recognition using "Microsoft Speech SDK 5.1". It will listen for your voice from a microphone, and then it will check if the recorded voice is similar to the given words, if yes then it does something, in this case you say a color and the background color of the form will change accordingly(sorry no MsgBox tutorial today, or at least not here, in this tutorial).




1. Download and install the Microsoft Speech SDK :
NOTE: The link is from Microsoft, so I almost can guarantee that it is safe to download.
http://www.microsoft.com/en-us/download/details.aspx?id=10121
[Image: vQpygsA.png]

2. First thing is first, we need to make a new project :
You need to have Framework 3.0 or 3.5, not 4.0 or 4.5 because this SDK is a bit old.
[Image: Gw9jns2.png]

3. As you can see, we don't need to add anything to the form. You also don't need to edit any properties.
[Image: FBH2vKV.png]

4. Lets add the Speech library to our program :
Project > Add Reference > .NET (tab) > System.Speech (select from the list).
[Image: G3dDvYM.png]

[Image: gBaAgUP.png]

5. And now to the code:

First we need to import the SDK to the code :
Code
Imports System.Speech

Now lets declare the class that we are using here, in this case System.Speech:
Code
Dim WithEvents listen As New Recognition.SpeechRecognizer

Here we set up some stuff, this needs to be in the Form Load sub:
Code
Dim gram As New Recognition.SrgsGrammar.SrgsDocument
  Dim rule As New Recognition.SrgsGrammar.SrgsRule("word") '(ID) - This is used to tell the program what are you detecting, but for general use you can leave it as it is. For number(number), For colors(color)
  Dim list As New Recognition.SrgsGrammar.SrgsOneOf("Red", "Green", "Blue", "White", "Black", "Gray", "Yellow", "Pink", "Purple", "Orange", "Gold", "Silver") ' Add the word that you want to use later in the program.
  rule.Add(list)
  gram.Rules.Add(rule)
  gram.Root = rule
  listen.LoadGrammar(New Recognition.Grammar(gram))

This is the sub that actually detects when you say something and then it recognizes what you sat.
Code
Private Sub speech(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles listen.SpeechRecognized

  End Sub

In the newly created sub add the following code, it will just change the color of the form We are using here a Select Case statement, but you can use If as well.
Code
Select Case (e.Result.Text)
    Case "Red"
    Me.BackColor = Color.Red
    Case "Green"
    Me.BackColor = Color.Green
    Case "Blue"
    Me.BackColor = Color.Blue
    Case "White"
    Me.BackColor = Color.White
    Case "Black"
    Me.BackColor = Color.Black
    Case "Gray"
    Me.BackColor = Color.Gray
    Case "Yellow"
    Me.BackColor = Color.Yellow
    Case "Pink"
    Me.BackColor = Color.Pink
    Case "Purple"
    Me.BackColor = Color.Purple
    Case "Orange"
    Me.BackColor = Color.Orange
    Case "Gold"
    Me.BackColor = Color.Gold
    Case "Silver"
    Me.BackColor = Color.Silver
  End Select

And that was actually it, see a very simple thing. Now double check your code and run it, it should change the color of the form accordingly on that what you say on the microphone.

Here is the final code:
Code
'Code by: Electroalek <http://www.electroalek.blogspot.com/>, 2013
Imports System.Speech
Public Class Form1
    Dim WithEvents listen As New Recognition.SpeechRecognizer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim gram As New Recognition.SrgsGrammar.SrgsDocument
  Dim rule As New Recognition.SrgsGrammar.SrgsRule("word")
  Dim list As New Recognition.SrgsGrammar.SrgsOneOf("Red", "Green", "Blue", "White", "Black", "Gray", "Yellow", "Pink", "Purple", "Orange", "Gold", "Silver")
  rule.Add(list)
  gram.Rules.Add(rule)
  gram.Root = rule
  listen.LoadGrammar(New Recognition.Grammar(gram))
    End Sub

    Private Sub speech(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles listen.SpeechRecognized
  Select Case (e.Result.Text)
    Case "Red"
    Me.BackColor = Color.Red
    Case "Green"
    Me.BackColor = Color.Green
    Case "Blue"
    Me.BackColor = Color.Blue
    Case "White"
    Me.BackColor = Color.White
    Case "Black"
    Me.BackColor = Color.Black
    Case "Gray"
    Me.BackColor = Color.Gray
    Case "Yellow"
    Me.BackColor = Color.Yellow
    Case "Pink"
    Me.BackColor = Color.Pink
    Case "Purple"
    Me.BackColor = Color.Purple
    Case "Orange"
    Me.BackColor = Color.Orange
    Case "Gold"
    Me.BackColor = Color.Gold
    Case "Silver"
    Me.BackColor = Color.Silver
  End Select
    End Sub
End Class

[Image: siKw1DH.png]



I hope you liked it 😉

[img]http://electroalek.comoj.com/ElectroalekLogo.gif[/img]


* - iDuL - Feb 21, 2013

Looks like an incredible Tutorial, thank you for the contribution!!


* - tweety - Feb 21, 2013

(Feb 21, 2013 10:59 AM)Electroalek Wrote:  Now lets declare the the recognition sub :
Code
Dim WithEvents listen As New Recognition.SpeechRecognizer

That's not a sub, it's a declaration of the class your using.
Also you didn't really explain what your doing to be honest, nevertheless, decent tutorial.. Just about.


* - SIM-1456245 - Feb 21, 2013

(Feb 21, 2013 11:09 AM)Tweety. Wrote:  That's not a sub, it's a declaration of the class your using.
Also you didn't really explain what your doing to be honest, nevertheless, decent tutorial.. Just about.

Ooops, mistake. Fixed. You don't really need to explain much more.


Please replay to keep this thread up.


* - XvI Beta IvX - Feb 21, 2013

I will test this out later. Thanks
/thirtyfivechar


* - Cait Snow - Feb 21, 2013

Great tutorial, OP!
Thanks for sharing this to the community!

PunkoHead 🤝


* - SIM-1456245 - Feb 21, 2013

Thanks a lot :)


Tomorrow I will post a tutorial for a speech program(the program says something to you). And later on I will combine them together and make something cool, because this SDK is very cool. And I just realized that Microsoft Speech SDK 5.1 is a bit outdated. There are newer versions of it, maybe in the near future I will make something else.


* - Foursenal - Feb 21, 2013

Very detailed tutorial. Easy to go through as well.
Nice tutorial.


* - Meteen - Feb 22, 2013

Got an error: "System.PlatformNotSupportedException" on this line: "listen.LoadGrammar(New Recognition.Grammar(gram))"

Please help?


* - Support. - Feb 22, 2013

I like this but I've found SpeechRecognitionEngine better with bigger projects. Would make a good tut with good explaining of Grammar, GrammarBuilder, Choices, Confidence & how easy it is to get devices like your phone to work with it sending wav recordings.


* - x2SiKJaYzZ - Feb 22, 2013

Very HQ tutorial , well explained and not that much to read , and with photo's haha. thanks


* - Xyzo - Feb 22, 2013

Nice tutorial, will try it out now! :)


* - 4RG0S - Feb 22, 2013

Thanks for the Tut, looks great. I will try it :)


* - SIM-1456245 - Feb 22, 2013

(Feb 22, 2013 05:46 AM)Meteen Wrote:  Got an error: "System.PlatformNotSupportedException" on this line: "listen.LoadGrammar(New Recognition.Grammar(gram))"

Please help?

LOL, I had the same error, but I fixed it. The funny thing here is that I don't remember how. If I remember I will post.


Have you installed all of the files from the SDK ?
Have you done everything as it says ?


Try downloading and installing this:http://activex.microsoft.com/activex/controls/agent2/tv_enua.exe
(This is not a virus, it can solve your problem. I downloaded it, it is clean, but if you don't trust me don't download it. Not my server, Microsoft website.


* - Debroski - Feb 23, 2013

Instead of having a huge switch case, why not just

Code
Me.BackColor = Color.FromName(e.Result.Text)

so you don't have to continuously add as you want more colors.