import json


def read_homography_dict(path_to_homography_dict):
    """ To apply homography transformation with matrix H to vector.

        Parameters
        ----------
        path_to_homography_dict: str
            The path to the json with a homography dict.
                json format: {"frame_no": {"H":[]},
                              ...
                              "frame_no": {"H":[]},
                              "resize_info": {"h": (int), "w": (int))}}.
        Returns
        ----------
        homography_dict: dict
            A dict of homography matrix.
                format: {frame_no: {"H":[]},
                              ...
                          frame_no: {"H":[]}}.

        resize_info: dict
            An image shape which is used to get homography dictionary.
                format:{"h": (int), "w": (int))}.
    """
    with open(path_to_homography_dict, "r") as curr_json:
        homography_dict = json.load(curr_json)
        if "resize_info" in homography_dict:
            resize_info = homography_dict["resize_info"]
        else:
            raise ValueError("Specify the height and width of the frame "
                             "for which the homography matrix was obtained")
        resize_info = resize_info
        del homography_dict["resize_info"]
        homography_dict = {int(k): v for k, v in homography_dict.items()}
    return homography_dict, resize_info