Skip to content

Programming with pocketsphinx

Col·lectivaT edited this page Nov 22, 2018 · 9 revisions

In order to access the full capabilities of pocketsphinx toolkit we need to be able to access its internal functions, i.e. we need to use code. There are various wrappers for pocketsphinx and in these examples we will use the python one.

Python installation

Let's make sure that we have a modern version of python. Additionally to install python libraries we want to have pip, and we also would like to work with virtual environments, through virtualenv. This way we keep our local environment clean, and work in isolated environments for each python project.


Debian based Linux

sudo apt-get install python3, python-pip
sudo pip3 install --upgrade pip
sudo pip3 install virtualenv

Mac OS

brew install pip3
pip3 install virtualenv

Now check that everything is working as expected by doing:

virtualenv --python=python3 venv
source venv/bin/activate

To exit the virtual environment just type deactivate.

PocketSphinx in Python

Assuming that we already have pocketsphinx installed, now we want to install its python wrapper. For that we first need to install a couple of things more.


Debian based Linux

sudo apt-get install swig
sudo apt-get install libpulse-dev

Mac OS

brew install swig

swig is the main wrapper tool that connects pocketsphinxs C code to its python wrappers. Finally we are ready to install pocketsphinx for python. First activate the virtual environment via source venv/bin/activate and then:

pip install pocketsphinx

You can test your installation in the interactive shell by typing

python -c "import pocketsphinx"

If you did not get the error ImportError: No module named 'pocketsphinx' your installation was successful.

Examples

Now we can execute the script test_ps_tasks.py that includes many different tasks. Although most of them are executable from the command line tool, now we have access to more information such as the time stamps and match confidence.

python scripts/test_ps_tasks.py

Now open the script with you favorite text editor and start changing the files, keywords that you want to be searched and language models. You can do this by changing the contents of the config dictionary and then calling the pertinent method.