|
| 1 | +# Pocketsphinx Python |
| 2 | + |
| 3 | +[](https://pypi.python.org/pypi/pocketsphinx) |
| 4 | +[](https://pypi.python.org/pypi/pocketsphinx) |
| 5 | +[](https://pypi.python.org/pypi/pocketsphinx) |
| 6 | +[](https://travis-ci.org/bambocher/pocketsphinx-python) |
| 7 | +[](https://pypi.python.org/pypi/pocketsphinx) |
| 8 | + |
| 9 | +Pocketsphinx is a part of the [CMU Sphinx](http://cmusphinx.sourceforge.net) Open Source Toolkit For Speech Recognition. |
| 10 | + |
| 11 | +This package provides a python interface to CMU [Sphinxbase](https://github.com/cmusphinx/sphinxbase) and [Pocketsphinx](https://github.com/cmusphinx/pocketsphinx) libraries created with [SWIG](http://www.swig.org) and [Setuptools](https://setuptools.readthedocs.io). |
| 12 | + |
| 13 | +## Supported platforms |
| 14 | + |
| 15 | +* Windows |
| 16 | +* Linux |
| 17 | +* Mac OS X |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```shell |
| 22 | +# Make sure we have up-to-date versions of pip, setuptools and wheel |
| 23 | +python -m pip install --upgrade pip setuptools wheel |
| 24 | +pip install --upgrade pocketsphinx |
| 25 | +``` |
| 26 | + |
| 27 | +More binary distributions for manual installation are available [here](https://pypi.python.org/pypi/pocketsphinx). |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### LiveSpeech |
| 32 | + |
| 33 | +It's an iterator class for continuous recognition or keyword search from a microphone. |
| 34 | + |
| 35 | +```python |
| 36 | +from pocketsphinx import LiveSpeech |
| 37 | +for phrase in LiveSpeech(): print(phrase) |
| 38 | +``` |
| 39 | + |
| 40 | +An example of a keyword search: |
| 41 | + |
| 42 | +```python |
| 43 | +from pocketsphinx import LiveSpeech |
| 44 | + |
| 45 | +speech = LiveSpeech(lm=False, keyphrase='forward', kws_threshold=1e+20) |
| 46 | +for phrase in speech: |
| 47 | + print(phrase.segments(detailed=True)) |
| 48 | +``` |
| 49 | + |
| 50 | +With your model and dictionary: |
| 51 | + |
| 52 | +```python |
| 53 | +import os |
| 54 | +from pocketsphinx import LiveSpeech, get_model_path |
| 55 | + |
| 56 | +model_path = get_model_path() |
| 57 | + |
| 58 | +speech = LiveSpeech( |
| 59 | + verbose=False, |
| 60 | + sampling_rate=16000, |
| 61 | + buffer_size=2048, |
| 62 | + no_search=False, |
| 63 | + full_utt=False, |
| 64 | + hmm=os.path.join(model_path, 'en-us'), |
| 65 | + lm=os.path.join(model_path, 'en-us.lm.bin'), |
| 66 | + dic=os.path.join(model_path, 'cmudict-en-us.dict') |
| 67 | +) |
| 68 | + |
| 69 | +for phrase in speech: |
| 70 | + print(phrase) |
| 71 | +``` |
| 72 | + |
| 73 | +### AudioFile |
| 74 | + |
| 75 | +It's an iterator class for continuous recognition or keyword search from a file. |
| 76 | + |
| 77 | +```python |
| 78 | +from pocketsphinx import AudioFile |
| 79 | +for phrase in AudioFile(): print(phrase) # => "go forward ten meters" |
| 80 | +``` |
| 81 | + |
| 82 | +An example of a keyword search: |
| 83 | + |
| 84 | +```python |
| 85 | +from pocketsphinx import AudioFile |
| 86 | + |
| 87 | +audio = AudioFile(lm=False, keyphrase='forward', kws_threshold=1e+20) |
| 88 | +for phrase in audio: |
| 89 | + print(phrase.segments(detailed=True)) # => "[('forward', -617, 63, 121)]" |
| 90 | +``` |
| 91 | + |
| 92 | +With your model and dictionary: |
| 93 | + |
| 94 | +```python |
| 95 | +import os |
| 96 | +from pocketsphinx import AudioFile, get_model_path, get_data_path |
| 97 | + |
| 98 | +model_path = get_model_path() |
| 99 | +data_path = get_data_path() |
| 100 | + |
| 101 | +config = { |
| 102 | + 'verbose': False, |
| 103 | + 'audio_file': os.path.join(data_path, 'goforward.raw'), |
| 104 | + 'buffer_size': 2048, |
| 105 | + 'no_search': False, |
| 106 | + 'full_utt': False, |
| 107 | + 'hmm': os.path.join(model_path, 'en-us'), |
| 108 | + 'lm': os.path.join(model_path, 'en-us.lm.bin'), |
| 109 | + 'dict': os.path.join(model_path, 'cmudict-en-us.dict') |
| 110 | +} |
| 111 | + |
| 112 | +audio = AudioFile(**config) |
| 113 | +for phrase in audio: |
| 114 | + print(phrase) |
| 115 | +``` |
| 116 | + |
| 117 | +### Pocketsphinx |
| 118 | + |
| 119 | +It's a simple and flexible proxy class to `pocketsphinx.Decode`. |
| 120 | + |
| 121 | +```python |
| 122 | +from pocketsphinx import Pocketsphinx |
| 123 | +print(Pocketsphinx().decode()) # => "go forward ten meters" |
| 124 | +``` |
| 125 | + |
| 126 | +A more comprehensive example: |
| 127 | + |
| 128 | +```python |
| 129 | +from __future__ import print_function |
| 130 | +import os |
| 131 | +from pocketsphinx import Pocketsphinx, get_model_path, get_data_path |
| 132 | + |
| 133 | +model_path = get_model_path() |
| 134 | +data_path = get_data_path() |
| 135 | + |
| 136 | +config = { |
| 137 | + 'hmm': os.path.join(model_path, 'en-us'), |
| 138 | + 'lm': os.path.join(model_path, 'en-us.lm.bin'), |
| 139 | + 'dict': os.path.join(model_path, 'cmudict-en-us.dict') |
| 140 | +} |
| 141 | + |
| 142 | +ps = Pocketsphinx(**config) |
| 143 | +ps.decode( |
| 144 | + audio_file=os.path.join(data_path, 'goforward.raw'), |
| 145 | + buffer_size=2048, |
| 146 | + no_search=False, |
| 147 | + full_utt=False |
| 148 | +) |
| 149 | + |
| 150 | +print(ps.segments()) # => ['<s>', '<sil>', 'go', 'forward', 'ten', 'meters', '</s>'] |
| 151 | +print('Detailed segments:', *ps.segments(detailed=True), sep='\n') # => [ |
| 152 | +# word, prob, start_frame, end_frame |
| 153 | +# ('<s>', 0, 0, 24) |
| 154 | +# ('<sil>', -3778, 25, 45) |
| 155 | +# ('go', -27, 46, 63) |
| 156 | +# ('forward', -38, 64, 116) |
| 157 | +# ('ten', -14105, 117, 152) |
| 158 | +# ('meters', -2152, 153, 211) |
| 159 | +# ('</s>', 0, 212, 260) |
| 160 | +# ] |
| 161 | + |
| 162 | +print(ps.hypothesis()) # => go forward ten meters |
| 163 | +print(ps.probability()) # => -32079 |
| 164 | +print(ps.score()) # => -7066 |
| 165 | +print(ps.confidence()) # => 0.04042641466841839 |
| 166 | + |
| 167 | +print(*ps.best(count=10), sep='\n') # => [ |
| 168 | +# ('go forward ten meters', -28034) |
| 169 | +# ('go for word ten meters', -28570) |
| 170 | +# ('go forward and majors', -28670) |
| 171 | +# ('go forward and meters', -28681) |
| 172 | +# ('go forward and readers', -28685) |
| 173 | +# ('go forward ten readers', -28688) |
| 174 | +# ('go forward ten leaders', -28695) |
| 175 | +# ('go forward can meters', -28695) |
| 176 | +# ('go forward and leaders', -28706) |
| 177 | +# ('go for work ten meters', -28722) |
| 178 | +# ] |
| 179 | +``` |
| 180 | + |
| 181 | +### Default config |
| 182 | + |
| 183 | +If you don't pass any argument while creating an instance of the Pocketsphinx, AudioFile or LiveSpeech class, it will use next default values: |
| 184 | + |
| 185 | +```python |
| 186 | +verbose = False |
| 187 | +logfn = /dev/null or nul |
| 188 | +audio_file = site-packages/pocketsphinx/data/goforward.raw |
| 189 | +audio_device = None |
| 190 | +sampling_rate = 16000 |
| 191 | +buffer_size = 2048 |
| 192 | +no_search = False |
| 193 | +full_utt = False |
| 194 | +hmm = site-packages/pocketsphinx/model/en-us |
| 195 | +lm = site-packages/pocketsphinx/model/en-us.lm.bin |
| 196 | +dict = site-packages/pocketsphinx/model/cmudict-en-us.dict |
| 197 | +``` |
| 198 | + |
| 199 | +Any other option must be passed into the config as is, without using symbol `-`. |
| 200 | + |
| 201 | +If you want to disable default language model or dictionary, you can change the value of the corresponding options to False: |
| 202 | + |
| 203 | +```python |
| 204 | +lm = False |
| 205 | +dict = False |
| 206 | +``` |
| 207 | + |
| 208 | +### Verbose |
| 209 | + |
| 210 | +Send output to stdout: |
| 211 | + |
| 212 | +```python |
| 213 | +from pocketsphinx import Pocketsphinx |
| 214 | + |
| 215 | +ps = Pocketsphinx(verbose=True) |
| 216 | +ps.decode() |
| 217 | + |
| 218 | +print(ps.hypothesis()) |
| 219 | +``` |
| 220 | + |
| 221 | +Send output to file: |
| 222 | + |
| 223 | +```python |
| 224 | +from pocketsphinx import Pocketsphinx |
| 225 | + |
| 226 | +ps = Pocketsphinx(verbose=True, logfn='pocketsphinx.log') |
| 227 | +ps.decode() |
| 228 | + |
| 229 | +print(ps.hypothesis()) |
| 230 | +``` |
| 231 | + |
| 232 | +### Compatibility |
| 233 | + |
| 234 | +Parent classes are still available: |
| 235 | + |
| 236 | +```python |
| 237 | +import os |
| 238 | +from pocketsphinx import DefaultConfig, Decoder, get_model_path, get_data_path |
| 239 | + |
| 240 | +model_path = get_model_path() |
| 241 | +data_path = get_data_path() |
| 242 | + |
| 243 | +# Create a decoder with a certain model |
| 244 | +config = DefaultConfig() |
| 245 | +config.set_string('-hmm', os.path.join(model_path, 'en-us')) |
| 246 | +config.set_string('-lm', os.path.join(model_path, 'en-us.lm.bin')) |
| 247 | +config.set_string('-dict', os.path.join(model_path, 'cmudict-en-us.dict')) |
| 248 | +decoder = Decoder(config) |
| 249 | + |
| 250 | +# Decode streaming data |
| 251 | +buf = bytearray(1024) |
| 252 | +with open(os.path.join(data_path, 'goforward.raw'), 'rb') as f: |
| 253 | + decoder.start_utt() |
| 254 | + while f.readinto(buf): |
| 255 | + decoder.process_raw(buf, False, False) |
| 256 | + decoder.end_utt() |
| 257 | +print('Best hypothesis segments:', [seg.word for seg in decoder.seg()]) |
| 258 | +``` |
| 259 | + |
| 260 | +## Install development version |
| 261 | + |
| 262 | +### Install requirements |
| 263 | + |
| 264 | +Windows requirements: |
| 265 | + |
| 266 | +* [Python](https://www.python.org/downloads) |
| 267 | +* [Git](http://git-scm.com/downloads) |
| 268 | +* [Swig](http://www.swig.org/download.html) |
| 269 | +* [Visual Studio Community](https://www.visualstudio.com/ru-ru/downloads/download-visual-studio-vs.aspx) |
| 270 | + |
| 271 | +Ubuntu requirements: |
| 272 | + |
| 273 | +```shell |
| 274 | +sudo apt-get install -qq python python-dev python-pip build-essential swig git libpulse-dev |
| 275 | +``` |
| 276 | + |
| 277 | +### Install with pip |
| 278 | + |
| 279 | +```shell |
| 280 | +pip install https://github.com/bambocher/pocketsphinx-python/archive/master.zip |
| 281 | +``` |
| 282 | + |
| 283 | +### Install with distutils |
| 284 | + |
| 285 | +```shell |
| 286 | +git clone --recursive https://github.com/bambocher/pocketsphinx-python |
| 287 | +cd pocketsphinx-python |
| 288 | +python setup.py install |
| 289 | +``` |
| 290 | + |
| 291 | +## Projects using pocketsphinx-python |
| 292 | + |
| 293 | +* [SpeechRecognition](https://github.com/Uberi/speech_recognition) - Library for performing speech recognition, with support for several engines and APIs, online and offline. |
| 294 | + |
| 295 | +## License |
| 296 | + |
| 297 | +[The BSD License](https://github.com/bambocher/pocketsphinx-python/blob/master/LICENSE) |
0 commit comments