如何使用Python模块Dragonfly识别语音?
我一直在尝试弄明白怎么使用Dragonfly模块。我看过它的说明文档,但就是搞不清楚怎么用。我只是想识别几个短语,然后根据这些短语做一些操作。
3 个回答
1
我觉得在这个文档中给出的使用示例非常简单,而且很容易理解:
一个非常简单的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.
3
首先,如果你是在使用Linux系统,你需要知道Dragonfly只支持Windows语音识别或者Dragon NaturallySpeaking加上Natlink。虽然可以通过虚拟机和Aenea在Linux上运行,但这不在我们讨论的范围内。
如果你是在用WSR(Windows语音识别),那么只需要确保Dragonfly在你的Python路径中,然后在你的主脚本最后加上以下代码:
while True:
pythoncom.PumpWaitingMessages()
time.sleep(0.1)
如果你是在用Dragon NaturallySpeaking,先点击上面的链接去Natlink网站,按照那里的说明安装和激活Natlink,然后再尝试使用Dragonfly。安装时可以选择所有默认选项。安装完成后,你可以把Dragonfly的脚本放到C:\NatLink\NatLink\MacroSystem文件夹里,这样在你启动Dragon NaturallySpeaking时,它们就会自动激活。
5
没错,这个例子会结束。我见过这个例子很多次,但它缺少了一些重要的功能。
首先,pythoncom没有被导入。这个库提供了程序的主循环。
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.
while True:
pythoncom.PumpWaitingMessages()
sleep(.1)