src.acoustools.HandTracking

Requires the leap motion python module - will not run without it

  1'''
  2Requires the leap motion python module - will not run without it
  3'''
  4try:
  5    import leap
  6
  7    class HandTracker():
  8        '''
  9        Higher level interface to use hand tracking with a leapmotion controller.\n
 10        Requires Ultraleap API https://leap2.ultraleap.com/downloads/
 11
 12        ```python
 13        from acoustools.HandTracking import HandTracker
 14
 15        tracker = HandTracker()
 16
 17        with tracker.connection.open():
 18            tracker.start()
 19            running = True
 20            while running:
 21                hand = tracker.get_hands(right = False)
 22                if hand is not None:
 23                    pos = hand.palm.position
 24                    print(pos.x, pos.y, pos.z)
 25                    print(hand.grab_strength)
 26                    for digit in hand.digits:
 27                        bones = digit.bones
 28                        for bone in bones:
 29                            joint = bone.next_joint
 30                            print('(',joint.x, joint.y, joint.z, ')', end= ' ')
 31                        print()
 32                else:
 33                    print('no hand')
 34            ```
 35        '''
 36
 37        def __init__(self):
 38            self.listener:HandListener = HandListener()
 39            '''
 40            @private
 41            '''
 42
 43
 44            self.connection:leap.Connection = leap.Connection()
 45            '''
 46            @private
 47            '''
 48            self.connection.add_listener(self.listener)
 49
 50        
 51        def start(self):
 52            '''
 53            Starts the tracking
 54            '''
 55            self.connection.set_tracking_mode(leap.TrackingMode.Desktop)
 56        
 57        def get_hands(self, right:bool=True, left:bool= True):
 58            '''
 59            Returns the last scanned data
 60            :param right: If `True` return data on the right hand
 61            :param left: If `True` return data on the left hand
 62            '''
 63
 64            if right and left:
 65                return self.listener.left_hand, self.listener.right_hand
 66            elif right:
 67                return self.listener.right_hand
 68            elif left:
 69                return self.listener.left_hand
 70            else:
 71                raise Exception("Need either left or right")
 72                        
 73
 74
 75
 76    class HandListener(leap.Listener):
 77        '''
 78        @private
 79        Class to listen for hand tracking updates from a leapmotion controller.\n
 80        Requires Ultraleap API https://leap2.ultraleap.com/downloads/
 81        '''
 82
 83        def __init__(self):
 84        
 85            self.left_hand = None
 86    
 87            self.right_hand = None
 88        
 89
 90        def on_connection_event(self, event):
 91            '''
 92            prints connected when connection starts
 93            '''
 94            print("Connected")
 95
 96        def on_device_event(self, event):
 97            try:
 98                with event.device.open():
 99                    info = event.device.get_info()
100            except leap.LeapCannotOpenDeviceError:
101                info = event.device.get_info()
102
103            print(f"Found device {info.serial}")
104
105        def on_tracking_event(self, event, reset=True):
106            if reset:
107                self.left_hand = None
108                self.right_hand = None
109            
110            for hand in event.hands:
111                if str(hand.type) == "HandType.Left":
112                    self.left_hand = hand
113                else:
114                    self.right_hand = hand
115                
116
117except ImportError:
118    pass