def run_example(*args, **kwargs): # Create the SerialDataReader sdr = SerialDataReader(kwargs['port'], expected_axis=6, verbose=False) # Create the SampleManager manager = DiscreteSampleManager() # Attach the manager sdr.attach_manager(manager) # Create a classifier classifier = SVMClassifier(model_path=args[0]) # Load the model classifier.load() # Create a ClassifierPredictor predictor = ClassifierPredictor(classifier) # Attach the classifier predictor manager.attach_receiver(predictor) # Create a CallbackManager callback_mg = CallbackManager(verbose=True) # Attach the callback manager predictor.attach_callback_manager(callback_mg) # Open the serial connection sdr.open() print("Opened!") # Start the main loop sdr.mainloop()
def test_save_and_load_model(self): # Load and train the model self.classifier.load() # Load samples multiple times to have a large dataset for n in range(100): self.classifier.load_samples_data() self.assertGreater(self.classifier.train_model(), 0.9) model_path = os.path.join("test_dir_svm_classifier", "model.svm") # Save the model self.classifier.save_model(model_path) # Create a new classifier and load the saved model new_classifier = SVMClassifier(model_path=model_path) # Load the model from the saved model new_classifier.load() # Check if the loaded model works correctly test_sample1 = Sample(data=[[1], [1]]) test_sample2 = Sample(data=[[1], [0]]) self.assertEqual(new_classifier.predict(test_sample1), "0") self.assertEqual(new_classifier.predict(test_sample2), "1")
def run_example(*args, **kwargs): # Create the SerialDataReader sdr = SerialDataReader(kwargs['port'], expected_axis=6, verbose=False) # Create the SampleManager manager = StreamSampleManager(step=20, window=20) # Attach the manager sdr.attach_manager(manager) # Create a threshold middleware middleware = GradientThresholdMiddleware(verbose=False, threshold=40, sample_group_delay=5, group=True) # Attach the middleware manager.attach_receiver(middleware) # Create a classifier classifier = SVMClassifier(model_path=args[0]) # Load the model classifier.load() # Print classifier info classifier.print_info() # Create a ClassifierPredictor predictor = ClassifierPredictor(classifier) # Filter the samples that are too short or too long lfmiddleware = LengthThresholdMiddleware(verbose=True, min_len=180, max_len=600) middleware.attach_receiver(lfmiddleware) # Attach the classifier predictor lfmiddleware.attach_receiver(predictor) # Create a CallbackManager callback_mg = CallbackManager(verbose=True) # Attach the callback manager predictor.attach_callback_manager(callback_mg) # Attach the callbacks callback_mg.attach_callback("knock", receive_gesture) callback_mg.attach_callback("doubleknock", receive_gesture) # Open the serial connection sdr.open() print("Opened!") # Start the main loop sdr.mainloop()
def receive_character(number): pyautogui.doubleClick(155, 69) if number != "1": pyautogui.typewrite(number) else: pyautogui.keyDown('backspace') pyautogui.keyUp('backspace') # Create the SerialDataReader sdr = SerialDataReader(PORT, expected_axis=6, verbose=False) # Create the SampleManager manager = StreamSampleManager() # Attach the manager sdr.attach_manager(manager) # Create a classifier classifier = SVMClassifier(model_path=MODEL_PATH) # Load the model classifier.load() # Print classifier info classifier.print_info() # Create a ClassifierPredictor predictor = ClassifierPredictor(classifier) # Attach the classifier predictor manager.attach_receiver(predictor) # Create a CallbackManager callback_mg = CallbackManager(verbose=True) # Attach the callback manager predictor.attach_callback_manager(callback_mg) # Bind all the numbers callback_mg.attach_callback(" ", receive_character) for c in ["1", "2", "3", "4"]: callback_mg.attach_callback(c, receive_character) # Open the serial connection sdr.open() print("Opened!") # Start the main loop sdr.mainloop()
# Create the SerialDataReader sdr = SerialDataReader(PORT, expected_axis=6, verbose=False) # Create the SampleManager manager = DiscreteSampleManager() # Attach the manager sdr.attach_manager(manager) # Create a classifier classifier = SVMClassifier(model_path=MODEL_PATH) # Load the model classifier.load() # Print classifier info classifier.print_info() # Create a ClassifierPredictor predictor = ClassifierPredictor(classifier) # Attach the classifier predictor manager.attach_receiver(predictor) # Create a CallbackManager callback_mg = CallbackManager(verbose=True) # Attach the callback manager predictor.attach_callback_manager(callback_mg)
class SVMClassifierTestCase(unittest.TestCase): """ Tests to check SVMClassifier consistency. In order to test the classifier, this test will try to implement a XOR gate using the classifier. """ def setUp(self): # Create a test directory if it doesn't exists if not os.path.exists("test_dir_svm_classifier"): os.makedirs("test_dir_svm_classifier") # Create the samples with the 4 possibilities of the XOR gate sample1 = Sample(data=[[1], [1]], gesture_id="0") sample2 = Sample(data=[[0], [0]], gesture_id="0") sample3 = Sample(data=[[1], [0]], gesture_id="1") sample4 = Sample(data=[[0], [1]], gesture_id="1") # Save the samples in the test directory sample1.save_to_file(os.path.join("test_dir_svm_classifier", "0_1.txt")) sample2.save_to_file(os.path.join("test_dir_svm_classifier", "0_2.txt")) sample3.save_to_file(os.path.join("test_dir_svm_classifier", "1_1.txt")) sample4.save_to_file(os.path.join("test_dir_svm_classifier", "1_2.txt")) # Initialize an AbstractClassifier self.classifier = SVMClassifier(dataset_path="test_dir_svm_classifier", test_size=0.5) def tearDown(self): # Destroy the test directory shutil.rmtree("test_dir_svm_classifier") # Destroy the AbstractClassifier self.classifier = None def test_load_samples_loaded_correctly(self): # Load the samples self.classifier.load() # Test if samples has been loaded correctly # Check there are 2 0s and 2 1s self.assertEqual(len(list(filter(lambda x: x == 0, self.classifier.y_data))), 2) self.assertEqual(len(list(filter(lambda x: x == 1, self.classifier.y_data))), 2) # Convert to list from numpy array x_data_as_list = map(lambda x: x.tolist(), self.classifier.x_data) self.assertIn([1, 1], x_data_as_list) self.assertIn([1, 0], x_data_as_list) self.assertIn([0, 1], x_data_as_list) self.assertIn([0, 0], x_data_as_list) # Test if gestures ids has been loaded correctly self.assertIn("0", self.classifier.gestures) self.assertIn("1", self.classifier.gestures) def test_train_model(self): self.classifier.load() # Load samples multiple times to have a large dataset for n in range(100): self.classifier.load_samples_data() self.assertGreater(self.classifier.train_model(), 0.9) def test_predict(self): self.classifier.load() # Load samples multiple times to have a large dataset for n in range(100): self.classifier.load_samples_data() self.assertGreater(self.classifier.train_model(), 0.9) test_sample1 = Sample(data=[[1], [1]]) test_sample2 = Sample(data=[[1], [0]]) self.assertEqual(self.classifier.predict(test_sample1), "0") self.assertEqual(self.classifier.predict(test_sample2), "1") def test_save_model_before_training_should_fail(self): self.assertRaises(ValueError, self.classifier.save_model, "path") def test_save_and_load_model(self): # Load and train the model self.classifier.load() # Load samples multiple times to have a large dataset for n in range(100): self.classifier.load_samples_data() self.assertGreater(self.classifier.train_model(), 0.9) model_path = os.path.join("test_dir_svm_classifier", "model.svm") # Save the model self.classifier.save_model(model_path) # Create a new classifier and load the saved model new_classifier = SVMClassifier(model_path=model_path) # Load the model from the saved model new_classifier.load() # Check if the loaded model works correctly test_sample1 = Sample(data=[[1], [1]]) test_sample2 = Sample(data=[[1], [0]]) self.assertEqual(new_classifier.predict(test_sample1), "0") self.assertEqual(new_classifier.predict(test_sample2), "1")