Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python detect keywords

I'm doing and application that do the fallowing:

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:I have to detect some words on it. There are only, 5 to 10 words to detect.

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?

Thanks.

like image 322
Carlos Porta Avatar asked Feb 06 '16 21:02

Carlos Porta


1 Answers

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/

The numbers are thresholds for detection

like image 105
Nikolay Shmyrev Avatar answered Oct 08 '22 10:10

Nikolay Shmyrev