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

  • What have you got so far? What have you tried?
    – CL22
    Oct 25 '16 at 4:37
  • I just finished installing pocketsphinx and was able to recognize some words, what do i need to do from there to trigger commands? Im new to python programming thanks Oct 25 '16 at 9:03

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)

# Process audio chunk by chunk. On keyword detected perform action and restart search
decoder = Decoder(config)
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

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.

  • thanks for your answer, I was wondering if this would be suitable if I plan to have numerous keywords with a corresponding command(around 40+ words) Oct 26 '16 at 2:15
  • If you want to have more intelligent assistant it is better to have a single activation keyphrase like "ok google" so it can listen continuously. Once activation keyword is recognized you switch to language model mode for command recognition, execute command and switch back after some itme. Oct 26 '16 at 8:38
  • Check this answer stackoverflow.com/questions/39069614/… for details Oct 26 '16 at 8:39
  • 1
    nikolay, thanks for the tips. the keyword spotting feature is working and it can recognize multiple keywords now. But, I have problems with input overflow in pyaudio Traceback (most recent call last): File "/home/pi/pythonsound/record.py", line 35, in <module> data = stream.read(CHUNK) File "/usr/local/lib/python2.7/dist-packages/pyaudio.py", line 605, in read return pa.read_stream(self._stream, num_frames) IOError: [Errno Input overflowed] -9981 Nov 17 '16 at 16:07
  • How do we add our own .dic and .lm ?
    – Petro
    Nov 12 '17 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.