Executing different commands with different words in PocketSphinx

I have found ways to activate pocketsphinx using multiple keywords, but I want to run different commands depending on which keyword was said. I already connected it to the Amazon Alexa server when it said "Alexa" and now I want to add a command when I say "TV Off" and "TV On".

0


source to share


1 answer


Your best bet is to use python, something like this:



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()

      

0


source







All Articles