import json
import os
import pickle

from filters import filter_none, filter_ratio

# NOTE(Tianyi): representations of path need to be modified
json_path = './data/train.json'
json_path_post = './data/train_post.json'

# path to fake bboxes
coco_fake_bboxes_path = './fake_bbox'

split = 'train'

# NOTE(Tianyi): save as json
filtered_proposal_dict = {}
dd = 0 # Q?(<NAME>) what dd means?
class_names = sorted(os.listdir(coco_fake_bboxes_path))

for ci, class_name in enumerate(class_names):
    filenames = sorted(os.listdir(os.path.join(coco_fake_bboxes_path, class_name)))

    # dd += len(filenames)
    for fi, filename in enumerate(filenames):
        base_filename = os.path.splitext(filename)[0]
        # path to bboxes' pkl
        cur_img_pro_path = os.path.join(coco_fake_bboxes_path, class_name, filename)

        cur_img_proposal = []
        with open(cur_img_pro_path, 'rb') as f:
            while True:
                try:
                    img_bboxes = pickle.load(f)
                    cur_img_proposal.append(img_bboxes)
                except EOFError:
                    break

        g = []
        for region in cur_img_proposal:
            # NOTE(Tianyi): 2 bounding boxes
            cur_rect_1 = region['bbox1']
            cur_rect_2 = region['bbox2']
            try:
                filtered_proposal_dict[base_filename].append(cur_rect_1)
            except KeyError as e:
                filtered_proposal_dict[base_filename] = []
                filtered_proposal_dict[base_filename].append(cur_rect_1)
            filtered_proposal_dict[base_filename].append(cur_rect_2)

        # # NOTE(Tianyi): 没过filter了，需要的话再改
        # props_size_ratio = filter_ratio(cur_img_proposal, r=3)
        # # print(len(props_size_ratio))
        # props_none = filter_none(cur_img_proposal)

        # if len(props_none) > 0:
        #     filtered_proposal_dict[base_filename] = props_none
        # elif len(props_size_ratio) > 0:
        #     filtered_proposal_dict[base_filename] = props_size_ratio

with open(json_path, 'w') as f:
    json.dump(filtered_proposal_dict, f)

print(dd)

import json
import os
import pickle

from filters import filter_none, filter_ratio

# NOTE(Tianyi): representations of path need to be modified
json_path = './data/train.json'
json_path_post = './data/train_post.json'

# path to fake bboxes
coco_fake_bboxes_path = './fake_bbox'

split = 'train'

# NOTE(Tianyi): save as json
filtered_proposal_dict = {}
dd = 0 # Q?(<NAME>) what dd means?
class_names = sorted(os.listdir(coco_fake_bboxes_path))

for ci, class_name in enumerate(class_names):
    filenames = sorted(os.listdir(os.path.join(coco_fake_bboxes_path, class_name)))

    # dd += len(filenames)
    for fi, filename in enumerate(filenames):
        base_filename = os.path.splitext(filename)[0]
        # path to bboxes' pkl
        cur_img_pro_path = os.path.join(coco_fake_bboxes_path, class_name, filename)

        cur_img_proposal = []
        with open(cur_img_pro_path, 'rb') as f:
            while True:
                try:
                    img_bboxes = pickle.load(f)
                    cur_img_proposal.append(img_bboxes)
                except EOFError:
                    break

        g = []
        for region in cur_img_proposal:
            # NOTE(Tianyi): 2 bounding boxes
            cur_rect_1 = region['bbox1']
            cur_rect_2 = region['bbox2']
            try:
                filtered_proposal_dict[base_filename].append(cur_rect_1)
            except KeyError as e:
                filtered_proposal_dict[base_filename] = []
                filtered_proposal_dict[base_filename].append(cur_rect_1)
            filtered_proposal_dict[base_filename].append(cur_rect_2)

        # # NOTE(Tianyi): 没过filter了，需要的话再改
        # props_size_ratio = filter_ratio(cur_img_proposal, r=3)
        # # print(len(props_size_ratio))
        # props_none = filter_none(cur_img_proposal)

        # if len(props_none) > 0:
        #     filtered_proposal_dict[base_filename] = props_none
        # elif len(props_size_ratio) > 0:
        #     filtered_proposal_dict[base_filename] = props_size_ratio

with open(json_path, 'w') as f:
    json.dump(filtered_proposal_dict, f)

print(dd)

# NOTE(Tianyi): representations of path need to be modified # path to fake bboxes # NOTE(Tianyi): save as json # Q?(<NAME>) what dd means? # dd += len(filenames) # path to bboxes' pkl # NOTE(Tianyi): 2 bounding boxes # # NOTE(Tianyi): 没过filter了，需要的话再改 # props_size_ratio = filter_ratio(cur_img_proposal, r=3) # # print(len(props_size_ratio)) # props_none = filter_none(cur_img_proposal) # if len(props_none) > 0: #     filtered_proposal_dict[base_filename] = props_none # elif len(props_size_ratio) > 0: #     filtered_proposal_dict[base_filename] = props_size_ratio