Python检测关键字

2024-04-27 07:27:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做和应用程序做休耕:

1:如果麦克风检测到一些噪声,它将开始录制音频,直到没有检测到噪声为止。之后,音频被记录到wav文件中。

2:我必须在上面检测一些单词。只有,5到10个单词可以检测。

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

我正在考虑使用声音的频谱图,创建一个训练数据库,并使用一些分类器来预测。例如,this是一个单词的谱图。这是个好的技巧吗?

谢谢。


Tags: 文件代码应用程序声音列表记录help音频
1条回答
网友
1楼 · 发布于 2024-04-27 07:27:57

您可以使用python中的pocketspinx,使用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/

数字是检测的阈值

相关问题 更多 >