ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Speech recognition in Windows using the MS Speech API
Submitter: Inigo Surguy (other recipes)
Last Updated: 2002/02/10
Version no: 1.1
Category:

 

Not Rated yet


Description:

This is an example of using the Microsoft Speech SDK 5.1 under Windows for command and control speech recognition in Python.

Source: Text Source

from win32com.client import constants
import win32com.client
import pythoncom

"""Sample code for using the Microsoft Speech SDK 5.1 via COM in Python.
    Requires that the SDK be installed; it's a free download from
            http://microsoft.com/speech
    and that MakePy has been used on it (in PythonWin,
    select Tools | COM MakePy Utility | Microsoft Speech Object Library 5.1).

    After running this, then saying "One", "Two", "Three" or "Four" should
    display "You said One" etc on the console. The recognition can be a bit
    shaky at first until you've trained it (via the Speech entry in the Windows
    Control Panel."""
class SpeechRecognition:
    """ Initialize the speech recognition with the passed in list of words """
    def __init__(self, wordsToAdd):
        # For text-to-speech
        self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
        # For speech recognition - first create a listener
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
        # Then a recognition context
        self.context = self.listener.CreateRecoContext()
        # which has an associated grammar
        self.grammar = self.context.CreateGrammar()
        # Do not allow free word recognition - only command and control
        # recognizing the words in the grammar only
        self.grammar.DictationSetState(0)
        # Create a new rule for the grammar, that is top level (so it begins
        # a recognition) and dynamic (ie we can change it at runtime)
        self.wordsRule = self.grammar.Rules.Add("wordsRule",
                        constants.SRATopLevel + constants.SRADynamic, 0)
        # Clear the rule (not necessary first time, but if we're changing it
        # dynamically then it's useful)
        self.wordsRule.Clear()
        # And go through the list of words, adding each to the rule
        [ self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd ]
        # Set the wordsRule to be active
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)
        # Commit the changes to the grammar
        self.grammar.Rules.Commit()
        # And add an event handler that's called back when recognition occurs
        self.eventHandler = ContextEvents(self.context)
        # Announce we've started using speech synthesis
        self.say("Started successfully")
    """Speak a word or phrase"""
    def say(self, phrase):
        self.speaker.Speak(phrase)


"""The callback class that handles the events raised by the speech object.
    See "Automation | SpSharedRecoContext (Events)" in the MS Speech SDK
    online help for documentation of the other events supported. """
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    """Called when a word/phrase is successfully recognized  -
        ie it is found in a currently open grammar with a sufficiently high
        confidence"""
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print "You said: ",newResult.PhraseInfo.GetText()
    
if __name__=='__main__':
    wordsToAdd = [ "One", "Two", "Three", "Four" ]
    speechReco = SpeechRecognition(wordsToAdd)
    while 1:
        pythoncom.PumpWaitingMessages()

Discussion:

Python is a natural choice for a speech recognition control application, since it's very easy to support user scripting.

I have a simple voice recognition application based on the above code, that sits in the system tray and runs short chunks of Python script via exec when it recognizes a word. I've found the Windows Scripting Host particularly useful, particularly the SendKeys method: eg

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys({PGUP})

mapped to saying "Page up", and so on. (The full code for the GUI version is on my web page at http://inigo.0catch.com - it uses wxWindows)



Add comment

Number of comments: 4

Program runs, but aborts?, Dirk Krause, 2001/12/28
Isn't there a loop missing? The program starts, a voice says 'Started successfully' and then the program ends.
Add comment

Now fixed, Inigo Surguy, 2002/02/10
Yes, you're right. It worked fine running under PythonWin, but needed a message loop running from the command line. I've now added it.
Add comment

wxPython Version, Andre Honsberg, 2008/01/03
Can you please tell what version of wxPython you use because the one I installed keeps giving me errors in the time conversion. Maybe I have the wrong wxPython version or am using a newer one. Thanks in Advance Andre
Add comment

Program aborts, George LeCompte, 2008/03/16
Downloaded code and Microsoft Speech SDK 5.1 to same file. Atempted to run code.
Traceback (most recent call last):
File "L:\SpeechRecognition\Speech.py", line 55, in class
ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
TypeError: Error when calling the metaclass bases
cannot create 'NoneType' instances
>>>
I'm using Python 2.5.1 on Windows XP system. Any thoughts on speech recognition?
Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.