def run_test():
    if platform == 'linux':
        os.environ['CUDA_VISIBLE_DEVICES'] = '2'

    data_type = torch.float32
    device = torch.device('cuda')
    pose_size = 72
    beta_size = 10

    np.random.seed(9608)
    model = SMPLModel(
        device=device,
        model_path='./model_lsp.pkl',
        data_type=data_type,
    )
    dataset = Human36MDataset(model, max_item=100, calc_mesh=True)

    # generate mesh, align with 14 point ground truth
    case_num = 10
    data = dataset[:case_num]
    meshes = data['meshes']
    input = data['lsp_joints']
    target_2d = data['gt2d']
    target_3d = data['gt3d']

    transforms = map_3d_to_2d(input, target_2d, target_3d)

    # Important: mesh should be centered at the origin!
    deformed_meshes = transforms(meshes)
    mesh_3d = deformed_meshes.detach().cpu().numpy()
    # visualize(data['imagename'], mesh_3d[:,:,:2].astype(np.int),
    #    target_2d.detach().cpu().numpy().astype(np.int))

    for i, mesh in enumerate(mesh_3d):
        model.write_obj(mesh,
                        '_test_cache/real_mesh_{}.obj'.format(i))  # weird.

        UV_position_map, UV_scatter, rgbs_backup = get_UV(mesh, 300)

        # write colorized coordinates to ply
        write_ply('_test_cache/colored_mesh_{}.ply'.format(i), mesh,
                  rgbs_backup)

        out = np.concatenate((UV_position_map, UV_scatter), axis=1)

        imsave('_test_cache/UV_position_map_{}.png'.format(i), out)
        resampled_mesh = resample(UV_position_map)

        model.write_obj(resampled_mesh,
                        '_test_cache/recon_mesh_{}.obj'.format(i))
def create_UV_maps(UV_label_root=None, uv_prefix='smpl_fbx_template'):
    if platform == 'linux':
        os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    data_type = torch.float32
    device=torch.device('cuda')
    pose_size = 72
    beta_size = 10

    np.random.seed(9608)
    model = SMPLModel(
            device=device,
            model_path='./model_lsp.pkl',
            data_type=data_type,
        )
    dataset = Human36MWashedDataset(model, calc_mesh=True, root_dir='/home/wzeng/mydata/h3.6m/images_washed')
    
    generator = UV_Map_Generator(
        UV_height=256,
        UV_pickle=uv_prefix+'.pickle'
    )
    
    # create root folder for UV labels
    if UV_label_root is None:
        UV_label_root=dataset.root_dir.replace('_washed', 
                '_UV_map_{}'.format(uv_prefix[:-9]))
    
    if not os.path.isdir(UV_label_root):
        os.makedirs(UV_label_root)
        subs = [sub for sub in os.listdir(dataset.root_dir)
            if os.path.isdir(dataset.root_dir + '/' + sub)]
        for sub in subs:
            os.makedirs(UV_label_root + '/' + sub)
    else:
        print('{} folder exists, process terminated...'.format(UV_label_root))
        return
    
    # generate mesh, align with 14 point ground truth
    batch_size = 64
    total_batch_num = dataset.length // batch_size + 1
    _loop = tqdm(range(total_batch_num), ncols=80)
    
    for batch_id in _loop:
        data = dataset[batch_id * batch_size: (batch_id + 1) * batch_size]
        meshes = data['meshes']
        input = data['lsp_joints']
        target_2d = data['gt2d']
        target_3d = data['gt3d']
        imagename = [UV_label_root + str for str in data['imagename']]
        
        transforms = map_3d_to_2d(input, target_2d, target_3d)
        
        # Important: mesh should be centered at the origin!
        deformed_meshes = transforms(meshes)
        mesh_3d = deformed_meshes.detach().cpu().numpy()
        '''
        test_folder = '_test_radvani'
        if not os.path.isdir(test_folder):
            os.makedirs(test_folder)
        visualize(test_folder, data['imagename'], mesh_3d[:,:,:2].astype(np.int), 
           target_2d.detach().cpu().numpy().astype(np.int), dataset.root_dir)
        '''
        s=time()
        for name, mesh in zip(imagename, mesh_3d):
            UV_position_map, verts_backup = \
                generator.get_UV_map(mesh)
            imwrite(name, (UV_position_map * 255).astype(np.uint8))
            
            # write colorized coordinates to ply
            '''
def run_test():
    if platform == 'linux':
        os.environ['CUDA_VISIBLE_DEVICES'] = '2'

    data_type = torch.float32
    device = torch.device('cuda')
    pose_size = 72
    beta_size = 10

    np.random.seed(9608)
    model = SMPLModel(
        device=device,
        model_path='./model_lsp.pkl',
        data_type=data_type,
    )
    dataset = Human36MDataset(model, max_item=100, calc_mesh=True)

    # generate mesh, align with 14 point ground truth
    case_num = 10
    data = dataset[:case_num]
    meshes = data['meshes']
    input = data['lsp_joints']
    target_2d = data['gt2d']
    target_3d = data['gt3d']

    transforms = map_3d_to_2d(input, target_2d, target_3d)

    # Important: mesh should be centered at the origin!
    deformed_meshes = transforms(meshes)
    mesh_3d = deformed_meshes.detach().cpu().numpy()

    file_prefix = 'smpl_fbx_template'
    generator = UV_Map_Generator(UV_height=256,
                                 UV_pickle=file_prefix + '.pickle')

    test_folder = '_test_smpl_fbx'
    if not os.path.isdir(test_folder):
        os.makedirs(test_folder)

    visualize(test_folder, data['imagename'], mesh_3d[:, :, :2].astype(np.int),
              target_2d.detach().cpu().numpy().astype(np.int))

    s = time()
    for i, mesh in enumerate(mesh_3d):
        model.write_obj(mesh, '{}/real_mesh_{}.obj'.format(test_folder,
                                                           i))  # weird.

        UV_position_map, verts_backup = \
            generator.get_UV_map(mesh)

        # write colorized coordinates to ply
        UV_scatter, _, _ = generator.render_point_cloud(verts=mesh)
        generator.write_ply('{}/colored_mesh_{}.ply'.format(test_folder, i),
                            mesh)

        out = np.concatenate((UV_position_map, UV_scatter), axis=1)

        imsave('{}/UV_position_map_{}.png'.format(test_folder, i), out)

        resampled_mesh = generator.resample(UV_position_map)

        model.write_obj(resampled_mesh,
                        '{}/recon_mesh_{}.obj'.format(test_folder, i))
    print('{} cases for {}s'.format(case_num, time() - s))