leap_interface.py
Go to the documentation of this file.
00001 #################################################################################
00002 # Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved.                #
00003 # Leap Motion proprietary and confidential. Not for distribution.               #
00004 # Use subject to the terms of the Leap Motion SDK Agreement available at        #
00005 # https://developer.leapmotion.com/sdk_agreement, or another agreement          #
00006 # between Leap Motion and you, your company or other organization.              #
00007 #################################################################################
00008 
00009 #################################################################################
00010 # Altered LEAP example by Florian Lier, you need to have the LEAP SDK installed #
00011 # for this to work properly ;)                                                  #
00012 # This interface provides access to the LEAP MOTION hardware, you will need to  #
00013 # have the official LEAP MOTION SDK installed in order to load the shared       #
00014 # provided with the SDK.                                                        #
00015 #################################################################################
00016 
00017 import sys
00018 import time
00019 # Set (append) your PYTHONPATH properly, or just fill in the location of your LEAP
00020 # SDK folder, e.g., $HOME/LeapSDK/lib where the Leap.py lives and /LeapSDK/lib/x64 or
00021 # x86 where the *.so files reside.
00022 
00023 # Below, you can see the "dirty" version - NOT RECOMMENDED!
00024 
00025 # sys.path.append("/home/YOUR_NAME/path/to/Leap_Developer/LeapSDK/lib")
00026 # sys.path.append("/home/YOUR_NAME/path/to/Leap_Developer/Leap_Developer/LeapSDK/lib/x64")
00027 import threading
00028 import Leap
00029 from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
00030 
00031 
00032 class LeapInterface(Leap.Listener):
00033     def on_init(self, controller):
00034         # These variables as probably not thread safe
00035         # TODO: Make thread safe ;)
00036         self.hand           = [0,0,0]
00037         self.right_hand = False
00038         self.left_hand = False
00039         self.hand_direction = [0,0,0]
00040         self.hand_normal    = [0,0,0]
00041         self.hand_palm_pos  = [0,0,0]
00042         self.hand_pitch     = 0.0
00043         self.hand_yaw       = 0.0
00044         self.hand_roll      = 0.0
00045         print "Initialized Leap Motion Device"
00046 
00047     def on_connect(self, controller):
00048         print "Connected to Leap Motion Controller"
00049 
00050         # Enable gestures
00051         controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
00052         controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
00053         controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
00054         controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
00055 
00056     def on_disconnect(self, controller):
00057         # Note: not dispatched when running in a debugger.
00058         print "Disconnected Leap Motion"
00059 
00060     def on_exit(self, controller):
00061         print "Exited Leap Motion Controller"
00062 
00063     def on_frame(self, controller):
00064         # Get the most recent frame and report some basic information
00065         frame = controller.frame()
00066 
00067         print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
00068               frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures()))
00069 
00070         if not frame.hands.is_empty: #recently changed in API
00071             # Get the first hand
00072             
00073             
00074             #we are seeking one left and one right hands
00075             there_is_right_hand=False
00076             there_is_left_hand=False
00077             
00078             for hand in frame.hands:
00079             
00080                 if hand.is_right:
00081                     there_is_right_hand=True
00082                     self.right_hand=hand
00083                 elif hand.is_left:
00084                     there_is_left_hand=True
00085                     
00086                     self.left_hand=hand
00087             
00088             if not there_is_right_hand:
00089                 self.right_hand=False
00090             
00091             if not there_is_left_hand:
00092                 self.left_hand=False
00093                         
00094             self.hand = frame.hands[0] #old way
00095 
00096             # Check if the hand has any fingers
00097             #fingers = self.hand.fingers
00098             #if not fingers.empty:
00099                 # Calculate the hand's average finger tip position
00100                 #avg_pos = Leap.Vector()
00101                 #for finger in fingers:
00102                     #avg_pos += finger.tip_position
00103                 # avg_pos /= len(fingers)
00104                 # print "Hand has %d fingers, average finger tip position: %s" % (len(fingers), avg_pos)
00105 
00106             # Get the hand's sphere radius and palm position
00107             # print "Hand sphere radius: %f mm, palm position: %s" % (self.hand.sphere_radius, hand.palm_position)
00108 
00109             # Get the hand's normal vector and direction
00110             normal = self.hand.palm_normal
00111             direction = self.hand.direction
00112             pos = self.hand.palm_position
00113 
00114             self.hand_direction[0] = direction.x
00115             self.hand_direction[1] = direction.y
00116             self.hand_direction[2] = direction.z
00117             self.hand_normal[0]    = normal.x
00118             self.hand_normal[1]    = normal.y
00119             self.hand_normal[2]    = normal.z
00120             self.hand_palm_pos[0]  = pos.x
00121             self.hand_palm_pos[1]  = pos.y
00122             self.hand_palm_pos[2]  = pos.z
00123             self.hand_pitch        = direction.pitch * Leap.RAD_TO_DEG
00124             self.hand_yaw          = normal.yaw * Leap.RAD_TO_DEG
00125             self.hand_roll         = direction.roll * Leap.RAD_TO_DEG
00126 
00127             # Calculate the hand's pitch, roll, and yaw angles
00128             print "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (self.hand_pitch, self.hand_roll, self.hand_yaw)
00129 
00130             '''
00131             # Gestures
00132             for gesture in frame.gestures():
00133                 if gesture.type == Leap.Gesture.TYPE_CIRCLE:
00134                     circle = CircleGesture(gesture)
00135 
00136                     # Determine clock direction using the angle between the pointable and the circle normal
00137                     if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
00138                         clockwiseness = "clockwise"
00139                     else:
00140                         clockwiseness = "counterclockwise"
00141 
00142                     # Calculate the angle swept since the last frame
00143                     swept_angle = 0
00144                     if circle.state != Leap.Gesture.STATE_START:
00145                         previous_update = CircleGesture(controller.frame(1).gesture(circle.id))
00146                         swept_angle =  (circle.progress - previous_update.progress) * 2 * Leap.PI
00147 
00148                     print "Circle id: %d, %s, progress: %f, radius: %f, angle: %f degrees, %s" % (
00149                             gesture.id, self.state_string(gesture.state),
00150                             circle.progress, circle.radius, swept_angle * Leap.RAD_TO_DEG, clockwiseness)
00151 
00152                 if gesture.type == Leap.Gesture.TYPE_SWIPE:
00153                     swipe = SwipeGesture(gesture)
00154                     print "Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % (
00155                             gesture.id, self.state_string(gesture.state),
00156                             swipe.position, swipe.direction, swipe.speed)
00157 
00158                 if gesture.type == Leap.Gesture.TYPE_KEY_TAP:
00159                     keytap = KeyTapGesture(gesture)
00160                     print "Key Tap id: %d, %s, position: %s, direction: %s" % (
00161                             gesture.id, self.state_string(gesture.state),
00162                             keytap.position, keytap.direction )
00163 
00164                 if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP:
00165                     screentap = ScreenTapGesture(gesture)
00166                     print "Screen Tap id: %d, %s, position: %s, direction: %s" % (
00167                             gesture.id, self.state_string(gesture.state),
00168                             screentap.position, screentap.direction )
00169 
00170         if not (frame.hands.empty and frame.gestures().empty):
00171             print ""
00172 
00173     def state_string(self, state):
00174         if state == Leap.Gesture.STATE_START:
00175             return "STATE_START"
00176 
00177         if state == Leap.Gesture.STATE_UPDATE:
00178             return "STATE_UPDATE"
00179 
00180         if state == Leap.Gesture.STATE_STOP:
00181             return "STATE_STOP"
00182 
00183         if state == Leap.Gesture.STATE_INVALID:
00184             return "STATE_INVALID"
00185     '''
00186 
00187     def get_hand_direction(self):
00188         return self.hand_direction
00189 
00190     def get_hand_normal(self):
00191         return self.hand_normal
00192 
00193     def get_hand_palmpos(self):
00194         return self.hand_palm_pos
00195 
00196     def get_hand_yaw(self):
00197         return self.hand_yaw
00198 
00199     def get_hand_pitch(self):
00200         return self.hand_pitch
00201 
00202     def get_hand_roll(self):
00203         return self.hand_roll
00204 
00205 
00206 class Runner(threading.Thread):
00207 
00208     def __init__(self,arg=None):
00209         threading.Thread.__init__(self)
00210         self.arg=arg
00211         self.listener = LeapInterface()
00212         self.controller = Leap.Controller()
00213         self.controller.add_listener(self.listener)
00214     
00215     def __del__(self):
00216         self.controller.remove_listener(self.listener)
00217 
00218     def get_hand_direction(self):
00219         return self.listener.get_hand_direction()
00220 
00221     def get_hand_normal(self):
00222         return self.listener.get_hand_normal()
00223 
00224     def get_hand_palmpos(self):
00225         return self.listener.get_hand_palmpos()
00226 
00227     def get_hand_roll(self):
00228         return self.listener.get_hand_roll()
00229 
00230     def get_hand_pitch(self):
00231         return self.listener.get_hand_pitch()
00232 
00233     def get_hand_yaw(self):
00234         return self.listener.get_hand_yaw()
00235 
00236     def run (self):
00237         while True:
00238             # Save some CPU time
00239             time.sleep(0.001)
00240 


leap_motion
Author(s): Florian Lier
autogenerated on Thu Aug 27 2015 13:50:42