User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 430,118 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,306 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 697 | Replies: 14
Reply
Join Date: Jul 2008
Posts: 32
Reputation: Dekudude is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Dekudude Dekudude is offline Offline
Light Poster

Spoken (raw_) input?

  #1  
Jul 8th, 2008
Hi there! For starters, let me say I am something of a Python newbie at the moment, but I'm learning more every day! Unfortunately, my script is at a standstill, and I can't figure out how to continue. I don't have an error, per se, but rather the inability to create one.

You see, due to my semi-lazy nature, I am creating myself a computer assistant in Python. So far, it's going swell! I can have it open programs and files, respond to simple phrases, and stuff of that sort. It can even use the Microsoft Speech API to speak verbally.

Now, my problem--- I want to be able to speak as well! At the moment, I'm using raw_input() to state commands. ("Open Firefox, please! Thanks!") Now, that works just fine, but I want to be able to use the Microsoft Speech API to not only synthesize voice, but recognize speech input as well. I already have a default Speech Recognition profile, et cetera, but I have no way to implement it into Python! I just want a function that captures voice input like raw_input() does, but as of present, I have no idea how to do that.

http://surguy.net/articles/speechrecognition.xml

I've already seen the above example, but I haven't been able to modify it to suit my needs. I want to use the built-in Microsoft dictionary, not one I define by myself.

Any help would be greatly appreciated! Thanks a lot! Unfortunately, I don't have any code examples, because I haven't made any headway on voice recognition. Sorry, and thanks!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Spoken (raw_) input?

  #2  
Jul 9th, 2008
Search this forum for posts by Seagull One. She has gotten a lot of this code working.

Jeff
Reply With Quote  
Join Date: Jul 2008
Posts: 32
Reputation: Dekudude is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Dekudude Dekudude is offline Offline
Light Poster

Re: Spoken (raw_) input?

  #3  
Jul 9th, 2008
Thanks for your response:

Unfortunately, in my searches, I can't find what I'm looking for. I guess I could just use predefined words, if there is no way to access the Microsoft Dictionary, but now Inigo Surguy's code (which was working for me in the past...) doesn't work anymore.

  1. from win32com.client import constants
  2. import win32com.client
  3. import pythoncom
  4.  
  5. class SpeechRecognition:
  6. """ Initialize the speech recognition with the passed in list of words """
  7. def __init__(self, wordsToAdd):
  8. # For text-to-speech
  9. self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
  10. # For speech recognition - first create a listener
  11. self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
  12. # Then a recognition context
  13. self.context = self.listener.CreateRecoContext()
  14. # which has an associated grammar
  15. self.grammar = self.context.CreateGrammar()
  16. # Do not allow free word recognition - only command and control
  17. # recognizing the words in the grammar only
  18. self.grammar.DictationSetState(0)
  19. # Create a new rule for the grammar, that is top level (so it begins
  20. # a recognition) and dynamic (ie we can change it at runtime)
  21. self.wordsRule = self.grammar.Rules.Add("wordsRule",
  22. constants.SRATopLevel + constants.SRADynamic, 0)
  23. # Clear the rule (not necessary first time, but if we're changing it
  24. # dynamically then it's useful)
  25. self.wordsRule.Clear()
  26. # And go through the list of words, adding each to the rule
  27. [ self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd ]
  28. # Set the wordsRule to be active
  29. self.grammar.Rules.Commit()
  30. self.grammar.CmdSetRuleState("wordsRule", 1)
  31. # Commit the changes to the grammar
  32. self.grammar.Rules.Commit()
  33. # And add an event handler that's called back when recognition occurs
  34. self.eventHandler = ContextEvents(self.context)
  35. # Announce we've started
  36. self.say("Started successfully")
  37. def say(self, phrase):
  38. self.speaker.Speak(phrase)
  39.  
  40.  
  41. """The callback class that handles the events raised by the speech object.
  42. See "Automation | SpSharedRecoContext (Events)" in the MS Speech SDK
  43. online help for documentation of the other events supported. """
  44. class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
  45. """Called when a word/phrase is successfully recognized -
  46. ie it is found in a currently open grammar with a sufficiently high
  47. confidence"""
  48. def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
  49. newResult = win32com.client.Dispatch(Result)
  50. print "You said: ",newResult.PhraseInfo.GetText()
  51.  
  52. if __name__=='__main__':
  53. wordsToAdd = [ "One", "Two", "Three", "Four" ]
  54. speechReco = SpeechRecognition(wordsToAdd)
  55. while 1:
  56. pythoncom.PumpWaitingMessages()


I get the following error:

Traceback (most recent call last):
File "C:\Python25\Projects\SUSIE\VoiceRECO.py", line 54, in <module>
speechReco = SpeechRecognition(wordsToAdd)
File "C:\Python25\Projects\SUSIE\VoiceRECO.py", line 13, in __init__
self.context = self.listener.CreateRecoContext()
File "C:\Python25\lib\site-packages\win32com\gen_py\C866CA3A-32F7-11D2-9602-00C04F8EE628x0x5x0.py", line 2468, in CreateRecoContext
ret = self._oleobj_.InvokeTypes(10, LCID, 1, (9, 0), (),)
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147221164), None)

Do you have any idea what that means?
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Spoken (raw_) input?

  #4  
Jul 12th, 2008
No, I don't.
Reply With Quote  
Join Date: Jul 2008
Posts: 32
Reputation: Dekudude is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Dekudude Dekudude is offline Offline
Light Poster

Re: Spoken (raw_) input?

  #5  
Jul 13th, 2008
Do you know what Python module versions that script requires?
Reply With Quote  
Join Date: Aug 2008
Posts: 10
Reputation: gundlach is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
gundlach gundlach is offline Offline
Newbie Poster

Re: Spoken (raw_) input?

  #6  
Aug 7th, 2008
Hi,

I've written a simple speech synthesis and recognition module for Python that should help you out: speech.py . To install it, type
easy_install speech
in the C:\Program Files\Python25\Scripts directory. (If that doesn't work, save and run the script at http://peak.telecommunity.com/dist/ez_setup.py , then try it again.)

then try running the following code:

    
    import speech
    import time
    def do_stuff(phrase, listener):
       speech.say("You said: %s" % phrase)
    speech.listenforanything(do_stuff)
    while True:
        time.sleep(.1)

That accesses the Windows dictionary that you were talking about, so that whatever you say, Windows tries its best to hear you, and then runs do_stuff with the phrase that it heard. speech.say will speak your text back to you out loud. You can use speech.listenfor() instead if you have a specific set of phrases in mind.

Be aware that Windows speech recognition sucks for general dictation. You need to train it via the Speech Control Panel entry before it does even a half-good job. And an excellent microphone helps.

Good luck, and if you run into trouble visit http://pyspeech.googlecode.com) to report bugs or ask more questions! I *think* I'll see replies to this thread show up in email, but I'm new to this forum, so I'm not sure...

- Michael
Reply With Quote  
Join Date: Jul 2008
Posts: 32
Reputation: Dekudude is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Dekudude Dekudude is offline Offline
Light Poster

Re: Spoken (raw_) input?

  #7  
Aug 9th, 2008
I don't quite understand how to install it. What do you mean to type that in the directory? Is there a specific python script I have to type it in? You just gave me a folder name. O_o
Reply With Quote  
Join Date: Aug 2008
Posts: 10
Reputation: gundlach is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
gundlach gundlach is offline Offline
Newbie Poster

Re: Spoken (raw_) input?

  #8  
Aug 10th, 2008
Hi,

What I meant was to type that at the Windows Command Prompt. Click the Start Menu, then click Run, and then type "cmd". That gives you the command prompt.

Then change directories to get to the directory I mentioned: type
cd C:\Program Files\Python25\Scripts
or maybe
cd C:\Python25\Scripts
depending on where you installed Python.

Then in there, type
easy_install speech
and see what happens. If it says that easy_install isn't found, you have to install it, by saving the Python script at the link that I gave you, then running it.

On the other hand, if easy_install runs, then it will have installed speech for you, and you'll be able to type "import speech" in Python and it will work.

Hope this makes things clearer,
Michael
Reply With Quote  
Join Date: Jul 2008
Posts: 32
Reputation: Dekudude is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Dekudude Dekudude is offline Offline
Light Poster

Re: Spoken (raw_) input?

  #9  
Aug 11th, 2008
Okay... I managed to install it. However, I have a problem.

import speech
import time
def do_stuff(phrase, listener):
	speech.say("You said: %s" % phrase)
speech.listenforanything(do_stuff)

That's my code... but I get an error. I'm thinking it's something obvious (do_stuff isn't defined... I don't know how the function works...)

Is it possible to, like raw_input(), use the do_stuff function, and have it put what I said in a string variable?

EX:
I say "hi".
Code puts "hi" in a variable.

Thnaks for all of your help!
Reply With Quote  
Join Date: Aug 2008
Posts: 10
Reputation: gundlach is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
gundlach gundlach is offline Offline
Newbie Poster

Re: Spoken (raw_) input?

  #10  
Aug 11th, 2008
Originally Posted by Dekudude View Post
Is it possible to, like raw_input(), use the do_stuff function, and have it put what I said in a string variable?

EX:
I say "hi".
Code puts "hi" in a variable.

Thnaks for all of your help!

Hi,

Your suggestion of a raw_input() like function is fabulous! There wasn't one, and I've been working with the code too long to notice the need for something like that. I just added one and released a new version of speech.py. Thank you!

To get it, type
easy_install speech
as you did before -- this will suck down the latest version (0.5.0).

That makes simple programs much easier. Try typing this in and see if it works:

import speech

# Prints a prompt and returns whatever text it heard you say
answer = speech.input("How do you like your eggs?")
print "Oh, you like them %s" % answer

# Prints a prompt and only returns when it hears one of a few phrases
answer = speech.input("Are you there?", ["Yes", "No", "Shut up"])
print "You said: %s" % answer

# Both the prompt and the list of phrases are optional.
answer = speech.input()
print "You said: %s" % answer

answer = speech.input(phraselist=["Goodbye", "Hello"])
print "You said: %s" % answer

I've given you credit on the pyspeech homepage -- look at http://pyspeech.googlecode.com at the bottom. Thanks again and good luck!

Michael Gundlach
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 3:18 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC