import json
import numpy as np
import cv2
import os
import argparse
import pathlib
import os.path

"""
This code makes png image from Json format
"""

if os.path.exists("ann_f") and os.path.exists("out_F"):
        input_path = pathlib.Path("ann_f")
        label_paths_sorted = sorted(list(input_path.glob("*.json")))
        print(label_paths_sorted)
        for image_path in label_paths_sorted:
            print('converting: {}'.format(os.path.basename(image_path)))
            with open(image_path) as json_file:
                data = json.load(json_file)
                size = data["size"]
                height = size["height"]
                width = size["width"]
                pts = data["objects"][1]["points"]["exterior"]
                print(pts)
                black = np.zeros((height, width, 3), dtype='uint8')
                pts = np.asarray(pts)
                cv2.fillPoly(black, np.int_([pts]), (0, 255, 0))
                cv2.imwrite('out_F/' + os.path.basename(image_path).replace('.json', ''), black)