pocketsphinx - 如何从关键字识别切换到语法模式

pocketsphinx - how to switch from keyword spotting to grammar mode

我将 pocketsphinx 与 raspberry pi 一起用于家庭自动化。我用支持的命令编写了一个简单的 JSGF 语法文件。现在,我想在命令之前使用诸如 "hey computer" 之类的激活短语,以避免错误检测,并且仅在说出激活短语后才执行语音识别。

如果我没记错的话,pocketsphinx 支持两种语音识别模式:关键字识别模式和语言模型/JSGF 语法模式。

pocketsphinx FAQ中解决如何拒绝不符合语法的词的问题时,它说:

If you want to recognize several commands, you can use keyword spotting mode or keyword activation mode combined with the switch to grammar to perform actual operation.

我的问题是,这个 "switching" 从关键字识别模式到语法模式究竟是如何实现的? (我应该怎么做才能实现它?)。与此相关,"keyword spotting mode" 和 "keyword activation mode" 之间有什么区别?

谢谢!

引自tutorial:

开发者可以配置多个具有不同语法和语言模型的“搜索”对象,并在运行时切换它们,为用户提供交互体验。

有多种可能的搜索模式:

  • keyword - 有效地查找关键词并忽略其他语音。 允许配置检测阈值
  • 语法 - 识别语音 根据 JSGF 语法。不像关键短语语法搜索不 忽略不在语法中但试图识别它们的词。
  • ngram/lm - 使用语言模型识别自然语音。
  • allphone - 使用语音语言模型识别音素。

每个搜索都有一个名称,可以通过名称引用,名称是特定于应用程序的。函数 ps_set_search 允许激活之前通过名称添加的搜索。

要添加搜索,需要指向描述搜索的 grammar/language 模型。语法的位置是特定于应用程序的。如果只需要简单的识别,添加一个搜索就足够了,或者只用配置选项配置所需的模式。

搜索的具体设计取决于您的应用程序。例如,您可能希望先监听激活关键字,一旦识别出关键字,就切换到 ngram 搜索以识别实际命令。识别命令后,您可以切换到语法搜索以识别确认,然后切换回关键字侦听模式以等待另一个命令。

在 Python 中切换搜索的代码如下所示:

# Init decoder
config = Decoder.default_config()
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
config.set_string('-dict', path.join(MODELDIR, 'en-us/cmudict-en-us.dict'))
decoder = Decoder(config)

# Add searches
decoder.set_kws('keyword', 'keyword.list')
decoder.set_lm_file('lm', 'query.lm')
decoder.set_search('keyword')

import pyaudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
stream.start_stream()

in_speech_bf = False
decoder.start_utt()
while True:
    buf = stream.read(1024)
    if buf:
        decoder.process_raw(buf, False, False)
        if decoder.get_in_speech() != in_speech_bf:
            in_speech_bf = decoder.get_in_speech()
            if not in_speech_bf:
                decoder.end_utt()

                # Print hypothesis and switch search to another mode
                print 'Result:', decoder.hyp().hypstr

                if decoder.get_search() == 'keyword':
                     decoder.set_search('lm')
                else:
                     decoder.set_search('keyword')

                decoder.start_utt()
    else:
        break
decoder.end_utt()