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

半世苍凉 提交于 2020-07-10 03:11:10

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/62572429/python-pocketsphinx-keyword-isnt-recognised-when-using-decoder-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!