Python Pocketsphinx:使用解码器类时无法识别关键字 [英] Python Pocketsphinx: Keyword isn't recognised when using Decoder class

查看:30
本文介绍了Python Pocketsphinx:使用解码器类时无法识别关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Pocketsphinx 从 .wav 文件中检测关键字,特别是使用解码器类.当我给它 this .wav 文件并打印它检测到的内容时,它甚至没有关闭.代码如下:

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

它打印:

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

有人知道这是为什么吗?

Does anyone know why this is?

推荐答案

感谢 Nikolay Shmyrev!

Credit goes to 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()

这篇关于Python Pocketsphinx:使用解码器类时无法识别关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆