def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h=256, uv_w=256, image_h=256, image_w=256):
    image_name = image_path.strip().split('/')[(- 1)]
    image = (io.imread(image_path) / 255)
    [h, w, c] = image.shape
    info = sio.loadmat(mat_path)
    pose_para = info['Pose_Para'].T.astype(np.float32)
    shape_para = info['Shape_Para'].astype(np.float32)
    exp_para = info['Exp_Para'].astype(np.float32)
    vertices = bfm.generate_vertices(shape_para, exp_para)
    s = pose_para[((- 1), 0)]
    angles = pose_para[:3, 0]
    t = pose_para[3:6, 0]
    transformed_vertices = bfm.transform_3ddfa(vertices, s, angles, t)
    projected_vertices = transformed_vertices.copy()
    image_vertices = projected_vertices.copy()
    image_vertices[:, 1] = ((h - image_vertices[:, 1]) - 1)
    kpt = image_vertices[bfm.kpt_ind, :].astype(np.int32)
    left = np.min(kpt[:, 0])
    right = np.max(kpt[:, 0])
    top = np.min(kpt[:, 1])
    bottom = np.max(kpt[:, 1])
    center = np.array([(right - ((right - left) / 2.0)), (bottom - ((bottom - top) / 2.0))])
    old_size = ((((right - left) + bottom) - top) / 2)
    size = int((old_size * 1.5))
    marg = (old_size * 0.1)
    t_x = (((np.random.rand() * marg) * 2) - marg)
    t_y = (((np.random.rand() * marg) * 2) - marg)
    center[0] = (center[0] + t_x)
    center[1] = (center[1] + t_y)
    size = (size * ((np.random.rand() * 0.2) + 0.9))
    src_pts = np.array([[(center[0] - (size / 2)), (center[1] - (size / 2))], [(center[0] - (size / 2)), (center[1] + (size / 2))], [(center[0] + (size / 2)), (center[1] - (size / 2))]])
    DST_PTS = np.array([[0, 0], [0, (image_h - 1)], [(image_w - 1), 0]])
    tform = skimage.transform.estimate_transform('similarity', src_pts, DST_PTS)
    angle = ((np.random.rand() * 90) - 45)
    (rows, cols) = (image.shape[0], image.shape[1])
    center = ((np.array((cols, rows)) / 2.0) - 0.5)
    tform1 = SimilarityTransform(translation=center)
    tform2 = SimilarityTransform(rotation=np.deg2rad(angle))
    tform3 = SimilarityTransform(translation=(- center))
    rotate_transform = ((tform3 + tform2) + tform1)
    tform = (rotate_transform + tform)
    opt = ran.randint(1, 2)
    cropped_image = skimage.transform.warp(image, tform.inverse, output_shape=(image_h, image_w))
    position = image_vertices.copy()
    position[:, 2] = 1
    position = np.dot(position, tform.params.T)
    position[:, 2] = (image_vertices[:, 2] * tform.params[(0, 0)])
    position[:, 2] = (position[:, 2] - np.min(position[:, 2]))
    uv_position_map = mesh.render.render_colors(uv_coords, bfm.full_triangles, position, uv_h, uv_w, c=3)
    io.imsave('{}\\{}'.format(save_folder, image_name), np.squeeze(cropped_image))
    np.save('{}\\{}'.format(save_folder, image_name.replace('jpg', 'npy')), uv_position_map)
    io.imsave('{}\\{}'.format('results/uv_maps/', image_name.replace('.jpg', '_posmap.jpg')), (uv_position_map / abs(uv_position_map.max())))
    uv_texture_map_rec = cv2.remap(cropped_image, uv_position_map[:, :, :2].astype(np.float32), None, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=0)
def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h=256, uv_w=256, image_h=256, image_w=256):
    image_name = image_path.strip().split('/')[(- 1)]
    image = (io.imread(image_path) / 255)
    [h, w, c] = image.shape
    info = sio.loadmat(mat_path)
    pose_para = info['Pose_Para'].T.astype(np.float32)
    shape_para = info['Shape_Para'].astype(np.float32)
    exp_para = info['Exp_Para'].astype(np.float32)
    vertices = bfm.generate_vertices(shape_para, exp_para)
    s = pose_para[((- 1), 0)]
    angles = pose_para[:3, 0]
    t = pose_para[3:6, 0]
    transformed_vertices = bfm.transform_3ddfa(vertices, s, angles, t)
    projected_vertices = transformed_vertices.copy()
    image_vertices = projected_vertices.copy()
    image_vertices[:, 1] = ((h - image_vertices[:, 1]) - 1)
    kpt = image_vertices[bfm.kpt_ind, :].astype(np.int32)
    left = np.min(kpt[:, 0])
    right = np.max(kpt[:, 0])
    top = np.min(kpt[:, 1])
    bottom = np.max(kpt[:, 1])
    center = np.array([(right - ((right - left) / 2.0)), (bottom - ((bottom - top) / 2.0))])
    old_size = ((((right - left) + bottom) - top) / 2)
    size = int((old_size * 1.5))
    marg = (old_size * 0.1)
    t_x = (((np.random.rand() * marg) * 2) - marg)
    t_y = (((np.random.rand() * marg) * 2) - marg)
    center[0] = (center[0] + t_x)
    center[1] = (center[1] + t_y)
    size = (size * ((np.random.rand() * 0.2) + 0.9))
    src_pts = np.array([[(center[0] - (size / 2)), (center[1] - (size / 2))], [(center[0] - (size / 2)), (center[1] + (size / 2))], [(center[0] + (size / 2)), (center[1] - (size / 2))]])
    DST_PTS = np.array([[0, 0], [0, (image_h - 1)], [(image_w - 1), 0]])
    tform = skimage.transform.estimate_transform('similarity', src_pts, DST_PTS)
    angle = ((np.random.rand() * 90) - 45)
    (rows, cols) = (image.shape[0], image.shape[1])
    center = ((np.array((cols, rows)) / 2.0) - 0.5)
    tform1 = SimilarityTransform(translation=center)
    tform2 = SimilarityTransform(rotation=np.deg2rad(angle))
    tform3 = SimilarityTransform(translation=(- center))
    rotate_transform = ((tform3 + tform2) + tform1)
    tform = (rotate_transform + tform)
    opt = ran.randint(1, 2)
    cropped_image = skimage.transform.warp(image, tform.inverse, output_shape=(image_h, image_w))
    position = image_vertices.copy()
    position[:, 2] = 1
    position = np.dot(position, tform.params.T)
    position[:, 2] = (image_vertices[:, 2] * tform.params[(0, 0)])
    position[:, 2] = (position[:, 2] - np.min(position[:, 2]))
    uv_position_map = mesh.render.render_colors(uv_coords, bfm.full_triangles, position, uv_h, uv_w, c=3)
    io.imsave('{}\\{}'.format(save_folder, image_name), np.squeeze(cropped_image))
    np.save('{}\\{}'.format(save_folder, image_name.replace('jpg', 'npy')), uv_position_map)
    io.imsave('{}\\{}'.format('results/uv_maps/', image_name.replace('.jpg', '_posmap.jpg')), (uv_position_map / abs(uv_position_map.max())))
    uv_texture_map_rec = cv2.remap(cropped_image, uv_position_map[:, :, :2].astype(np.float32), None, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=0)