import torch


def random_joints2D_deviation(joints2D,
                              delta_j2d_dev_range=[-5, 5],
                              delta_j2d_hip_dev_range=[-15, 15]):
    """
    Deviate 2D joint locations with uniform random noise.
    :param joints2D: (bs, num joints, num joints)
    :param delta_j2d_dev_range: uniform noise range.
    :param delta_j2d_hip_dev_range: uniform noise range for hip joints. You may wish to make
    this bigger than for other joints since hip joints are semantically hard to localise and
    can be predicted inaccurately by joint detectors.
    """
    hip_joints = [11, 12]
    other_joints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14,15, 16]
    batch_size = joints2D.shape[0]
    device = joints2D.device

    l, h = delta_j2d_dev_range
    delta_j2d_dev = (h - l) * torch.rand(batch_size, len(other_joints), 2, device=device) + l
    joints2D[:, other_joints, :] = joints2D[:, other_joints, :] + delta_j2d_dev

    l, h = delta_j2d_hip_dev_range
    delta_j2d_hip_dev_range = (h - l) * torch.rand(batch_size, len(hip_joints), 2, device=device) + l
    joints2D[:, hip_joints, :] = joints2D[:, hip_joints, :] + delta_j2d_hip_dev_range

    return joints2D