00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 import sys
00018 import time
00019
00020
00021
00022
00023
00024
00025
00026
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
00052 bone = finger.bone(getattr(Leap.Bone, 'TYPE_%s' % boneName.upper()))
00053 setattr(self, boneName, bone.prev_joint.to_float_array())
00054
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
00062
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
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
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
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:
00101
00102
00103
00104
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]
00125
00126
00127 fingers = self.hand.fingers
00128 if not fingers.is_empty:
00129 for fingerName in self.fingerNames:
00130
00131
00132 finger = fingers.finger_type(getattr(Leap.Finger, 'TYPE_%s' % fingerName.upper()))[0]
00133 getattr(self, fingerName).importFinger(finger)
00134
00135
00136
00137
00138
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
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
00274 time.sleep(0.001)
00275