Exemple #1
0
    def __process_gesture(self, gesture):
        current_time = time.time()
        if current_time < self.__last_gesture_time + self.__gesture_cooldown_duration:
            return
        # Swipes (including ones that are only in-progress) take precedence over taps.
        if gesture.type is Leap.Gesture.TYPE_SWIPE:
            if gesture.state is Leap.Gesture.STATE_STOP:
                swipe = Leap.SwipeGesture(gesture)
                for pointable in swipe.pointables:
                    if not pointable.is_finger:
                        continue

                    finger = Leap.Finger(pointable)
                    if finger.type is not Leap.Finger.TYPE_INDEX:
                        continue

                    swipe_offset = swipe.position - swipe.start_position
                    planar_swipe_offset_length_squared = swipe_offset.x**2 + swipe_offset.y**2
                    if planar_swipe_offset_length_squared < self.__min_swipe_length**2:
                        continue

                    assert len(gesture.hands) == 1
                    gesturing_hand = gesture.hands[0]
                    self.__gesturing_hand_id = gesturing_hand.id
                    swipe_angle = math.atan2(swipe.direction.y,
                                             swipe.direction.x)
                    tau = 2.0 * math.pi
                    if (swipe_angle < 0.0):
                        swipe_angle += tau
                    assert 0.0 <= swipe_angle and swipe_angle <= tau
                    # lerp the real number range [0,tau] onto the integer range [0,8]
                    menu_index = int(round(8.0 * swipe_angle / tau))
                    if menu_index == 8:  # wrap 8 to 0.
                        menu_index = 0
                    handedness = "lefthand" if gesturing_hand.is_left else "righthand"
                    activated_compass_direction_name = self.__compass_direction_name[
                        menu_index]
                    gesture_name = handedness + ":" + activated_compass_direction_name
                    #Log.record('swiped {0}\n{1}\n'.format(gesture_name, self.__compass_direction_image[menu_index]))
                    if self.__call_upon_gesture != None:
                        self.__call_upon_gesture(gesture_name)
                    self.__last_gesture_time = current_time
        elif gesture.type is Leap.Gesture.TYPE_KEY_TAP:
            key_tap = Leap.KeyTapGesture(gesture)
            if key_tap.pointable.is_finger:
                assert len(gesture.hands) == 1
                gesturing_hand = gesture.hands[0]
                self.__gesturing_hand_id = gesturing_hand.id
                finger = Leap.Finger(key_tap.pointable)
                handedness = "lefthand" if gesturing_hand.is_left else "righthand"
                tap_name = "T-" + self.__finger_names[finger.type]
                gesture_name = handedness + ":" + tap_name
                #Log.record('tapped {0}\n'.format(gesture_name))
                if self.__call_upon_gesture != None:
                    self.__call_upon_gesture(gesture_name)
                self.__last_gesture_time = current_time
    def on_frame(self, controller):
        # print ("Frame available")
        frame = controller.frame()
        if (frame.hands.is_empty == False):
            hand = frame.hands[0]

        for gesture in frame.gestures():
            if gesture.type is Leap.Gesture.TYPE_SWIPE:
                swipe = Leap.SwipeGesture(gesture)
                pointable = swipe.pointable
                if (pointable.is_finger == False):
                    continue
                finger = Leap.Finger(pointable)
                hand = HandWrapper(finger.hand)

                if (finger.type != Leap.Finger.TYPE_INDEX
                        and finger.type != Leap.Finger.TYPE_MIDDLE):
                    continue
                if (hand.specific_fingers_extended(
                        Leap.Finger.TYPE_MIDDLE,
                        Leap.Finger.TYPE_INDEX) == False):
                    continue
                current_gesture = GestureInfo(swipe)
                self.is_new_gesture(current_gesture)
                break
Exemple #3
0
def poke():
    controller = Leap.Controller()

    finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky']

    try:
        while True:
            # time.sleep(1)
            frame = controller.frame()

            for hand in frame.hands:
                index = None

                pointables = hand.pointables
                for pointable in pointables:
                    if pointable.is_finger:
                        finger = Leap.Finger(pointable)

                        if not (finger_names[finger.type] == "Index"):
                            continue

                        # zone = pointable.touch_zone
                        # print "zone: ", zone

                        dist = pointable.touch_distance
                        # print "distance: ", dist

                        if dist < 0:
                            print "click: %s" % (dist)

    except KeyboardInterrupt:
        print "\nFinished"
Exemple #4
0
    def on_frame(self, controller):
        global is_hand_present

        frame = controller.frame()
        fingers = frame.fingers

        if is_hand_present and fingers.is_empty:
            is_hand_present = False
            socket.emit('hand_detection', {'hand-detected': is_hand_present})
            print "no hand"

        elif not is_hand_present and not fingers.is_empty:
            is_hand_present = True
            socket.emit('hand_detection', {'hand-detected': is_hand_present})
            print "HAND DETECTED"

        if not fingers.is_empty:
            index = fingers.finger_type(Leap.Finger.TYPE_INDEX)
            index = Leap.Finger(index[0])
            tip_pos = index.stabilized_tip_position

            if not is_calibrated:
                calibrate(tip_pos)
            else:
                if frame.id % 10 == 0:
                    pos = refresh(tip_pos)
                    print pos
                    socket.emit('leap-event', {'x': pos[0], 'y': pos[1]})
Exemple #5
0
 def calibrate(self, reps=3, skip_time=2, hold_time=5, gap_time=0.25):
     feat_len = int(hold_time / gap_time)
     features = Features(feat_len, reps)
     reps_completed = 0
     printed = False
     extended = True
     index_middle_finger = 2
     # array of middle finger averages of lengths. size is equal to number of reps
     middle_len = []
     time_elapsed = 0
     feat_index = 0
     while self.controller.is_connected:
         if reps_completed == reps:
             print "Calibration is finished!"
             self.middle_len = np.mean(middle_len)
             self.write_calibration()
             return
         else:
             frame = self.controller.frame()
             hands = frame.hands
             if len(hands) == 0:
                 feat_index = 0
                 time_elapsed = 0
                 if not printed:
                     print 'Bring hand in view and extend all the fingers'
                     printed = True
                     extended = True
             elif feat_index < feat_len:
                 for hand in hands:
                     # only for right hand as of now
                     if hand.is_right and time_elapsed > skip_time:
                         pointables = frame.pointables
                         if len(pointables.extended()) != 5:
                             print "Please extend all the fingers for calibration"
                             extended = True
                         else:
                             if extended:
                                 print "Good! Calibration is starting. Do NOT move the hand..."
                                 #time.sleep(10 * gap_time)
                                 extended = False
                             # Relative origin(used to calculate the relative distances)
                             hand_center = hand.stabilized_palm_position
                             for pointable in pointables:
                                 finger = Leap.Finger(pointable)
                                 pointable_pos = pointable.stabilized_tip_position
                                 relative_pos = pointable_pos - hand_center
                                 features.finger_lengths[feat_index][finger.type] = relative_pos.magnitude
                             print "Finger lengths", features.finger_lengths[feat_index]
                             feat_index += 1
             elif feat_index == feat_len:
                 feat_index += 1
                 middle_len.append(np.mean(features.finger_lengths, axis=0)[index_middle_finger])
                 reps_completed += 1
                 print "Remove hand from view"
                 printed = False
                 extended = False
             time.sleep(gap_time)
             time_elapsed += gap_time
 def getFinger(self, hand, index):
     pointables = hand.pointables
     for pointable in pointables:
         if pointable.is_finger:
             #check for fingertype
             finger = Leap.Finger(pointable)
             if finger.type == index:
                 return finger
     return None
Exemple #7
0
 def isHand(self, index=Leap.Finger.TYPE_INDEX):
     '''pro zjisteni jestli je v obraze naka ruka, aby se zbytecne nevykreslovalo'''
     frame = self.controller.frame()
     hands = frame.hands
     found = False
     for hand in hands:
         if hand.is_right:
             for pointable in hand.pointables:
                 if pointable.is_finger:
                     finger = Leap.Finger(pointable)
                     if finger.type() == index:
                         found = True
     return found
Exemple #8
0
def main():
    controller = Leap.Controller()
    while (not (controller.is_connected)):
        continue
    print "connected"
    enable_swipe(controller)
    enable_screentap(controller)
    lastFrameID = -1
    previousTapID = -1
    previousTapreached = False
    while (True):
        # frame = controller.frame()
        # if(frame.is_valid):
        #     print frame.id
        #     for gesture in frame.gestures():
        #         if(gesture.type is Leap.Gesture.TYPE_SWIPE):
        #             swipe = Leap.SwipeGesture(gesture)
        #             print "                    SWIPE "

        thisFrame = controller.frame()

        maxHistory = 60

        for i in range(maxHistory):
            frame = controller.frame(i)

            if (frame.id == lastFrameID): break
            if (not frame.is_valid): break
            if (frame.is_valid):
                for gesture in frame.gestures():
                    # if((i== 0) and (gesture.type is Leap.Gesture.TYPE_SWIPE)):
                    #     swipe = Leap.SwipeGesture(gesture)
                    #     print "                    SWIPE "
                    if (gesture.is_valid and
                        (gesture.type is Leap.Gesture.TYPE_SCREEN_TAP)):
                        if (gesture.id != previousTapID):
                            screenTap = Leap.ScreenTapGesture(gesture)
                            tapper = screenTap.pointable
                            if (tapper.is_finger):
                                finger = Leap.Finger(tapper)
                                if (finger.is_valid and finger.type == 1):
                                    previousTapID = gesture.id
                                    print "SCREEN_TAP"
                                    selectTile(screenTap)
                        else:
                            previousTapreached = True
                if (previousTapreached): break

            lastFrameID = thisFrame.id

    return None
Exemple #9
0
    def on_frame(self):
        frame = self.controller.frame()
        if self.historyframe is None:
            self.historyframe = frame
        gestureDetected = False
        hands = frame.hands
        for hand in frame.hands:
            handType = 'Left Hand' if hand.is_left else 'Right Hand'
            if handType == 'Right Hand':
                self.leapbinder.CallFunction('Zoom', frame, self.historyframe)
                self.leapbinder.CallFunction('Rotate', frame,
                                             self.historyframe)

        for gesture in frame.gestures():
            if gesture.type == Leap.Gesture.TYPE_CIRCLE:
                gestureDetected = True
                circle = CircleGesture(gesture)
                if circle.pointable.is_finger:
                    tappingFinger = Leap.Finger(circle.pointable)
                    if tappingFinger.type == Leap.Finger.TYPE_INDEX:
                        self.circle = True
                        if not self.historycircle:
                            self.historycircle = True
                            if circle.pointable.direction.angle_to(
                                    circle.normal) <= Leap.PI / 2:
                                clockwiseness = 'CircleLeft'
                            else:
                                clockwiseness = 'CircleRight'
                            try:
                                self.leapbinder.CallFunction(
                                    clockwiseness, frame, self.historyframe)
                                self.leapbinder.CallFunction(
                                    clockwiseness, frame, self.historyframe)
                            except Exception as e:
                                print str(e)

        if self.circle and not gestureDetected:
            self.circle = False
        if self.circle == False and self.historycircle == True:
            self.historycircle = False
        self.historyframe = frame
        if self.run:
            qt.QTimer.singleShot(100, self.on_frame)
Exemple #10
0
    def get_finger_position(self, index=Leap.Finger.TYPE_INDEX):
        '''ziskani pozice konecku prstu'''
        frame = self.controller.frame()
        hands = frame.hands

        found = False
        for hand in hands:
            if hand.is_right:
                for pointable in hand.pointables:
                    if pointable.is_finger:
                        finger = Leap.Finger(pointable)
                        if finger.type() == index:
                            position = pointable.tip_position
                            found = True

        if found:
            return [position[0], position[1], position[2]], frame.id

        else:
            return [0, 0, 0], 0
Exemple #11
0
 def on_init(self, controller):
     LOG.v("Initialized", "on_init")
     self.t = 0  # time var for automated testing
     
     # Initialize empty frame
     self.frame = None
     
     # Initialize fingers and hands
     self.hand = Leap.Hand()
     self.fingers = { FINGER_NAMES[i] : Leap.Finger() 
         for i in range(5)}
     
     # Initialize joint names for JointState messages
     self.joint_names = []
     
     # Initialize node
     rospy.init_node('hand_tracker', anonymous=True)
     
     # Initialize publishers
     self.js_pub = rospy.Publisher(JS_TOPIC, JointState, queue_size=10)
     self.ps_pub = rospy.Publisher(PS_TOPIC, PoseStamped, queue_size=10)
     self.ts_pub = rospy.Publisher(TS_TOPIC, TwistStamped, queue_size=10)
Exemple #12
0
 def on_frame(self, controller):
     # Get the most recent frame and report some basic information
     frame = controller.frame()
     # Get gestures
     for gesture in frame.gestures():
         if gesture.type == Leap.Gesture.TYPE_KEY_TAP:
             keytap = KeyTapGesture(gesture)
             tapper = keytap.pointable
             finger = Leap.Finger(tapper)
             tap_id = finger.type
             if finger.type == 1:
                 PressKey(VK_R)
             elif finger.type == 2:
                 PressKey(VK_E)
             elif finger.type == 3:
                 PressKey(VK_W)
             elif finger.type == 4:
                 PressKey(VK_Q)
             time.sleep(.2)
             print "  Key Tap id: %d, %s, position: %s, direction: %s" % (
                     finger.type, self.state_names[gesture.state],
                     keytap.position, keytap.direction )
             return
Exemple #13
0
    def on_frame(self, controller):

        # Get the most recent frame and report some basic information
        frame = controller.frame()
        fps = frame.current_frames_per_second

        print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d" % (
            frame.id, frame.timestamp, len(frame.hands), len(frame.fingers))
        one_frame = []
        # Get hands
        for hand in frame.hands:

            handType = "Left hand" if hand.is_left else "Right hand"

            print "  %s, id %d, position: %s" % (handType, hand.id,
                                                 hand.palm_position)

            # Get the hand's normal vector and direction
            hand_val = {}
            normal = hand.palm_normal
            direction = hand.direction
            hand_val["normal"] = [hand.palm_normal[i] for i in range(3)]
            hand_val["position"] = [hand.palm_position[i] for i in range(3)]
            hand_val["velocity"] = [hand.palm_velocity[i] for i in range(3)]
            hand_val["confidence"] = hand.confidence
            hand_val["pinch_strength"] = hand.pinch_strength
            hand_val["sphere_center"] = [
                hand.sphere_center[i] for i in range(3)
            ]
            hand_val["sphere_radius"] = hand.sphere_radius
            hand_val["grab_strength"] = hand.grab_strength
            # Calculate the hand's pitch, roll, and yaw angles
            print "  pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (
                direction.pitch * Leap.RAD_TO_DEG,
                normal.roll * Leap.RAD_TO_DEG, direction.yaw * Leap.RAD_TO_DEG)

            # Get arm bone
            arm = hand.arm
            print "  Arm direction: %s, wrist position: %s, elbow position: %s" % (
                arm.direction, arm.wrist_position, arm.elbow_position)

            # Get fingers
            # finger.JOINT_PIP = 1
            # finger.JOINT_MCP = 0
            all_finger = {}
            for finger in hand.pointables:
                finger = Leap.Finger(finger)
                finger_val = {}
                finger_val["finger"] = finger.id - hand.id * 10 + 1
                finger_val["direction"] = [
                    finger.direction[i] for i in range(3)
                ]
                finger_val["length"] = finger.length
                finger_val["tipPosition"] = [
                    finger.tip_position[i] for i in range(3)
                ]
                finger_val["tipVelocity"] = [
                    finger.tip_velocity[i] for i in range(3)
                ]
                finger_val["dipPosition"] = [
                    finger.joint_position(2)[i] for i in range(3)
                ]
                finger_val["pipPosition"] = [
                    finger.joint_position(1)[i] for i in range(3)
                ]
                finger_val["mcpPosition"] = [
                    finger.joint_position(0)[i] for i in range(3)
                ]
                all_finger[finger_val["finger"]] = finger_val
                all_finger["6"] = hand_val
            if all_finger:
                self.final_json.append(all_finger)
                if len(self.final_json) == 60:
                    self.count += 1
                    write_json([{
                        "label": "BAD",
                        "data": self.final_json
                    }], self.count)
                    self.final_json = []
        if not frame.hands.is_empty:
            print ""
Exemple #14
0
    def on_frame(self, controller):
        frame = controller.frame()

        for gesture in frame.gestures():
            if gesture.type is Leap.Gesture.TYPE_SWIPE and gesture.state is Leap.Gesture.STATE_START:
                swipe = Leap.SwipeGesture(gesture)
                swiper = swipe.pointable
                direction = swipe.direction
                if (swiper.is_finger
                        and Leap.Finger(swiper).type == Leap.Finger.TYPE_INDEX
                        and direction.z < -0.1):
                    if (direction.x < -0.2 and direction.y > 0.1):
                        print "Red light on"
                        s.sendall(bytearray([0, 1]))
                    elif (direction.x < -0.2 and direction.y < -0.1):
                        print "Red light off"
                        s.sendall(bytearray([0, 0]))

                    if (abs(direction.x) <= 0.2 and direction.y > 0.1):
                        print "Green light on"
                        s.sendall(bytearray([1, 1]))
                    elif (abs(direction.x) <= 0.2 and direction.y < -0.1):
                        print "Green light off"
                        s.sendall(bytearray([1, 0]))

                    if (direction.x > 0.2 and direction.y > 0.1):
                        print "Blue light on"
                        s.sendall(bytearray([2, 1]))
                    elif (direction.x > 0.2 and direction.y < -0.1):
                        print "Blue light off"
                        s.sendall(bytearray([2, 0]))

                if (direction.y < -0.80 and len(frame.hands)
                        == 2):  #Universal turn off all lights function
                    print "Lights out!"
                    s.sendall(bytearray([0, 0]))
                    s.sendall(bytearray([1, 0]))
                    s.sendall(bytearray([2, 0]))

            if gesture.type is Leap.Gesture.TYPE_CIRCLE:
                circle = Leap.CircleGesture(gesture)
                clockwise = circle.pointable.direction.angle_to(
                    circle.normal) <= Leap.PI / 2

                global circling
                completedTurns = (math.floor(circle.progress) >= 2)
                if not completedTurns:
                    continue
                if (circling and gesture.state is Leap.Gesture.STATE_STOP
                        and not clockwise):
                    print "Counterclockwise / Volume decreased / STOP"
                    s.sendall(bytearray([255, 0]))
                    circling = False
                elif (circling and gesture.state is Leap.Gesture.STATE_STOP
                      and clockwise):
                    print "Clockwise / Volume increased / STOP"
                    s.sendall(bytearray([255, 1]))
                    circling = False
                elif (not circling and not clockwise):
                    print "Counterclockwise / Volume decreased / START"
                    s.sendall(bytearray([255, 2]))
                    circling = True
                elif (not circling and clockwise):
                    print "Clockwise / Volume increased / START"
                    s.sendall(bytearray([255, 3]))
                    circling = True