Exemplo n.º 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


#m= load_model( '../models/basicModel_f_lbs_10_207_0_v1.0.0.pkl' )
Exemplo n.º 2
0
def load_model(fname_or_dict, params=None):
    dd = ready_arguments(fname_or_dict)

    #save_model_json(fname_or_dict)
    #stop

    #dd['pose'][5] = 0.78

    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'],
    }

    #global tmp
    #tmp+=0.5
    #print tmp
    dd['betas'][3] = 20

    args2 = {
        'trans': dd['trans'],
        'pose': dd['pose'],
        'v_template': dd['v_template'],
        'J': dd['J_regressor'],
        'weights': dd['weights'],
        'kintree_table': dd['kintree_table'],
        'bs_style': dd['bs_style'],
        'f': dd['f'],
        'bs_type': dd['bs_type'],
        'posedirs': dd['posedirs'],
        'betas': dd['betas'],
        'shapedirs': dd['shapedirs'],
        'want_Jtr': True
    }

    # FOR SHAPE
    return verts_decorated(**args2)

    # pose, v, J, weights, kintree_table, want_Jtr, xp
    result, Jtr = verts_core(**args)
    result = result + dd['trans'].reshape((1, 3))
    result.J_transformed = Jtr + dd['trans'].reshape((1, 3))

    #print result.J_transformed.shape

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

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

    from 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))
    else:
        smpl_data = fname_or_dict

    rot = 3  # for global orientation!!!

    if use_pca:
        hands_components = smpl_data[hands_components]  # PCA components
    else:
        hands_components = np.eye(
            45)  # directly modify 15x3 articulation angles
    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))

    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
    result.dd = dd
    return result
def load_model(fname_or_dict='./models/SMPLH_female.pkl',
               ncomps=12,
               flat_hand_mean=False,
               v_template=None,
               mano_path='./smpl_data/mano_v1_2/models/'):
    ''' This model loads the fully articulable SMPL model,
    and replaces the 156-prefix last DOFS by ncomps from PCA'''

    # import imp
    # lm = imp.load_source('verts_core',
    #                      './smpl_data/mano_v1_2/webuser/verts.py')
    # from smpl_data.mano_v1_2.verts import verts_core

    from os.path import dirname, relpath
    import numpy as np
    import chumpy as ch
    import pickle
    import scipy.sparse as sp
    import sys
    import os

    dir_path = dirname(relpath(__file__))
    sys.path.insert(1, dir_path)
    from verts import verts_core

    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

    body_pose_dofs = 66

    with open(os.path.join(mano_path, 'MANO_LEFT.pkl'), 'rb') as infile:
        manoLeft = pickle.load(infile, encoding='latin1')

    hands_componentsl = manoLeft['hands_components']
    hands_meanl = np.zeros(hands_componentsl.shape[1]
                           ) if flat_hand_mean else manoLeft['hands_mean']
    hands_coeffsl = manoLeft['hands_coeffs'][:, :ncomps // 2]

    with open(os.path.join(mano_path, 'MANO_RIGHT.pkl'), 'rb') as infile:
        manoRight = pickle.load(infile, encoding='latin1')

    hands_componentsr = manoRight['hands_components']
    hands_meanr = np.zeros(hands_componentsl.shape[1]
                           ) if flat_hand_mean else manoRight['hands_mean']
    hands_coeffsr = manoRight['hands_coeffs'][:, :ncomps // 2]

    selected_components = np.vstack(
        (np.hstack((hands_componentsl[:ncomps // 2],
                    np.zeros_like(hands_componentsl[:ncomps // 2]))),
         np.hstack((np.zeros_like(hands_componentsr[:ncomps // 2]),
                    hands_componentsr[:ncomps // 2]))))
    hands_mean = np.concatenate((hands_meanl, hands_meanr))

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

    smpl_data['fullpose'] = ch.concatenate(
        (pose_coeffs[:body_pose_dofs], 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))
    # very 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))

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

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

    #################################
    result.fullpose = dd['fullpose']
    #################################

    if (hasattr(result, 'pose_subjects')):
        # assign random instances of hand poses to the original flat-handed training poses
        for ipsub, psub in enumerate(result.pose_subjects):
            for iframe, pose_parm in enumerate(psub['pose_parms']):
                # posep = np.concatenate((pose_parm[:body_pose_dofs],
                # 2.*np.random.randn(ncomps)))
                lidx = np.random.randint(len(hands_coeffsl))
                ridx = np.random.randint(len(hands_coeffsr))
                posep = np.concatenate(
                    (pose_parm[:body_pose_dofs], hands_coeffsl[lidx],
                     hands_coeffsr[ridx]))
                result.pose_subjects[ipsub]['pose_parms'][iframe] = posep

    result.part2num = {
        'global': 0,
        'head': 15,
        'leftCalf': 4,
        'leftFoot': 7,
        'leftForeArm': 18,
        'leftHand': 20,
        'leftShoulder': 13,
        'leftThigh': 1,
        'leftToes': 10,
        'leftUpperArm': 16,
        'neck': 12,
        'rightCalf': 5,
        'rightFoot': 8,
        'rightForeArm': 19,
        'rightHand': 21,
        'rightShoulder': 14,
        'rightThigh': 2,
        'rightToes': 11,
        'rightUpperArm': 17,
        'spine': 3,
        'spine1': 6,
        'spine2': 9
    }
    body_parts = len(result.part2num)
    hand_count = 0
    for side in ('l', 'r'):
        for part in ('Index', 'Middle', 'Pinky', 'Ring', 'Thumb'):
            for iphalanx in range(3):
                result.part2num['%s%s%d' %
                                (side, part,
                                 iphalanx)] = body_parts + hand_count
                hand_count += 1

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

    return result