Пример #1
0
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()
Пример #2
0
def record_new_samples(port, gesture_id, target_dir, expected_axis):
    """
    Used to record new samples for the specified gesture_id
    """
    print("RECORDING NEW SAMPLES")

    # Create the target directory if it doesn't exists
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)

    gestures = []

    # If the gesture id is made of multiple gestures separated by a comma, extract them
    if ',' in gesture_id:
        gestures = map(lambda x: x.strip(), gesture_id.split(","))
    else:
        gestures = [gesture_id]

    print("SERIAL PORT:", port)
    print("TARGET DIRECTORY:", target_dir)
    print("GESTURE ID:", gesture_id)

    # Create the SerialDataReader
    sdr = SerialDataReader(port, expected_axis=expected_axis, verbose=False)

    # Create the SampleManager
    manager = DiscreteSampleManager()

    # Attach the manager
    sdr.attach_manager(manager)

    # Create the gesture chooser
    gesture_chooser = RandomGestureChooser(gestures)

    # Create a FileGestureRecorder
    recorder = FileGestureRecorder(target_dir=target_dir, forced_gesture_id=gesture_chooser, verbose=True)

    # Attach the recorder to the manager
    manager.attach_receiver(recorder)

    # Open the serial connection
    print("Opening serial port...")
    sdr.open()
    print("Opened!")

    print("To exit the loop, press Ctrl+C.")

    # Start the main loop
    sdr.mainloop()
Пример #3
0
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 the VerboseMiddleware that prints the received sample
    middleware = PlotterMiddleware()

    # Attach the middleware
    manager.attach_receiver(middleware)

    # Open the serial connection
    sdr.open()
    print("Opened!")

    # Start the main loop
    sdr.mainloop()
Пример #4
0
def run_example(*args, **kwargs):
    # Create the SerialDataReader
    fdr = FileDataReader(args[0], verbose=False)

    # Create the SampleManager
    manager = DiscreteSampleManager()

    # Attach the manager
    fdr.attach_manager(manager)

    # Create the VerboseMiddleware that prints the received sample
    middleware = PlotterMiddleware(blocking=True)

    # Attach the middleware
    manager.attach_receiver(middleware)

    # Open the serial connection
    fdr.open()
    print("Opened!")

    # Start the main loop
    fdr.mainloop()
from pygarl.data_readers import SerialDataReader
from pygarl.predictors import ClassifierPredictor
from pygarl.sample_managers import DiscreteSampleManager

MODEL_PATH="/home/dhwani/Desktop/gesture-keyboard/model.svm"
PORT="/dev/rfcomm0"

def receive_character(number):
    print(number)


# 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)
Пример #6
0
 def setUp(self):
     # Initialize the DiscreteSampleManager
     self.manager = DiscreteSampleManager(min_sample_length=1)
Пример #7
0
class DiscreteSampleManagerTestCase(unittest.TestCase):
    """
    Tests to check DiscreteSampleManager behaviour
    """
    def setUp(self):
        # Initialize the DiscreteSampleManager
        self.manager = DiscreteSampleManager(min_sample_length=1)

    def tearDown(self):
        # Destroy the sample manager
        self.manager = None

    def test_sample_received_correctly(self):
        # Initialize a Mock Receiver
        receiver = MockReceiver()
        self.assertIsNone(receiver.received_sample)

        # Attach the receiver
        self.manager.attach_receiver(receiver)

        # No data at the beginning
        self.assertEqual(len(self.manager.buffer), 0)
        # Send the START signal
        self.manager.receive_signal(ControlSignal.START)
        # Still no data
        self.assertEqual(len(self.manager.buffer), 0)

        # Send some data
        self.manager.receive_data([1, 2, 3])
        self.manager.receive_data([4, 5, 6])
        self.manager.receive_data([7, 8, 9])

        # Check the data has been received
        self.assertEqual(len(self.manager.buffer), 3)

        # Send the STOP signal
        self.manager.receive_signal(ControlSignal.STOP)

        # Check if the Sample has been received correctly by the receiver
        self.assertIsNotNone(receiver.received_sample)
        # Check if the arrays are the same
        self.assertTrue(
            sp.allclose(receiver.received_sample.data,
                        sp.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])))

    def test_sample_with_min_sample_length_should_fail(self):
        # Initialize a Mock Receiver
        receiver = MockReceiver()
        self.assertIsNone(receiver.received_sample)

        self.manager.min_sample_length = 10  # Set the minimum number of frames to 10

        # Attach the receiver
        self.manager.attach_receiver(receiver)

        # No data at the beginning
        self.assertEqual(len(self.manager.buffer), 0)
        # Send the START signal
        self.manager.receive_signal(ControlSignal.START)
        # Still no data
        self.assertEqual(len(self.manager.buffer), 0)

        # Send some data
        self.manager.receive_data([1, 2, 3])
        self.manager.receive_data([4, 5, 6])
        self.manager.receive_data([7, 8, 9])

        # Check the data has been received
        self.assertEqual(len(self.manager.buffer), 3)

        # Send the STOP signal
        self.manager.receive_signal(ControlSignal.STOP)

        # The sample doesn't have enough frames, so the receiver should not be notified
        self.assertIsNone(receiver.received_sample)

    def test_start_again_should_reset_buffer(self):
        # Initialize a Mock Receiver
        receiver = MockReceiver()
        self.assertIsNone(receiver.received_sample)

        # Attach the receiver
        self.manager.attach_receiver(receiver)

        # No data at the beginning
        self.assertEqual(len(self.manager.buffer), 0)
        # Send the START signal
        self.manager.receive_signal(ControlSignal.START)
        # Still no data
        self.assertEqual(len(self.manager.buffer), 0)

        # Send some data
        self.manager.receive_data([1, 2, 3])
        self.manager.receive_data([4, 5, 6])
        self.manager.receive_data([7, 8, 9])

        # Check the data has been received
        self.assertEqual(len(self.manager.buffer), 3)

        # Send the STOP signal
        self.manager.receive_signal(ControlSignal.STOP)

        # Check if the Sample has been received correctly by the receiver
        self.assertIsNotNone(receiver.received_sample)
        # Check if the arrays are the same
        self.assertTrue(
            sp.allclose(receiver.received_sample.data,
                        sp.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])))

        # Start a new sample
        self.manager.receive_signal(ControlSignal.START)
        # No data
        self.assertEqual(len(self.manager.buffer), 0)