Speech Recognition in Python with CMU PocketSphinx (Ubuntu Box)

This is a crisp post on playing with the Open Source Speech Recognition Toolkit – PocketSphinx by Carnegie Mellon University.

We need to install the sphinx packages before we can proceed with it:

  • python-pocketsphinx
  • pocketsphinx-hmm-wsj1
  • pocketsphinx-lm-wsj

The packages can be installed from Ubuntu Software center or via apt-get.

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

Once you are done we can move on with out python script for speech recognition.

Have a voice recorded WAV file in the directory where you are running the python script. You can record voice by your fav voice recorder software.

Script: pythonPocketSphinxTest.py

#!/usr/bin/ python

import sys
import pocketsphinx

if __name__ == "__main__":

   hmdir = "/usr/share/pocketsphinx/model/hmm/wsj1"
   lmdir = "/usr/share/pocketsphinx/model/lm/wsj/wlist5o.3e-7.vp.tg.lm.DMP"
   dictd = "/usr/share/pocketsphinx/model/lm/wsj/wlist5o.dic"
   wavfile = sys.argv[1]

   speechRec = pocketsphinx.Decoder(hmm = hmdir, lm = lmdir, dict = dictd)
   wavFile = file(wavfile,'rb')
   speechRec.decode_raw(wavFile)
   result = speechRec.get_hyp()

   print result

Execute:

$ python pythonPocketSphinxTest.py sndClip.wav

Note: sndClip.wav is my voice recorded file, and lies in the same folder as the script pythonPocketSphinxTest.

Output:

 'LITTLE WHO FALLS MORE IN THE IN THE FULL AND WHO IS NEWS UNLIKE TO THE USE THAN THIS SO ALL HER WHOSE'

Yash Sharma is a Big Data & Machine Learning Engineer, A newbie OpenSource contributor, Plays guitar and enjoys teaching as part time hobby.
Talk to Yash about Distributed Systems and Data platform designs.

3 thoughts on “Speech Recognition in Python with CMU PocketSphinx (Ubuntu Box)

  1. This code is working well for files which are less than 30 secs.
    Is there a way where i can accomplish the task for files which are more than
    30 seconds?

    Thanks in advance

  2. When i installed pocketsphinx-hmm-wsj1 and pocketsphinx-lm-wsj, the error message comes like
    E: Unable to locate package pocketsphinx-hmm-wsj1
    E: Unable to locate package pocketsphinx-lm-wsj

    Can anybody give the solution??

Leave a Reply

Your email address will not be published. Required fields are marked *