Create a gist now

Instantly share code, notes, and snippets.

################################################################################
# Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. #
# Leap Motion proprietary and confidential. Not for distribution. #
# Use subject to the terms of the Leap Motion SDK Agreement available at #
# https://developer.leapmotion.com/sdk_agreement, or another agreement #
# between Leap Motion and you, your company or other organization. #
################################################################################
import Leap, sys, thread, time, math
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
class SampleListener(Leap.Listener):
finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky']
bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal']
state_names = ['STATE_INVALID', 'STATE_START', 'STATE_UPDATE', 'STATE_END']
mov_codes_prev = [None, None, None, None, None]
velocity_threshold = 200 # 200mm
table_input = [['a','b','c','d','e','f'],
['e','f','g','h','i','j'],
['k','l','m','n','o','p'],
['q','r','s','t','u','v'],
['w','x','y','z','.','.']]
def on_init(self, controller):
print "Initialized"
def on_connect(self, controller):
print "Connected"
# Enable gestures
controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
def on_disconnect(self, controller):
# Note: not dispatched when running in a debugger.
print "Disconnected"
def on_exit(self, controller):
print "Exited"
def on_frame(self, controller):
# Get the most recent frame and report some basic information
frame = controller.frame()
# Get hands
for hand in frame.hands:
hand_direction = hand.direction
hand_position = hand.palm_position
hand_velocity = hand.palm_velocity
# Get fingers
for finger in hand.fingers:
if finger.direction.y - hand_direction.y < -0.5:
prev_frame = controller.frame(1)
prev_hand_position = prev_frame.hand(hand.id).palm_position
x = hand_velocity.x
y = hand_velocity.y
z = hand_velocity.z
mov_x = math.fabs(x) > self.velocity_threshold
mov_y = math.fabs(y) > self.velocity_threshold
mov_z = math.fabs(z) > self.velocity_threshold
mov_code = None
if mov_x or mov_y or mov_z:
if mov_x:
if x >=0:
mov_code = 0
else:
mov_code = 1
if mov_y:
if y >=0:
mov_code = 2
else:
mov_code = 3
if mov_z:
if z >=0:
mov_code = 4
else:
mov_code = 5
mov_code_prev = self.mov_codes_prev[finger.type]
if mov_code != mov_code_prev:
print self.table_input[finger.type][mov_code]
self.mov_codes_prev[finger.type] = mov_code
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)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment