Python检测关键字

2024-04-23

我正在做的应用程序执行以下操作:

1:如果麦克风检测到一些噪音,它就会开始录制音频,直到检测不到噪音。之后,音频被录制到 wav 文件中。

2:我必须检测上面的一些文字。只有 5 到 10 个单词需要检测。

到目前为止,我的代码只执行第一部分(检测噪声并记录音频)。现在,我有一个包含以下单词的列表:help, please, yes, no, could, you, after, tomorrow。我需要一种离线方式来检测我的声音是否包含这些单词。这可能吗?我怎样才能做到这一点?我使用的是linux,无法将我的操作系统更改为windows或使用虚拟机。

我正在考虑使用声音的频谱图,创建一个训练数据库并使用一些分类器来预测。例如,this http://www.zimagez.com/zimage/capturadetela2016-02-0713-15-26.php是一个单词的频谱图。这是一个很好的技术吗?

Thanks.


您可以使用 python 中的 pocketsphinx,安装pip install pocketsphinx。代码如下所示:

import sys, os
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *


modeldir = "../../../model"
datadir = "../../../test/data"

# 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', 'command.list')


# Open file to read the data
stream = open(os.path.join(datadir, "goforward.raw"), "rb")

# Alternatively you can read from microphone
# import pyaudio
# 
# 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() != None:
        print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
        print ("Detected keyword, restarting search")
        decoder.end_utt()
        decoder.start_utt()

关键字列表应如下所示:

  forward /1e-1/
  down /1e-1/
  other phrase /1e-20/

这些数字是检测阈值

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python检测关键字 的相关文章

随机推荐