Python Pocketsphinx:使用Decoder类时无法识别关键字

时间:2020-06-25 09:43:32

标签: python raspberry-pi speech-recognition voice-recognition pocketsphinx

我正在尝试使用Pocketsphinx,尤其是与解码器类,从.wav文件中检测关键字。当我给它this .wav文件并打印它检测到的文件时,它甚至没有关闭。这是代码:

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

它打印此:

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

有人知道这是为什么吗?

1 个答案:

答案 0 :(得分:0)

信用归Nikolay Shmyrev!

正确的代码是:

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