Restore pocketsphinx files.

This commit is contained in:
Andres Elizondo 2020-04-21 08:25:58 -05:00
parent 1c3fe878b1
commit 26e6d25ffa
5 changed files with 249 additions and 0 deletions

View File

View File

@ -0,0 +1,69 @@
#!/usr/bin/env python3
# Copyright 2019 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
from typing import *
from typing import BinaryIO
from precise.params import pr
from precise.util import audio_to_buffer
class PocketsphinxListener:
"""Pocketsphinx listener implementation used for comparison with Precise"""
def __init__(self, key_phrase, dict_file, hmm_folder, threshold=1e-90, chunk_size=-1):
from pocketsphinx import Decoder
config = Decoder.default_config()
config.set_string('-hmm', hmm_folder)
config.set_string('-dict', dict_file)
config.set_string('-keyphrase', key_phrase)
config.set_float('-kws_threshold', float(threshold))
config.set_float('-samprate', 16000)
config.set_int('-nfft', 2048)
config.set_string('-logfn', '/dev/null')
self.key_phrase = key_phrase
self.buffer = b'\0' * pr.sample_depth * pr.buffer_samples
self.pr = pr
self.read_size = -1 if chunk_size == -1 else pr.sample_depth * chunk_size
try:
self.decoder = Decoder(config)
except RuntimeError:
options = dict(key_phrase=key_phrase, dict_file=dict_file,
hmm_folder=hmm_folder, threshold=threshold)
raise RuntimeError('Invalid Pocketsphinx options: ' + str(options))
def _transcribe(self, byte_data):
self.decoder.start_utt()
self.decoder.process_raw(byte_data, False, False)
self.decoder.end_utt()
return self.decoder.hyp()
def found_wake_word(self, frame_data):
hyp = self._transcribe(frame_data + b'\0' * int(2 * 16000 * 0.01))
return bool(hyp and self.key_phrase in hyp.hypstr.lower())
def update(self, stream: Union[BinaryIO, np.ndarray, bytes]) -> float:
if isinstance(stream, np.ndarray):
chunk = audio_to_buffer(stream)
else:
if isinstance(stream, (bytes, bytearray)):
chunk = stream
else:
chunk = stream.read(self.read_size)
if len(chunk) == 0:
raise EOFError
self.buffer = self.buffer[len(chunk):] + chunk
return float(self.found_wake_word(self.buffer))

View File

View File

@ -0,0 +1,67 @@
#!/usr/bin/env python3
# Copyright 2019 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from precise_runner import PreciseRunner
from precise_runner.runner import ListenerEngine
from prettyparse import Usage
from threading import Event
from precise.pocketsphinx.listener import PocketsphinxListener
from precise.scripts.base_script import BaseScript
from precise.util import activate_notify
class PocketsphinxListenScript(BaseScript):
usage = Usage('''
Run Pocketsphinx on microphone audio input
:key_phrase str
Key phrase composed of words from dictionary
:dict_file str
Filename of dictionary with word pronunciations
:hmm_folder str
Folder containing hidden markov model
:-th --threshold str 1e-90
Threshold for activations
:-c --chunk-size int 2048
Samples between inferences
''')
def run(self):
def on_activation():
activate_notify()
def on_prediction(conf):
print('!' if conf > 0.5 else '.', end='', flush=True)
args = self.args
runner = PreciseRunner(
ListenerEngine(
PocketsphinxListener(
args.key_phrase, args.dict_file, args.hmm_folder, args.threshold, args.chunk_size
)
), 3, on_activation=on_activation, on_prediction=on_prediction
)
runner.start()
Event().wait() # Wait forever
main = PocketsphinxListenScript.run_main
if __name__ == '__main__':
main()

View File

@ -0,0 +1,113 @@
#!/usr/bin/env python3
# Copyright 2019 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import wave
from prettyparse import Usage
from subprocess import check_output, PIPE
from precise.pocketsphinx.listener import PocketsphinxListener
from precise.scripts.base_script import BaseScript
from precise.scripts.test import Stats
from precise.train_data import TrainData
class PocketsphinxTestScript(BaseScript):
usage = Usage('''
Test a dataset using Pocketsphinx
:key_phrase str
Key phrase composed of words from dictionary
:dict_file str
Filename of dictionary with word pronunciations
:hmm_folder str
Folder containing hidden markov model
:-th --threshold str 1e-90
Threshold for activations
:-t --use-train
Evaluate training data instead of test data
:-nf --no-filenames
Don't show the names of files that failed
...
''') | TrainData.usage
def __init__(self, args):
super().__init__(args)
self.listener = PocketsphinxListener(
args.key_phrase, args.dict_file, args.hmm_folder, args.threshold
)
self.outputs = []
self.targets = []
self.filenames = []
def get_stats(self):
return Stats(self.outputs, self.targets, self.filenames)
def run(self):
args = self.args
data = TrainData.from_both(args.tags_file, args.tags_folder, args.folder)
print('Data:', data)
ww_files, nww_files = data.train_files if args.use_train else data.test_files
self.run_test(ww_files, 'Wake Word', 1.0)
self.run_test(nww_files, 'Not Wake Word', 0.0)
stats = self.get_stats()
if not self.args.no_filenames:
fp_files = stats.calc_filenames(False, True, 0.5)
fn_files = stats.calc_filenames(False, False, 0.5)
print('=== False Positives ===')
print('\n'.join(fp_files))
print()
print('=== False Negatives ===')
print('\n'.join(fn_files))
print()
print(stats.counts_str(0.5))
print()
print(stats.summary_str(0.5))
def eval_file(self, filename) -> float:
transcription = check_output(
['pocketsphinx_continuous', '-kws_threshold', '1e-20', '-keyphrase', 'hey my craft',
'-infile', filename], stderr=PIPE)
return float(bool(transcription) and not transcription.isspace())
def run_test(self, test_files, label_name, label):
print()
print('===', label_name, '===')
for test_file in test_files:
try:
with wave.open(test_file) as wf:
frames = wf.readframes(wf.getnframes())
except (OSError, EOFError):
print('?', end='', flush=True)
continue
out = int(self.listener.found_wake_word(frames))
self.outputs.append(out)
self.targets.append(label)
self.filenames.append(test_file)
print('!' if out else '.', end='', flush=True)
print()
main = PocketsphinxTestScript.run_main
if __name__ == '__main__':
main()