Python speech recognition 'module' object has no attribute 'VARIANT'

1.7k Views Asked by At

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.

0

There are 0 best solutions below