3

After pocketsphinx recognizes a certain keyword or phrase, how do i make it trigger an action?

For example, I want to open a simple .py script from pocketsphinx

1 Answers1

1

Update: in 2020 we recommend to try our new software library called Vosk. It provides much better accuracy than pocketsphinx.

You need to write python code, like this:

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

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', 'command.list')

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True)
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)

    decoder.process_raw(buf, False, False)

    if decoder.hyp() != None:
        print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
        print ("Detected keyword, restarting search")
        #
        # Here you run the code you want based on keyword
        #
        decoder.end_utt()
        decoder.start_utt()

Keyword list should look like this

forward /1e-1/
down /1e-1/
other phrase /1e-20/

The numbers are thresholds for command activation, you can read more about them in tutorial.