def learn(): labels = get_labels() # mlr = MultiLogisticRegression(labels, conf.lr_inputs) svm = Svm() dataset = list(read_examples('data')) rd.shuffle(dataset) m = len(dataset) train_m = int(0.7 * m) train_set = dataset[:train_m] test_set = dataset[train_m:] test_m = len(test_set) print('Learning...') svm.learn(train_set) print('Learnt') correct = 0 for x, y in train_set: pred = svm.get(x) if pred == y: correct += 1 print('Results on train set: {:2f}% accuracy'.format(100 * correct / train_m)) correct = 0 for x, y in test_set: pred = svm.get(x) if pred == y: correct += 1 print('Results on test set: {:2f}% accuracy'.format(100 * correct / test_m)) with open('trained_data.pickle.b64', 'w') as wf: wf.write(svm.export_model()) print('Weights written to trained_data.pickle.b64')
def recognize(): with AudioRecorder(rate=conf.sampling_rate) as recorder: svm = Svm() with open('trained_data.pickle.b64') as f: svm.import_model(f.read()) while True: print() print('\x1b[A\x1b[2K' 'Press Enter to start recording or Ctrl+C to exit') input() recorder.start_recording() print('\x1b[A\x1b[2K' '\x1b[31mREC\x1b[0m Press Enter to stop recording') input() data = list(recorder.bytes_to_numseq(recorder.finish_recording())) transformed_data = list(sliding_diff(data, conf.sliding_diff_winsize)) scaled_data = scale_array(transformed_data, conf.svm_inputs) pred = svm.get(scaled_data) print('\x1b[A\x1b[2K' 'Predicted output: {}'.format(pred))