Python检测关键字 [英] Python detect keywords

查看:163
本文介绍了Python检测关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做和休闲的应用程序:

I'm doing and application that do the fallowing:

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

1:If some noise is detected by the microphone, its starts to record audio, until no noise is detected. After it, the audio is recorded to a wav file.

2:我必须检测一些单词.只能检测5到10个字.

2:I have to detect some words on it. There are only, 5 to 10 words to detect.

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

So far, my code only does the first part (detect noise and record audio). Now, I have a list with the following words: help, please, yes, no, could, you, after, tomorrow. I need an offline way to detect if my sound contains these words. Is this possible? How can I do that? I'm using linux and there is no way to change my operational system to windows or use virtual machine.

我正在考虑使用声音的频谱图,创建火车数据库,并使用一些分类器进行预测.例如,是一个单词的声谱图.这是一个好技巧吗?

I'm thinking to use the sound's spectrogram, create a train database and use some classifier to predict. For example, this is a spectrogram of a word. Is this a good technique to use?

谢谢.

推荐答案

您可以使用python中的pocketsphinx,并通过pip install pocketsphinx安装.代码如下:

You can use pocketsphinx from python, install with pip install pocketsphinx. Code looks like this:

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

关键字列表应如下所示:

The list of keywords should look like this:

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

数字是检测阈值

这篇关于Python检测关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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