Exemple #1
0
    def on_frame(self, controller):
        frame = controller.frame()
        # Code for transformed data points from Leap Motion developer guide
        for hand in frame.hands:
            hand_x_basis = hand.basis.x_basis
            hand_y_basis = hand.basis.y_basis
            hand_z_basis = hand.basis.z_basis
            hand_origin = hand.palm_position
            hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis,
                                         hand_z_basis, hand_origin)
            hand_transform = hand_transform.rigid_inverse()

            fingertips = {}
            for finger in hand.fingers:
                fingertipPos = finger.bone(3).next_joint
                fingertips[self.finger_names[finger.type]] = fingertipPos
                transformed_position = hand_transform.transform_point(
                    finger.tip_position)
                transformed_direction = hand_transform.transform_direction(
                    finger.direction)
                signData[self.finger_names[finger.type] +
                         " position"] = (transformed_position.x,
                                         transformed_position.y,
                                         transformed_position.z)
                signData[self.finger_names[finger.type] +
                         " direction"] = (transformed_direction.x,
                                          transformed_direction.y,
                                          transformed_direction.z)
            for i in range(len(fingertips) - 1):
                signData[self.finger_names[i][0] +
                         self.finger_names[i + 1][0] + "distance"] = distance(
                             fingertips[i], fingertips[i + 1])
Exemple #2
0
def get_normalised_fingers_features(hand):
    hand_x_basis = hand.basis.x_basis
    hand_y_basis = hand.basis.y_basis
    hand_z_basis = hand.basis.z_basis
    hand_origin = hand.palm_position
    hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis,
                                 hand_origin)
    hand_transform = hand_transform.rigid_inverse()

    trans_wrist = (hand_transform.transform_point(hand.wrist_position),
                   'hand_wrist_position_transformed')
    trans_sphere = (hand_transform.transform_point(hand.sphere_center),
                    'hand_sphere_center_transformed')

    features = itertools.chain(flatten(trans_wrist), flatten(trans_sphere))
    # force some sort of order for ML
    for finger in hand.fingers:
        if finger.type == 0:
            thumb = process_finger(finger, hand_transform)
        elif finger.type == 1:
            index = process_finger(finger, hand_transform)
        elif finger.type == 2:
            middle = process_finger(finger, hand_transform)
        elif finger.type == 3:
            ring = process_finger(finger, hand_transform)
        elif finger.type == 4:
            pinky = process_finger(finger, hand_transform)

    features = itertools.chain(features, thumb, index, middle, ring, pinky)

    return features
 def __init__(self, bone_obj):
     basis = bone_obj.basis
     x_basis = basis.x_basis
     y_basis = basis.y_basis
     z_basis = basis.z_basis
     self.matrix = Leap.Matrix(x_basis, y_basis, z_basis).to_array_3x3()
     self.matrix = np.reshape(self.matrix, newshape=(3, 3))
     assert self.validate(self.matrix)
 def get_rotation_mat(bone_obj):
     basis = bone_obj.basis
     x_basis = basis.x_basis
     y_basis = basis.y_basis
     z_basis = basis.z_basis
     matrix = Leap.Matrix(x_basis, y_basis, z_basis).to_array_3x3()
     matrix = np.reshape(matrix, newshape=(3, 3))
     assert Orientation.validate(matrix)
     return matrix
Exemple #5
0
def hand_rotator():
    previous_frame = None
    while True:
        hand = (yield)
        if previous_frame and hand and hand.rotation_probability(
                previous_frame) > 0.1:
            view = list(cmd.get_view())
            matrix = hand.rotation_matrix(previous_frame)
            matrix *= Leap.Matrix(Leap.Vector(*view[0:3]),
                                  Leap.Vector(*view[3:6]),
                                  Leap.Vector(*view[6:9]))
            view[:9] = matrix.to_array_3x3()
            cmd.set_view(view)
        if hand:
            previous_frame = hand.frame
        else:
            previous_frame = None
    def getFingerTipAttributes(self,origHand,fingerName):
        
        
        ### wuold need abstipPos and relativeTipDirection
        hand_x_basis = origHand.basis.x_basis
        hand_y_basis = origHand.basis.y_basis
        hand_z_basis = origHand.basis.z_basis
        hand_origin = origHand.palm_position
        
        hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis, hand_origin)
        hand_transform = hand_transform.rigid_inverse()
        
        
        absTipPosition,relativeTipDirection = None,None
        
        
        
        for finger in origHand.fingers:
            

            
            if fingerName == self.finger_names[finger.type]:
            
                fingtip_transformed_position = hand_transform.transform_point(finger.stabilized_tip_position)
                fingtip_transformed_direction = hand_transform.transform_direction(finger.direction)
                
                absTipPosition = finger.stabilized_tip_position #[x,y,z]
                absTipDirection = finger.direction #[pitch,yaw,roll]
                
                relativeTipPosition = fingtip_transformed_position 
                relativeTipDirection = fingtip_transformed_direction
                
                
                
                relativeTipDirection = np.array(relativeTipDirection)*Leap.RAD_TO_DEG        
        
        return absTipPosition,relativeTipDirection
    def setFingerTipAttributes(self, origHand):

        hand_x_basis = origHand.basis.x_basis
        hand_y_basis = origHand.basis.y_basis
        hand_z_basis = origHand.basis.z_basis
        hand_origin = origHand.palm_position

        hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis,
                                     hand_origin)
        hand_transform = hand_transform.rigid_inverse()

        for finger in origHand.fingers:

            newFinger = LeapFinger3()

            newFinger.type = finger.type

            newFinger.typeName = self.finger_names[finger.type]

            fingtip_transformed_position = hand_transform.transform_point(
                finger.stabilized_tip_position)
            fingtip_transformed_direction = hand_transform.transform_direction(
                finger.direction)

            newFinger.absTipPosition = CoOrdinates(
                finger.stabilized_tip_position, 'pos')
            newFinger.absTipDirection = CoOrdinates(finger.direction, 'dir')

            newFinger.relativeTipPosition = CoOrdinates(
                fingtip_transformed_position, 'pos')
            newFinger.relativeTipDirection = CoOrdinates(
                fingtip_transformed_direction, 'dir')

            self.fingers.append(newFinger)

        return self
    def on_frame(self, controller):
        """
        getting the controller properties
        :param controller:
        :return:
        """
        frame = controller.frame()
        fps = frame.current_frames_per_second
        print fps
        # n is number of bones in a finger
        n = 4
        frameID = "Frame ID:" + str(frame.id)
        timeStamp = 'TimeStamp:' + str(frame.timestamp)
        hands_in_frame = 'Hands:' + str(len(frame.hands))

        for hands in frame.hands:

            # getting the palm and wrist co-ordinates
            palm_cord = 'Palm cordinates:' + str(hands.palm_position)
            wrist_cord = 'Wrist cordinates:' + str(hands.wrist_position)
            print frameID, timeStamp, hands_in_frame

            position = hands.palm_position  # 3dcoordinate of the palm center point in mm from the Leap Motion origin
            velocity = hands.palm_velocity  # instantaneous motion of the hand in mm/s.
            direc = hands.direction  # direction the fingers point from palm center

            # These functions are defined in the Vector class.
            pitch = 'Pitch:' + str(
                hands.direction.pitch
            )  # Pitch - It is the angle around the x-axis ,
            yaw = 'Yaw:' + str(hands.direction.yaw
                               )  # Yaw - It is the angle around the y-axis,
            roll = 'Roll:' + str(hands.palm_normal.roll
                                 )  # Roll - It is the angle around the z-axis
            hand_scale_motion = hands.scale_factor(
                frame
            )  # A positive value representing the heuristically determined scaling change ratio.

            if hands.is_right:
                side = 'Right Hand'
            else:
                side = 'Left Hand'
            print side + ',' + palm_cord + ', ' + wrist_cord
            print 'Postion:' + str(position) + ', velocity:' + str(
                velocity) + ', direction vector:' + str(direc)
            print pitch + ',' + yaw + ',' + roll
            hand_scale_motion = hand.scale_factor(start_frame)

            # Transforming Finger Coordinates into the Hand’s Frame of Reference
            hand_x_basis = hands.basis.x_basis  # The basis orients the x-axis sideways across the hand,
            hand_y_basis = hands.basis.y_basis  # the y-axis parallel with the palm normal.
            hand_z_basis = hands.basis.z_basis  # the z-axis pointing forward,
            hand_origin = hands.palm_position  # The origin of the transform is the palm_position.

            # Transforming Finger coordinates to Hand's Frame of reference.
            hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis,
                                         hand_z_basis, hand_origin)
            hand_transform = hand_transform.rigid_inverse()
            print hand_transform

            # getting data for each finger
            for finger in hands.fingers:
                pos = 'Transformed Position:'
                direc = 'Transformed Direction:'
                # getting the finger_type from all_fingers list
                finger_type = self.all_fingers[finger.type]

                # Transform matrix is created using the Leap Matrix class and it transform direction and positions.
                transformed_position = hand_transform.transform_point(
                    finger.tip_position)
                transformed_direction = hand_transform.transform_direction(
                    finger.direction)
                print 'Finger:' + finger_type
                print pos, transformed_position, direc, transformed_direction

                # getting the bone data for each individual finger
                for bones in range(0, n):
                    # getting the bone_type from all_bones list
                    bone_type = str(self.all_bones[finger.bone(bones).type])
                    # getting the direction vector all 4 bones in a finger. For thumb the distal will always be (0,0,0)
                    dir_vec = str(finger.bone(bones).direction)
                    print bone_type, dir_vec
Exemple #9
0
    def on_frame(self, controller):
        frame = controller.frame()
        # Code for transformed data points from Leap Motion developer guide
        for hand in frame.hands:
            hand_x_basis = hand.basis.x_basis
            hand_y_basis = hand.basis.y_basis
            hand_z_basis = hand.basis.z_basis
            hand_origin = hand.palm_position
            hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis,
                                         hand_z_basis, hand_origin)
            hand_transform = hand_transform.rigid_inverse()

            for finger in hand.fingers:
                transformed_position = hand_transform.transform_point(
                    finger.tip_position)
                transformed_direction = hand_transform.transform_direction(
                    finger.direction)
                signData[self.finger_names[finger.type] +
                         " position"] = (transformed_position.x,
                                         transformed_position.y,
                                         transformed_position.z)
                signData[self.finger_names[finger.type] +
                         " direction"] = (transformed_direction.x,
                                          transformed_direction.y,
                                          transformed_direction.z)

        for gesture in frame.gestures():
            if gesture.type == Leap.Gesture.TYPE_CIRCLE:
                circle = CircleGesture(gesture)

                # Determine clock direction using the angle between the pointable and the circle normal
                if circle.pointable.direction.angle_to(
                        circle.normal) <= Leap.PI / 2:
                    clockwiseness = "clockwise"
                else:
                    clockwiseness = "counterclockwise"

                # Calculate the angle swept since the last frame
                swept_angle = 0
                if circle.state != Leap.Gesture.STATE_START:
                    previous_update = CircleGesture(
                        controller.frame(1).gesture(circle.id))
                    swept_angle = (circle.progress -
                                   previous_update.progress) * 2 * Leap.PI

            if gesture.type == Leap.Gesture.TYPE_SWIPE:
                swipe = SwipeGesture(gesture)
                print "  Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % (
                    gesture.id, self.state_names[gesture.state],
                    swipe.position, swipe.direction, swipe.speed)

            if not (frame.hands.is_empty and frame.gestures().is_empty):
                print ""

        def state_string(self, state):
            if state == Leap.Gesture.STATE_START:
                return "STATE_START"

            if state == Leap.Gesture.STATE_UPDATE:
                return "STATE_UPDATE"

            if state == Leap.Gesture.STATE_STOP:
                return "STATE_STOP"

            if state == Leap.Gesture.STATE_INVALID:
                return "STATE_INVALID"
    def on_frame(self, controller):
        self.current_feature_dict = dict()
        # Get the most recent frame and report some basic information
        frame = controller.frame()
        current_time = time.time()
        if len(frame.hands) > 0:
            elapsed_time = current_time - self.__last_time_capture
            if elapsed_time > self.__cupture_every:
                self.__last_time_capture = time.time()
                features = {
                    "left": [0.0 for i in range(88)],
                    "right": [0.0 for i in range(88)],
                }
                for hand in frame.hands:
                    feature_fector = []
                    hand_type = "left" if hand.is_left else "right"
                    # get palm possition
                    feature_fector.append(math.fabs(
                        hand.palm_position[0]))  # pos in  x dir
                    feature_fector.append(math.fabs(
                        hand.palm_position[1]))  # pos in y dir
                    feature_fector.append(math.fabs(
                        hand.palm_position[2]))  # pos in z dir
                    # palm directions and angels
                    feature_fector.append(math.fabs(
                        hand.direction[0]))  # dir in  x dir
                    feature_fector.append(math.fabs(
                        hand.direction[1]))  # dir in y dir
                    feature_fector.append(math.fabs(
                        hand.direction[2]))  # dir in z dir
                    feature_fector.append(hand.direction.pitch *
                                          Leap.RAD_TO_DEG)
                    feature_fector.append(hand.direction.roll *
                                          Leap.RAD_TO_DEG)
                    feature_fector.append(hand.direction.yaw * Leap.RAD_TO_DEG)
                    # fingures
                    hand_x_basis = hand.basis.x_basis
                    hand_y_basis = hand.basis.y_basis
                    hand_z_basis = hand.basis.z_basis
                    hand_origin = hand.palm_position
                    hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis,
                                                 hand_z_basis, hand_origin)
                    hand_transform = hand_transform.rigid_inverse()
                    fingures_vectors = {
                        'Thumb': None,
                        'Index': None,
                        'Middle': None,
                        'Ring': None,
                        'Pinky': None
                    }
                    for finger in hand.fingers:
                        fingure_name = self.finger_names[finger.type]
                        transformed_direction = hand_transform.transform_direction(
                            finger.direction)
                        fingures_vectors[fingure_name] = Leap.Vector(
                            finger.direction)
                        feature_fector.append(
                            math.fabs(transformed_direction[0]))
                        feature_fector.append(
                            math.fabs(transformed_direction[1]))
                        feature_fector.append(
                            math.fabs(transformed_direction[3]))
                        for i in range(0, 4):
                            bone = finger.bone(i)
                            diff_x = math.fabs(bone.next_joint[0] -
                                               hand.palm_position[0])
                            diff_y = math.fabs(bone.next_joint[1] -
                                               hand.palm_position[1])
                            diff_z = math.fabs(bone.next_joint[2] -
                                               hand.palm_position[2])
                            feature_fector.extend([diff_x, diff_y, diff_z])

                    # get angles between fingures
                    for count in range(len(self.finger_names) - 1):
                        current_fingure_vector = fingures_vectors[
                            self.finger_names[count]]
                        next_fingure_vector = fingures_vectors[
                            self.finger_names[count + 1]]
                        feature_fector.append(
                            current_fingure_vector.angle_to(
                                next_fingure_vector) * (180.0 / math.pi))
                    features[hand_type] = feature_fector

                total_fingure_vector = [len(frame.hands)]
                total_fingure_vector.extend(features['left'])
                total_fingure_vector.extend(features['right'])
                if self.__new_feature_signal:
                    self.__new_feature_signal(total_fingure_vector)
Exemple #11
0
while True:
    message = socket.recv()  # RECEIVE REQUEST

    frame = controller.frame()
    hands = frame.hands

    if not hands.is_empty:

        hand = hands[0]

        hand_x_basis = hand.basis.x_basis
        hand_y_basis = hand.basis.y_basis
        hand_z_basis = hand.basis.z_basis
        hand_origin = hand.palm_position
        handTransform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis,
                                    hand_origin)
        handTransform = handTransform.rigid_inverse()

        fingers = hand.fingers
        palmPosition = hand.palm_position

        thumbTipDirection = None
        indexTipDirection = None
        middleTipDirection = None
        ringTipDirection = None
        pinkyTipDirection = None

        indexDirection = None
        middleDirection = None
        ringDirection = None
        pinkyDirection = None
Exemple #12
0
    def on_frame(self, controller):
        # Get the most recent frame and report some basic information
        global writeheaders
        global second_iteration
        global start_time
        global elapsed, gate, d

        cach_dict = OrderedDict((key, 0) for key in fieldnames)

        frame = controller.frame()
        #for gesture in frame.gestures():
        gesture = frame.gestures()
        if (gesture[0].type == Leap.Gesture.TYPE_KEY_TAP or second_iteration):

            if (second_iteration == False):
                print("START recording AFTER:")
                for x in range(1, 4, 1):
                    print(x)
                    time.sleep(1)
                    if (x == 3):
                        print("RECORDING")
                        print("\n")
                        print("\n")
                second_iteration = True

            if (len(frame.hands) <= 2) and (len(frame.hands) != 0):
                total_speed = []

                #cach_dict.update({"Frame id" : str(frame.id) , "timestamp" : str(frame.timestamp) , "hands" : str(len(frame.hands)) ,"fingers" : str(len(frame.fingers))})

                for hand in frame.hands:
                    # 0 for right hand and 1 for left
                    handType = "L" if hand.is_left else "R"
                    hand_x_basis = hand.basis.x_basis
                    hand_y_basis = hand.basis.y_basis
                    hand_z_basis = hand.basis.z_basis
                    hand_origin = hand.palm_position
                    hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis,
                                                 hand_z_basis, hand_origin)
                    hand_transform = hand_transform.rigid_inverse()

                    normal = hand.palm_normal
                    direction = hand.direction

                    arm = hand.arm

                    sphere = hand.sphere_center

                    j = 0
                    prev_finger_position = 0
                    prev_finger_direction = 0
                    for finger in hand.fingers:
                        transformed_position = hand_transform.transform_point(
                            finger.tip_position)
                        transformed_direction = hand_transform.transform_direction(
                            finger.direction)
                        sp = finger.tip_velocity.magnitude
                        #print finger.tip_position
                        #print transformed_position
                        total_speed.append(sp)
                        name = str(self.finger_names[finger.type])

                        #cach_dict.update({handType+"."+name+"_id" :  str(finger.id) , handType+"."+name+"_length" : str(finger.length)
                        #                , handType+"."+name+"_width" : str(finger.width)})
                        # TTP: tip to palm
                        cach_dict.update({
                            handType + "." + name + ".TTP":
                            str(transformed_position.magnitude)
                        })
                        # TNA: to normal angel
                        cach_dict.update({
                            handType + "." + name + ".TNA":
                            str(
                                normal.angle_to(transformed_direction) *
                                (180 / 3.14))
                        })
                        # print normal.angle_to(transformed_direction)*(180/3.14)

                        if (j == 0):
                            #print name
                            prev_name = name
                            prev_finger_direction = finger.direction
                            j = j + 1
                        else:
                            #print name
                            #print prev_finger_direction.angle_to(finger.direction)*(180/3.14)
                            cach_dict.update({
                                handType + "." + prev_name + "_" + name + ".ang":
                                str(
                                    prev_finger_direction.angle_to(
                                        finger.direction) * (180 / 3.14))
                            })
                            prev_finger_position = finger.tip_position
                            prev_finger_direction = finger.direction
                            prev_name = name
                            j = j + 1

                        # Get bones
                        for b in range(0, 4):
                            bone = finger.bone(b)
                            name2 = handType + "." + name + "_" + str(
                                self.bone_names[bone.type])
                            #cach_dict.update({name2+ "_s_x" : str(bone.prev_joint[0]) ,name2+ "_s_y" : str(bone.prev_joint[1]) ,name2+ "_s_z" : str(bone.prev_joint[2]) ,name2 + "_e_x" : str(bone.next_joint[0]),name2 + "_e_y" : str(bone.next_joint[1]) ,name2 + "_e_z" : str(bone.next_joint[2])  ,name2 +"_d_x" : str(bone.direction[0]),name2 +"_d_y" : str(bone.direction[1]),name2 +"_d_z" : str(bone.direction[2])})

                if not frame.hands.is_empty:
                    pass

                c = pd.DataFrame.from_dict(cach_dict.values(),
                                           orient="columns",
                                           dtype=float)

                c = c.transpose()

                #print(c)
                #print(c.shape)

                d = d.append(c, ignore_index=True)

                if ((max(total_speed)) <= 70):

                    w = averaging(d)

                    #print(w.shape)

                    #print(w)

                    #print(w.ix[0])
                    w = w.ix[0]
                    w = w.values.reshape(1, -1)
                    #print(w.shape)

                    prd(w)

                    d = pd.DataFrame(dtype=float)
                    time.sleep(1)
Exemple #13
0
            def on_frame(self, controller):
                    # Get the most recent frame and report some basic information
                    global writeheaders
                    global second_iteration
                    global start_time
                    global elapsed,gate,person,s_r,s_l , finished , cam_fin ,cam_start
                    org_path = os.getcwd()
                    path= org_path + "//dataset//original//"+ ges_type + "//p" + str(person) + "//"
                    if not os.path.exists(path):
                                    os.makedirs(path)
                                    os.chdir(path)
                    with open(path + filename + ".csv", 'ab')as csvfile:
                        cach_dict = {key: 0 for key in fieldnames}
                        w = csv.DictWriter(csvfile, fieldnames=fieldnames)

                        if writeheaders==True:
                            w.writeheader()
                            writeheaders=False

                        frame = controller.frame()
                        #for gesture in frame.gestures():
                        gesture=frame.gestures()

                        if(second_iteration==False):
                                        print("START recording AFTER:")
                                        for x in range(1,4,1):
                                            time.sleep(1)
                                            if(x==3):
                                                t=Thread(target=func_to_be_threaded,args=(path,filename))
                                                t.start()
                                                while(1):
                                                    if cam_start:
                                                        break
                                                print ("Leap_RECORDING")
                                                print u"ابدأ"
                                            print(x)
                                        second_iteration = True

                        if (len(frame.hands) <= 2) and (len(frame.hands)!= 0):
                                        R_speed = []
                                        L_speed = []
                                        total_speed=[]
                                        R_palm=Leap.Vector(0,0,0)
                                        R_Thumb=Leap.Vector(0,0,0)
                                        R_Index=Leap.Vector(0,0,0)
                                        R_Middle=Leap.Vector(0,0,0)
                                        R_Ring=Leap.Vector(0,0,0)
                                        R_Pinky=Leap.Vector(0,0,0)
                                        L_palm=Leap.Vector(0,0,0)
                                        L_Thumb=Leap.Vector(0,0,0)
                                        L_Index=Leap.Vector(0,0,0)
                                        L_Middle=Leap.Vector(0,0,0)
                                        L_Ring=Leap.Vector(0,0,0)
                                        L_Pinky=Leap.Vector(0,0,0)

                                        # previous = controller.frame(1)
                                        cach_dict.update({"Frame id" : str(frame.id) , "timestamp" : str(frame.timestamp) , "hands" : str(len(frame.hands)) ,
                                                          "fingers" : str(len(frame.fingers))})
                                        #print(cach_dict["Frame id"])




                                        for hand in frame.hands:
                                            # 0 for right hand and 1 for left
                                            handType = "L" if hand.is_left else "R"
                                            hand_x_basis = hand.basis.x_basis
                                            hand_y_basis = hand.basis.y_basis
                                            hand_z_basis = hand.basis.z_basis
                                            hand_origin = hand.palm_position
                                            hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis,
                                                                         hand_origin)
                                            hand_transform = hand_transform.rigid_inverse()

                                            cach_dict.update({handType+".hand": 1,handType+".hand.id": str(hand.id) ,handType+".hand.palm_position_x": str(hand.palm_position[0]),handType+".hand.palm_position_y": str(hand.palm_position[1]),handType+".hand.palm_position_z": str(hand.palm_position[2])})
                                            #print hand.palm_position


                                            # Get the hand's normal vector and direction
                                            normal = hand.palm_normal
                                            cach_dict.update({handType+".normal direction_x" : str(normal[0]) , handType+".normal direction_y" : str(normal[1])
                                            ,handType+".normal direction_z" : str(normal[2]) })
                                            #print normal
                                            direction = hand.direction
                                            cach_dict.update({handType+".hand direction_x" : str(direction[0]) , handType+".hand direction_y" : str(direction[1])
                                            ,handType+".hand direction_z" : str(direction[2]) })

                                            # Calculate the hand's pitch, roll, and yaw angles
                                            cach_dict.update({handType+".pitch" : str(direction.pitch * Leap.RAD_TO_DEG) , handType+".roll" : str(normal.roll*Leap.RAD_TO_DEG) , handType+".yaw" : str(direction.yaw * Leap.RAD_TO_DEG)})


                                        # Get arm bone
                                            arm = hand.arm
                                            cach_dict.update({handType+".Arm direction_x" : str(arm.direction[0]) ,  handType+".wrist position_x" : str(arm.wrist_position[0]) , handType+".elbow position_x" : str(arm.elbow_position[0]),
                                                        handType+".Arm direction_y" : str(arm.direction[1]) ,  handType+".wrist position_y" : str(arm.wrist_position[1]) , handType+".elbow position_y" : str(arm.elbow_position[1]),
                                                        handType+".Arm direction_z" : str(arm.direction[2]) ,  handType+".wrist position_z" : str(arm.wrist_position[2]) , handType+".elbow position_z" : str(arm.elbow_position[2]),
                                                                                 })
                                            sphere=hand.sphere_center


                                            cach_dict.update({handType + ".sphere_center_x": str(sphere[0]),
                                                              handType + ".sphere_center_y": str(sphere[1]),
                                                              handType + ".sphere_center_z": str(sphere[2])})

                                            cach_dict.update({ handType + ".sphere_radius": str(hand.sphere_radius) })

                                            cach_dict.update({ 'R_scaling_factor': str(s_r),'L_scaling_factor': str(s_l) })

                                            # Get fingers
                                            j=0
                                            prev_finger_position = 0
                                            prev_finger_direction = 0


                                            for finger in hand.fingers:
                                                transformed_position = hand_transform.transform_point(finger.tip_position)
                                                transformed_direction = hand_transform.transform_direction(finger.direction)
                                                sp = finger.tip_velocity.magnitude
                                                #print finger.tip_position
                                                #print transformed_position

                                                if(handType=="R"):
                                                    R_speed.append(sp)
                                                    R_palm=hand.palm_position
                                                    R_Thumb=hand.fingers[0].tip_position
                                                    R_Index=hand.fingers[1].tip_position
                                                    R_Middle=hand.fingers[2].tip_position
                                                    R_Ring=hand.fingers[3].tip_position
                                                    R_Pinky=hand.fingers[4].tip_position
                                                else:
                                                    L_speed.append(sp)
                                                    L_palm=hand.palm_position
                                                    L_Thumb=hand.fingers[0].tip_position
                                                    L_Index=hand.fingers[1].tip_position
                                                    L_Middle=hand.fingers[2].tip_position
                                                    L_Ring=hand.fingers[3].tip_position
                                                    L_Pinky=hand.fingers[4].tip_position

                                                total_speed.append(sp)
                                                name = str(self.finger_names[finger.type])

                                                cach_dict.update({handType+"."+name+"_id" :  str(finger.id) , handType+"."+name+"_length" : str(finger.length)
                                                                , handType+"."+name+"_width" : str(finger.width)})
                                                #without transformation
                                                cach_dict.update({handType+"."+name+"_tip_w_x" : str(finger.tip_position[0]) ,  handType+"."+name+"_tip_w_y" : str(finger.tip_position[1]) ,
                                                 handType+"."+name+"_tip_w_z" : str(finger.tip_position[2]) })

                                                cach_dict.update({handType+"."+name+"_tip_x" : str(transformed_position[0]) ,  handType+"."+name+"_tip_y" : str(transformed_position[1]) ,
                                                 handType+"."+name+"_tip_z" : str(transformed_position[2]) })

                                                 #scaling x ,y and z with range -1:1
                                                if(handType=="R"):
                                                 cach_dict.update({handType+"."+name+"_tip_scaled_x" : str(transformed_position[0]/s_r) ,  handType+"."+name+"_tip_scaled_y" : str(transformed_position[1]/s_r) ,
                                                  handType+"."+name+"_tip_scaled_z" : str(transformed_position[2]/s_r) })
                                                else:
                                                 cach_dict.update({handType+"."+name+"_tip_scaled_x" : str(transformed_position[0]/s_l) ,  handType+"."+name+"_tip_scaled_y" : str(transformed_position[1]/s_l) ,
                                                  handType+"."+name+"_tip_scaled_z" : str(transformed_position[2]/s_l) })

                                                # TTP: tip to palm
                                                cach_dict.update({handType + "." + name + ".TTP": str(transformed_position.magnitude)})
                                                if(handType=="R"):
                                                    cach_dict.update({handType + "." + name + ".TTP_sacled": str(transformed_position.magnitude/s_r)})
                                                else:
                                                    cach_dict.update({handType + "." + name + ".TTP_sacled": str(transformed_position.magnitude/s_l)})
                                                # TNA: to normal angel
                                                cach_dict.update({handType + "." + name + ".TNA": str(normal.angle_to(transformed_direction)*(180/3.14))})
                                                # print normal.angle_to(transformed_direction)*(180/3.14)
                                                #THA: tip to hand angel
                                                cach_dict.update({handType + "." + name + ".THA": str(direction.angle_to(transformed_direction)*(180/3.14))})

                                                cach_dict.update({handType + "." + name + ".transformed_direction_x": str(transformed_direction[0]),
                                                                    handType + "." + name + ".transformed_direction_y": str(transformed_direction[1]),
                                                                    handType + "." + name + ".transformed_direction_z": str(transformed_direction[2])})

                                                cach_dict.update({handType + "." + name + ".direction_x": str(finger.direction[0]),
                                                                    handType + "." + name + ".direction_y": str(finger.direction[1]),
                                                                    handType + "." + name + ".direction_z": str(finger.direction[2])})

                                                if (j==0):
                                                    #print name
                                                    prev_name=name
                                                    prev_finger_direction = finger.direction
                                                    j=j+1
                                                else:
                                                    #print name
                                                    #print prev_finger_direction.angle_to(finger.direction)*(180/3.14)
                                                    cach_dict.update({handType + "." +prev_name+"_"+ name + ".ang": str(prev_finger_direction.angle_to(finger.direction)*(180/3.14))})
                                                    prev_finger_position = finger.tip_position
                                                    prev_finger_direction = finger.direction
                                                    prev_name=name
                                                    j = j + 1



                                                #R.name_tip "tuple for test"  -+   R.name_tip  L.name_tip`````R.name_tip: transformed_position

                                                '''
                                                print"noraml",normal
                                                print"position",name,transformed_position

                                                #other solutions for normal to tip angels
                                                print normal.angle_to(transformed_position)*(180/3.14)
                                                print normal.angle_to(finger.tip_position) * (180 / 3.14)
                                                print normal.angle_to(finger.direction) * (180 / 3.14)

                                                #other solution angel between fingers
                                                prev_finger_position=finger.tip_position
                                                print prev_finger_position.angle_to(finger.tip_position)*(180/3.14)

                                                '''
                                                #print transformed_position.magnitude
                                                #print  hand_origin.distance_to(finger.tip_position)     right function also


                                                # Get bones
                                                for b in range(0, 4):
                                                    bone = finger.bone(b)
                                                    name2=handType+"."+name+ "_" + str(self.bone_names[bone.type])
                                                    cach_dict.update({name2+ "_s_x" : str(bone.prev_joint[0]) ,name2+ "_s_y" : str(bone.prev_joint[1]) ,name2+ "_s_z" : str(bone.prev_joint[2]) ,name2 + "_e_x" : str(bone.next_joint[0]),name2 + "_e_y" : str(bone.next_joint[1]) ,
                                                    name2 + "_e_z" : str(bone.next_joint[2])  ,name2 +"_d_x" : str(bone.direction[0]),name2 +"_d_y" : str(bone.direction[1]),name2 +"_d_z" : str(bone.direction[2])})
                                            if handType == 'R':
                                                cach_dict.update({"R_speed": str(max(R_speed)) })
                                            else:
                                                cach_dict.update({"L_speed": str(max(L_speed)) })


                                        if (len(frame.hands) < 2):
                                            cach_dict.update({'palms_dis':str(1000000),'Thumb_dis':str(1000000),'Index_dis':str(1000000),
                                            'Middle_dis':str(1000000),'Ring_dis':str(1000000),'Pinky_dis':str(1000000)})
                                        else:
                                            cach_dict.update({'palms_dis':str(R_palm.distance_to(L_palm)),'Thumb_dis':str(R_Thumb.distance_to(L_Thumb)),
                                            'Index_dis':str(R_Index.distance_to(L_Index)),
                                            'Middle_dis':str(R_Middle.distance_to(L_Middle)),'Ring_dis':str(R_Ring.distance_to(L_Ring)),
                                            'Pinky_dis':str(R_Pinky.distance_to(L_Pinky))})

                                        if not frame.hands.is_empty:
                                            pass

                                        w.writerow(cach_dict)                               #changed

                                        if ((max(total_speed)) <= 150):
                                            if (gate):
                                                w.writerow({})
                                            gate=False

                                        if ((max(total_speed))  <= 120):

                                            if(start_time==0):
                                                start_time = time.time()
                                                #print(start_time)

                                            elapsed=time.time()-start_time
                                            print("time:",elapsed)
                                            if (elapsed >=0.5 ):
                                                finished= True
                                                print("Leap oFF")
                                                time.sleep(1)
                                                if cam_fin:
                                                    csvfile.close()
                                                    sys.exit("REOCRDING FINSHED")

                                        if ((max(total_speed))  >= 120):
                                            start_time=0
                                            gate=True


                                    # if((total_speed / len(frame.hands))<=70):

                                     #       sys.exit("REOCRDING FINSHED")


                    csvfile.close()
Exemple #14
0
def rot_matrix_from_basis(basis):
    x_basis, y_basis, z_basis = basis
    matrix = Leap.Matrix(Leap.Vector(*x_basis), Leap.Vector(*y_basis),
                         Leap.Vector(*z_basis)).to_array_3x3()
    return np.reshape(matrix, newshape=(3, 3))
Exemple #15
0
            def on_frame(self, controller):
                    # Get the most recent frame and report some basic information
                        global writeheaders
                        global second_iteration
                        global start_time
                        global count,gate,person, l_s , r_s



                        frame = controller.frame()
                        #for gesture in frame.gestures():
                        gesture=frame.gestures()

                        if(second_iteration==False):
                                        print("START recording AFTER:")
                                        for x in range(1,4,1):
                                            print(x)
                                            time.sleep(1)
                                            if(x==3):
                                                print ("RECORDING")
                                        second_iteration = True

                        if (len(frame.hands) <= 2) and (len(frame.hands)!= 0):
                                        total_speed = []



                                        for hand in frame.hands:
                                            # 0 for right hand and 1 for left
                                            handType = "L" if hand.is_left else "R"
                                            hand_x_basis = hand.basis.x_basis
                                            hand_y_basis = hand.basis.y_basis
                                            hand_z_basis = hand.basis.z_basis
                                            hand_origin = hand.palm_position
                                            hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis,
                                                                         hand_origin)
                                            hand_transform = hand_transform.rigid_inverse()

                                            #cach_dict.update({handType+".hand": 1,handType+".hand.id": str(hand.id) ,handType+".hand.palm_position_x": str(hand.palm_position[0]),handType+".hand.palm_position_y": str(hand.palm_position[1]),handType+".hand.palm_position_z": str(hand.palm_position[2])})
                                            #print hand.palm_position

                                            # Get the hand's normal vector and direction
                                            normal = hand.palm_normal
                                            #print normal
                                            direction = hand.direction
                                            # Calculate the hand's pitch, roll, and yaw angles
                                            #cach_dict.update({handType+".pitch" : str(direction.pitch * Leap.RAD_TO_DEG) , handType+".roll" : str(normal.roll*Leap.RAD_TO_DEG) , handType+".yaw" : str(direction.yaw * Leap.RAD_TO_DEG)})


                                            # Get arm bone
                                            arm = hand.arm
                                            sphere=hand.sphere_center

                                            f=hand.fingers[2]
                                            scaling_factor=hand_transform.transform_point(f.tip_position)
                                            s=scaling_factor.magnitude

                                            if (handType=="R"):
                                                r_s=r_s + s

                                            if (handType=="L"):
                                                l_s=l_s + s




                                        if (count == 100):


                                                   print(l_s/100)
                                                   print(r_s/100)
                                                   z=np.array(((r_s/100),(l_s/100)))
                                                   print(z)

                                                   org_path= os.getcwd()
                                                   req_path=org_path+"\\dataset\\original\\static\\p" + str(person) + "\\"

                                                   if not os.path.exists(req_path):
                                                          os.makedirs(req_path)
                                                          os.chdir(req_path)
                                                   np.save(filename +'.npy', z)
                                                   req_path=org_path+"\\dataset\\original\\dynamic\\p" + str(person) + "\\"
                                                   if not os.path.exists(req_path):
                                                          os.makedirs(req_path)
                                                          os.chdir(req_path)
                                                   np.save(filename +'.npy', z)
                                                   os.chdir(org_path)
                                                   sys.exit("REOCRDING FINSHED")

                                        count=count+1

                                        if not frame.hands.is_empty:
                                            pass
Exemple #16
0
            def on_frame(self, controller):
                    # Get the most recent frame and report some basic information
                    global writeheaders
                    global second_iteration
                    global start_time
                    global elapsed,gate,d



                    cach_dict = OrderedDict((key, 0) for  key in fieldnames)


                    frame = controller.frame()
                        #for gesture in frame.gestures():
                    gesture=frame.gestures()
                    if (gesture[0].type == Leap.Gesture.TYPE_KEY_TAP or second_iteration):

                                if(second_iteration==False):
                                    print("START recording AFTER:")
                                    for x in range(1,4,1):
                                        print(x)
                                        time.sleep(1)
                                        if(x==3):
                                            print ("RECORDING")
                                    second_iteration = True

                                if (len(frame.hands) <= 2) and (len(frame.hands)!= 0):
                                    total_speed = []

                                    # previous = controller.frame(1)
                                    cach_dict.update({"Frame id" : str(frame.id) , "timestamp" : str(frame.timestamp) , "hands" : str(len(frame.hands)) ,
                                                      "fingers" : str(len(frame.fingers))})
                                    #print(cach_dict["Frame id"])


                                    for hand in frame.hands:
                                        # 0 for right hand and 1 for left
                                        handType = "L" if hand.is_left else "R"
                                        hand_x_basis = hand.basis.x_basis
                                        hand_y_basis = hand.basis.y_basis
                                        hand_z_basis = hand.basis.z_basis
                                        hand_origin = hand.palm_position
                                        hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis,
                                                                     hand_origin)
                                        hand_transform = hand_transform.rigid_inverse()

                                        cach_dict.update({handType+".hand": 1,handType+".hand.id": str(hand.id) ,handType+".hand.palm_position_x": str(hand.palm_position[0]),handType+".hand.palm_position_y": str(hand.palm_position[1]),handType+".hand.palm_position_z": str(hand.palm_position[2])})
                                        #print hand.palm_position


                                        # Get the hand's normal vector and direction
                                        normal = hand.palm_normal
                                        #print normal
                                        direction = hand.direction
                                        # Calculate the hand's pitch, roll, and yaw angles
                                        cach_dict.update({handType+".pitch" : str(direction.pitch * Leap.RAD_TO_DEG) , handType+".roll" : str(normal.roll*Leap.RAD_TO_DEG) , handType+".yaw" : str(direction.yaw * Leap.RAD_TO_DEG)})

                                    # Get arm bone
                                        arm = hand.arm
                                        cach_dict.update({handType+".Arm direction_x" : str(arm.direction[0]) ,  handType+".wrist position_x" : str(arm.wrist_position[0]) , handType+".elbow position_x" : str(arm.elbow_position[0]),
                                                    handType+".Arm direction_y" : str(arm.direction[1]) ,  handType+".wrist position_y" : str(arm.wrist_position[1]) , handType+".elbow position_y" : str(arm.elbow_position[1]),
                                                    handType+".Arm direction_z" : str(arm.direction[2]) ,  handType+".wrist position_z" : str(arm.wrist_position[2]) , handType+".elbow position_z" : str(arm.elbow_position[2]),
                                                                             })
                                        sphere=hand.sphere_center


                                        cach_dict.update({handType + ".sphere_center_x": str(sphere[0]),
                                                          handType + ".sphere_center_y": str(sphere[1]),
                                                          handType + ".sphere_center_z": str(sphere[2])})

                                        # Get fingers
                                        j=0
                                        prev_finger_position = 0
                                        prev_finger_direction = 0
                                        for finger in hand.fingers:
                                            transformed_position = hand_transform.transform_point(finger.tip_position)
                                            transformed_direction = hand_transform.transform_direction(finger.direction)
                                            sp = finger.tip_velocity.magnitude
                                            #print finger.tip_position
                                            #print transformed_position
                                            total_speed.append(sp)
                                            name = str(self.finger_names[finger.type])

                                            cach_dict.update({handType+"."+name+"_id" :  str(finger.id) , handType+"."+name+"_length" : str(finger.length)
                                                            , handType+"."+name+"_width" : str(finger.width)})
                                            # TTP: tip to palm
                                            cach_dict.update({handType + "." + name + ".TTP": str(transformed_position.magnitude)})
                                            # TNA: to normal angel
                                            cach_dict.update({handType + "." + name + ".TNA": str(normal.angle_to(transformed_direction)*(180/3.14))})
                                            # print normal.angle_to(transformed_direction)*(180/3.14)

                                            if (j==0):
                                                #print name
                                                prev_name=name
                                                prev_finger_direction = finger.direction
                                                j=j+1
                                            else:
                                                #print name
                                                #print prev_finger_direction.angle_to(finger.direction)*(180/3.14)
                                                cach_dict.update({handType + "." +prev_name+"_"+ name + ".ang": str(prev_finger_direction.angle_to(finger.direction)*(180/3.14))})
                                                prev_finger_position = finger.tip_position
                                                prev_finger_direction = finger.direction
                                                prev_name=name
                                                j = j + 1




                                            # Get bones
                                            for b in range(0, 4):
                                                bone = finger.bone(b)
                                                name2=handType+"."+name+ "_" + str(self.bone_names[bone.type])
                                                cach_dict.update({name2+ "_s_x" : str(bone.prev_joint[0]) ,name2+ "_s_y" : str(bone.prev_joint[1]) ,name2+ "_s_z" : str(bone.prev_joint[2]) ,name2 + "_e_x" : str(bone.next_joint[0]),name2 + "_e_y" : str(bone.next_joint[1]) ,name2 + "_e_z" : str(bone.next_joint[2])  ,name2 +"_d_x" : str(bone.direction[0]),name2 +"_d_y" : str(bone.direction[1]),name2 +"_d_z" : str(bone.direction[2])})


                                    if not frame.hands.is_empty:
                                        pass

                                    #dealing with frames

                                    c=pd.DataFrame.from_dict(cach_dict, orient="index", dtype=float)
                                    c=c.transpose()
                                    c.columns=fieldnames

                                    c= c[['L.hand.palm_position_x', 'L.hand.palm_position_y','L.hand.palm_position_z'
                                        , 'L.pitch', 'L.roll', 'L.yaw', 'L.Arm direction_x', 'L.Arm direction_y', 'L.Arm direction_z'
                                        , 'R.hand.palm_position_x','R.hand.palm_position_y', 'R.hand.palm_position_z', 'R.pitch',
                                        'R.roll', 'R.yaw', 'R.Arm direction_x', 'R.Arm direction_y','R.Arm direction_z',
                                        'R.Thumb.TTP','R.Index.TTP','R.Middle.TTP','R.Ring.TTP','R.Pinky.TTP','L.Thumb.TTP',
                                        'L.Index.TTP','L.Middle.TTP','L.Ring.TTP','L.Pinky.TTP',
                                        'R.Thumb.TNA', 'R.Index.TNA', 'R.Middle.TNA', 'R.Ring.TNA', 'R.Pinky.TNA', 'L.Thumb.TNA',
                                        'L.Index.TNA', 'L.Middle.TNA', 'L.Ring.TNA', 'L.Pinky.TNA',
                                        'R.Thumb_Index.ang', 'R.Index_Middle.ang', 'R.Middle_Ring.ang', 'R.Ring_Pinky.ang',
                                        'L.Thumb_Index.ang', 'L.Index_Middle.ang', 'L.Middle_Ring.ang', 'L.Ring_Pinky.ang']]
                                    #print(c.shape)
                                    d=d.append(c,ignore_index=True )

                                    '''
                                    if ((max(total_speed)) <= 150):
                                        if (gate):
                                            w.writerow({})
                                        gate=False
                                    '''
                                    #if ((max(total_speed)) >= 80):
                                    #    gate=0


                                    if (((max(total_speed)) <= 80) ):
                                        #print("ddddddd is :",d.shape)
                                        #d.to_csv("before.csv", sep=',', index=True, header=True)
                                        w=averaging(d)
                                        #w.to_csv("after.csv", sep=',', index=True, header=True)
                                        #print(w.shape)
                                        ze=w.shape[1]
                                        print(w.shape)

                                        #print(w)
                                        data=np.zeros((1,135,ze))

                                        ind = w.shape[0]
                                        #print(w.ix[0])
                                        #data[0, 0,:] = w.ix[1]
                                        #print(data[0, 0,:])
                                        #print('data2')
                                        #print(data[0, 1,:])
                                        #sys.exit("REOCRDING FINSHED")
                                        #print(data)

                                        for j in range(ind):
                                            data[0, j] = w.ix[j]
                                        #print(data)

                                        #if((gate==0)):
                                        predict_fun(data)

                                        d=pd.DataFrame(dtype=float)
Exemple #17
0
    def on_frame(self, controller):
        # Get the most recent frame and report some basic information
        global writeheaders
        global second_iteration
        global start_time
        global elapsed, gate, d

        cach_dict = {key: 0 for key in fieldnames}

        frame = controller.frame()
        #for gesture in frame.gestures():
        gesture = frame.gestures()
        if (gesture[0].type == Leap.Gesture.TYPE_KEY_TAP or second_iteration):

            if (second_iteration == False):
                print("START recording AFTER:")
                for x in range(1, 4, 1):
                    print(x)
                    time.sleep(1)
                    if (x == 3):
                        print("RECORDING")
                second_iteration = True

            if (len(frame.hands) <= 2) and (len(frame.hands) != 0):
                total_speed = []

                # previous = controller.frame(1)
                cach_dict.update({
                    "Frame id": str(frame.id),
                    "timestamp": str(frame.timestamp),
                    "hands": str(len(frame.hands)),
                    "fingers": str(len(frame.fingers))
                })
                #print(cach_dict["Frame id"])

                for hand in frame.hands:
                    # 0 for right hand and 1 for left
                    handType = "L" if hand.is_left else "R"
                    hand_x_basis = hand.basis.x_basis
                    hand_y_basis = hand.basis.y_basis
                    hand_z_basis = hand.basis.z_basis
                    hand_origin = hand.palm_position
                    hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis,
                                                 hand_z_basis, hand_origin)
                    hand_transform = hand_transform.rigid_inverse()

                    cach_dict.update({
                        handType + ".hand":
                        1,
                        handType + ".hand.id":
                        str(hand.id),
                        handType + ".hand.palm_position_x":
                        str(hand.palm_position[0]),
                        handType + ".hand.palm_position_y":
                        str(hand.palm_position[1]),
                        handType + ".hand.palm_position_z":
                        str(hand.palm_position[2])
                    })
                    #print hand.palm_position

                    # Get the hand's normal vector and direction
                    normal = hand.palm_normal
                    #print normal
                    direction = hand.direction
                    # Calculate the hand's pitch, roll, and yaw angles
                    cach_dict.update({
                        handType + ".pitch":
                        str(direction.pitch * Leap.RAD_TO_DEG),
                        handType + ".roll":
                        str(normal.roll * Leap.RAD_TO_DEG),
                        handType + ".yaw":
                        str(direction.yaw * Leap.RAD_TO_DEG)
                    })

                    # Get arm bone
                    arm = hand.arm
                    cach_dict.update({
                        handType + ".Arm direction_x":
                        str(arm.direction[0]),
                        handType + ".wrist position_x":
                        str(arm.wrist_position[0]),
                        handType + ".elbow position_x":
                        str(arm.elbow_position[0]),
                        handType + ".Arm direction_y":
                        str(arm.direction[1]),
                        handType + ".wrist position_y":
                        str(arm.wrist_position[1]),
                        handType + ".elbow position_y":
                        str(arm.elbow_position[1]),
                        handType + ".Arm direction_z":
                        str(arm.direction[2]),
                        handType + ".wrist position_z":
                        str(arm.wrist_position[2]),
                        handType + ".elbow position_z":
                        str(arm.elbow_position[2]),
                    })
                    sphere = hand.sphere_center

                    cach_dict.update({
                        handType + ".sphere_center_x":
                        str(sphere[0]),
                        handType + ".sphere_center_y":
                        str(sphere[1]),
                        handType + ".sphere_center_z":
                        str(sphere[2])
                    })

                    # Get fingers
                    j = 0
                    prev_finger_position = 0
                    prev_finger_direction = 0
                    for finger in hand.fingers:
                        transformed_position = hand_transform.transform_point(
                            finger.tip_position)
                        transformed_direction = hand_transform.transform_direction(
                            finger.direction)
                        sp = finger.tip_velocity.magnitude
                        #print finger.tip_position
                        #print transformed_position
                        total_speed.append(sp)
                        name = str(self.finger_names[finger.type])

                        cach_dict.update({
                            handType + "." + name + "_id":
                            str(finger.id),
                            handType + "." + name + "_length":
                            str(finger.length),
                            handType + "." + name + "_width":
                            str(finger.width)
                        })
                        # TTP: tip to palm
                        cach_dict.update({
                            handType + "." + name + ".TTP":
                            str(transformed_position.magnitude)
                        })
                        # TNA: to normal angel
                        cach_dict.update({
                            handType + "." + name + ".TNA":
                            str(
                                normal.angle_to(transformed_direction) *
                                (180 / 3.14))
                        })
                        # print normal.angle_to(transformed_direction)*(180/3.14)

                        if (j == 0):
                            #print name
                            prev_name = name
                            prev_finger_direction = finger.direction
                            j = j + 1
                        else:
                            #print name
                            #print prev_finger_direction.angle_to(finger.direction)*(180/3.14)
                            cach_dict.update({
                                handType + "." + prev_name + "_" + name + ".ang":
                                str(
                                    prev_finger_direction.angle_to(
                                        finger.direction) * (180 / 3.14))
                            })
                            prev_finger_position = finger.tip_position
                            prev_finger_direction = finger.direction
                            prev_name = name
                            j = j + 1

                        #R.name_tip "tuple for test"  -+   R.name_tip  L.name_tip`````R.name_tip: transformed_position
                        '''
                                            print"noraml",normal
                                            print"position",name,transformed_position

                                            #other solutions for normal to tip angels
                                            print normal.angle_to(transformed_position)*(180/3.14)
                                            print normal.angle_to(finger.tip_position) * (180 / 3.14)
                                            print normal.angle_to(finger.direction) * (180 / 3.14)

                                            #other solution angel between fingers
                                            prev_finger_position=finger.tip_position
                                            print prev_finger_position.angle_to(finger.tip_position)*(180/3.14)

                                            '''
                        #print transformed_position.magnitude
                        #print  hand_origin.distance_to(finger.tip_position)     right function also

                        # Get bones
                        for b in range(0, 4):
                            bone = finger.bone(b)
                            name2 = handType + "." + name + "_" + str(
                                self.bone_names[bone.type])
                            cach_dict.update({
                                name2 + "_s_x":
                                str(bone.prev_joint[0]),
                                name2 + "_s_y":
                                str(bone.prev_joint[1]),
                                name2 + "_s_z":
                                str(bone.prev_joint[2]),
                                name2 + "_e_x":
                                str(bone.next_joint[0]),
                                name2 + "_e_y":
                                str(bone.next_joint[1]),
                                name2 + "_e_z":
                                str(bone.next_joint[2]),
                                name2 + "_d_x":
                                str(bone.direction[0]),
                                name2 + "_d_y":
                                str(bone.direction[1]),
                                name2 + "_d_z":
                                str(bone.direction[2])
                            })

                if not frame.hands.is_empty:
                    pass

                #dealing with frames
                c = pd.Series(cach_dict.values(), dtype=float)
                #print(c.shape)
                #print(c.dtypes)
                #c=c.drop([0], axis=1)
                #c=pd.to_numeric(c)
                c = c.transpose()
                #print(c.shape)
                #print(c.dtypes)
                d = d.append(c, ignore_index=True)
                #print("d is :",d.shape)
                #print("d is :",d.dtypes)
                #    w.writerow()                               #changed
                '''
                                    if ((max(total_speed)) <= 150):
                                        if (gate):
                                            w.writerow({})
                                        gate=False
                                    '''
                if ((max(total_speed)) <= 70):
                    #print("ddddddd is :",d.shape)
                    #d.to_csv("before.csv", sep=',', index=True, header=True)
                    w = averaging(d)
                    #w.to_csv("after.csv", sep=',', index=True, header=True)
                    print(w.shape)

                    #print(w)
                    data = np.zeros((1, 135, 426))

                    ind = w.shape[0]
                    #print(w.ix[0])
                    data[0, 0, :] = w.ix[1]
                    #print(data[0, 0,:])
                    #print('data2')
                    #print(data[0, 1,:])
                    #sys.exit("REOCRDING FINSHED")
                    print(data)
                    """for j in range(ind):
                                            data[0, j] = w.ix[j]
                                        print(data)

                                        """
                    predict_fun(data)
                    #print(data.shape)
                    d = pd.DataFrame(dtype=float)
                    time.sleep(1)
                    '''
Exemple #18
0
    def on_frame(self, controller):
        # Get the most recent frame and report some basic information
        global writeheaders
        global second_iteration
        global start_time
        global elapsed, gate, d

        cach_dict = OrderedDict((key, 0) for key in fieldnames)

        frame = controller.frame()
        #for gesture in frame.gestures():
        gesture = frame.gestures()
        if (gesture[0].type == Leap.Gesture.TYPE_KEY_TAP or second_iteration):

            if (second_iteration == False):
                print("يبدأ التسجيل بعد 3 ثواني:")
                for x in range(1, 4, 1):
                    #print(x)
                    time.sleep(1)
                    if (x == 3):
                        print("ابدأ")
                second_iteration = True

            if (len(frame.hands) <= 2) and (len(frame.hands) != 0):
                R_speed = []
                L_speed = []
                total_speed = []
                R_palm = Leap.Vector(0, 0, 0)
                R_Thumb = Leap.Vector(0, 0, 0)
                R_Index = Leap.Vector(0, 0, 0)
                R_Middle = Leap.Vector(0, 0, 0)
                R_Ring = Leap.Vector(0, 0, 0)
                R_Pinky = Leap.Vector(0, 0, 0)
                L_palm = Leap.Vector(0, 0, 0)
                L_Thumb = Leap.Vector(0, 0, 0)
                L_Index = Leap.Vector(0, 0, 0)
                L_Middle = Leap.Vector(0, 0, 0)
                L_Ring = Leap.Vector(0, 0, 0)
                L_Pinky = Leap.Vector(0, 0, 0)

                # previous = controller.frame(1)
                cach_dict.update({
                    "Frame id": str(frame.id),
                    "timestamp": str(frame.timestamp),
                    "hands": str(len(frame.hands)),
                    "fingers": str(len(frame.fingers))
                })
                #print(cach_dict["Frame id"])

                for hand in frame.hands:
                    # 0 for right hand and 1 for left
                    handType = "L" if hand.is_left else "R"
                    hand_x_basis = hand.basis.x_basis
                    hand_y_basis = hand.basis.y_basis
                    hand_z_basis = hand.basis.z_basis
                    hand_origin = hand.palm_position
                    hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis,
                                                 hand_z_basis, hand_origin)
                    hand_transform = hand_transform.rigid_inverse()

                    cach_dict.update({
                        handType + ".hand":
                        1,
                        handType + ".hand.id":
                        str(hand.id),
                        handType + ".hand.palm_position_x":
                        str(hand.palm_position[0]),
                        handType + ".hand.palm_position_y":
                        str(hand.palm_position[1]),
                        handType + ".hand.palm_position_z":
                        str(hand.palm_position[2])
                    })
                    #print hand.palm_position

                    # Get the hand's normal vector and direction
                    normal = hand.palm_normal
                    cach_dict.update({
                        handType + ".normal direction_x":
                        str(normal[0]),
                        handType + ".normal direction_y":
                        str(normal[1]),
                        handType + ".normal direction_z":
                        str(normal[2])
                    })
                    #print normal
                    direction = hand.direction
                    cach_dict.update({
                        handType + ".hand direction_x":
                        str(direction[0]),
                        handType + ".hand direction_y":
                        str(direction[1]),
                        handType + ".hand direction_z":
                        str(direction[2])
                    })

                    # Calculate the hand's pitch, roll, and yaw angles
                    cach_dict.update({
                        handType + ".pitch":
                        str(direction.pitch * Leap.RAD_TO_DEG),
                        handType + ".roll":
                        str(normal.roll * Leap.RAD_TO_DEG),
                        handType + ".yaw":
                        str(direction.yaw * Leap.RAD_TO_DEG)
                    })

                    # Get arm bone
                    arm = hand.arm
                    cach_dict.update({
                        handType + ".Arm direction_x":
                        str(arm.direction[0]),
                        handType + ".wrist position_x":
                        str(arm.wrist_position[0]),
                        handType + ".elbow position_x":
                        str(arm.elbow_position[0]),
                        handType + ".Arm direction_y":
                        str(arm.direction[1]),
                        handType + ".wrist position_y":
                        str(arm.wrist_position[1]),
                        handType + ".elbow position_y":
                        str(arm.elbow_position[1]),
                        handType + ".Arm direction_z":
                        str(arm.direction[2]),
                        handType + ".wrist position_z":
                        str(arm.wrist_position[2]),
                        handType + ".elbow position_z":
                        str(arm.elbow_position[2]),
                    })
                    sphere = hand.sphere_center

                    cach_dict.update({
                        handType + ".sphere_center_x":
                        str(sphere[0]),
                        handType + ".sphere_center_y":
                        str(sphere[1]),
                        handType + ".sphere_center_z":
                        str(sphere[2])
                    })

                    cach_dict.update(
                        {handType + ".sphere_radius": str(hand.sphere_radius)})

                    cach_dict.update({
                        'R_scaling_factor': str(s_r),
                        'L_scaling_factor': str(s_l)
                    })

                    # Get fingers
                    j = 0
                    prev_finger_position = 0
                    prev_finger_direction = 0

                    for finger in hand.fingers:
                        transformed_position = hand_transform.transform_point(
                            finger.tip_position)
                        transformed_direction = hand_transform.transform_direction(
                            finger.direction)
                        sp = finger.tip_velocity.magnitude
                        #print finger.tip_position
                        #print transformed_position

                        if (handType == "R"):
                            R_speed.append(sp)
                            R_palm = hand.palm_position
                            R_Thumb = hand.fingers[0].tip_position
                            R_Index = hand.fingers[1].tip_position
                            R_Middle = hand.fingers[2].tip_position
                            R_Ring = hand.fingers[3].tip_position
                            R_Pinky = hand.fingers[4].tip_position
                        else:
                            L_speed.append(sp)
                            L_palm = hand.palm_position
                            L_Thumb = hand.fingers[0].tip_position
                            L_Index = hand.fingers[1].tip_position
                            L_Middle = hand.fingers[2].tip_position
                            L_Ring = hand.fingers[3].tip_position
                            L_Pinky = hand.fingers[4].tip_position

                        total_speed.append(sp)
                        name = str(self.finger_names[finger.type])

                        cach_dict.update({
                            handType + "." + name + "_id":
                            str(finger.id),
                            handType + "." + name + "_length":
                            str(finger.length),
                            handType + "." + name + "_width":
                            str(finger.width)
                        })
                        #without transformation
                        cach_dict.update({
                            handType + "." + name + "_tip_w_x":
                            str(finger.tip_position[0]),
                            handType + "." + name + "_tip_w_y":
                            str(finger.tip_position[1]),
                            handType + "." + name + "_tip_w_z":
                            str(finger.tip_position[2])
                        })

                        cach_dict.update({
                            handType + "." + name + "_tip_x":
                            str(transformed_position[0]),
                            handType + "." + name + "_tip_y":
                            str(transformed_position[1]),
                            handType + "." + name + "_tip_z":
                            str(transformed_position[2])
                        })

                        #scaling x ,y and z with range -1:1
                        if (handType == "R"):
                            cach_dict.update({
                                handType + "." + name + "_tip_scaled_x":
                                str(transformed_position[0] / s_r),
                                handType + "." + name + "_tip_scaled_y":
                                str(transformed_position[1] / s_r),
                                handType + "." + name + "_tip_scaled_z":
                                str(transformed_position[2] / s_r)
                            })
                        else:
                            cach_dict.update({
                                handType + "." + name + "_tip_scaled_x":
                                str(transformed_position[0] / s_l),
                                handType + "." + name + "_tip_scaled_y":
                                str(transformed_position[1] / s_l),
                                handType + "." + name + "_tip_scaled_z":
                                str(transformed_position[2] / s_l)
                            })

                        # TTP: tip to palm
                        cach_dict.update({
                            handType + "." + name + ".TTP":
                            str(transformed_position.magnitude)
                        })
                        if (handType == "R"):
                            cach_dict.update({
                                handType + "." + name + ".TTP_sacled":
                                str(transformed_position.magnitude / s_r)
                            })
                        else:
                            cach_dict.update({
                                handType + "." + name + ".TTP_sacled":
                                str(transformed_position.magnitude / s_l)
                            })
                        # TNA: to normal angel
                        cach_dict.update({
                            handType + "." + name + ".TNA":
                            str(
                                normal.angle_to(transformed_direction) *
                                (180 / 3.14))
                        })
                        # print normal.angle_to(transformed_direction)*(180/3.14)
                        #THA: tip to hand angel
                        cach_dict.update({
                            handType + "." + name + ".THA":
                            str(
                                direction.angle_to(transformed_direction) *
                                (180 / 3.14))
                        })

                        cach_dict.update({
                            handType + "." + name + ".transformed_direction_x":
                            str(transformed_direction[0]),
                            handType + "." + name + ".transformed_direction_y":
                            str(transformed_direction[1]),
                            handType + "." + name + ".transformed_direction_z":
                            str(transformed_direction[2])
                        })

                        cach_dict.update({
                            handType + "." + name + ".direction_x":
                            str(finger.direction[0]),
                            handType + "." + name + ".direction_y":
                            str(finger.direction[1]),
                            handType + "." + name + ".direction_z":
                            str(finger.direction[2])
                        })

                        if (j == 0):
                            #print name
                            prev_name = name
                            prev_finger_direction = finger.direction
                            j = j + 1
                        else:
                            #print name
                            #print prev_finger_direction.angle_to(finger.direction)*(180/3.14)
                            cach_dict.update({
                                handType + "." + prev_name + "_" + name + ".ang":
                                str(
                                    prev_finger_direction.angle_to(
                                        finger.direction) * (180 / 3.14))
                            })
                            prev_finger_position = finger.tip_position
                            prev_finger_direction = finger.direction
                            prev_name = name
                            j = j + 1

                        # Get bones
                        for b in range(0, 4):
                            bone = finger.bone(b)
                            name2 = handType + "." + name + "_" + str(
                                self.bone_names[bone.type])
                            cach_dict.update({
                                name2 + "_s_x":
                                str(bone.prev_joint[0]),
                                name2 + "_s_y":
                                str(bone.prev_joint[1]),
                                name2 + "_s_z":
                                str(bone.prev_joint[2]),
                                name2 + "_e_x":
                                str(bone.next_joint[0]),
                                name2 + "_e_y":
                                str(bone.next_joint[1]),
                                name2 + "_e_z":
                                str(bone.next_joint[2]),
                                name2 + "_d_x":
                                str(bone.direction[0]),
                                name2 + "_d_y":
                                str(bone.direction[1]),
                                name2 + "_d_z":
                                str(bone.direction[2])
                            })
                    if handType == 'R':
                        cach_dict.update({"R_speed": str(max(R_speed))})
                    else:
                        cach_dict.update({"L_speed": str(max(L_speed))})

                if (len(frame.hands) < 2):
                    cach_dict.update({
                        'palms_dis': str(1000000),
                        'Thumb_dis': str(1000000),
                        'Index_dis': str(1000000),
                        'Middle_dis': str(1000000),
                        'Ring_dis': str(1000000),
                        'Pinky_dis': str(1000000)
                    })
                else:
                    cach_dict.update({
                        'palms_dis':
                        str(R_palm.distance_to(L_palm)),
                        'Thumb_dis':
                        str(R_Thumb.distance_to(L_Thumb)),
                        'Index_dis':
                        str(R_Index.distance_to(L_Index)),
                        'Middle_dis':
                        str(R_Middle.distance_to(L_Middle)),
                        'Ring_dis':
                        str(R_Ring.distance_to(L_Ring)),
                        'Pinky_dis':
                        str(R_Pinky.distance_to(L_Pinky))
                    })

                if not frame.hands.is_empty:
                    pass

                #dealing with frames

                c = pd.DataFrame.from_dict(cach_dict,
                                           orient="index",
                                           dtype=float)
                c = c.transpose()
                c.columns = fieldnames

                c = c[[
                    'L.pitch', 'L.roll', 'L.sphere_radius', 'R_speed',
                    'L_speed', 'L.yaw', 'L.Arm direction_x',
                    'L.Arm direction_y', 'L.Arm direction_z',
                    'R.hand.palm_position_x', 'R.hand.palm_position_y',
                    'R.hand.palm_position_z', 'R.pitch', 'R.roll', 'R.yaw',
                    'R.Arm direction_x', 'R.Arm direction_y',
                    'R.Arm direction_z', 'R.Thumb.TTP', 'R.Index.TTP',
                    'R.Middle.TTP', 'R.Ring.TTP', 'R.Pinky.TTP', 'L.Thumb.TTP',
                    'L.Index.TTP', 'L.Middle.TTP', 'L.Ring.TTP', 'L.Pinky.TTP',
                    'R.Thumb.TNA', 'R.Index.TNA', 'R.Middle.TNA', 'R.Ring.TNA',
                    'R.Pinky.TNA', 'L.Thumb.TNA', 'L.Index.TNA',
                    'L.Middle.TNA', 'L.Ring.TNA', 'L.Pinky.TNA',
                    'R.Thumb_Index.ang', 'R.Index_Middle.ang',
                    'R.Middle_Ring.ang', 'R.Ring_Pinky.ang',
                    'L.Thumb_Index.ang', 'L.Index_Middle.ang',
                    'L.Middle_Ring.ang', 'L.Ring_Pinky.ang',
                    'R.Thumb.TTP_sacled', 'R.Index.TTP_sacled',
                    'R.Middle.TTP_sacled', 'R.Ring.TTP_sacled',
                    'R.Pinky.TTP_sacled', 'L.Thumb.TTP_sacled',
                    'L.Index.TTP_sacled', 'L.Middle.TTP_sacled',
                    'L.Ring.TTP_sacled', 'L.Pinky.TTP_sacled',
                    "L.hand direction_x", "L.hand direction_y",
                    "L.hand direction_z", "R.hand direction_x",
                    "R.hand direction_y", "R.hand direction_z", 'R.Thumb.THA',
                    'R.Index.THA', 'R.Middle.THA', 'R.Ring.THA', 'R.Pinky.THA',
                    'L.Thumb.THA', 'L.Index.THA', 'L.Middle.THA', 'L.Ring.THA',
                    'L.Pinky.THA', 'R.sphere_radius',
                    'R.Thumb.transformed_direction_x',
                    'R.Thumb.transformed_direction_y',
                    'R.Thumb.transformed_direction_z',
                    'R.Index.transformed_direction_x',
                    'R.Index.transformed_direction_y',
                    'R.Index.transformed_direction_z',
                    'R.Middle.transformed_direction_x',
                    'R.Middle.transformed_direction_y',
                    'R.Middle.transformed_direction_z',
                    'R.Ring.transformed_direction_x',
                    'R.Ring.transformed_direction_y',
                    'R.Ring.transformed_direction_z',
                    'R.Pinky.transformed_direction_x',
                    'R.Pinky.transformed_direction_y',
                    'R.Pinky.transformed_direction_z',
                    'L.Thumb.transformed_direction_x',
                    'L.Thumb.transformed_direction_y',
                    'L.Thumb.transformed_direction_z',
                    'L.Index.transformed_direction_x',
                    'L.Index.transformed_direction_y',
                    'L.Index.transformed_direction_z',
                    'L.Middle.transformed_direction_x',
                    'L.Middle.transformed_direction_y',
                    'L.Middle.transformed_direction_z',
                    'L.Ring.transformed_direction_x',
                    'L.Ring.transformed_direction_y',
                    'L.Ring.transformed_direction_z',
                    'L.Pinky.transformed_direction_x',
                    'L.Pinky.transformed_direction_y',
                    'L.Pinky.transformed_direction_z', 'L.wrist position_x',
                    'L.wrist position_y', 'L.wrist position_z',
                    'R.wrist position_x', 'R.wrist position_y',
                    'R.wrist position_z'
                ]]
                #print(c.shape)
                d = d.append(c, ignore_index=True)
                '''
                                                        if ((max(total_speed)) <= 150):
                                                            if (gate):
                                                                w.writerow({})
                                                            gate=False
                                                        '''
                #if ((max(total_speed)) >= 80):
                #    gate=0

                if (((max(total_speed)) <= 80)):

                    if (start_time == 0):
                        start_time = time.time()

                    elapsed = time.time() - start_time
                    #print("time:",elapsed)
                    if (elapsed >= 0.5):

                        w = averaging(d)

                        ze = w.shape[1]
                        #print(w.shape)

                        data = np.zeros((1, 145, ze))

                        ind = w.shape[0]

                        for j in range(ind):
                            data[0, j] = w.ix[j]

                        predict_fun(data)

                        d = pd.DataFrame(dtype=float)

                        second_iteration = False

                if ((max(total_speed)) >= 120):
                    start_time = 0
                    gate = True