diff --git a/cython/CMakeLists.txt b/cython/CMakeLists.txt index 67a2ebb0..2180dbd5 100644 --- a/cython/CMakeLists.txt +++ b/cython/CMakeLists.txt @@ -17,7 +17,7 @@ target_include_directories( _pocketsphinx PRIVATE ${CMAKE_BINARY_DIR} # for config.h ) python_extension_module(_pocketsphinx) -install(TARGETS _pocketsphinx LIBRARY DESTINATION cython/pocketsphinx5) +install(TARGETS _pocketsphinx LIBRARY DESTINATION cython/pocketsphinx) if(NOT USE_INSTALLED_POCKETSPHINX) - install(DIRECTORY ${PROJECT_SOURCE_DIR}/model DESTINATION cython/pocketsphinx5) + install(DIRECTORY ${PROJECT_SOURCE_DIR}/model DESTINATION cython/pocketsphinx) endif() diff --git a/cython/README.md b/cython/README.md index 9e77391b..256aa76e 100644 --- a/cython/README.md +++ b/cython/README.md @@ -22,7 +22,7 @@ Installation You should be able to install this with pip for recent platforms and versions of Python: - pip3 install pocketsphinx5 + pip3 install pocketsphinx Alternately, you can also compile it from the source tree. I highly suggest doing this in a virtual environment (replace @@ -46,7 +46,7 @@ Usage See the [examples directory](../examples/) for a number of examples of using the library from Python. You can also read the [documentation -for the Python API](https://pocketsphinx5.readthedocs.io) or [the C +for the Python API](https://pocketsphinx.readthedocs.io) or [the C API](https://cmusphinx.github.io/doc/pocketsphinx/). It also mostly supports the same APIs as the previous @@ -60,14 +60,14 @@ microphone. For example, to do speech-to-text with the default (some kind of US English) model: ```python -from pocketsphinx5 import LiveSpeech +from pocketsphinx import LiveSpeech for phrase in LiveSpeech(): print(phrase) ``` Or to do keyword search: ```python -from pocketsphinx5 import LiveSpeech +from pocketsphinx import LiveSpeech speech = LiveSpeech(keyphrase='forward', kws_threshold=1e-20) for phrase in speech: @@ -78,7 +78,7 @@ With your model and dictionary: ```python import os -from pocketsphinx5 import LiveSpeech, get_model_path +from pocketsphinx import LiveSpeech, get_model_path speech = LiveSpeech( sampling_rate=16000, # optional @@ -98,14 +98,14 @@ from a file. Currently it supports only raw, single-channel, 16-bit PCM data in native byte order. ```python -from pocketsphinx5 import AudioFile +from pocketsphinx import AudioFile for phrase in AudioFile("goforward.raw"): print(phrase) # => "go forward ten meters" ``` An example of a keyword search: ```python -from pocketsphinx5 import AudioFile +from pocketsphinx import AudioFile audio = AudioFile("goforward.raw", keyphrase='forward', kws_threshold=1e-20) for phrase in audio: @@ -116,7 +116,7 @@ With your model and dictionary: ```python import os -from pocketsphinx5 import AudioFile, get_model_path +from pocketsphinx import AudioFile, get_model_path model_path = get_model_path() @@ -136,7 +136,7 @@ for phrase in audio: Convert frame into time coordinates: ```python -from pocketsphinx5 import AudioFile +from pocketsphinx import AudioFile # Frames per Second fps = 100 diff --git a/cython/_pocketsphinx.pyx b/cython/_pocketsphinx.pyx index bf050d52..73b692e7 100644 --- a/cython/_pocketsphinx.pyx +++ b/cython/_pocketsphinx.pyx @@ -12,11 +12,11 @@ from libc.stdlib cimport malloc, free from libc.string cimport memcpy import itertools import logging -import pocketsphinx5 +import pocketsphinx import os cimport _pocketsphinx -LOGGER = logging.getLogger("pocketsphinx5") +LOGGER = logging.getLogger("pocketsphinx") cdef class Config: """Configuration object for PocketSphinx. @@ -94,7 +94,7 @@ cdef class Config: """ # Yes, full of race conditions, don't really care (should we?) if self.get_string("-hmm") is None: - default_am = pocketsphinx5.get_model_path("en-us/en-us") + default_am = pocketsphinx.get_model_path("en-us/en-us") if os.path.exists(os.path.join(default_am, "means")): self.set_string("-hmm", default_am) if (self.get_string("-lm") is None @@ -103,11 +103,11 @@ cdef class Config: and self.get_string("-lmctl") is None and self.get_string("-kws") is None and self.get_string("-keyphrase") is None): - default_lm = pocketsphinx5.get_model_path("en-us/en-us.lm.bin") + default_lm = pocketsphinx.get_model_path("en-us/en-us.lm.bin") if os.path.exists(default_lm): self.set_string("-lm", default_lm) if self.get_string("-dict") is None: - default_dict = pocketsphinx5.get_model_path("en-us/cmudict-en-us.dict") + default_dict = pocketsphinx.get_model_path("en-us/cmudict-en-us.dict") if os.path.exists(default_dict): self.set_string("-dict", default_dict) @@ -319,7 +319,7 @@ cdef class Config: raise ValueError("Unknown type %d in argument %s" % (base_type, name)) arg = arg + 1 - yield pocketsphinx5.Arg(name=name, default=default, doc=doc, + yield pocketsphinx.Arg(name=name, default=default, doc=doc, type=arg_type, required=required) cdef class LogMath: @@ -1727,7 +1727,7 @@ def _ps_default_modeldir(): """Get the system default model path from the PocketSphinx library. Do not use this function directly, use - pocketsphinx5.get_model_path() instead. + pocketsphinx.get_model_path() instead. Returns: str: System default model path from PocketSphinx library. diff --git a/cython/pocketsphinx5/__init__.py b/cython/pocketsphinx/__init__.py similarity index 100% rename from cython/pocketsphinx5/__init__.py rename to cython/pocketsphinx/__init__.py diff --git a/cython/pocketsphinx5/segmenter.py b/cython/pocketsphinx/segmenter.py similarity index 100% rename from cython/pocketsphinx5/segmenter.py rename to cython/pocketsphinx/segmenter.py diff --git a/cython/pypocketsphinx-examples.py b/cython/pypocketsphinx-examples.py new file mode 100644 index 00000000..cdc0848c --- /dev/null +++ b/cython/pypocketsphinx-examples.py @@ -0,0 +1,177 @@ +from pocketsphinx import LiveSpeech +for phrase in LiveSpeech(): print(phrase) +from pocketsphinx import LiveSpeech + +speech = LiveSpeech(lm=False, keyphrase='forward', kws_threshold=1e-20) +for phrase in speech: + print(phrase.segments(detailed=True)) + +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) + +from pocketsphinx import AudioFile +for phrase in AudioFile(): print(phrase) # => "go forward ten meters" + +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)]" + +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) + + +from pocketsphinx import AudioFile + +# Frames per Second +fps = 100 + +for phrase in AudioFile(frate=fps): # frate (default=100) + print('-' * 28) + print('| %5s | %3s | %4s |' % ('start', 'end', 'word')) + print('-' * 28) + for s in phrase.seg(): + print('| %4ss | %4ss | %8s |' % (s.start_frame / fps, s.end_frame / fps, s.word)) + print('-' * 28) + +# ---------------------------- +# | start | end | word | +# ---------------------------- +# | 0.0s | 0.24s | | +# | 0.25s | 0.45s | | +# | 0.46s | 0.63s | go | +# | 0.64s | 1.16s | forward | +# | 1.17s | 1.52s | ten | +# | 1.53s | 2.11s | meters | +# | 2.12s | 2.6s | | +# ---------------------------- + + +from pocketsphinx import Pocketsphinx +print(Pocketsphinx().decode()) # => "go forward ten meters" + + + +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()) # => ['', '', 'go', 'forward', 'ten', 'meters', ''] +print('Detailed segments:', *ps.segments(detailed=True), sep='\n') # => [ +# word, prob, start_frame, end_frame +# ('', 0, 0, 24) +# ('', -3778, 25, 45) +# ('go', -27, 46, 63) +# ('forward', -38, 64, 116) +# ('ten', -14105, 117, 152) +# ('meters', -2152, 153, 211) +# ('', 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) +# ] + + +from pocketsphinx import Pocketsphinx + +ps = Pocketsphinx(verbose=True) +ps.decode() + +print(ps.hypothesis()) + + +from pocketsphinx import Pocketsphinx + +ps = Pocketsphinx(verbose=True, logfn='pocketsphinx.log') +ps.decode() + +print(ps.hypothesis()) + +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()]) diff --git a/cython/test/config_test.py b/cython/test/config_test.py index 0cc05b6f..85aff863 100644 --- a/cython/test/config_test.py +++ b/cython/test/config_test.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import os -from pocketsphinx5 import Decoder, Config +from pocketsphinx import Decoder, Config import unittest MODELDIR = os.path.join(os.path.dirname(__file__), "../../model") diff --git a/cython/test/continuous_test.py b/cython/test/continuous_test.py index d61f2764..79f5b8d9 100644 --- a/cython/test/continuous_test.py +++ b/cython/test/continuous_test.py @@ -1,7 +1,7 @@ #!/usr/bin/python import os -from pocketsphinx5 import Decoder +from pocketsphinx import Decoder import unittest DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data") diff --git a/cython/test/decoder_test.py b/cython/test/decoder_test.py index a7e2ffc5..10a36404 100644 --- a/cython/test/decoder_test.py +++ b/cython/test/decoder_test.py @@ -1,7 +1,7 @@ #!/usr/bin/python import os -from pocketsphinx5 import Decoder +from pocketsphinx import Decoder import unittest DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data") diff --git a/cython/test/endpointer_test.py b/cython/test/endpointer_test.py index 7cc0aff7..8e09a4f9 100644 --- a/cython/test/endpointer_test.py +++ b/cython/test/endpointer_test.py @@ -3,7 +3,7 @@ Segment live speech from the default audio device. """ -from pocketsphinx5 import Vad, Endpointer, set_loglevel +from pocketsphinx import Vad, Endpointer, set_loglevel from contextlib import closing import unittest import subprocess diff --git a/cython/test/fsg_test.py b/cython/test/fsg_test.py index 6adf4b16..10a19903 100644 --- a/cython/test/fsg_test.py +++ b/cython/test/fsg_test.py @@ -1,7 +1,7 @@ #!/usr/bin/python import unittest -from pocketsphinx5 import LogMath, FsgModel +from pocketsphinx import LogMath, FsgModel class FsgTest(unittest.TestCase): diff --git a/cython/test/jsgf_test.py b/cython/test/jsgf_test.py index 9d3b262e..1b711a54 100644 --- a/cython/test/jsgf_test.py +++ b/cython/test/jsgf_test.py @@ -2,7 +2,7 @@ import unittest import os -from pocketsphinx5 import Decoder, Jsgf +from pocketsphinx import Decoder, Jsgf DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data") diff --git a/cython/test/kws_test.py b/cython/test/kws_test.py index a2a9ae37..7785e93f 100644 --- a/cython/test/kws_test.py +++ b/cython/test/kws_test.py @@ -2,7 +2,7 @@ import unittest import sys, os -from pocketsphinx5 import Decoder +from pocketsphinx import Decoder DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data") diff --git a/cython/test/lattice_test.py b/cython/test/lattice_test.py index 230e97e2..f2c5766a 100644 --- a/cython/test/lattice_test.py +++ b/cython/test/lattice_test.py @@ -2,7 +2,7 @@ import unittest import os -from pocketsphinx5 import Decoder +from pocketsphinx import Decoder DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data") diff --git a/cython/test/lm_test.py b/cython/test/lm_test.py index 346ac7ee..b8ed4492 100644 --- a/cython/test/lm_test.py +++ b/cython/test/lm_test.py @@ -2,7 +2,7 @@ import unittest import os -from pocketsphinx5 import Decoder, NGramModel +from pocketsphinx import Decoder, NGramModel DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data") diff --git a/cython/test/logmath_test.py b/cython/test/logmath_test.py index 1cf93874..be4d915f 100644 --- a/cython/test/logmath_test.py +++ b/cython/test/logmath_test.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from pocketsphinx5 import LogMath +from pocketsphinx import LogMath import unittest diff --git a/cython/test/phoneme_test.py b/cython/test/phoneme_test.py index d534c764..c2f5c26b 100644 --- a/cython/test/phoneme_test.py +++ b/cython/test/phoneme_test.py @@ -2,7 +2,7 @@ import os import unittest -from pocketsphinx5 import Decoder, get_model_path +from pocketsphinx import Decoder, get_model_path DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data") diff --git a/cython/test/pypocketsphinx_test.py b/cython/test/pypocketsphinx_test.py index 938bf240..57354650 100644 --- a/cython/test/pypocketsphinx_test.py +++ b/cython/test/pypocketsphinx_test.py @@ -8,7 +8,7 @@ be compatible. """ import os -from pocketsphinx5 import Pocketsphinx, AudioFile, NGramModel, Jsgf +from pocketsphinx import Pocketsphinx, AudioFile, NGramModel, Jsgf from unittest import TestCase, main MODELDIR = os.path.join(os.path.dirname(__file__), "../../model") diff --git a/cython/test/vad_test.py b/cython/test/vad_test.py index 2ad85480..b0046524 100644 --- a/cython/test/vad_test.py +++ b/cython/test/vad_test.py @@ -3,7 +3,7 @@ import wave import os from memory_profiler import memory_usage -from pocketsphinx5 import Vad +from pocketsphinx import Vad DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data/vad")