Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live recognition with Python and Pocketsphinx

I have recently been working with pocket sphinx in python. I have successfully got the example below to work recognising a recorded wav.

#!/usr/bin/env python

import sys,os



def decodeSpeech(hmmd,lmdir,dictp,wavfile):

    """

    Decodes a speech file

    """

    try:

        import pocketsphinx as ps

        import sphinxbase

    except:

        print """Pocket sphinx and sphixbase is not installed

        in your system. Please install it with package manager.

        """

    speechRec = ps.Decoder(hmm = hmmd, lm = lmdir, dict = dictp)

    wavFile = file(wavfile,'rb')

    wavFile.seek(44)

    speechRec.decode_raw(wavFile)

    result = speechRec.get_hyp()



    return result[0]



if __name__ == "__main__":

    hmdir = "/home/jaganadhg/Desktop/Docs_New/kgisl/model/hmm/wsj1"

    lmd = "/home/jaganadhg/Desktop/Docs_New/kgisl/model/lm/wsj/wlist5o.3e-7.vp.tg.lm.DMP"

    dictd = "/home/jaganadhg/Desktop/Docs_New/kgisl/model/lm/wsj/wlist5o.dic"

    wavfile = "/home/jaganadhg/Desktop/Docs_New/kgisl/sa1.wav"

    recognised = decodeSpeech(hmdir,lmd,dictd,wavfile)

    print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"

    print recognised

    print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"

The problem is how can I do real time speech recognition from a microphone? In a while loop with a if statement so that if a set word is recognised from the microphone a function can be called?

like image 480
Tonderai Ratisai Avatar asked Jan 13 '13 20:01

Tonderai Ratisai


People also ask

What is Pocketsphinx in Python?

Pocketsphinx Python. Pocketsphinx is a part of the CMU Sphinx Open Source Toolkit For Speech Recognition. This package provides a python interface to CMU Sphinxbase and Pocketsphinx libraries created with SWIG and Setuptools.

What is Pocketsphinx used for?

The Pocketsphinx API is designed to ease the use of speech recognizer functionality in your applications: It is very likely to remain stable both in terms of source and binary compatibility, due to the use of abstract types. It is fully re-entrant, so there is no problem having multiple decoders in the same process.

How do you use the CMU Sphinx speech recognition in Python?

Make sure you have the latest version of pip, setuptools, and wheel. If not, then write the following commands one by one and hit enter. Once you have installed pocketsphinx on your machine, you are a step closer to Speech recognition without internet connection.

Is Python speech recognition offline?

For performing Speech Recognition, there is SpeechRecognition library which is open source and the best thing is that several engines and APIs provide in both modes online and offline mode. For Speech Recognition is in python.


1 Answers

The code for realtime recognition looks like this:

config = Decoder.default_config()
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
config.set_string('-lm', path.join(MODELDIR, 'en-us/en-us.lm.bin'))
config.set_string('-dict', path.join(MODELDIR, 'en-us/cmudict-en-us.dict'))
config.set_string('-logfn', '/dev/null')
decoder = Decoder(config)

import pyaudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
stream.start_stream()

in_speech_bf = False
decoder.start_utt()
while True:
    buf = stream.read(1024)
    if buf:
        decoder.process_raw(buf, False, False)
        if decoder.get_in_speech() != in_speech_bf:
            in_speech_bf = decoder.get_in_speech()
            if not in_speech_bf:
                decoder.end_utt()
                print 'Result:', decoder.hyp().hypstr
                decoder.start_utt()
    else:
        break
decoder.end_utt()

You can also use gstreamer python bindings in pocketsphinx, check livedemo.py

like image 91
Nikolay Shmyrev Avatar answered Sep 29 '22 02:09

Nikolay Shmyrev