refactor!: switch to "pocketsphinx" as package name

This commit is contained in:
David Huggins-Daines
2022-09-07 14:27:12 -04:00
parent 9c8012d997
commit 8b97f8387d
19 changed files with 208 additions and 31 deletions
+2 -2
View File
@@ -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()
+9 -9
View File
@@ -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
+7 -7
View File
@@ -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.
+177
View File
@@ -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 | <s> |
# | 0.25s | 0.45s | <sil> |
# | 0.46s | 0.63s | go |
# | 0.64s | 1.16s | forward |
# | 1.17s | 1.52s | ten |
# | 1.53s | 2.11s | meters |
# | 2.12s | 2.6s | </s> |
# ----------------------------
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()) # => ['<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)
# ]
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()])
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -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
+1 -1
View File
@@ -1,7 +1,7 @@
#!/usr/bin/python
import unittest
from pocketsphinx5 import LogMath, FsgModel
from pocketsphinx import LogMath, FsgModel
class FsgTest(unittest.TestCase):
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
from pocketsphinx5 import LogMath
from pocketsphinx import LogMath
import unittest
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -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")