我正在尝试开始在 python 中使用语音识别。首先,我使用以下代码尝试了 PySpeech ( https://code.google.com/p/pyspeech/ ):
def listen():
while 1:
said = speech.input()
print said
if said == "off":
break
并得到以下回溯:
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'
然后我按照 PySpeech 的 GoogleCode 页面顶部的建议尝试了蜻蜓,使用蜻蜓文档上常见的以下示例代码:
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 安装并使用 python 2.7 解释器运行。对我来说,这似乎是一个 python 版本问题,因为两个不同的模块会抛出相同的错误来实现相同的东西,但我很困惑如何继续。
非常感谢任何帮助,我很乐意根据要求提供更多代码/信息。谢谢!
编辑 1:对于碰巧偶然发现这篇文章的任何遇到类似问题的人,请尝试https://pypi.python.org/pypi/SpeechRecognition/作为 py2.7 的替代方案。如果它运行没有错误但行为不一致或无限循环,请尝试在第 100 行附近修改init .py中识别器类的 init 方法。能量阈值需要对我进行一些修改(100-> 300),这可能是由于具体情况你的麦克风设置。我还增加了我的安静时间(0.5->0.7),因为它有时会打断我。在这些更改之后,它对我来说效果很好,在捕获结束后约 2 秒内返回输入语音的非常准确的文本。