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


leap_motion
Author(s): Florian Lier , Mirza Shah , Isaac IY Saito
autogenerated on Sat Jun 8 2019 18:47:25