def calc_thresh(self, model_file, samples_folder): from precise_runner import PreciseEngine engine = PreciseEngine(self.engine_exe, model_file, self.chunk_size) engine.start() all_max = [] for sample_file in glob(join(samples_folder, '*.wav')): with wave.open(sample_file, 'r') as wr: frames = wr.readframes(wr.getnframes() - 1) chop = len(frames) % self.chunk_size max_pred = float('-inf') for i in range(10): engine.get_prediction(b'\0' * self.chunk_size) for pos in range(chop + self.chunk_size, len(frames) + 1, self.chunk_size): pred = engine.get_prediction(frames[pos - self.chunk_size:pos]) max_pred = max(max_pred, pred) print('MAX PRED:', max_pred) all_max.append(max_pred) av_max = sum(all_max) / len(all_max) stddev = sqrt(sum([(i - av_max)**2 for i in all_max])) good_max = [i for i in all_max if i > av_max - stddev] good_av = sum(good_max) / len(good_max) stddev = sqrt(sum([(i - good_av)**2 for i in good_max])) thresh = good_av - stddev return thresh
def handle_train(self, subfolder, dialog_file): if self.handle_precise_download(): return name = self.get_response('ask.speaker.name', validator=lambda x: x and len(x.split()) < 4, on_fail=lambda utterance: self.dialog_renderer .render('name.error', {'name': utterance})) if not name: return self.speak_dialog(dialog_file) wait_while_speaking() from precise_runner import PreciseEngine engine = PreciseEngine(self.engine_exe, self.model_file, self.chunk_size) engine.start() recording = self.record_wav() with wave.open(recording, 'r') as wr: orig_params = wr.getparams() frames = wr.readframes(wr.getnframes() - 1) ww_positions = self.extract_ww_positions(frames, engine) engine.stop() samples_folder = join(self.folder, 'samples', name) samples_raw_folder = join(samples_folder, 'not-wake-word') makedirs(samples_raw_folder, exist_ok=True) self.split_recording(frames, samples_raw_folder, ww_positions, orig_params) self.speak_dialog('recording.complete') models_folder = join(self.folder, 'user-models') makedirs(models_folder, exist_ok=True) model_file = join(models_folder, '{}.{}.net'.format('hey-mycroft', name)) self.transfer_train(samples_folder, model_file) self.speak_dialog('model.confirm') thresh = self.calc_thresh(model_file, samples_raw_folder) print("THRESH:", thresh)