0

I'm trying to get started using speech recognition in python. First I tried PySpeech (https://code.google.com/p/pyspeech/) using this code:

def listen():
    while 1:
        said = speech.input()
        print said
        if said == "off":
            break

and got the following traceback:

Traceback (most recent call last):
  File "C:/Users/REDACTED/Documents/Python Projects/listen.py", line 59, in <module> useful.listen()

  File "C:/Users/REDACTED/Documents/Python Projects/listen.py", line 48, in listen 
said = speech.input()

  File "C:\Python27\lib\site-packages\speech.py", line 162, in input
    listener = listenforanything(response)

  File "C:\Python27\lib\site-packages\speech.py", line 193, in listenforanything
    return _startlistening(None, callback)

  File "C:\Python27\lib\site-packages\speech.py", line 223, in _startlistening
    grammar = context.CreateGrammar()

  File "C:\Users\REDACTED\AppData\Local\Temp\gen_py\2.7\C866CA3A-32F7-11D2-9602-00C04F8EE628x0x5x4.py", line 2298, in CreateGrammar
ret = self._oleobj_.InvokeTypes(14, LCID, 1, (9, 0), ((12, 49),),GrammarId

AttributeError: 'module' object has no attribute 'VARIANT'

Then I tried dragonfly per the suggestion at the top of the GoogleCode page for PySpeech using the following example code commonly found on dragonfly docs:

from dragonfly.all import Grammar, CompoundRule

# Voice command rule combining spoken form and recognition processing.
class ExampleRule(CompoundRule):
   spec = "do something computer"                  # Spoken form of command.
   def _process_recognition(self, node, extras):   # Callback when command is spoken.
       print "Voice command spoken."

# Create a grammar which contains and loads the command rule.
grammar = Grammar("example grammar")                # Create a grammar to contain the command        rule.
grammar.add_rule(ExampleRule())                     # Add the command rule to the grammar.
grammar.load()                                      # Load the grammar.

and got this very similar traceback:

Traceback (most recent call last):

  File "C:/Users/REDACTED/Documents/Python Projects/listen.py", line 14, in <module>
    grammar.load()                                      # Load the grammar.

  File "C:\Python27\lib\site-packages\dragonfly\grammar\grammar_base.py", line 302, in load
    self._engine.load_grammar(self)

  File "C:\Python27\lib\site-packages\dragonfly\engines\engine_sapi5.py", line 79, in load_grammar
    handle = self._compiler.compile_grammar(grammar, context)

  File "C:\Python27\lib\site-packages\dragonfly\engines\compiler_sapi5.py", line 68, in compile_grammar
    grammar_handle = context.CreateGrammar()

  File "C:\Users\REDACTED\AppData\Local\Temp\gen_py\2.7\C866CA3A-32F7-11D2-9602-00C04F8EE628x0x5x4.py", line 2298, in CreateGrammar
    ret = self._oleobj_.InvokeTypes(14, LCID, 1, (9, 0), ((12, 49),),GrammarId

AttributeError: 'module' object has no attribute 'VARIANT'

Both modules were installed using PIP and run using python 2.7 interpreter. It seems like a python version issue to me given that the same error is thrown for two different modules implementing the same thing but I'm pretty stuck on how to proceed.

Any help is greatly appreciated and I'm happy to provide more code/info as its requested. Thanks!

EDIT 1: For anyone experiencing a similar problem who happens to stumble across this post, try https://pypi.python.org/pypi/SpeechRecognition/ as an alternative on py2.7. If it runs without error but behaves inconsistently or infinite loops, try modifying the init method of the recognizer class in init.py around line 100. The energy threshold required some tinkering for me (100->300) which is likely due to the specifics of your mic setup. I also increased my quiet duration (0.5->0.7) because it would cut me off sometimes. After these changes it works fairly well for me, returning very accurate text of the input speech in ~2 seconds after capture ends.

Kara
  • 6,115
  • 16
  • 50
  • 57
Forkstealer
  • 21
  • 1
  • 4
  • it seems to support python 2.4 and 2.5 – Padraic Cunningham Oct 19 '14 at 18:52
  • @PadraicCunningham Do you know of any speech recognition frameworks for python that support 2.7? I'd prefer to stick with 1 version of python on this machine for simplicity's sake but I'm open to downloading 2.5 if that seems to be the only way to make this work. – Forkstealer Oct 19 '14 at 19:01
  • I have not tried using it but on pypi the last upload was 2009 and it states that you need win xp or vista and python 2.4 or 2.5 so I imagine that is the issue. A quick google turned up this https://github.com/t4ngo/dragonfly it seems to be an active project – Padraic Cunningham Oct 19 '14 at 19:13
  • Dragonfly was the 2nd module referenced in OP. It threw similar error. I've since found https://pypi.python.org/pypi/SpeechRecognition/ so I'm trying to get that to work with PyAudio. I'll repoort back with my findings. Thanks for your help! – Forkstealer Oct 19 '14 at 19:20
  • pypi.python.org/pypi/SpeechRecognition does not throw an error with py2.7 but is slow and unreliable. I may dig into the source (its only 275 lines o_O) later today and try to fix it – Forkstealer Oct 19 '14 at 20:06
  • I saw that but was not sure if that was suitable. Well at least it works ;) – Padraic Cunningham Oct 19 '14 at 20:09

0 Answers0