leap_interface.py
Go to the documentation of this file.
1 #################################################################################
2 # Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. #
3 # Leap Motion proprietary and confidential. Not for distribution. #
4 # Use subject to the terms of the Leap Motion SDK Agreement available at #
5 # https://developer.leapmotion.com/sdk_agreement, or another agreement #
6 # between Leap Motion and you, your company or other organization. #
7 #################################################################################
8 
9 #################################################################################
10 # Altered LEAP example by Florian Lier, you need to have the LEAP SDK installed #
11 # for this to work properly ;) #
12 # This interface provides access to the LEAP MOTION hardware, you will need to #
13 # have the official LEAP MOTION SDK installed in order to load the shared #
14 # provided with the SDK. #
15 #################################################################################
16 
17 """ For backwards compatibility with the old driver files
18  Will be DELETED in the future """
19 
20 import sys
21 import time
22 # Set (append) your PYTHONPATH properly, or just fill in the location of your LEAP
23 # SDK folder, e.g., $HOME/LeapSDK/lib where the Leap.py lives and /LeapSDK/lib/x64 or
24 # x86 where the *.so files reside.
25 
26 # Below, you can see the "dirty" version - NOT RECOMMENDED!
27 
28 # sys.path.append("/home/YOUR_NAME/path/to/Leap_Developer/LeapSDK/lib")
29 # sys.path.append("/home/YOUR_NAME/path/to/Leap_Developer/Leap_Developer/LeapSDK/lib/x64")
30 import threading
31 import Leap
32 from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
33 
34 class LeapFinger():
35  def __init__(self, finger=None):
36  self.boneNames = ['metacarpal',
37  'proximal',
38  'intermediate',
39  'distal']
40  for boneName in self.boneNames:
41  setattr(self, boneName, [0.0, 0.0, 0.0])
42  self.tip = [0.0, 0.0, 0.0]
43 
44  self.leapBoneNames = [Leap.Bone.TYPE_METACARPAL,
45  Leap.Bone.TYPE_PROXIMAL,
46  Leap.Bone.TYPE_INTERMEDIATE,
47  Leap.Bone.TYPE_DISTAL]
48 
49  if finger is not None:
50  self.importFinger(finger)
51 
52  def importFinger(self, finger):
53  for boneName in self.boneNames:
54  # Get the base of each bone
55  bone = finger.bone(getattr(Leap.Bone, 'TYPE_%s' % boneName.upper()))
56  setattr(self, boneName, bone.prev_joint.to_float_array())
57  # For the tip, get the end of the distal bone
58  self.tip = finger.bone(Leap.Bone.TYPE_DISTAL).next_joint.to_float_array()
59 
60 
61 
63  def on_init(self, controller):
64  # These variables as probably not thread safe
65  # TODO: Make thread safe ;)
66  self.hand = [0,0,0]
67  self.right_hand = False
68  self.left_hand = False
69  self.hand_direction = [0,0,0]
70  self.hand_normal = [0,0,0]
71  self.hand_palm_pos = [0,0,0]
72  self.hand_pitch = 0.0
73  self.hand_yaw = 0.0
74  self.hand_roll = 0.0
75  self.fingerNames = ['thumb', 'index', 'middle', 'ring', 'pinky']
76  for fingerName in self.fingerNames:
77  setattr(self, fingerName, LeapFinger())
78  print "Initialized Leap Motion Device"
79 
80  def on_connect(self, controller):
81  print "Connected to Leap Motion Controller"
82 
83  # Enable gestures
84  controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
85  controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
86  controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
87  controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
88 
89  def on_disconnect(self, controller):
90  # Note: not dispatched when running in a debugger.
91  print "Disconnected Leap Motion"
92 
93  def on_exit(self, controller):
94  print "Exited Leap Motion Controller"
95 
96  def on_frame(self, controller):
97  # Get the most recent frame and report some basic information
98  frame = controller.frame()
99 
100  print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
101  frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures()))
102 
103  if not frame.hands.is_empty: #recently changed in API
104  # Get the first hand
105 
106 
107  #we are seeking one left and one right hands
108  there_is_right_hand=False
109  there_is_left_hand=False
110 
111  for hand in frame.hands:
112 
113  if hand.is_right:
114  there_is_right_hand=True
115  self.right_hand=hand
116  elif hand.is_left:
117  there_is_left_hand=True
118 
119  self.left_hand=hand
120 
121  if not there_is_right_hand:
122  self.right_hand=False
123 
124  if not there_is_left_hand:
125  self.left_hand=False
126 
127  self.hand = frame.hands[0] #old way
128 
129  # Check if the hand has any fingers
130  fingers = self.hand.fingers
131  if not fingers.is_empty:
132  for fingerName in self.fingerNames:
133  #finger = fingers.finger_type(Leap.Finger.TYPE_THUMB)[0]
134  #self.thumb.importFinger(finger)
135  finger = fingers.finger_type(getattr(Leap.Finger, 'TYPE_%s' % fingerName.upper()))[0]
136  getattr(self, fingerName).importFinger(finger)
137 
138  # Get the hand's sphere radius and palm position
139  # print "Hand sphere radius: %f mm, palm position: %s" % (self.hand.sphere_radius, hand.palm_position)
140 
141  # Get the hand's normal vector and direction
142  normal = self.hand.palm_normal
143  direction = self.hand.direction
144  pos = self.hand.palm_position
145 
146  self.hand_direction[0] = direction.x
147  self.hand_direction[1] = direction.y
148  self.hand_direction[2] = direction.z
149  self.hand_normal[0] = normal.x
150  self.hand_normal[1] = normal.y
151  self.hand_normal[2] = normal.z
152  self.hand_palm_pos[0] = pos.x
153  self.hand_palm_pos[1] = pos.y
154  self.hand_palm_pos[2] = pos.z
155  self.hand_pitch = direction.pitch * Leap.RAD_TO_DEG
156  self.hand_yaw = normal.yaw * Leap.RAD_TO_DEG
157  self.hand_roll = direction.roll * Leap.RAD_TO_DEG
158 
159  # Calculate the hand's pitch, roll, and yaw angles
160  print "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (self.hand_pitch, self.hand_roll, self.hand_yaw)
161 
162  '''
163  # Gestures
164  for gesture in frame.gestures():
165  if gesture.type == Leap.Gesture.TYPE_CIRCLE:
166  circle = CircleGesture(gesture)
167 
168  # Determine clock direction using the angle between the pointable and the circle normal
169  if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
170  clockwiseness = "clockwise"
171  else:
172  clockwiseness = "counterclockwise"
173 
174  # Calculate the angle swept since the last frame
175  swept_angle = 0
176  if circle.state != Leap.Gesture.STATE_START:
177  previous_update = CircleGesture(controller.frame(1).gesture(circle.id))
178  swept_angle = (circle.progress - previous_update.progress) * 2 * Leap.PI
179 
180  print "Circle id: %d, %s, progress: %f, radius: %f, angle: %f degrees, %s" % (
181  gesture.id, self.state_string(gesture.state),
182  circle.progress, circle.radius, swept_angle * Leap.RAD_TO_DEG, clockwiseness)
183 
184  if gesture.type == Leap.Gesture.TYPE_SWIPE:
185  swipe = SwipeGesture(gesture)
186  print "Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % (
187  gesture.id, self.state_string(gesture.state),
188  swipe.position, swipe.direction, swipe.speed)
189 
190  if gesture.type == Leap.Gesture.TYPE_KEY_TAP:
191  keytap = KeyTapGesture(gesture)
192  print "Key Tap id: %d, %s, position: %s, direction: %s" % (
193  gesture.id, self.state_string(gesture.state),
194  keytap.position, keytap.direction )
195 
196  if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP:
197  screentap = ScreenTapGesture(gesture)
198  print "Screen Tap id: %d, %s, position: %s, direction: %s" % (
199  gesture.id, self.state_string(gesture.state),
200  screentap.position, screentap.direction )
201 
202  if not (frame.hands.empty and frame.gestures().empty):
203  print ""
204 
205  def state_string(self, state):
206  if state == Leap.Gesture.STATE_START:
207  return "STATE_START"
208 
209  if state == Leap.Gesture.STATE_UPDATE:
210  return "STATE_UPDATE"
211 
212  if state == Leap.Gesture.STATE_STOP:
213  return "STATE_STOP"
214 
215  if state == Leap.Gesture.STATE_INVALID:
216  return "STATE_INVALID"
217  '''
218 
220  return self.hand_direction
221 
222  def get_hand_normal(self):
223  return self.hand_normal
224 
225  def get_hand_palmpos(self):
226  return self.hand_palm_pos
227 
228  def get_hand_yaw(self):
229  return self.hand_yaw
230 
231  def get_hand_pitch(self):
232  return self.hand_pitch
233 
234  def get_hand_roll(self):
235  return self.hand_roll
236 
237  def get_finger_point(self, fingerName, fingerPointName):
238  return getattr(getattr(self, fingerName), fingerPointName)
239 
240 
241 class Runner(threading.Thread):
242 
243  def __init__(self,arg=None):
244  threading.Thread.__init__(self)
245  self.arg=arg
248  self.controller.add_listener(self.listener)
249 
250  def __del__(self):
251  self.controller.remove_listener(self.listener)
252 
254  return self.listener.get_hand_direction()
255 
256  def get_hand_normal(self):
257  return self.listener.get_hand_normal()
258 
259  def get_hand_palmpos(self):
260  return self.listener.get_hand_palmpos()
261 
262  def get_hand_roll(self):
263  return self.listener.get_hand_roll()
264 
265  def get_hand_pitch(self):
266  return self.listener.get_hand_pitch()
267 
268  def get_hand_yaw(self):
269  return self.listener.get_hand_yaw()
270 
271  def get_finger_point(self, fingerName, fingerPointName):
272  return self.listener.get_finger_point(fingerName, fingerPointName)
273 
274  def run (self):
275  while True:
276  # Save some CPU time
277  time.sleep(0.001)
278 
def __init__(self, arg=None)
def on_frame(self, controller)
def get_finger_point(self, fingerName, fingerPointName)
def on_connect(self, controller)
def on_init(self, controller)
def importFinger(self, finger)
def __init__(self, finger=None)
def on_exit(self, controller)
def on_disconnect(self, controller)
def get_finger_point(self, fingerName, fingerPointName)


leap_motion
Author(s): Florian Lier , Mirza Shah , Isaac IY Saito
autogenerated on Tue Jun 2 2020 03:58:01