Beispiel #1
0
def load_model(fname_or_dict):
    dd = ready_arguments(fname_or_dict)

    args = {
        'pose': dd['pose'],
        'v': dd['v_posed'],
        'J': dd['J'],
        'weights': dd['weights'],
        'kintree_table': dd['kintree_table'],
        'xp': ch,
        'want_Jtr': True,
        'bs_style': dd['bs_style']
    }

    result, Jtr = verts_core(**args)
    result = result + dd['trans'].reshape((1, 3))
    result.J_transformed = Jtr + dd['trans'].reshape((1, 3))

    for k, v in dd.items():
        setattr(result, k, v)

    return result
Beispiel #2
0
def load_model(fname_or_dict, ncomps=6, flat_hand_mean=False, v_template=None):
    ''' This model loads the fully articulable HAND SMPL model,
    and replaces the pose DOFS by ncomps from PCA'''

    from mano.webuser.verts import verts_core
    import numpy as np
    import chumpy as ch
    import pickle
    import scipy.sparse as sp
    np.random.seed(1)

    if not isinstance(fname_or_dict, dict):
        smpl_data = pickle.load(open(fname_or_dict, 'rb'), encoding='latin1')
        # smpl_data = pickle.load(open(fname_or_dict, 'rb'))
    else:
        smpl_data = fname_or_dict

    rot = 3  # for global orientation!!!

    hands_components = smpl_data['hands_components']
    hands_mean = np.zeros(hands_components.shape[1]
                          ) if flat_hand_mean else smpl_data['hands_mean']
    hands_coeffs = smpl_data['hands_coeffs'][:, :ncomps]

    selected_components = np.vstack((hands_components[:ncomps]))
    hands_mean = hands_mean.copy()

    pose_coeffs = ch.zeros(rot + selected_components.shape[0])
    full_hand_pose = pose_coeffs[rot:(rot + ncomps)].dot(selected_components)

    smpl_data['fullpose'] = ch.concatenate(
        (pose_coeffs[:rot], hands_mean + full_hand_pose))
    smpl_data['pose'] = pose_coeffs

    Jreg = smpl_data['J_regressor']
    if not sp.issparse(Jreg):
        smpl_data['J_regressor'] = (sp.csc_matrix(
            (Jreg.data, (Jreg.row, Jreg.col)), shape=Jreg.shape))

    # slightly modify ready_arguments to make sure that it uses the fullpose
    # (which will NOT be pose) for the computation of posedirs
    dd = ready_arguments(smpl_data, posekey4vposed='fullpose')

    # create the smpl formula with the fullpose,
    # but expose the PCA coefficients as smpl.pose for compatibility
    args = {
        'pose': dd['fullpose'],
        'v': dd['v_posed'],
        'J': dd['J'],
        'weights': dd['weights'],
        'kintree_table': dd['kintree_table'],
        'xp': ch,
        'want_Jtr': True,
        'bs_style': dd['bs_style'],
    }

    result_previous, meta = verts_core(**args)

    result = result_previous + dd['trans'].reshape((1, 3))
    result.no_translation = result_previous

    if meta is not None:
        for field in ['Jtr', 'A', 'A_global', 'A_weighted']:
            if (hasattr(meta, field)):
                setattr(result, field, getattr(meta, field))

    setattr(result, 'Jtr', meta)
    if hasattr(result, 'Jtr'):
        result.J_transformed = result.Jtr + dd['trans'].reshape((1, 3))

    for k, v in dd.items():
        setattr(result, k, v)

    if v_template is not None:
        result.v_template[:] = v_template

    return result
def load_model_withInputs_poseCoeffs(
    fname_or_dict,
    chRot,
    chPoseCoeff,
    chTrans,
    chBetas,
    ncomps=6,
    flat_hand_mean=False,
    v_template=None,
    shared_args=None,
):

    import numpy as np
    import chumpy as ch
    import pickle
    import scipy.sparse as sp
    np.random.seed(1)

    if not isinstance(fname_or_dict, dict):
        # smpl_data = pickle.load(open(fname_or_dict))
        smpl_data = pickle.load(open(fname_or_dict, 'rb'), encoding='latin1')
    else:
        smpl_data = fname_or_dict

    rot = 3  # for global orientation!!!
    dof = 20

    # smpl_data['hands_components'] = np.eye(45)
    from sklearn.preprocessing import normalize
    smpl_data['hands_components'] = normalize(smpl_data['hands_components'],
                                              axis=1)
    hands_components = smpl_data['hands_components']
    hands_mean = np.zeros(hands_components.shape[1]
                          ) if flat_hand_mean else smpl_data['hands_mean']
    hands_coeffs = smpl_data['hands_coeffs'][:, :ncomps]

    selected_components = np.vstack((hands_components[:ncomps]))
    hands_mean = hands_mean.copy()

    pose_coeffs = ch.concatenate([chRot, chPoseCoeff], axis=0)
    full_hand_pose = pose_coeffs[rot:(rot + ncomps)].dot(selected_components)

    smpl_data['fullpose'] = ch.concatenate(
        (pose_coeffs[:rot], hands_mean + full_hand_pose))

    smpl_data['pose'] = pose_coeffs

    Jreg = smpl_data['J_regressor']
    if not sp.issparse(Jreg):
        smpl_data['J_regressor'] = (sp.csc_matrix(
            (Jreg.data, (Jreg.row, Jreg.col)), shape=Jreg.shape))

    # slightly modify ready_arguments to make sure that it uses the fullpose
    # (which will NOT be pose) for the computation of posedirs
    dd = ready_arguments(smpl_data,
                         posekey4vposed='fullpose',
                         shared_args=shared_args,
                         chTrans=chTrans,
                         chBetas=chBetas)

    # create the smpl formula with the fullpose,
    # but expose the PCA coefficients as smpl.pose for compatibility
    args = {
        'pose': dd['fullpose'],
        'v': dd['v_posed'],
        'J': dd['J'],
        'weights': dd['weights'],
        'kintree_table': dd['kintree_table'],
        'xp': ch,
        'want_Jtr': True,
        'bs_style': dd['bs_style'],
    }
    # print(dd['J'].r)

    result_previous, meta = verts_core(**args)
    result_noRel = result_previous + dd['trans'].reshape((1, 3))
    result = result_noRel
    result.no_translation = result_previous

    if meta is not None:
        for field in ['Jtr', 'A', 'A_global', 'A_weighted']:
            if (hasattr(meta, field)):
                setattr(result, field, getattr(meta, field))

    if hasattr(result, 'Jtr'):
        result.J_transformed = (result.Jtr + dd['trans'].reshape((1, 3)))

    for k, v in dd.items():
        setattr(result, k, v)

    if v_template is not None:
        result.v_template[:] = v_template

    return result
def load_model_1(fname_or_dict,
                 ncomps=6,
                 flat_hand_mean=False,
                 v_template=None,
                 shared_args=None,
                 optwrt='pose_coeff',
                 relRot=np.eye(3),
                 relTrans=np.array([0.0, 0.0, 0.0])):
    ''' This model loads the fully articulable HAND SMPL model,
    and replaces the pose DOFS by ncomps from PCA'''

    import numpy as np
    import chumpy as ch
    import pickle
    import scipy.sparse as sp
    np.random.seed(1)

    if not isinstance(fname_or_dict, dict):
        with open(fname_or_dict, 'rb') as f:
            smpl_data = pickle.load(f, encoding='latin1')
    else:
        smpl_data = fname_or_dict

    rot = 3  # for global orientation!!!
    dof = 20

    # smpl_data['hands_components'] = np.eye(45)
    from sklearn.preprocessing import normalize
    smpl_data['hands_components'] = normalize(smpl_data['hands_components'],
                                              axis=1)
    hands_components = smpl_data['hands_components']
    std = np.linalg.norm(hands_components, axis=1)
    hands_mean = np.zeros(hands_components.shape[1]
                          ) if flat_hand_mean else smpl_data['hands_mean']
    hands_coeffs = smpl_data['hands_coeffs'][:, :ncomps]

    selected_components = np.vstack((hands_components[:ncomps]))
    hands_mean = hands_mean.copy()

    if shared_args is not None and 'pose_coeffs' in shared_args:
        pose_coeffs = ch.zeros(rot + selected_components.shape[0])
        pose_coeffs[:len(shared_args['pose_coeffs']
                         )] = shared_args['pose_coeffs']
    else:
        pose_coeffs = ch.zeros(rot + selected_components.shape[0])
    full_hand_pose = pose_coeffs[rot:(rot + ncomps)].dot(selected_components)

    smpl_data['fullpose'] = ch.concatenate(
        (pose_coeffs[:rot], hands_mean + full_hand_pose))
    pose_dof = ch.zeros(rot + dof)

    smpl_data['pose'] = pose_coeffs
    smpl_data['pose_dof'] = pose_dof

    Jreg = smpl_data['J_regressor']
    if not sp.issparse(Jreg):
        smpl_data['J_regressor'] = (sp.csc_matrix(
            (Jreg.data, (Jreg.row, Jreg.col)), shape=Jreg.shape))

    # slightly modify ready_arguments to make sure that it uses the fullpose
    # (which will NOT be pose) for the computation of posedirs
    dd = ready_arguments(smpl_data, posekey4vposed='fullpose')

    # create the smpl formula with the fullpose,
    # but expose the PCA coefficients as smpl.pose for compatibility
    args = {
        'pose': dd['fullpose'],
        'v': dd['v_posed'],
        'J': dd['J'],
        'weights': dd['weights'],
        'kintree_table': dd['kintree_table'],
        'xp': ch,
        'want_Jtr': True,
        'bs_style': dd['bs_style'],
    }
    # print(dd['J'].r)

    result_previous, meta = verts_core(**args)
    result_noRel = result_previous + dd['trans'].reshape((1, 3))
    result = result_noRel.dot(relRot) + relTrans
    result.no_translation = result_previous

    if meta is not None:
        for field in ['Jtr', 'A', 'A_global', 'A_weighted']:
            if (hasattr(meta, field)):
                setattr(result, field, getattr(meta, field))

    if hasattr(result, 'Jtr'):
        result.J_transformed = (result.Jtr + dd['trans'].reshape(
            (1, 3))).dot(relRot) + relTrans

    for k, v in dd.items():
        setattr(result, k, v)

    if v_template is not None:
        result.v_template[:] = v_template

    return result