import torch


def kpts_2_img_coordinates(kpt_coordinates: torch.Tensor,
                           img_shape: tuple) -> torch.Tensor:
    """ Converts the (h, w) key-point coordinates from video structure format [-1,1]x[-1,1]
        to image coordinates in [0,W]x[0,H].

    :param kpt_coordinates: Torch tensor in (N, T, K, 2/3)
    :param img_shape: Tuple of image size (H, W)
    """
    img_coordinates = torch.empty_like(kpt_coordinates)
    img_coordinates[..., 0] = ((kpt_coordinates[..., 1] + 1.0)/2.0) * img_shape[-1]  # W
    img_coordinates[..., 1] = ((-kpt_coordinates[..., 0] + 1.0)/2.0) * img_shape[-2]  # H
    if kpt_coordinates.shape[-1] == 3:
        img_coordinates[..., 2] = kpt_coordinates[..., 2]

    return img_coordinates