code and the oracular

Pocketsphinx Voice Recognition with Python

with 5 comments

I downloaded pocketsphinx and the corresponding python module with:

sudo apt-get install python-pocketsphinx pocketsphinx-hmm-wsj1 pocketsphinx-lm-wsj

and then downloaded Pyaudio from

Pocketsphinx needs a 16 bit mono wav file at a bitrate of 16k, as you can see I set this in the code.

This code lets you record a bit of speech and then it reads it back to you, just to test the idea. It could be the beginning of a speech recognition system of great usefulness but for me the fidelity wasn’t that good. If you want to train Sphinx to your voice this means creating your own acoustic model which takes some time and is detailed here: I may do this later. The real dream is to have a talking chatbot.

import sys,os
import pyaudio
import wave

hmdir = "/usr/share/pocketsphinx/model/hmm/wsj1"
lmd   = "/usr/share/pocketsphinx/model/lm/wsj/"
dictd = "/usr/share/pocketsphinx/model/lm/wsj/wlist5o.dic"

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

    import pocketsphinx as ps
    import sphinxbase

    speechRec = ps.Decoder(hmm = hmmd, lm = lmdir, dict = dictp)
    wavFile = file(wavfile,'rb')
    result = speechRec.get_hyp()

    return result[0]

CHUNK = 1024
FORMAT = pyaudio.paInt16
RATE = 16000

for x in range(10):
    fn = "o"+str(x)+".wav"
    p = pyaudio.PyAudio()
    stream =, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
    print("* recording")
    frames = []
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data =
    print("* done recording")
    wf =, 'wb')
    wavfile = fn
    recognised = decodeSpeech(hmdir,lmd,dictd,wavfile)
    print recognised
    cm = 'espeak "'+recognised+'"'

for a screencast example of using pshinx for a talking chatbot see here


Written by Luke Dunn

June 6, 2013 at 9:12 am

5 Responses

Subscribe to comments with RSS.

  1. How was the output ? you should consider putting in a video of the output ! 🙂

    Arkapravo Bhaumik

    June 11, 2013 at 12:30 pm

    • this is a real starter only because anyone who pastes this code may find they want to tweak it a lot before it performs well and frankly I hope someone will, because I was disappointed by the fidelity. certainly a lot worse than the dictate app on my android phone.

      Maybe my first step would be a better mike too, but I wasn’t sure I was missing something since PyAudio didn’t want to set sample rate only bit rate and there may be something hooky going on between those two.

      Trip Technician

      October 10, 2013 at 1:49 pm

  2. Thank you very much for the code – I was looking a long time to find something like that.

    Unfortunately, Python shows me an invalid syntax error in line 8 “import pocketsphinx as ps” and I don’t understand why. I hope you can help me.

    J. Knopp

    August 3, 2014 at 8:54 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: