|
|
 |
|
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:
|
|
|
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):
self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
self.context = self.listener.CreateRecoContext()
self.grammar = self.context.CreateGrammar()
self.grammar.DictationSetState(0)
self.wordsRule = self.grammar.Rules.Add("wordsRule",
constants.SRATopLevel + constants.SRADynamic, 0)
self.wordsRule.Clear()
[ self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd ]
self.grammar.Rules.Commit()
self.grammar.CmdSetRuleState("wordsRule", 1)
self.grammar.Rules.Commit()
self.eventHandler = ContextEvents(self.context)
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
|
|
|
|
|
 |
|