locked
speech recognition in VB RRS feed

  • Question

  • I have done a project on 'Home appliance control through speech' using parallel port relay board & matlab code. I have been using windows speech recognition from a long time & would like to know if it is possible to use windows inbuilt speech recognition in VB to trigger one of the PC parallel port pins to ON or OFF.  Basically i want to do this to control the relays connected to the parallel port. I just want my program to recognize a few custom commands such as LIGHT ON or FAN ON to trigger the relays. I'm from electronics background & beginner in VB. I would we grateful if you could help me on this. 

    Friday, October 29, 2010 6:24 PM

Answers

All replies

  • .NET has a whole speech library built in for text to speech and speech recognition. You need to set a reference to the system.speech.dll.

    As far as using the classes once you set the reference, there are a good number of examples around the web.

    Since you are only looking to detect certain words, it shouldn't be all that difficult. It gets more complex when you actually want to do something like dictation.


    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
    • Marked as answer by Liliane Teng Sunday, November 7, 2010 2:11 PM
    Friday, October 29, 2010 8:24 PM
  • Hello rajesh_reddy,

    Thanks for your post.

    You could check bolow articles about Speech Recognition for more information. Hope they could make you get some ideas.

    http://blogs.msdn.com/b/robertbrown/archive/2005/06/14/428967.aspx
    (Speech sample in VB.NET)
    http://msdn.microsoft.com/en-us/library/dd148511.aspx#
    (Enabling Speech Recognition in Microsoft Word 2007 with Visual Studio 2008)
    http://msdn.microsoft.com/en-us/library/cc627340.aspx#
    (Creating a Text-to-speech add-in for Microsoft Word 2007 with Visual Studio 2008)
    http://msdn.microsoft.com/en-us/magazine/cc163663.aspx
    (Exploring New Speech Recognition And Synthesis APIs In Windows Vista)

    If you have any concerns, please feel free to follow up.

    Best regards,
    Liliane
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. Thanks
    • Marked as answer by Liliane Teng Sunday, November 7, 2010 2:11 PM
    Thursday, November 4, 2010 6:56 AM
  • I am trying to do the same thing but i have the opposite problem... i programmed the speech recognition part but don't know how to send an input to the relays.

    Anyway, i hope this code can help you. It is different from the usual speech recognition projects that use commads in the code. Mine loads the commands from an external text file which has a structure:

    command^action^commandtype

    these lines are loaded into an array which is split into 3 strings that you can handle. the 1st contains the command, the 2nd contains an action or a sentence to speak as confirmation, and the 3rd contains a classifier according to which the programs handles the 2nd parameter in a different way. In my case, classifier 'social' means it is social interaction and the 2nd parameter is a phrase to be spoken with speech synthesis, whereas classifier 'apps' means that the 2nd parameter contains the name of a process to be launched (e.g.: explorer.exe or iexplorer.exe), and 'internal' indicates an internal action (that wil be your case)

    Imports System.Speech
    Imports System.Speech.Recognition
    Imports System.Runtime.InteropServices
    Imports System.IO
    
    Public Class vera
    
        Dim WithEvents reco As New Recognition.SpeechRecognitionEngine
    
        Dim commandset() As String
    
        'definition of constant to control volume 
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function
        Const WM_APPCOMMAND As UInteger = &H319
        Const APPCOMMAND_VOLUME_UP As UInteger = &HA
        Const APPCOMMAND_VOLUME_DOWN As UInteger = &H9
        Const APPCOMMAND_VOLUME_MUTE As UInteger = &H8
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'loading the 1st text file (commands only) in the grammarbuilder
            Dim npath As String = Application.StartupPath & "\commandlist.txt"
            Dim nsr As StreamReader = File.OpenText(npath)
            Dim i As Integer
            Dim ls As String
            For Each ls In File.ReadLines(npath) 'populates the listbox with the commands for reference
                ReDim Preserve commandset(i)
                commandset(i) = ls
                i += 1
                ListBox1.Items.Add(ls)
                Application.DoEvents()
            Next
    
            reco.SetInputToDefaultAudioDevice() 'defines the standard input device
    
            'loads the commands set
            Dim cmdList As New GrammarBuilder
            cmdList.Append(New Choices(commandset))
            reco.LoadGrammar(New Recognition.Grammar(cmdList))
            reco.RecognizeAsync()
    
    
        End Sub
    
        Private Sub reco_RecognizeCompleted(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognizeCompletedEventArgs) Handles reco.RecognizeCompleted
    
            reco.RecognizeAsync()
        End Sub
    
    
        Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles reco.SpeechRecognized
            'handles what happens when a speech is recognized
            Dim synth As New Synthesis.SpeechSynthesizer
            Dim npath As String = Application.StartupPath & "\commandactionlist.txt" 'loads the 2nd txt file command^action^comtype
            Dim nsr As StreamReader = File.OpenText(npath)
            Dim ls As String
            Dim answer As String = ""
    
            Dim params(2) As String 'defines the array
            Dim execute = e.Result.Text.ToLower
            For Each ls In File.ReadLines(npath)
    
    
                params = ls.Split("^"c) 'splits the array into 3 strings
                Dim Command As String = params(0).ToLower
                Dim comtype As String = params(2)
                Dim action As String = params(1)
    
                'checks the comtype and if the command (parameter 1) is contained in the spoken sentences
                If execute.Contains(Command) And comtype = "social" Then
                    Dim robotvoice = CreateObject("sapi.spvoice")
                    answer = Action
                    robotvoice.Speak(answer)
    
                ElseIf execute.Contains(Command) And comtype = "apps" Then
                    Dim robotvoice = CreateObject("sapi.spvoice")
    
                    If execute.Contains("open") Then
                        answer = "Opening application"
                        robotvoice.Speak(answer)
                        Process.Start(Action)
                    ElseIf execute.Contains("close") Then
                        answer = "Closing application"
                        robotvoice.Speak(answer)
                        For Each myprocess As Process In Process.GetProcessesByName(Action)
                            myprocess.Kill()
                        Next
                    End If
    
    
                    'some examples of internal actions - notice they dont take care of the action part of the array so that is where you will write the code to handle the external peripheral or the serial/parallel port
                ElseIf execute.Contains(Command) And comtype = "internal" Then
                    Dim robotvoice = CreateObject("sapi.spvoice")
                    answer = "Ok Creator"
                    robotvoice.Speak(answer)
                    If Command = "go full screen" Then
                        Me.WindowState = FormWindowState.Maximized
    
                    ElseIf Command = "go behind screen" Then
                        Me.WindowState = FormWindowState.Minimized
    
                    ElseIf Command = "restore screen" Then
                        Me.WindowState = FormWindowState.Normal
    
    
                    ElseIf Command = "volume up" Then
                        SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_UP * &H10000)
                        answer = "Volume increased "
                        robotvoice.Speak(answer)
    
                    ElseIf Command = "volume down" Then
                        SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_DOWN * &H10000)
                        answer = "Volume decreased "
                        robotvoice.Speak(answer)
    
                    ElseIf Command = "shout up" Then
                        answer = "Volume muting "
                        robotvoice.Speak(answer)
                        SendMessage(Me.Handle, WM_APPCOMMAND, &H200EB0, APPCOMMAND_VOLUME_MUTE * &H10000)
    
                        'here you can add your commands as lights on or ligts off each in a elseif condition
    
    
                    End If
                End If
                Application.DoEvents()
            Next
    
    
        End Sub
    
    End Class

    the 1st text file (commandlist.txt) will contain only the comands:

     volume up

    volume down

    shut up

    .

    .

    .

    lights on

    lights off

    .

    .

    the second file (commandactionlist.txt) will contain the array:

    hello computer^hello master how are you?^social

    volume up^noaction^internal

    open internet^iexplore.exe^apps

    lights on^noaction^internal

    Notice that your commands that handle relays shall be identified as 'internal' so that the 2nd parameter is not taken into account, and in the Elseif sequence you shall insert the code handling the relay.

    Hope this can help


    +++ Alex +++

    Sunday, June 28, 2015 10:33 PM