pythonraspberry-pispeech-recognitionvoice-recognitionpocketsphinx

Python Pocketsphinx: Keyword isn't recognised when using Decoder class


I'm trying to detect a keyword from a .wav file using Pocketsphinx, specifically with the decoder class. When I give it this .wav file and print what it detects it isnt even close. Here is the code:

import pocketsphinx as ps
import requests
import json
import sys, os
import subprocess

model_path = ps.get_model_path()
data_path = ps.get_data_path()

print("start")
print(os.getcwd())
subprocess.call("sox -V4 /home/miro/client_audio.wav -r 16000 -c 1 client_audio.wav", shell=True)

config = ps.Decoder.default_config()
config.set_string('-kws', 'keyphrase.list')
config.set_string('-hmm', os.path.join(model_path, 'en-us'))
config.set_string('-lm', os.path.join(model_path, 'en-us.lm.bin'))
config.set_string('-dict', os.path.join(model_path, 'cmudict-en-us.dict'))

stream = open("client_audio.wav", "rb")

decoder = ps.Decoder(config)
decoder.start_utt()
while True:
    buf = stream.read(1024)
    if buf:
         decoder.process_raw(buf, False, False)
    else:
         break
    if decoder.hyp() != None:
        # print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
        words=[]
        [words.append(seg.word) for seg in decoder.seg()]
        print(words)
        decoder.end_utt()
        decoder.start_utt()

It prints this:

['<s>', "it's"]

Does anyone know why this is?


Solution

  • Credit goes to Nikolay Shmyrev!

    The correct code is:

    import pocketsphinx as ps
    import requests
    import json
    import sys, os
    import subprocess
    
    model_path = ps.get_model_path()
    data_path = ps.get_data_path()
    
    print("start")
    print(os.getcwd())
    subprocess.call("sox -V4 /home/miro/client_audio.wav -r 16000 -c 1 client_audio.wav", shell=True)
    
    config = ps.Decoder.default_config()
    config.set_string('-kws', 'keyphrase.list')
    config.set_string('-hmm', os.path.join(model_path, 'en-us'))
    config.set_string('-dict', os.path.join(model_path, 'cmudict-en-us.dict'))
    
    stream = open("client_audio.wav", "rb")
    
    decoder = ps.Decoder(config)
    decoder.start_utt()
    while True:
        buf = stream.read(1024)
        if buf:
             decoder.process_raw(buf, False, False)
        else:
             break
        if decoder.hyp() != None:
            # print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
            words=[]
            [words.append(seg.word) for seg in decoder.seg()]
            print(words)
            decoder.end_utt()
            decoder.start_utt()