Exemplo n.º 1
0
    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 = []
Exemplo n.º 2
0
def main():
    args = TrainData.parse_args(create_parser(usage))
    data = TrainData.from_both(args.tags_file, args.tags_folder, args.folder)
    data_files = data.train_files if args.use_train else data.test_files
    listener = PocketsphinxListener(args.key_phrase, args.dict_file,
                                    args.hmm_folder, args.threshold)

    print('Data:', data)
    stats = test_pocketsphinx(listener, data_files)
    show_stats(stats, not args.no_filenames)
Exemplo n.º 3
0
def main():
    parser = create_parser(usage)
    parser.add_argument(
        'models',
        nargs='*',
        help='List of model filenames in format: wake-word.yy-mm-dd.net')
    args = TrainData.parse_args(parser)
    if not (bool(args.pocketsphinx_dict) == bool(args.pocketsphinx_folder) ==
            bool(args.pocketsphinx_wake_word)):
        parser.error('Must pass all or no Pocketsphinx arguments')

    data = TrainData.from_both(args.tags_file, args.tags_folder, args.folder)
    data_files = data.train_files if args.use_train else data.test_files
    print('Data:', data)

    metrics = {}

    if args.pocketsphinx_dict and args.pocketsphinx_folder and args.pocketsphinx_wake_word:
        if not isfile(args.pocketsphinx_dict):
            parser.error('No such file: ' + args.pocketsphinx_dict)
        if not isdir(args.pocketsphinx_folder):
            parser.error('No such folder: ' + args.pocketsphinx_folder)
        listener = PocketsphinxListener(args.pocketsphinx_wake_word,
                                        args.pocketsphinx_dict,
                                        args.pocketsphinx_folder,
                                        args.pocketsphinx_threshold)
        stats = test_pocketsphinx(listener, data_files)
        metrics[args.pocketsphinx_dict] = stats_to_dict(stats)

    for model_name in args.models:
        print('Calculating', model_name + '...')
        inject_params(model_name)

        train, test = data.load(args.use_train, not args.use_train)
        inputs, targets = train if args.use_train else test
        predictions = Listener.find_runner(model_name)(model_name).predict(
            inputs)

        stats = Stats(predictions, targets, sum(data_files, []))

        print('----', model_name, '----')
        print(stats.counts_str())
        print()
        print(stats.summary_str())
        print()
        metrics[model_name] = stats.to_dict(args.threshold)

    print('Writing to:', args.output)
    with open(args.output, 'w') as f:
        json.dump(metrics, f)
Exemplo n.º 4
0
def main():
    args = create_parser(usage).parse_args()

    def on_activation():
        Popen(['aplay', '-q', 'data/activate.wav'])

    def on_prediction(conf):
        print('!' if conf > 0.5 else '.', end='', flush=True)

    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
Exemplo n.º 5
0
    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
Exemplo n.º 6
0
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()