import json


def read_annots(coco_json, coco_names_path):
    """
    Read annotations from VIA exported coco json
    format and creates data dicts and names output
    file.

    Parameters
    ----------
    coco_json : str
        COCO json input file
    coco_names_path : str
        Names or labels output file

    Returns
    -------
    images : dict
        images dictionary
    annotations : dict
        annotations dictionary
    categories : dict
        categories or labels dictionary
    """
    coco = json.load(open(coco_json))
    images = coco["images"]
    annotations = coco["annotations"]
    categories = coco["categories"]

    class_to_id = {}

    with open(coco_names_path, 'w') as fp:
        idx = 0
        for cat in categories:
            id = cat["name"].strip()
            class_name = cat["name"].strip()
            fp.write(class_name+"\n")

            class_to_id[class_name] = idx
            idx+=1

    print("size of images {}".format(len(images)))
    print("size of annotations {}".format(len(annotations)))
    print("size of categories {}".format(len(categories)))

    return images, annotations, categories, class_to_id