示例#1
0
    def extract_features(self,
                         reps=5,
                         skip_time=2,
                         hold_time=5,
                         gap_time=0.25,
                         print_feat=True):
        feat_len = int(hold_time / gap_time)
        feat_index = 0
        time_elapsed = 0
        features = Features(feat_len, reps)
        reps_completed = 0
        printed = False
        while self.controller.is_connected:
            if reps_completed == reps:
                print "Final", features.final_feat
                return features.final_feat
            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'
                        printed = 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
                            fingers = frame.fingers
                            # palm direction feature
                            features.palm_direction[
                                feat_index] = hand.direction.to_tuple()
                            # palm sphere radius
                            features.palm_radius[
                                feat_index] = hand.sphere_radius
                            # hand grab strength
                            features.palm_grab[feat_index] = hand.grab_strength
                            # hand pinch strength
                            features.palm_pinch[
                                feat_index] = hand.pinch_strength
                            # inner_distances features
                            combinations = list(
                                itertools.combinations(pointables, 2))
                            for comb, position in zip(combinations,
                                                      range(
                                                          len(combinations))):
                                finger1 = comb[0].stabilized_tip_position
                                finger2 = comb[1].stabilized_tip_position
                                inner_tip = (finger1 - finger2).magnitude
                                features.inner_distances[feat_index][
                                    position] = inner_tip

                        # Relative origin(used to calculate the relative distances)
                            hand_center = hand.stabilized_palm_position
                            for pointable in pointables:
                                finger = Leap.Finger(pointable)
                                if finger.is_extended:
                                    features.extended_fingers[feat_index][
                                        finger.type] = 1.0
                                    pointable_pos = pointable.stabilized_tip_position
                                    relative_pos = pointable_pos - hand_center
                                    # Scaling the lengths of fingers to the length of middle finger (cal_param)
                                    features.finger_lengths[feat_index][finger.type] = \
                                        relative_pos.magnitude/self.calibration.middle_len
                            feat_per_finger = np.zeros((12, 5))
                            #for i in range(0,4):
                            #feat_per_finger[i] = np.zeros((12,1))
                            i = 0
                            for finger in fingers:
                                feat1 = (finger.bone(
                                    Bone.TYPE_METACARPAL).next_joint -
                                         hand_center).to_float_array()
                                feat2 = (finger.bone(
                                    Bone.TYPE_PROXIMAL).next_joint -
                                         hand_center).to_float_array()
                                feat3 = (finger.bone(
                                    Bone.TYPE_INTERMEDIATE).next_joint -
                                         hand_center).to_float_array()
                                feat4 = (
                                    finger.bone(Bone.TYPE_DISTAL).next_joint -
                                    hand_center).to_float_array()
                                feat_per_finger[:, i] = np.concatenate(
                                    (feat1, feat2, feat3, feat4), axis=0)
                                i = i + 1
                            features.finger_bones[feat_index] = np.concatenate(
                                (feat_per_finger[:, 0], feat_per_finger[:, 1],
                                 feat_per_finger[:, 2], feat_per_finger[:, 3],
                                 feat_per_finger[:, 4]),
                                axis=0)

                            if print_feat:
                                print "Extended Fingers", features.extended_fingers[
                                    feat_index]
                                print "Finger lengths", features.finger_lengths[
                                    feat_index]
                                print "Inter distances between tips", features.inner_distances[
                                    feat_index]
                                print "Palm direction", features.palm_direction[
                                    feat_index]
                                print "Palm sphere radius", features.palm_radius[
                                    feat_index]
                                print "Palm grab strength", features.palm_grab[
                                    feat_index]
                                print "Palm pinch strength", features.palm_pinch[
                                    feat_index]
                                print "Bones", features.finger_bones[
                                    feat_index]
                            feat_index += 1
                elif feat_index == feat_len:
                    feat_index += 1
                    features.avg_and_append_features(int(self.label),
                                                     reps_completed)
                    reps_completed += 1
                    print "Remove hand from view"
                    printed = False
                time.sleep(gap_time)
                time_elapsed += gap_time
示例#2
0
    def extract_features(self, reps=3, skip_time=1.5, hold_time=2, gap_time=0.25, print_feat=True):
        """Method to extract features
        :return: final feature list
        :rtype: list
        """

        BoneType = self.enum(TYPE_DISTAL=3, TYPE_INTERMEDIATE=2, TYPE_PROXIMAL=1, TYPE_METACARPAL=0)
        feat_len = int(hold_time / gap_time)
        feat_index = 0
        time_elapsed = 0
        features = Features(feat_len, reps)
        reps_completed = 0
        printed = False
        start_frame = None

        while self.controller.is_connected:

            if self.get_stop_thread_flag():
                return features.final_feat
            if reps_completed == reps:
                return features.final_feat
            else:
                frame = self.controller.frame()
                hands = frame.hands
                if len(hands) == 0:
                    feat_index = 0
                    time_elapsed = 0
                    start_frame = None
                    if not printed:
                        print 'Bring hand in view'
                        self.setStatus('Bring hand in view')
                        printed = 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:
                            ordered_finger_list = []
                            ordered_pointable_list = []
                            pointables = hand.pointables
                            for pointable in pointables:
                                finger = Leap.Finger(pointable)
                                ordered_finger_list.insert(finger.type, finger)
                                ordered_pointable_list.insert(finger.type, pointable)

                            # setting up features related to hand only
                            self.set_hand_features(features, feat_index, hand)

                            # setting up the boolean vector of extended fingers
                            self.set_extended_fingers(features, feat_index, ordered_finger_list)

                            # setting up the lengths of bones
                            # needs to be refactored
                            self.set_lengths(features, feat_index, ordered_finger_list,hand,  BoneType.TYPE_DISTAL)
                            self.set_lengths(features, feat_index, ordered_finger_list, hand, BoneType.TYPE_INTERMEDIATE)
                            self.set_lengths(features, feat_index, ordered_finger_list, hand, BoneType.TYPE_PROXIMAL)
                            self.set_lengths(features, feat_index, ordered_finger_list, hand, BoneType.TYPE_METACARPAL)

                            # setting up the inner distances between all types of bones
                            # needs to be refactored in feature (too long list of same parameters)
                            self.set_inner_distances(features, feat_index, ordered_finger_list, BoneType.TYPE_DISTAL)
                            self.set_inner_distances(features, feat_index, ordered_finger_list, BoneType.TYPE_INTERMEDIATE)
                            self.set_inner_distances(features, feat_index, ordered_finger_list, BoneType.TYPE_PROXIMAL)
                            self.set_inner_distances(features, feat_index, ordered_finger_list, BoneType.TYPE_METACARPAL)

                            # setting up the angles between the fingers (degrees)
                            self.set_angle_between_tips(features, feat_index, ordered_finger_list)

                            # setting up the angles between the fingers and palm plane (degrees)
                            self.set_angle_between_fingers_and_palm(features, feat_index, ordered_finger_list, hand)

                            # Movement features:
                            if not start_frame:
                                print 'Capturing First Frame'
                                start_frame = frame
                                printed = False
                            elif feat_index == feat_len - 1:
                                end_frame = frame
                                self.set_movement_features(features, start_frame, end_frame, hand)
                                if print_feat:
                                    self.print_dynamic_features(features)
                            if print_feat:
                                self.print_static_features(features, feat_index)
                            feat_index += 1
                elif feat_index == feat_len:
                    feat_index += 1
                    features.avg_and_append_features(int(self.label), reps_completed)
                    reps_completed += 1
                    print "Remove hand from view"
                    self.setStatus("Remove hand from view")
                    self.send_iter_rep_to_process("Remove hand from view", str(reps), str(reps_completed))
                    printed = False
                if len(hands) != 0:
                    time.sleep(gap_time)
                    time_elapsed += gap_time