speech.py
speech.py is a Python module that provides a clean interface to Windows's voice recognition and text-to-speech capabilities. It's very easy to use within a program that needs to listen for specific phrases or general speech, or that needs to speak.
It is available on PyPI at http://pypi.python.org/pypi/speech/ .
Questions? You can contact Michael Gundlach, the developer, at gundlach at gmail. Or, just file a bug under "Issues" above.
Example code
Here's a very simple program that repeats whatever it hears you say, until you say "turn off".
import speech
while True:
phrase = speech.input()
speech.say("You said %s" % phrase)
if phrase == "turn off":
breakspeech.input() in the above example blocks the program until it hears you. Below is the same example using non-blocking listeners, so that your program can do other things while waiting for input.
import speech
def response(phrase, listener):
speech.say("You said %s" % phrase)
if phrase == "turn off":
listener.stoplistening()
listener = speech.listenforanything(response)
# Your program can do whatever it wants now, and when a spoken phrase is heard,
# response() will be called on a separate thread.
import time
while listener.islistening():
time.sleep(1)
print "Still waiting..."More complex examples are available here.
Requirements:
Windows XP or Vista, and Python 2.4 or 2.5. If you use Windows Vista, you'll need to say "start listening" if Speech Recognition is not awake.
Installation:
Type easy_install speech at the Windows command prompt. (If that doesn't work, download and run this Python script to install easy_install, then try again from C:\<Your python directory>\Scripts\ .)
If you don't already have it, you'll also need pywin32 (for Python 2.5 or for Python 2.4); and if you're on XP, you'll need the Microsoft Speech kit (installer here).
See Also:
You may also find Christo Butcher's Dragonfly package useful. Dragonfly is a Python-based speech recognition framework which lets you easily implement complex voice commands, including dynamic elements and free-form dictation.
Thanks
To Inigo Surguy for his original code which helped me figure out COM and the Microsoft Speech SDK, and to Dekudude for suggesting the need for speech.input().