在PocketSphinx中使用不同的单词运行不同的命令

时间:2016-06-29 04:33:00

标签: raspberry-pi cmusphinx pocketsphinx

我已经找到了使用多个关键字激活pocketsphinx的方法,但我想根据所说的关键字运行不同的命令。当我说“Alexa”时,我已经将它连接到亚马逊的Alexa服务器,现在我想在我说“TV Off”和“TV On”时添加一个命令。

1 个答案:

答案 0 :(得分:0)

最好的方法是使用python,如下所示:

import sys, os
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *
import pyaudio

modeldir = "../../../model"

# Create a decoder with certain model
config = Decoder.default_config()
config.set_string('-hmm', os.path.join(modeldir, 'en-us/en-us'))
config.set_string('-dict', os.path.join(modeldir, 'en-us/cmudict-en-us.dict'))
config.set_string('-kws', 'keyword.list')

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

# Process audio chunk by chunk. On keyword detected perform action and restart search
decoder = Decoder(config)
decoder.start_utt()
while True:
    buf = stream.read(1024)
    if buf:
         decoder.process_raw(buf, False, False)
    else:
         break
    if decoder.hyp() == "tv on":
        print ("Detected keyword tv on, turning on tv")
        os.system('beep')
        decoder.end_utt()
        decoder.start_utt()
    if decoder.hyp() == "tv off":
        print ("Detected keyword tv off, turning off tv")
        os.system('beep beep')
        decoder.end_utt()
        decoder.start_utt()