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 class LeapFinger():
00032     def __init__(self, finger=None):
00033         self.boneNames = ['metacarpal',
00034                           'proximal',
00035                           'intermediate',
00036                           'distal']
00037         for boneName in self.boneNames:
00038             setattr(self, boneName, [0.0, 0.0, 0.0])
00039         self.tip = [0.0, 0.0, 0.0]
00040 
00041         self.leapBoneNames = [Leap.Bone.TYPE_METACARPAL,
00042                               Leap.Bone.TYPE_PROXIMAL,
00043                               Leap.Bone.TYPE_INTERMEDIATE,
00044                               Leap.Bone.TYPE_DISTAL]
00045 
00046         if finger is not None:
00047             self.importFinger(finger)
00048 
00049     def importFinger(self, finger):
00050         for boneName in self.boneNames:
00051             # Get the base of each bone
00052             bone = finger.bone(getattr(Leap.Bone, 'TYPE_%s' % boneName.upper()))
00053             setattr(self, boneName, bone.prev_joint.to_float_array())
00054         # For the tip, get the end of the distal bone
00055         self.tip = finger.bone(Leap.Bone.TYPE_DISTAL).next_joint.to_float_array()
00056 
00057 
00058 
00059 class LeapInterface(Leap.Listener):
00060     def on_init(self, controller):
00061         # These variables as probably not thread safe
00062         # TODO: Make thread safe ;)
00063         self.hand           = [0,0,0]
00064         self.right_hand = False
00065         self.left_hand = False
00066         self.hand_direction = [0,0,0]
00067         self.hand_normal    = [0,0,0]
00068         self.hand_palm_pos  = [0,0,0]
00069         self.hand_pitch     = 0.0
00070         self.hand_yaw       = 0.0
00071         self.hand_roll      = 0.0
00072         self.fingerNames = ['thumb', 'index', 'middle', 'ring', 'pinky']
00073         for fingerName in self.fingerNames:
00074             setattr(self, fingerName, LeapFinger())
00075         print "Initialized Leap Motion Device"
00076 
00077     def on_connect(self, controller):
00078         print "Connected to Leap Motion Controller"
00079 
00080         # Enable gestures
00081         controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
00082         controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
00083         controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
00084         controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
00085 
00086     def on_disconnect(self, controller):
00087         # Note: not dispatched when running in a debugger.
00088         print "Disconnected Leap Motion"
00089 
00090     def on_exit(self, controller):
00091         print "Exited Leap Motion Controller"
00092 
00093     def on_frame(self, controller):
00094         # Get the most recent frame and report some basic information
00095         frame = controller.frame()
00096 
00097         print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
00098               frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures()))
00099 
00100         if not frame.hands.is_empty: #recently changed in API
00101             # Get the first hand
00102 
00103 
00104             #we are seeking one left and one right hands
00105             there_is_right_hand=False
00106             there_is_left_hand=False
00107 
00108             for hand in frame.hands:
00109 
00110                 if hand.is_right:
00111                     there_is_right_hand=True
00112                     self.right_hand=hand
00113                 elif hand.is_left:
00114                     there_is_left_hand=True
00115 
00116                     self.left_hand=hand
00117 
00118             if not there_is_right_hand:
00119                 self.right_hand=False
00120 
00121             if not there_is_left_hand:
00122                 self.left_hand=False
00123 
00124             self.hand = frame.hands[0] #old way
00125 
00126             # Check if the hand has any fingers
00127             fingers = self.hand.fingers
00128             if not fingers.is_empty:
00129                 for fingerName in self.fingerNames:
00130                     #finger = fingers.finger_type(Leap.Finger.TYPE_THUMB)[0]
00131                     #self.thumb.importFinger(finger)
00132                     finger = fingers.finger_type(getattr(Leap.Finger, 'TYPE_%s' % fingerName.upper()))[0]
00133                     getattr(self, fingerName).importFinger(finger)
00134 
00135             # Get the hand's sphere radius and palm position
00136             # print "Hand sphere radius: %f mm, palm position: %s" % (self.hand.sphere_radius, hand.palm_position)
00137 
00138             # Get the hand's normal vector and direction
00139             normal = self.hand.palm_normal
00140             direction = self.hand.direction
00141             pos = self.hand.palm_position
00142 
00143             self.hand_direction[0] = direction.x
00144             self.hand_direction[1] = direction.y
00145             self.hand_direction[2] = direction.z
00146             self.hand_normal[0]    = normal.x
00147             self.hand_normal[1]    = normal.y
00148             self.hand_normal[2]    = normal.z
00149             self.hand_palm_pos[0]  = pos.x
00150             self.hand_palm_pos[1]  = pos.y
00151             self.hand_palm_pos[2]  = pos.z
00152             self.hand_pitch        = direction.pitch * Leap.RAD_TO_DEG
00153             self.hand_yaw          = normal.yaw * Leap.RAD_TO_DEG
00154             self.hand_roll         = direction.roll * Leap.RAD_TO_DEG
00155 
00156             # Calculate the hand's pitch, roll, and yaw angles
00157             print "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (self.hand_pitch, self.hand_roll, self.hand_yaw)
00158 
00159             '''
00160             # Gestures
00161             for gesture in frame.gestures():
00162                 if gesture.type == Leap.Gesture.TYPE_CIRCLE:
00163                     circle = CircleGesture(gesture)
00164 
00165                     # Determine clock direction using the angle between the pointable and the circle normal
00166                     if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
00167                         clockwiseness = "clockwise"
00168                     else:
00169                         clockwiseness = "counterclockwise"
00170 
00171                     # Calculate the angle swept since the last frame
00172                     swept_angle = 0
00173                     if circle.state != Leap.Gesture.STATE_START:
00174                         previous_update = CircleGesture(controller.frame(1).gesture(circle.id))
00175                         swept_angle =  (circle.progress - previous_update.progress) * 2 * Leap.PI
00176 
00177                     print "Circle id: %d, %s, progress: %f, radius: %f, angle: %f degrees, %s" % (
00178                             gesture.id, self.state_string(gesture.state),
00179                             circle.progress, circle.radius, swept_angle * Leap.RAD_TO_DEG, clockwiseness)
00180 
00181                 if gesture.type == Leap.Gesture.TYPE_SWIPE:
00182                     swipe = SwipeGesture(gesture)
00183                     print "Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % (
00184                             gesture.id, self.state_string(gesture.state),
00185                             swipe.position, swipe.direction, swipe.speed)
00186 
00187                 if gesture.type == Leap.Gesture.TYPE_KEY_TAP:
00188                     keytap = KeyTapGesture(gesture)
00189                     print "Key Tap id: %d, %s, position: %s, direction: %s" % (
00190                             gesture.id, self.state_string(gesture.state),
00191                             keytap.position, keytap.direction )
00192 
00193                 if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP:
00194                     screentap = ScreenTapGesture(gesture)
00195                     print "Screen Tap id: %d, %s, position: %s, direction: %s" % (
00196                             gesture.id, self.state_string(gesture.state),
00197                             screentap.position, screentap.direction )
00198 
00199         if not (frame.hands.empty and frame.gestures().empty):
00200             print ""
00201 
00202     def state_string(self, state):
00203         if state == Leap.Gesture.STATE_START:
00204             return "STATE_START"
00205 
00206         if state == Leap.Gesture.STATE_UPDATE:
00207             return "STATE_UPDATE"
00208 
00209         if state == Leap.Gesture.STATE_STOP:
00210             return "STATE_STOP"
00211 
00212         if state == Leap.Gesture.STATE_INVALID:
00213             return "STATE_INVALID"
00214     '''
00215 
00216     def get_hand_direction(self):
00217         return self.hand_direction
00218 
00219     def get_hand_normal(self):
00220         return self.hand_normal
00221 
00222     def get_hand_palmpos(self):
00223         return self.hand_palm_pos
00224 
00225     def get_hand_yaw(self):
00226         return self.hand_yaw
00227 
00228     def get_hand_pitch(self):
00229         return self.hand_pitch
00230 
00231     def get_hand_roll(self):
00232         return self.hand_roll
00233 
00234     def get_finger_point(self, fingerName, fingerPointName):
00235         return getattr(getattr(self, fingerName), fingerPointName)
00236 
00237 
00238 class Runner(threading.Thread):
00239 
00240     def __init__(self,arg=None):
00241         threading.Thread.__init__(self)
00242         self.arg=arg
00243         self.listener = LeapInterface()
00244         self.controller = Leap.Controller()
00245         self.controller.add_listener(self.listener)
00246 
00247     def __del__(self):
00248         self.controller.remove_listener(self.listener)
00249 
00250     def get_hand_direction(self):
00251         return self.listener.get_hand_direction()
00252 
00253     def get_hand_normal(self):
00254         return self.listener.get_hand_normal()
00255 
00256     def get_hand_palmpos(self):
00257         return self.listener.get_hand_palmpos()
00258 
00259     def get_hand_roll(self):
00260         return self.listener.get_hand_roll()
00261 
00262     def get_hand_pitch(self):
00263         return self.listener.get_hand_pitch()
00264 
00265     def get_hand_yaw(self):
00266         return self.listener.get_hand_yaw()
00267 
00268     def get_finger_point(self, fingerName, fingerPointName):
00269         return self.listener.get_finger_point(fingerName, fingerPointName)
00270 
00271     def run (self):
00272         while True:
00273             # Save some CPU time
00274             time.sleep(0.001)
00275 


leap_motion
Author(s): Florian Lier
autogenerated on Tue Jan 17 2017 21:06:16