Advertisement
Guest User

Sample.py

a guest
Sep 23rd, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.93 KB | None | 0 0
  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. import os, sys, thread, time, inspect
  10. src_dir = os.path.dirname(inspect.getfile(inspect.currentframe()))
  11. arch_dir = '../lib/x64' if sys.maxsize > 2**32 else '../lib/x86'
  12. sys.path.insert(0, os.path.abspath(os.path.join(src_dir, arch_dir)))
  13. import Leap
  14. from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
  15.  
  16.  
  17. class SampleListener(Leap.Listener):
  18.     finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky']
  19.     bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal']
  20.     state_names = ['STATE_INVALID', 'STATE_START', 'STATE_UPDATE', 'STATE_END']
  21.  
  22.     def on_init(self, controller):
  23.         print "Initialized"
  24.  
  25.     def on_connect(self, controller):
  26.         print "Connected"
  27.  
  28.         # Enable gestures
  29.         controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
  30.         controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
  31.         controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
  32.         controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
  33.  
  34.     def on_disconnect(self, controller):
  35.         # Note: not dispatched when running in a debugger.
  36.         print "Disconnected"
  37.  
  38.     def on_exit(self, controller):
  39.         print "Exited"
  40.  
  41.     def on_frame(self, controller):
  42.         # Get the most recent frame and report some basic information
  43.         frame = controller.frame()
  44.  
  45.         print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
  46.               frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures()))
  47.  
  48.         # Get hands
  49.         for hand in frame.hands:
  50.  
  51.             handType = "Left hand" if hand.is_left else "Right hand"
  52.  
  53.             print "  %s, id %d, position: %s" % (
  54.                 handType, hand.id, hand.palm_position)
  55.  
  56.             # Get the hand's normal vector and direction
  57.             normal = hand.palm_normal
  58.             direction = hand.direction
  59.  
  60.             # Calculate the hand's pitch, roll, and yaw angles
  61.             print "  pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (
  62.                 direction.pitch * Leap.RAD_TO_DEG,
  63.                 normal.roll * Leap.RAD_TO_DEG,
  64.                 direction.yaw * Leap.RAD_TO_DEG)
  65.  
  66.             # Get arm bone
  67.             arm = hand.arm
  68.             print "  Arm direction: %s, wrist position: %s, elbow position: %s" % (
  69.                 arm.direction,
  70.                 arm.wrist_position,
  71.                 arm.elbow_position)
  72.  
  73.             # Get fingers
  74.             for finger in hand.fingers:
  75.  
  76.                 print "    %s finger, id: %d, length: %fmm, width: %fmm" % (
  77.                     self.finger_names[finger.type()],
  78.                     finger.id,
  79.                     finger.length,
  80.                     finger.width)
  81.  
  82.                 # Get bones
  83.                 for b in range(0, 4):
  84.                     bone = finger.bone(b)
  85.                     print "      Bone: %s, start: %s, end: %s, direction: %s" % (
  86.                         self.bone_names[bone.type],
  87.                         bone.prev_joint,
  88.                         bone.next_joint,
  89.                         bone.direction)
  90.  
  91.         # Get tools
  92.         for tool in frame.tools:
  93.  
  94.             print "  Tool id: %d, position: %s, direction: %s" % (
  95.                 tool.id, tool.tip_position, tool.direction)
  96.  
  97.         # Get gestures
  98.         for gesture in frame.gestures():
  99.             if gesture.type == Leap.Gesture.TYPE_CIRCLE:
  100.                 circle = CircleGesture(gesture)
  101.  
  102.                 # Determine clock direction using the angle between the pointable and the circle normal
  103.                 if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/2:
  104.                     clockwiseness = "clockwise"
  105.                 else:
  106.                     clockwiseness = "counterclockwise"
  107.  
  108.                 # Calculate the angle swept since the last frame
  109.                 swept_angle = 0
  110.                 if circle.state != Leap.Gesture.STATE_START:
  111.                     previous_update = CircleGesture(controller.frame(1).gesture(circle.id))
  112.                     swept_angle =  (circle.progress - previous_update.progress) * 2 * Leap.PI
  113.  
  114.                 print "  Circle id: %d, %s, progress: %f, radius: %f, angle: %f degrees, %s" % (
  115.                         gesture.id, self.state_names[gesture.state],
  116.                         circle.progress, circle.radius, swept_angle * Leap.RAD_TO_DEG, clockwiseness)
  117.  
  118.             if gesture.type == Leap.Gesture.TYPE_SWIPE:
  119.                 swipe = SwipeGesture(gesture)
  120.                 print "  Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % (
  121.                         gesture.id, self.state_names[gesture.state],
  122.                         swipe.position, swipe.direction, swipe.speed)
  123.  
  124.             if gesture.type == Leap.Gesture.TYPE_KEY_TAP:
  125.                 keytap = KeyTapGesture(gesture)
  126.                 print "  Key Tap id: %d, %s, position: %s, direction: %s" % (
  127.                         gesture.id, self.state_names[gesture.state],
  128.                         keytap.position, keytap.direction )
  129.  
  130.             if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP:
  131.                 screentap = ScreenTapGesture(gesture)
  132.                 print "  Screen Tap id: %d, %s, position: %s, direction: %s" % (
  133.                         gesture.id, self.state_names[gesture.state],
  134.                         screentap.position, screentap.direction )
  135.  
  136.         if not (frame.hands.is_empty and frame.gestures().is_empty):
  137.             print ""
  138.  
  139.     def state_string(self, state):
  140.         if state == Leap.Gesture.STATE_START:
  141.             return "STATE_START"
  142.  
  143.         if state == Leap.Gesture.STATE_UPDATE:
  144.             return "STATE_UPDATE"
  145.  
  146.         if state == Leap.Gesture.STATE_STOP:
  147.             return "STATE_STOP"
  148.  
  149.         if state == Leap.Gesture.STATE_INVALID:
  150.             return "STATE_INVALID"
  151.  
  152. def main():
  153.     # Create a sample listener and controller
  154.     listener = SampleListener()
  155.     controller = Leap.Controller()
  156.  
  157.     # Have the sample listener receive events from the controller
  158.     controller.add_listener(listener)
  159.  
  160.     # Keep this process running until Enter is pressed
  161.     print "Press Enter to quit..."
  162.     try:
  163.         sys.stdin.readline()
  164.     except KeyboardInterrupt:
  165.         pass
  166.     finally:
  167.         # Remove the sample listener when done
  168.         controller.remove_listener(listener)
  169.  
  170.  
  171. if __name__ == "__main__":
  172.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement