我正在尝试在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秒内返回非常准确的输入语音文本。