Advertisement
Guest User

reconnaissance

a guest
Mar 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1.  
  2. import Leap, sys, thread, time
  3.  
  4.  
  5. class SampleListener(Leap.Listener):
  6.     finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky']
  7.     bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal']
  8.  
  9.     def on_init(self, controller):
  10.         print ("Initialized")
  11.  
  12.     def on_connect(self, controller):
  13.         print ("Connected")
  14.  
  15.     def on_disconnect(self, controller):
  16.         # Note: not dispatched when running in a debugger.
  17.         print ("Disconnected")
  18.  
  19.     def on_exit(self, controller):
  20.         print ("Exited")
  21.  
  22.     def on_frame(self, controller):
  23.         # Get the most recent frame and report some basic information
  24.         frame = controller.frame()
  25.  
  26.         print ("Frame id: %d, timestamp: %d, hands: %d, fingers: %d" ) (
  27.               frame.id, frame.timestamp, len(frame.hands), len(frame.fingers))
  28.  
  29.         # Get hands
  30.         for hand in frame.hands:
  31.  
  32.             handType = "Left hand" if hand.is_left else "Right hand"
  33.  
  34.             print ("  %s, id %d, position: %s" ) (
  35.                 handType, hand.id, hand.palm_position)
  36.  
  37.             # Get the hand's normal vector and direction
  38.             normal = hand.palm_normal
  39.             direction = hand.direction
  40.  
  41.             # Calculate the hand's pitch, roll, and yaw angles
  42.             print ( "pitch: %f degrees, roll: %f degrees, yaw: %f degrees" ) (
  43.                 direction.pitch * Leap.RAD_TO_DEG,
  44.                 normal.roll * Leap.RAD_TO_DEG,
  45.                 direction.yaw * Leap.RAD_TO_DEG)
  46.  
  47.             # Get arm bone
  48.             arm = hand.arm
  49.             print ("  Arm direction: %s, wrist position: %s, elbow position: %s" ) (
  50.                 arm.direction,
  51.                 arm.wrist_position,
  52.                 arm.elbow_position)
  53.  
  54.             # Get fingers
  55.             for finger in hand.fingers:
  56.  
  57.                 print ("   %s finger, id: %d, length: %fmm, width: %fmm" ) (
  58.                     self.finger_names[finger.type],
  59.                     finger.id,
  60.                     finger.length,
  61.                     finger.width)
  62.  
  63.                 # Get bones
  64.                 for b in range(0, 4):
  65.                     bone = finger.bone(b)
  66.                     print ("      Bone: %s, start: %s, end: %s, direction: %s" ) (
  67.                         self.bone_names[bone.type],
  68.                         bone.prev_joint,
  69.                         bone.next_joint,
  70.                         bone.direction)
  71.  
  72.         if not frame.hands.is_empty:
  73.             print ("")
  74.  
  75. def main():
  76.     # Create a sample listener and controller
  77.     listener = SampleListener()
  78.     controller = Leap.Controller()
  79.  
  80.     # Have the sample listener receive events from the controller
  81.     controller.add_listener(listener)
  82.  
  83.     # Keep this process running until Enter is pressed
  84.     print ("Press Enter to quit...")
  85.     try:
  86.         sys.stdin.readline()
  87.     except KeyboardInterrupt:
  88.         pass
  89.     finally:
  90.         # Remove the sample listener when done
  91.         controller.remove_listener(listener)
  92.  
  93.  
  94. if __name__ == "__main__":
  95.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement