def main(): ''' TODO: Idea we had: able to change to a different state by using a specific hand gesture. E.g. when thumb and pinky touches together, we transition to state A, when thumb and ring finger touches together we transition to state B. This can *possibly* be done by always having two active listeners on the Leap controller. One is listening for "state change" gestures and the other one is the current active state's listener. ''' # Create a sample listener and controller music_listener = MusicListener() # morse_listener = MorseListener() controller = Leap.Controller() # Have the sample listener receive events from the controller controller.add_listener(music_listener) # controller.add_listener(morse_listener) # Keep this process running until Enter is pressed print "Press Enter to quit..." try: sys.stdin.readline() except KeyboardInterrupt: pass finally: # Remove the sample listener when done controller.remove_listener(listener)
def get_current_gesture(frame): for gesture in frame.gestures(): if gesture: print("id " + str(gesture.id)) if (gesture.state == Leap.Gesture.STATE_STOP): print("***STOPPED****") elif (gesture.state == Leap.Gesture.STATE_UPDATE): print("*** UPDATING ****") else: print("*** MOOOOOOVIIIING ****") if gesture.type is Leap.Gesture.TYPE_CIRCLE: circle = Leap.CircleGesture(gesture) if (circle.progress > 1): print("*CIRCLE*") print(circle.progress) global previous_progress if (circle.progress == previous_progress): previous_progress = 0 if (circle.pointable.direction.angle_to(circle.normal) <= Leap.PI / 2): return ACTION_GESTURE_CIRCLE_CLOCKWISE return ACTION_GESTURE_CIRCLE previous_progress = circle.progress if gesture.type is Leap.Gesture.TYPE_SWIPE: swipe = Leap.SwipeGesture(gesture) print("speed " + str(swipe.speed)) print("start " + str(swipe.start_position)) print("position" + str(swipe.position)) if (swipe.direction.x > 0): return ACTION_GESTURE_RIGHT return ACTION_GESTURE_LEFT print("BLOCK") return GESTURE_BLOCKING return None
def train_char(training_char): controller = Leap.Controller() for t in range(NUM_SAMPLES): time.sleep(SAMPLE_DELAY) sample = get_hand_position(controller, True) while len(sample) != NUM_FEATURES: print "Please place only right hand in view" sample = get_hand_position(controller, True) print sample add_data(sign=training_char, **sample) print "Done training"
def main(): listener = GestureListener() controller = Leap.Controller() controller.add_listener(listener) try: sys.stdin.readline() except KeyboardInterrupt: pass finally: # Remove the sample listener when done controller.remove_listener(listener)
def main(): # Create a sample listener and controller listener = HandListener() controller = Leap.Controller() # Have the sample listener receive events from the controller controller.add_listener(listener) # Keep this process running until Enter is pressed print "Press Enter to quit..." sys.stdin.readline() # Remove the sample listener when done controller.remove_listener(listener)
def guess(gLearner): gesture_list = [] listener = Listener() controller = Leap.Controller(listener) while True: letter = sys.stdin.readline() if letter[0] == 'q': print "Done recognizing" break if listener.recording: print "Recognized ", gLearner.predict(listener.stop_recording()) print "[Recognize] ", else: print "Recording..." print "[Stop] ", listener.start_recording()
def main(): # Create a sample listener and controller listener = SampleListener() controller = Leap.Controller() # Have the sample listener receive events from the controller controller.add_listener(listener) # Keep this process running until Enter is pressed print "Press Enter to quit..." try: sys.stdin.readline() except KeyboardInterrupt: pass finally: # Remove the sample listener when done controller.remove_listener(listener)
def velocity_histogram(frames, bins=8, range=(-1., 1.)): length = range[1] - range[0] bin_size = length / bins def hround(v): return min(bins - 1, int((v - range[0]) / bin_size)) l = list_3d(bins) for frame in frames: for hand in frame.hands(): for finger in hand.fingers(): v = finger.velocity() vector = Leap.Vector(v.x, v.y, v.z) x = v.x / utils.norm(vector) y = v.y / utils.norm(vector) z = v.z / utils.norm(vector) l[hround(x)][hround(y)][hround(z)] += 1 return l
def _do_actions(self, frame): """Perform actions with frame""" for n, hand in enumerate(frame.hands): avg_pos = Leap.Vector() for finger in hand.fingers: avg_pos += finger.tip_position avg_pos /= len(hand.fingers) x_range = X_MAX - X_MIN try: action_n = int(avg_pos[0] - X_MIN) / (x_range / len(self.actions)) except ValueError: continue action_y = avg_pos[1] < Y_MID action = self.actions[action_n][action_y] self._deck.highlight(action_n, action_y) action(self._arm)(STEP) print action
def guess_char(): global prev controller = Leap.Controller() controller.set_policy(Leap.Controller.POLICY_BACKGROUND_FRAMES) classes = clf.classes_ probs = zip( classes, clf.predict_proba([ v for k, v in get_hand_position(controller, True).iteritems() ])[0]) alpha = max([score for sym, score in probs]) most_probable = sorted([(sym, alpha * score + (1 - alpha)) for sym, score in probs], key=lambda t: t[1], reverse=True) print most_probable[:3] prev = most_probable[0][0] print prev
def listen(gesture_list): listener = Listener() controller = Leap.Controller(listener) print "- Press Enter to toggle recording" print "- Press 'q' + Enter to quit" print "[Record]", while True: letter = sys.stdin.readline() if letter[0] == 'q': print "Done recognizing gesture" break if listener.recording: print "Stoping record" print "[Record] ", gesture_list.append(listener.stop_recording()) else: print " ** Recording ** " print "[Stop] ", listener.start_recording()
def main(): listener = MusicianListener() controller = Leap.Controller() controller.add_listener(listener) t1 = threading.Thread(target=play_music) t1.start() # Keep this process running until Enter is pressed print("Press Enter to quit...") try: sys.stdin.readline() except KeyboardInterrupt: pass finally: # all note off Flags.FINISH = True Flags.start_event.set() midiout.send_message([0xB0, 123, 0]) controller.remove_listener(listener)
def __init__(self, callback, controller): super(TempoListener, self).__init__() self.average_velocity = Leap.Vector(0, 0, 0) self.angle_history = [] self.velocity_history = [] self.changing = False self.last_change_time = None self.bpm = 0 self.callback = callback self.controller = controller #settings self.default_alpha = .3 #for the kalman filters self.threshold_angle = .7 self.threshold_speed = .1 self.threshold_count = 3
def __init__(self): self.midi_interface = MidiInterface() self.recognizers = [] self.controllers = [] self.current_vol = 90 self.stopped = False self.current_track = 1 self.delay = 2.0 self.track_count = 3 self.tempo_count = 0 self.track_updates = {'up': time.time(), 'down': time.time()} for g_name in AbletonController.supported_gestures.keys(): callback = AbletonController.supported_gestures[g_name][0] recognizer = AbletonController.supported_gestures[g_name][1] r = recognizer(callback, self) self.recognizers.append(r) self.controllers.append(Leap.Controller(r)) print "Initialized a recognizer for %s" % g_name
def mul(vec, a): return Leap.Vector(vec.x * a, vec.y * a, vec.z * a)
def __init__(self): self._controller = Leap.Controller() self._controller.set_policy(Leap.Controller.POLICY_BACKGROUND_FRAMES)
from classifier import clf from flask import Flask, render_template, jsonify, request, json from hand_data import get_hand_position from lib import Leap import pickle import random import redis app = Flask(__name__) controller = Leap.Controller() controller.set_policy(Leap.Controller.POLICY_BACKGROUND_FRAMES) past_symbol = 'a' prev_prediction = None r = redis.StrictRedis(host='localhost', port=6379, db=0) @app.route('/') def home(): return render_template('splash.html') @app.route('/translate') def translate(): return render_template('ui.html') # @app.route('/tutorial') # def tutorial():
def add(vec1, vec2): return Leap.Vector(vec1.x + vec2.x, vec1.y + vec2.y, vec1.z * vec2.z)
bg = pygame.image.load(resource_path("assets\\background.png")) finish_image = pygame.image.load(resource_path("assets\\finish_image.png")) row_image = pygame.image.load(resource_path("assets\\row_selection.png")) clock = pygame.time.Clock() row1_Rect = [0, 44, 800, 100] row2_Rect = [0, 189, 800, 100] row3_Rect = [0, 334, 800, 100] image_width = 100 white = (255, 255, 255) black = (21, 21, 21) green = (85, 107, 47) red = (255, 0, 0) # Leap Motion globals controller = Leap.Controller() # Physical controller lastFrameID = 0 leftPointBuffer = 0 rightPointBuffer = 0 thumbPointBuffer = 0 # Tracks the order of the rows row1_Order = [1, 2, 3, 4, 5, 6, 7] row2_Order = [1, 2, 3, 4, 5, 6, 7] row3_Order = [1, 2, 3, 4, 5, 6, 7] combo = [5, 2, 7] ''' ============================================================= Leap Motion Listener: - provided leap motion class to handle intialization, connection, and disconnection of the physical Leap Motion
def ave_v(vectors): xs = [v.x for v in vectors] ys = [v.y for v in vectors] zs = [v.z for v in vectors] return Leap.Vector(ave(xs), ave(ys), ave(zs))
def average_position(vectors): ave_x = ave([v.x for v in vectors]) ave_y = ave([v.y for v in vectors]) ave_z = ave([v.z for v in vectors]) return Leap.Vector(ave_x, ave_y, ave_z)