Skip to main content

Simplified Python API for recognising speech with CMU Pocket Sphinx

Project description

Docs Status

Simplified Python API for the CMU Pocket Sphinx speech recogniser

This package provides a simple API for recognising speech using CMU Pocket Sphinx, an open source, lightweight speech recognition engine. More information on CMU Pocket Sphinx, and other CMU speech recognition libraries, may be found at cmusphinx.github.io.

There are some usage examples in the repository’s examples folder demonstrating how to use this library to scan and process speech audio from a microphone. Each of these examples require the PyAudio package, which may be installed by running the following command:

pip install pyaudio

Installation & dependencies

To install this package via pip, run the following command:

pip install sphinxwrapper

If you are installing in order to develop sphinxwrapper, clone/download the repository, move to the root directory and run:

pip install -e .

Either of the above commands will also install version 0.1.15 of the required pocketsphinx-python package.

Usage example

The following example demonstrates how to use sphinxwrapper and PyAudio to scan and interpret audio from the microphone using the default language model and dictionary.

import os
import time

from pyaudio import PyAudio, paInt16

from sphinxwrapper import PocketSphinx, DefaultConfig

# Initialise a Pocket Sphinx decoder with the default configuration.
config = DefaultConfig()
config.set_string("-logfn", os.devnull)  # Suppress log output.
ps = PocketSphinx(config)

# Define decoder callback functions.
def speech_start_callback():
    print("Speech started.")

def hypothesis_callback(hyp):
    hypstr = hyp.hypstr if hyp else None
    print("Hypothesis: %r" % hypstr)

# Set decoder callback functions.
ps.speech_start_callback = speech_start_callback
ps.hypothesis_callback = hypothesis_callback

# Open an audio stream on the default input audio device.
p = PyAudio()
stream = p.open(format=paInt16, channels=1, rate=16000, input=True,
                frames_per_buffer=2048, input_device_index=None)
stream.start_stream()

# Recognise from the microphone in a loop until interrupted.
try:
    print("Listening... Press Ctrl+C to exit...")
    while True:
        ps.process_audio(stream.read(2048))
        time.sleep(0.1)
except KeyboardInterrupt:
    stream.stop_stream()
    p.terminate()

Python versions

This package has been written for Python 2.7 and above. It should work the same way for each supported version. Please file an issue if you encounter a problem specific to the Python version you’re using.

Documentation

The documentation for this project is written in reStructuredText and built using the Sphinx documentation engine.

Run the following commands in the repository folder to build it locally:

cd docs
pip install -r requirements.txt
make html

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinxwrapper-1.3.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

sphinxwrapper-1.3.0-py2.py3-none-any.whl (11.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file sphinxwrapper-1.3.0.tar.gz.

File metadata

  • Download URL: sphinxwrapper-1.3.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.1

File hashes

Hashes for sphinxwrapper-1.3.0.tar.gz
Algorithm Hash digest
SHA256 f9296e9e13bfd18b83f86db95afe2cc84f6fc51e75bb0c9c7a4a128065de554c
MD5 b1013c3797743465455ed35942192b4d
BLAKE2b-256 4b0565d062b3cf1fafd6a4f5cb52432873ff994e8601244c541494fe1715a3e3

See more details on using hashes here.

File details

Details for the file sphinxwrapper-1.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for sphinxwrapper-1.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 581cf92947e0bd126164a8172f04d7a15edf27bb82a5fbf2b6fec64c3cc851c1
MD5 89399990f1383a8a54ae0f9ddb9de067
BLAKE2b-256 c794b5206b60b26cfba166889007a3f13c70374b9b9edb6deaf50494f4f9b0b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page