Beispiel #1
0
    "rfemur": "RThigh",
    "rtibia": "RLeg",
    "rfoot": "RFoot",
    "rtoes": "RToes",
}

targetmap = {}
for mocap_joint, rest_joint in joint_map.items():
    # if rest_joint not in ["RHand", "LHand"]:
    anim.rotations[:, rest_map[rest_joint]] += mocap.rotations[:, mocap_map[
        mocap_joint]]
    targetmap[rest_map[rest_joint]] = targets[:, mocap_map[mocap_joint]]

ik = JacobianInverseKinematics(anim,
                               targetmap,
                               iterations=5000,
                               damping=7,
                               silent=False)
ik()

BVH.save(retargetpath, anim, rest_names, 1.0 / 25, order='xyz')

############################################################################################

with open(constraintpath, 'r') as f:
    content = f.readlines()

content = [x.strip() for x in content]
joints = [list(filter(None, c.split('\t'))) for c in content]
from bvh import Bvh
with open(retargetpath) as f:
Beispiel #2
0
    print('%i of %i Processing %s' % (i + 1, len(database), filename))

    hdm05anim, names, ftime = BVH.load(filename)

    anim_scale = np.sqrt(np.mean(hdm05anim.offsets**2))

    targets = Animation.positions_global(hdm05anim)

    anim = rest.copy()
    anim.positions = anim.positions.repeat(len(targets), axis=0)
    anim.rotations.qs = anim.rotations.qs.repeat(len(targets), axis=0)
    anim.positions[:, 0] = targets[:, 0]
    anim.rotations[:, :] = hdm05anim.rotations[:, :]

    targetmap = {}
    for ti in range(targets.shape[1]):
        targetmap[ti] = targets[:, ti]

    ik = JacobianInverseKinematics(anim,
                                   targetmap,
                                   iterations=10,
                                   damping=2.0,
                                   silent=True)
    ik()

    BVH.save(
        './hdm05/' +
        os.path.split(filename)[1].replace('/', '_').replace('\\', '_'), anim,
        names, 1.0 / 120)
Beispiel #3
0
    def retarget_skeleton(self, normalizedPose):
        """
        Retargets the Panoptic Skeleton onto CMU skeleton
        :param normalizedPose: Panoptic skeleton (57, F)
        :return: retargeted animation
        """

        # reshape
        normalizedPose = np.transpose(normalizedPose)  # (frames,57)
        normalizedPose = normalizedPose.reshape(normalizedPose.shape[0], 19,
                                                3)  # (frames,19,3)

        # Flip Y axis
        normalizedPose[:, :, 1] = -normalizedPose[:, :, 1]

        # calculate panoptic height
        panopticThigh = normalizedPose[:, 6, :] - normalizedPose[:, 7, :]
        panopticThigh = panopticThigh**2
        panopticHeight = np.mean(np.sqrt(np.sum(panopticThigh, axis=1)))

        # load the rest skeleton
        rest, names, _ = BVH.load('meta/rest.bvh')  # temp

        # create a mock animation for the required duration
        anim = rest.copy()
        anim.positions = anim.positions.repeat(normalizedPose.shape[0], axis=0)
        anim.rotations.qs = anim.rotations.qs.repeat(normalizedPose.shape[0],
                                                     axis=0)

        # get the FK solved global positions
        cmuMocapJoints = Animation.positions_global(anim)

        # calculate CMU skeleton height
        cmuThigh = cmuMocapJoints[:, 2, :] - cmuMocapJoints[:, 3, :]
        cmuThigh = cmuThigh**2
        cmuMocapHeight = np.mean(np.sqrt(np.sum(cmuThigh, axis=1)))
        cmuMocapHeight = cmuMocapHeight * 0.9

        # scale the skelton appropriately
        scaleRatio = cmuMocapHeight / panopticHeight
        print("cmuMocapHeight: %f, panopticHeight %f, scaleRatio: %f " %
              (cmuMocapHeight, panopticHeight, scaleRatio))
        normalizedPose = normalizedPose * scaleRatio  # rescaling

        # compute mean across vector
        across1 = normalizedPose[:,
                                 3] - normalizedPose[:,
                                                     9]  # Right -> left (3)  Shoulder
        across0 = normalizedPose[:,
                                 6] - normalizedPose[:,
                                                     12]  # Right -> left (6) Hips
        across = across0 + across1  # frame x 3
        across = across / np.sqrt(
            (across**2).sum(axis=-1))[...,
                                      np.newaxis]  ##frame x 3. Unit vectors

        # compute forward direction
        forward = np.cross(across, np.array([[0, -1, 0]]))
        forward = forward / np.sqrt((forward**2).sum(axis=-1))[..., np.newaxis]
        target = np.array([[0, 0, 1]]).repeat(len(forward), axis=0)

        # Set root's movement by hipCenter joints (idx=2)
        anim.positions[:, 0] = normalizedPose[:, 2] + np.array([0.0, 2.4, 0.0])
        anim.rotations[:, 0:1] = -Quaternions.between(forward,
                                                      target)[:, np.newaxis]

        targetmap = {}
        for k in self.mapping:
            targetmap[k] = normalizedPose[:, self.mapping[k], :]

        # Retarget using JacobianIK
        ik = JacobianInverseKinematics(anim,
                                       targetmap,
                                       iterations=20,
                                       damping=10.0,
                                       silent=True)
        ik()

        # scale skeleton appropriately
        anim.positions = anim.positions * 6.25
        anim.offsets = anim.offsets * 6.25

        # Do FK recover 3D joint positions, select required Joints only
        positions = Animation.positions_global(anim)
        positions = positions[:, self.jointIdx]

        return positions
Beispiel #4
0
        18: 12,
        19: 13,
        20: 14,
        25: 8,
        26: 9,
        27: 10,
    }

    targetmap = {}

    for k in mapping:
        targetmap[k] = xsenstargets[:, mapping[k]]

    ik = JacobianInverseKinematics(xanim,
                                   targetmap,
                                   iterations=10,
                                   damping=2.0,
                                   silent=False)
    ik()

    BVH.save('./edin_xsens/capture_%03i.bvh' % i, xanim, names, 1.0 / 120)

    #########

    kinecttargets = io.loadmat('../external/edin_kinect/' + kin +
                               '.mat')['skel'].astype(np.float)
    kinecttargets = kinecttargets[kin_start:kin_stop, kin_skel, :, :3]

    kinecttargets = interpolate.griddata(np.linspace(0, 1,
                                                     len(kinecttargets) * 1),
                                         kinecttargets,
Beispiel #5
0
def jac_anim_for_projection_sparse(
        x, skeleton, curPose3D, rootTrans, curPose2D, floorNormal, floorPoint,
        pointWeights, data_joint_weights, valid3DInd, valid2DInd,
        jointsSmoothnessWeights, velConstraints, projWeight,
        smoothnessWeightVel, smoothnessWeightAcc, dataWeight, velWeight,
        floorWeight):
    ''' calc jacobian '''
    noFrames = curPose3D.shape[0]
    noProjPts = curPose3D.shape[1]
    valid3DInd = np.arange(noProjPts)
    valid2DInd = np.arange(noProjPts)
    noPts = curPose3D.shape[1]
    #forward kinematics
    x = np.reshape(x, [noFrames, -1])
    root = x[:, :3]
    angles = x[:, 3:]
    anim = skeleton.copy()
    anim.orients.qs = skeleton.orients.qs.copy()
    anim.offsets = skeleton.offsets.copy()
    anim.positions = skeleton.positions.repeat(noFrames, axis=0)
    anim.rotations = Quaternions.from_euler(angles.reshape(
        (noFrames, noPts, 3)),
                                            order='xyz',
                                            world=True)
    poses3D = Animation.positions_global(anim)
    poses3D[:, 0] = root

    y = 0 * poses3D
    for j in range(poses3D.shape[1]):
        y[:, j, :] = poses3D[:, BACKWARD_MAPPING[j], :]
    y = np.reshape(y, [-1])  #[root positions; joint positions]

    #calculate jacobian dE/dP = np.zeros((noTerms, noFrames * noPts * 3))
    jac_dp = jac_root_all_for_projection(y,
                                         curPose3D,
                                         curPose2D,
                                         floorNormal,
                                         floorPoint,
                                         pointWeights,
                                         data_joint_weights,
                                         valid3DInd,
                                         valid2DInd,
                                         jointsSmoothnessWeights,
                                         velConstraints,
                                         projWeight,
                                         smoothnessWeightVel,
                                         smoothnessWeightAcc,
                                         dataWeight,
                                         velWeight,
                                         floorWeight,
                                         root_idx=ROOT_IDX)

    #calculate jocabian dP/dR = np.zeros((noFrames * (noPts * 3) * (noPts * 3))
    targetmap = {}
    for ee in range(poses3D.shape[1] - 1):
        targetmap[ee + 1] = poses3D[:, ee + 1]
    ik = JacobianInverseKinematics(anim,
                                   targetmap,
                                   translate=False,
                                   iterations=50,
                                   damping=2,
                                   silent=False)
    gt = Animation.transforms_global(anim)
    gp = gt[:, :, :, 3]
    gp = gp[:, :, :3] / gp[:, :, 3, np.newaxis]
    gr = Quaternions.from_transforms(gt)
    """ Calculate Descendants """
    descendants = AnimationStructure.descendants_mask(anim.parents)
    tdescendants = np.eye(anim.shape[1]) + descendants
    first_descendants = descendants[:, 1:].repeat(3, axis=0).astype(int)
    first_tdescendants = tdescendants[:, 1:].repeat(3, axis=0).astype(int)
    jac_dr = ik.jacobian(angles, gp, gr, ik.targets, first_descendants,
                         first_tdescendants)

    #full jacobian dE/dR
    noVars = (noPts + 1) * 3
    jac = np.zeros((jac_dp.shape[0], noFrames * noVars))
    # print("jac dimension is %d %d" % (jac.shape[0], jac.shape[1]))
    for fr in range(noFrames):
        varIndex1 = fr * noPts * 3
        varIndex2 = fr * noVars
        jt1 = jac_dp[:, varIndex1:varIndex1 + noPts * 3]
        jt2 = np.zeros(jt1.shape)
        for p in range(noPts):
            q = FORWARD_MAPPING[p]
            jt2[:, p * 3:p * 3 + 3] = jt1[:, q * 3:q * 3 + 3]
        jt = np.zeros((jac_dp.shape[0], noVars))
        jt[:, :3] = jt2[:, :3]
        jt[:, 3:] = np.matmul(jt2[:, 3:], jac_dr[fr])
        jac[:, varIndex2:varIndex2 + noVars] = jt

    # smoothness term for euler angle velocity
    jac_smooth = np.zeros(((noFrames - 1) * noVars, noFrames * noVars))
    count = 0
    for fr in range(noFrames - 1):
        varIndex = fr * noVars
        varIndexNext = (fr + 1) * noVars
        for j in range(noPts + 1):
            jac_smooth[count + 0,
                       varIndex + 0] = smoothnessWeightVel * SMOOTH_VEL_EULER_X
            jac_smooth[count + 1,
                       varIndex + 1] = smoothnessWeightVel * SMOOTH_VEL_EULER_Y
            jac_smooth[count + 2,
                       varIndex + 2] = smoothnessWeightVel * SMOOTH_VEL_EULER_Z
            jac_smooth[count + 0, varIndexNext +
                       0] = -smoothnessWeightVel * SMOOTH_VEL_EULER_X
            jac_smooth[count + 1, varIndexNext +
                       1] = -smoothnessWeightVel * SMOOTH_VEL_EULER_Y
            jac_smooth[count + 2, varIndexNext +
                       2] = -smoothnessWeightVel * SMOOTH_VEL_EULER_Z
            count = count + 3
            varIndex = varIndex + 3
            varIndexNext = varIndexNext + 3
    jac = np.concatenate((jac, jac_smooth), axis=0)
    jac_sparse = sparse.lil_matrix(jac)

    return jac_sparse