Python语音识别“module”对象没有属性“VARIANT”

2024-04-24 04:41:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在python中开始使用语音识别。首先,我使用以下代码尝试PySpeech(https://code.google.com/p/pyspeech/):

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

得到了以下回溯:

^{pr2}$

然后,我按照GoogleCode页面顶部的建议为PySpeech尝试了dragonfly,使用了以下在dragonfly文档中常见的示例代码:

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.

得到了非常相似的回溯:

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'

这两个模块都是用PIP安装的,并使用python2.7解释器运行。对于我来说,这似乎是一个python版本的问题,因为实现相同功能的两个不同模块都会抛出相同的错误,但我很难继续。在

非常感谢任何帮助,我很乐意提供更多的代码/信息。谢谢!在

编辑1:对于遇到类似问题的人,如果碰巧看到了这篇文章,可以尝试使用https://pypi.python.org/pypi/SpeechRecognition/作为py2.7的替代方案。如果它运行正常,但行为不一致或无限循环,请尝试在init.py中修改recognizer类的init方法,大约在第100行。能量阈值对我来说需要一些修改(100->;300),这可能是由于你的麦克风设置的具体情况。我还增加了我的安静时间(0.5->0.7),因为它有时会切断我的联系。在这些更改之后,它对我来说运行得相当好,在捕获结束后的2秒内返回非常准确的输入语音文本。在


Tags: the代码inpyselflineloadrule