Python interface to CMU Sphinxbase and Pocketsphinx libraries
Project description
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.
Supported platforms
Windows
Linux
Mac OS X
Installation
# Make sure we have up-to-date versions of pip, setuptools and wheel:
$ pip install --upgrade pip setuptools wheel
$ pip install --upgrade pocketsphinx
More binary distributions for manual installation are available here.
Usage
LiveSpeech
It’s an iterator class for continuous recognition or keyword search from a microphone.
from pocketsphinx import LiveSpeech
for phrase in LiveSpeech(): print(phrase)
An example of a keyword search:
from pocketsphinx import LiveSpeech
speech = LiveSpeech(lm=False, keyphrase='forward', kws_threshold=1e+20)
for phrase in speech:
print(phrase.segments(detailed=True))
With your model and dictionary:
import os
from pocketsphinx import LiveSpeech, get_model_path
model_path = get_model_path()
speech = LiveSpeech(
verbose=False,
sampling_rate=16000,
buffer_size=2048,
no_search=False,
full_utt=False,
hmm=os.path.join(model_path, 'en-us'),
lm=os.path.join(model_path, 'en-us.lm.bin'),
dic=os.path.join(model_path, 'cmudict-en-us.dict')
)
for phrase in speech:
print(phrase)
AudioFile
It’s an iterator class for continuous recognition or keyword search from a file.
from pocketsphinx import AudioFile
for phrase in AudioFile(): print(phrase) # => "go forward ten meters"
An example of a keyword search:
from pocketsphinx import AudioFile
audio = AudioFile(lm=False, keyphrase='forward', kws_threshold=1e+20)
for phrase in audio:
print(phrase.segments(detailed=True)) # => "[('forward', -617, 63, 121)]"
With your model and dictionary:
import os
from pocketsphinx import AudioFile, get_model_path, get_data_path
model_path = get_model_path()
data_path = get_data_path()
config = {
'verbose': False,
'audio_file': os.path.join(data_path, 'goforward.raw'),
'buffer_size': 2048,
'no_search': False,
'full_utt': False,
'hmm': os.path.join(model_path, 'en-us'),
'lm': os.path.join(model_path, 'en-us.lm.bin'),
'dict': os.path.join(model_path, 'cmudict-en-us.dict')
}
audio = AudioFile(**config)
for phrase in audio:
print(phrase)
Pocketsphinx
It’s a simple and flexible proxy class to pocketsphinx.Decode.
from pocketsphinx import Pocketsphinx
print(Pocketsphinx().decode()) # => "go forward ten meters"
A more comprehensive example:
from __future__ import print_function
import os
from pocketsphinx import Pocketsphinx, get_model_path, get_data_path
model_path = get_model_path()
data_path = get_data_path()
config = {
'hmm': os.path.join(model_path, 'en-us'),
'lm': os.path.join(model_path, 'en-us.lm.bin'),
'dict': os.path.join(model_path, 'cmudict-en-us.dict')
}
ps = Pocketsphinx(**config)
ps.decode(
audio_file=os.path.join(data_path, 'goforward.raw'),
buffer_size=2048,
no_search=False,
full_utt=False
)
print(ps.segments()) # => ['<s>', '<sil>', 'go', 'forward', 'ten', 'meters', '</s>']
print('Detailed segments:', *ps.segments(detailed=True), sep='\n') # => [
# word, prob, start_frame, end_frame
# ('<s>', 0, 0, 24)
# ('<sil>', -3778, 25, 45)
# ('go', -27, 46, 63)
# ('forward', -38, 64, 116)
# ('ten', -14105, 117, 152)
# ('meters', -2152, 153, 211)
# ('</s>', 0, 212, 260)
# ]
print(ps.hypothesis()) # => go forward ten meters
print(ps.probability()) # => -32079
print(ps.score()) # => -7066
print(ps.confidence()) # => 0.04042641466841839
print(*ps.best(count=10), sep='\n') # => [
# ('go forward ten meters', -28034)
# ('go for word ten meters', -28570)
# ('go forward and majors', -28670)
# ('go forward and meters', -28681)
# ('go forward and readers', -28685)
# ('go forward ten readers', -28688)
# ('go forward ten leaders', -28695)
# ('go forward can meters', -28695)
# ('go forward and leaders', -28706)
# ('go for work ten meters', -28722)
# ]
Default config
If you don’t pass any argument while creating an instance of the Pocketsphinx, AudioFile or LiveSpeech class, it will use next default values:
verbose = False
logfn = /dev/null or nul
audio_file = site-packages/pocketsphinx/data/goforward.raw
audio_device = None
sampling_rate = 16000
buffer_size = 2048
no_search = False
full_utt = False
hmm = site-packages/pocketsphinx/model/en-us
lm = site-packages/pocketsphinx/model/en-us.lm.bin
dict = site-packages/pocketsphinx/model/cmudict-en-us.dict
Any other option must be passed into the config as is, without using symbol -.
If you want to disable default language model or dictionary, you can change the value of the corresponding options to False:
lm = False
dict = False
Verbose
Send output to stdout:
from pocketsphinx import Pocketsphinx
ps = Pocketsphinx(verbose=True)
ps.decode()
print(ps.hypothesis())
Send output to file:
from pocketsphinx import Pocketsphinx
ps = Pocketsphinx(verbose=True, logfn='pocketsphinx.log')
ps.decode()
print(ps.hypothesis())
Сompatibility
Parent classes are still available:
import os
from pocketsphinx import DefaultConfig, Decoder, get_model_path, get_data_path
model_path = get_model_path()
data_path = get_data_path()
# Create a decoder with a certain model
config = DefaultConfig()
config.set_string('-hmm', os.path.join(model_path, 'en-us'))
config.set_string('-lm', os.path.join(model_path, 'en-us.lm.bin'))
config.set_string('-dict', os.path.join(model_path, 'cmudict-en-us.dict'))
decoder = Decoder(config)
# Decode streaming data
buf = bytearray(1024)
with open(os.path.join(data_path, 'goforward.raw'), 'rb') as f:
decoder.start_utt()
while f.readinto(buf):
decoder.process_raw(buf, False, False)
decoder.end_utt()
print('Best hypothesis segments:', [seg.word for seg in decoder.seg()])
Install development version
Install requirements
Windows requirements:
Ubuntu requirements:
$ sudo apt-get install -qq python python-dev python-pip build-essential swig git libpulse-dev
Install with pip
$ pip install https://github.com/bambocher/pocketsphinx-python/archive/master.zip
Install with distutils
$ git clone --recursive https://github.com/bambocher/pocketsphinx-python
$ cd pocketsphinx-python
$ python setup.py install
Projects using pocketsphinx-python
SpeechRecognition - Library for performing speech recognition, with support for several engines and APIs, online and offline.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for pocketsphinx-0.1.3.win-amd64-py3.5.msi
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f5ec0f812c3e346efd8143427fe1064b3fdc10038396caae0aca7afcba782e1 |
|
MD5 | 4a3d5fc39aaade2fc7658715ffa2577b |
|
BLAKE2b-256 | 15bd5f52cfe904674e1de24aaf75c2e2f43c880b2466a7e9f6b23ca1eaad0820 |
Hashes for pocketsphinx-0.1.3.win-amd64-py3.5.exe
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dc0af673eb6d0523237ea66f2f72f9be38021c3e702e07d67718afd02220cac |
|
MD5 | 3386e5268c5b9d961ba5b6477b655faf |
|
BLAKE2b-256 | 278c2a53866b00df6df028d7399da2c5fa14a1971f0945f4e8df9ab8b9ee0243 |
Hashes for pocketsphinx-0.1.3.win-amd64-py2.7.msi
Algorithm | Hash digest | |
---|---|---|
SHA256 | da40cac571b9f2113bd0446b7fcb9a0e6d5e190e283d16864680fa83d45e2383 |
|
MD5 | cfcdc9b9205b084733234cb3828f3d09 |
|
BLAKE2b-256 | e384a5bf08cf35db31bc6a774724c2244b2a6435a6b8504f77ae637c85f07621 |
Hashes for pocketsphinx-0.1.3.win-amd64-py2.7.exe
Algorithm | Hash digest | |
---|---|---|
SHA256 | e05e0ed601916e78d4854b23ce3699cd287208d63143b0c38bc060dbe4ccbcec |
|
MD5 | 0360bc20341661fa7f5209dd17c9a1b3 |
|
BLAKE2b-256 | afe2d360a05a459f7a3fdefaf052e2900697bce04cdd93857473d2a9aa7e7107 |
Hashes for pocketsphinx-0.1.3.win32-py3.5.msi
Algorithm | Hash digest | |
---|---|---|
SHA256 | a57ab9c122540cd8b2d6a9658cbfa05197eb013120a9b91b11a49f96ce4f2a8c |
|
MD5 | c6413d6c4a878e730bf627f5a7b9f6e6 |
|
BLAKE2b-256 | 3500c26bb7a96b1f6126e3d2e8a140acf6c4fe80d34e42cc217e74f097e2271d |
Hashes for pocketsphinx-0.1.3.win32-py3.5.exe
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6701ef8b107605771b52d60a592b65bae093cf533c3c070bd2ddb2807c609fde |
|
MD5 | 4ada038755bfc4869aa88cd0e09bf2b3 |
|
BLAKE2b-256 | 49e723d56e95b1271341211efb923c0aaf89b8424821e3637e44938c4de2ee65 |
Hashes for pocketsphinx-0.1.3.win32-py2.7.msi
Algorithm | Hash digest | |
---|---|---|
SHA256 | a65ea9e5c9218b529de7d5560d302bc95aa21741fd9564896fdf362e2cd0e82d |
|
MD5 | bf9e5c71a07e62681a4732038234f159 |
|
BLAKE2b-256 | 977059d6dbcb027d5a58f808688ace273d0a7c1d0bbec9bfa9f1d59fcde22f8f |
Hashes for pocketsphinx-0.1.3.win32-py2.7.exe
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba144654675157c3c5ab890c86cfefbd495790e1ad9f8be97f64738bb0e7e785 |
|
MD5 | 70f3d7bc463a7362cd9b7d5f5484de76 |
|
BLAKE2b-256 | b5442ff32fc9039e8977b3bc03f4a0032d1687d6acf82357245fbc22c6d0a193 |
Hashes for pocketsphinx-0.1.3-py3.5-win-amd64.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a254518c4ff7f57aec7bdc86792d391622768bbc348bb0e0ebb1cbcf55184da |
|
MD5 | 6fd42b276e87018fc2cb00ce6d1132ec |
|
BLAKE2b-256 | 270794fb9090db6be0ef189009e3e3ed566097fd8a9f57973bc4485110e5d234 |
Hashes for pocketsphinx-0.1.3-py3.5-win32.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cb4b05b16b805c3460a323304b907f3a3609bb3e969ed09e361273f6ed16681 |
|
MD5 | 98c8ffdcc181a94ec9c74f1398fcc13b |
|
BLAKE2b-256 | df5fff3366170ea0f4cf3d65da6acb74505bcb9547dce9b20feadc16647e8aa6 |
Hashes for pocketsphinx-0.1.3-py3.5-macosx-10.6-intel.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21d0cfdeb78910b22562504c7d9a53868f64b9040888cc193c03198a849e5dae |
|
MD5 | 3b141d11d6fab95af79449392759b631 |
|
BLAKE2b-256 | 9bae142ec16d4b95aed25546bede07c7b1ac7c90c62026a1ae07f3a1b6d8e93f |
Hashes for pocketsphinx-0.1.3-py3.5-linux-x86_64.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1951834ebc1c0bad31bb0bbb8337921e6333b63be1582f6fe2c14132cfba162 |
|
MD5 | c2f9b4816b38e52189682ecde0f9ea2d |
|
BLAKE2b-256 | 58eb4fa5d423b3c708919ab9f08b2a42d445d088fcdbf1149c5f28589c148f1f |
Hashes for pocketsphinx-0.1.3-py2.7-win-amd64.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fc0d6de2c64448ff9370249723b9319c75ad2f524f0d57e30f80bf521fa26f1 |
|
MD5 | 34c0777d194a443baa61612da657446a |
|
BLAKE2b-256 | 99049dd10675be3661a852ba4b114ca382240393806fd0aa50d63d528896cee3 |
Hashes for pocketsphinx-0.1.3-py2.7-win32.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 019d2a2971f19aac3e319f2536fd1ff13b14ed9c7fbf9bc3440cb07fe261e5a4 |
|
MD5 | 9aca9af552957b825a1fac53cec51337 |
|
BLAKE2b-256 | 755101299d79ec354a0287a4fb3f1341be6bdf7dffb55448b1b3852bdc615a90 |
Hashes for pocketsphinx-0.1.3-py2.7-macosx-10.6-intel.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb3a4c5427de4975269f9d6fec975fc958431e3382f31921830fda08a8b795a6 |
|
MD5 | 6ba62f9d4f592b551cbd08f0f485b3e6 |
|
BLAKE2b-256 | 7904912784a4e4bf23751203e05cb31aaf20de529bbc56e1636b51b92c77a862 |
Hashes for pocketsphinx-0.1.3-py2.7-linux-x86_64.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cee94eb0e861f7bba0bd9081176cd48bb49909fabb01330c2d1d1c1b336e7b1 |
|
MD5 | 1b4ce66e44f53d23c981e789f84edf29 |
|
BLAKE2b-256 | e1e8448fb4ab687ecad1be8708d152eb7ed69455be7740fc5899255be2228b52 |
Hashes for pocketsphinx-0.1.3-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59f6de8ec032dccf059e0c5c9f1a21df553b9cfb6f4d5ebcfe580634c849f376 |
|
MD5 | acd1d1b45cb1f14b1de2f803d5eca0a9 |
|
BLAKE2b-256 | 228cb3b20eb831d9277865c3d43feef1c6fe6b74a7723b6b1331ebccb1dc3a27 |
Hashes for pocketsphinx-0.1.3-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 919503baba39604d6f1b8017a8fc239c41f77c7646748a0cc077001579a0bade |
|
MD5 | c2cf1f79be93d2bcd68caf341beed1e1 |
|
BLAKE2b-256 | f8001dfc90ece3f912bb78478b47a7fd0ce81ca8dd16149c9b6164fee2a712eb |
Hashes for pocketsphinx-0.1.3-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f932d7a6e2ec04ea8f0b019537fa0c92e702a226a02007223c55a46606c9568 |
|
MD5 | bb461fd33f4ec7c6860a54764ae9dd5b |
|
BLAKE2b-256 | 7cc1d209cfd1f99f1875343ab3215242f28e5f4a98c3d9604bf9b328b5ec7a3e |
Hashes for pocketsphinx-0.1.3-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b06fea365b55a40a0e51510b1b62a1575f264a38516ad20b940c133d569f6ef |
|
MD5 | f2ca17cc2363fa718d3729faede80911 |
|
BLAKE2b-256 | b23de37b6dfd325c9822556624a7bb5c15579fb90347c3b62239859ca686b726 |
Hashes for pocketsphinx-0.1.3-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccdc5fb5591649cd0edcdde541bc813b5f74bf5dd04872b937e551732eaa6847 |
|
MD5 | 5c60eaec561139f24139bcb75026bb63 |
|
BLAKE2b-256 | 7ca7a6926f840fd56c979133676363ee534f6a9aefda3998bdc64247729c00a0 |
Hashes for pocketsphinx-0.1.3-cp27-cp27m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdbf33d926aece1c56fc7772fe7763f371eb8b0029ed85b498558aba15a8eb21 |
|
MD5 | f0a97aa787d6e1bed0d6332f202ac2be |
|
BLAKE2b-256 | b3728c4bbaa3db8ce600c16f7ca4eebaebbbe7539612d66dc34e901b4c1d0216 |