Пример #1
0
def convert(phase, data_path):
    assert phase in ['train', 'val']
    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1
    annotation_id = 1

    bbs_dict = parse_cityperson_mat('%s/anno_%s.mat' % (data_path, phase), phase)

    fn_lst = glob.glob('%s/leftImg8bit/%s/*/*.png' % (data_path, phase))
    positive_box_num = 0
    ignore_box_num = 0
    for image_filename in fn_lst:
        base_name = os.path.basename(image_filename)
	
        if base_name in bbs_dict:
            image = Image.open(image_filename)
            image_info = pycococreatortools.create_image_info(
                image_id, image_filename[image_filename.index('leftImg8bit'):], image.size)
            coco_output["images"].append(image_info)

            boxes = bbs_dict[base_name]
            # go through each associated annotation
            slt_msk = np.logical_and(boxes[:, 0] == 1, boxes[:, 4] >= 50)
            boxes_gt = boxes[slt_msk, 1:5]
            positive_box_num += boxes_gt.shape[0]
            for annotation in boxes_gt:
                annotation = annotation.tolist()
                class_id = 1
                category_info = {'id': class_id, 'is_crowd': False}
                annotation_info = pycococreatortools.create_annotation_info(
                    annotation_id, image_id, category_info, annotation,
                    image.size)
                if annotation_info is not None:
                    coco_output["annotations"].append(annotation_info)
                annotation_id += 1

            slt_msk = np.logical_or(boxes[:, 0] != 1, boxes[:, 4] < 50)
            boxes_ig = boxes[slt_msk, 1:5]
            ignore_box_num += boxes_ig.shape[0]
            for annotation in boxes_ig:
                annotation = annotation.tolist()
                category_info = {'id': 1, 'is_crowd': True}
                annotation_info = pycococreatortools.create_annotation_info(
                    annotation_id, image_id, category_info, annotation, image.size)
                if annotation_info is not None:
                    coco_output["annotations"].append(annotation_info)
                annotation_id += 1

        image_id = image_id + 1
    print('positive_box_num: ', positive_box_num)
    print('ignore_box_num: ', ignore_box_num)
    with open(data_path + phase + '.json', 'w') as output_json_file:
        json.dump(coco_output, output_json_file)
Пример #2
0
def get_annotations_section(mask_sourcefile, mask_dir, im_id_dict, category_dict):
  # Key is ImageID, LabelName, XMin (rounded to 2 decimals) concatenated together
  annotations = []

  # Ten fields: MaskPath,ImageID,LabelName,BoxID,BoxXMin,BoxXMax,BoxYMin,BoxYMax,PredictedIoU,Clicks
  counter = 1
  with open(mask_sourcefile, 'r') as f:
    next(f) # Skip header line
    for line in f:
      mask_path, image_filename, label_name, _, x_min, x_max, y_min, y_max, _, _ = line.strip().split(',')
      subset = mask_dir.split('/')[-1]
      mask_sub_folder = '{}-masks-{}'.format(subset, mask_path[0]) # e.g. validation-masks-a
      mask_path = os.path.join(mask_dir, mask_sub_folder, mask_path)
      binary_mask = cv2.imread(mask_path, 0).astype(np.uint8)

      category_id = category_dict[label_name] # Maps class/label name to integer
      image_id = im_id_dict[image_filename]['id'] # alphanumerical string -> integer
      if image_filename not in im_id_dict:
        print(image_filename)
      category_info = {'id': category_id, 'is_crowd': 0}
      # Resize mask to image size
      im_width = im_id_dict[image_filename]['width']
      im_height = im_id_dict[image_filename]['height']
      binary_mask = cv2.resize(binary_mask, (im_width, im_height))
      assert(binary_mask.shape == (im_height, im_width))

      annotation_info = pycococreatortools.create_annotation_info(counter, image_id, category_info, binary_mask)
      if annotation_info is not None:
        annotations.append(annotation_info)
      counter += 1

    print('Generated annotations section.')
    return annotations
Пример #3
0
def main():

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 0
    segmentation_id = 0
    
    # filter for jpeg images
    for root, _, files in os.walk(IMAGE_DIR):
        image_files = filter_for_jpeg(root, files)

        # go through each image
        for image_filename in image_files:
            image = Image.open(image_filename)
            image_info = pycococreatortools.create_image_info(
                image_id, os.path.basename(image_filename), image.size)
            coco_output["images"].append(image_info)

            # filter for associated png annotations
            for root, _, files in os.walk(ANNOTATION_DIR):
                time_id = os.path.splitext(image_filename)[0].split('_')[-1]
                # annotation_files = filter_for_annotations(root, files, image_filename.replace('rgbImg', 'Mask4Seg'))
                annotation_files = filter_for_annotations(root, files, time_id)
                print(annotation_files)

                # go through each associated annotation
                for annotation_filename in annotation_files:
                    
                    print(annotation_filename)
                    class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename.split('/')[-1]][0]
                    print(class_id)

                    #COCO 支持两种类型的标注,其格式取决于标注的是单个物体(single object) 还是密集物体("crowd" objects).
                    category_info = {'id': class_id, 'is_crowd': 'crowd' in image_filename}
                    binary_mask = np.asarray(Image.open(annotation_filename)
                        .convert('1')).astype(np.uint8)
                    
                    # tolerance 参数表示了用于记录单个物体的轮廓精度. 该参数数值越大,则标注的质量越低,但文件大小也越小. 一般采用 tolerance=2
                    annotation_info = pycococreatortools.create_annotation_info(
                        segmentation_id, image_id, category_info, binary_mask,
                        image.size, tolerance=2)

                    if annotation_info is not None:
                        coco_output["annotations"].append(annotation_info)

                    segmentation_id = segmentation_id + 1

            image_id = image_id + 1

    with open('{}/{}'.format(ROOT_DIR, json_filename), 'w') as output_json_file:
        json.dump(coco_output, output_json_file)
Пример #4
0
def each_sub_proc(file_name, dir_name, dataset_root, image_id, labels, each_image_json):
    print("File name-{}-{}".format(file_name, image_id))
    file_name = file_name[:-4]
    instance_path = "{}/{}/instances/{}.png".format(dataset_root, dir_name, file_name)
    instance_image = Image.open(instance_path)
    instance_array = np.array(instance_image, dtype=np.uint16)
    image_label_instance_infomatrix = split_to_coco_creator(instance_array, labels)

    image_info = pycococreatortools.create_image_info(
        image_id, file_name + ".jpg", instance_image.size
    )
    each_image_json["images"].append(image_info)

    segmentation_id = 1
    for item in image_label_instance_infomatrix:
        class_id = convert_class_id(item["label_name"])
        category_info = {"id": class_id, "is_crowd": 0}
        binary_mask = item["image"]
        annotation_info = pycococreatortools.create_annotation_info(
            segmentation_id,
            image_id,
            category_info,
            binary_mask,
            instance_image.size,
            tolerance=2,
        )
        if annotation_info is not None:
            each_image_json["annotations"].append(annotation_info)
            segmentation_id = segmentation_id + 1

    if each_image_json["images"] is []:
        print("Image {} doesn't contain one image".format(image_id))
    save_path = "{}/{}/massive_annotations/image{}_info.json".format(
        dataset_root, dir_name, image_id
    )
    print("Saving to {}".format(save_path))
    with open(save_path, "w") as fp:
        json.dump(each_image_json, fp)
Пример #5
0
def main():

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1
    segmentation_id = 1

    # filter for jpeg images
    for root, _, files in os.walk(IMAGE_DIR):
        image_files = filter_for_jpeg(root, files)

        # go through each image
        for image_filename in image_files:
            image = Image.open(image_filename)
            image_info = pycococreatortools.create_image_info(
                image_id, os.path.basename(image_filename), image.size)
            coco_output["images"].append(image_info)

            # filter for associated png annotations
            for root, _, files in os.walk(ANNOTATION_DIR):
                annotation_files = filter_for_annotations(
                    root, files, image_filename)

                # go through each associated annotation
                for annotation_filename in annotation_files:

                    #                     print('Considering:', annotation_filename)
                    class_id = [
                        x['id'] for x in CATEGORIES
                        if x['name'] in annotation_filename
                    ][0]

                    category_info = {
                        'id': class_id,
                        'is_crowd': 'crowd' in image_filename
                    }
                    binary_mask = np.asarray(
                        Image.open(annotation_filename).convert('1')).astype(
                            np.uint8)

                    #                     print('Sending:', 'segmentation_id:', segmentation_id, 'image_id:', image_id,
                    #                           'category_info:', category_info, 'binary_mask:', binary_mask,
                    #                           'image.size:', image.size)
                    annotation_info = pycococreatortools.create_annotation_info(
                        segmentation_id,
                        image_id,
                        category_info,
                        binary_mask,
                        image.size,
                        tolerance=2)

                    #                     print('Annotation Info:', annotation_info)

                    #                     plt.imshow(binary_mask)
                    #                     plt.show()

                    if annotation_info is not None:
                        coco_output["annotations"].append(annotation_info)

                    segmentation_id = segmentation_id + 1

            image_id = image_id + 1

    with open('{}/fs_train_tmp.json'.format(ROOT_DIR),
              'w') as output_json_file:
        json.dump(coco_output, output_json_file)
Пример #6
0
def process(json_file_images_i, js_1, js_2):
    img_id = json_file_images_i['id']
    annotation_info_fgs1 = []
    annotation_info_fgs2 = []
    print(img_id)

    if len(js_1) == len(js_2):
        for j_idx, j1 in enumerate(js_1):
            if (not os.path.exists(os.path.join(mat_dir, json_file_images_i['file_name'].replace('jpg', 'mat'))))\
                    and (not os.path.exists(os.path.join(mat_dir_2007, json_file_images_i['file_name'].replace('2007_', '').replace('jpg', 'mat')))):
                print(json_file_images_i['file_name'], "not exist")
                annotation_info_fgs1.append(js_1[j_idx])
                annotation_info_fgs2.append(js_2[j_idx])

            else:
                mask1 = annToMask(js_1[j_idx])
                mask2 = annToMask(js_2[j_idx])
                refine_mask1, refine_mask2, flag1, flag2 = match_mcg_proposal(mask1, mask2, json_file_images_i['file_name'], js_1[j_idx]['bbox'], js_2[j_idx]['bbox'])
                category_info1 = {'id': int(js_1[j_idx]['category_id']), 'is_crowd': False}
                category_info2 = {'id': int(js_2[j_idx]['category_id']), 'is_crowd': False}
                tol = 0 if flag1 else 2
                annotation_info_fg = create_annotation_info(
                    js_1[j_idx]['id'], js_1[j_idx]['image_id'], category_info1, refine_mask1, [js_1[j_idx]['width'], js_1[j_idx]['height']], tolerance=tol,
                    bounding_box=torch.Tensor(js_1[j_idx]['bbox']))
                if annotation_info_fg is None:
                    annotation_info_fgs1.append(js_1[j_idx])
                else:
                    annotation_info_fgs1.append(annotation_info_fg)
                tol = 0 if flag2 else 2
                annotation_info_fg = create_annotation_info(
                    js_2[j_idx]['id'], js_2[j_idx]['image_id'], category_info2, refine_mask2,
                    [js_2[j_idx]['width'], js_2[j_idx]['height']], tolerance=tol,
                    bounding_box=torch.Tensor(js_2[j_idx]['bbox']))
                if annotation_info_fg is None:
                    annotation_info_fgs2.append(js_2[j_idx])
                else:
                    annotation_info_fgs2.append(annotation_info_fg)

    else:
        for j_idx, j in enumerate(js_1):
            if (not os.path.exists(os.path.join(mat_dir, json_file_images_i['file_name'].replace('jpg', 'mat')))) \
                    and (not os.path.exists(os.path.join(mat_dir_2007, json_file_images_i['file_name'].replace('2007_', '').replace('jpg', 'mat')))):
                print(json_file_images_i['file_name'], "not exist")
                annotation_info_fgs1.append(j)
            else:
                mask = annToMask(j)
                refine_mask, flag = match_mcg_proposal_single(mask, json_file_images_i['file_name'], j['bbox'])
                category_info = {'id': int(j['category_id']), 'is_crowd': False}
                tol = 0 if flag else 2
                annotation_info_fg = create_annotation_info(
                    j['id'], j['image_id'], category_info, refine_mask, [j['width'], j['height']], tolerance=tol,
                    bounding_box=torch.Tensor(j['bbox']))
                if annotation_info_fg is None:
                    annotation_info_fgs1.append(js_1[j_idx])
                else:
                    annotation_info_fgs1.append(annotation_info_fg)

        for j_idx, j in enumerate(js_2):
            if (not os.path.exists(os.path.join(mat_dir, json_file_images_i['file_name'].replace('jpg', 'mat')))) \
                    and (not os.path.exists(os.path.join(mat_dir_2007, json_file_images_i['file_name'].replace('2007_', '').replace('jpg', 'mat')))):
                print(json_file_images_i['file_name'], "not exist")
                annotation_info_fgs2.append(j)
            else:
                mask = annToMask(j)
                refine_mask, flag = match_mcg_proposal_single(mask, json_file_images_i['file_name'], j['bbox'])
                category_info = {'id': int(j['category_id']), 'is_crowd': False}
                tol = 0 if flag else 2

                annotation_info_fg = create_annotation_info(
                    j['id'], j['image_id'], category_info, refine_mask, [j['width'], j['height']], tolerance=tol,
                    bounding_box=torch.Tensor(j['bbox']))
                if annotation_info_fg is None:
                    annotation_info_fgs2.append(js_2[j_idx])
                else:
                    annotation_info_fgs2.append(annotation_info_fg)
    return json_file_images_i, annotation_info_fgs1, annotation_info_fgs2
Пример #7
0
def convert_masks_to_COCO(kits_val, kits_masks, kits_annotations):
    """

    Returns: Converted masks in COCO format

    """

    INFO = {
        "description": "KiTS Dataset",
        "url": "https://github.com/AbtinJ/Kidneys_MaskRCNN",
        "version": "1.0",
        "year": 2019,
        "contributor": "Abtin",
        "date_created": datetime.datetime.utcnow().isoformat(' ')
    }

    LICENSES = [{
        "id": 1,
        "name": "Attribution-NonCommercial-ShareAlike License",
        "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/"
    }]

    CATEGORIES = [
        {
            'id': 1,
            'name': 'kidney',
            'supercategory': 'organ',
        },
        {
            'id': 2,
            'name': 'tumor',
            'supercategory': 'organ',
        },
    ]

    def filter_for_jpeg(root, files):
        file_types = ['*.jpeg', '*.jpg']
        file_types = r'|'.join([fnmatch.translate(x) for x in file_types])
        files = [os.path.join(root, f) for f in files]
        files = [f for f in files if re.match(file_types, f)]

        return files

    def filter_for_annotations(root, files, image_filename):
        file_types = ['*.png']
        file_types = r'|'.join([fnmatch.translate(x) for x in file_types])
        basename_no_extension = os.path.splitext(
            os.path.basename(image_filename))[0]
        file_name_prefix = basename_no_extension + '.*'
        files = [os.path.join(root, f) for f in files]
        files = [f for f in files if re.match(file_types, f)]
        files = [
            f for f in files
            if re.match(file_name_prefix,
                        os.path.splitext(os.path.basename(f))[0])
        ]

        return files

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 0
    segmentation_id = 0

    # filter for jpeg images
    for root, _, files in os.walk(kits_val):
        image_files = filter_for_jpeg(root, files)

        # go through each image
        for image_filename in image_files:
            image = Image.open(image_filename)
            image_info = pycococreatortools.create_image_info(
                image_id, os.path.basename(image_filename), image.size)
            coco_output["images"].append(image_info)

            # filter for associated png annotations
            for root, _, files in os.walk(kits_masks):
                annotation_files = filter_for_annotations(
                    root, files, image_filename)

                # go through each associated annotation
                for annotation_filename in annotation_files:

                    print(annotation_filename)
                    class_id = [
                        x['id'] for x in CATEGORIES
                        if x['name'] in annotation_filename
                    ][0]

                    category_info = {
                        'id': class_id,
                        'is_crowd': 'crowd' in image_filename
                    }
                    binary_mask = np.asarray(
                        Image.open(annotation_filename).convert('1')).astype(
                            np.uint8)

                    annotation_info = pycococreatortools.create_annotation_info(
                        segmentation_id,
                        image_id,
                        category_info,
                        binary_mask,
                        image.size,
                        tolerance=2)

                    if annotation_info is not None:
                        coco_output["annotations"].append(annotation_info)

                    segmentation_id = segmentation_id + 1

            image_id = image_id + 1

    # with open('{}/instances_train2019.json'.format(ANNOTATION_DIR), 'w') as output_json_file:
    #     json.dump(coco_output, output_json_file)

    with open('{}/instances_val2019.json'.format(kits_annotations),
              'w') as output_json_file:
        json.dump(coco_output, output_json_file)
                print("no box", img_name)
            else:
                for score, mask, class_id, box in zip(ann['score'],
                                                      ann['mask'],
                                                      ann['class'],
                                                      ann['box']):
                    category_info = {'id': int(class_id), 'is_crowd': False}
                    box = [
                        box[0], box[1], box[2] - box[0] + 1,
                        box[3] - box[1] + 1
                    ]

                    annotation_info_fg = create_annotation_info(
                        instance_id,
                        int(img_id),
                        category_info,
                        mask, [w, h],
                        tolerance=1,
                        bounding_box=torch.Tensor(box))

                    if annotation_info_fg == None:
                        continue

                    instance_id += 1
                    coco_output['annotations'].append(annotation_info_fg)
    print("time", time.time() - start_time)

    with open(
            'datasets/%s_nimg_%d_th_%s.json' %
        (mask_name, len(img_list), th_fg), 'w') as outfile:
        json.dump(coco_output, outfile)
Пример #9
0
    def main():

        coco_output = {
            "info": INFO,
            "licenses": LICENSES,
            "categories": CATEGORIES,
            "images": [],
            "annotations": []
        }

        image_id = 0
        segmentation_id = 0

        # filter for jpeg images
        for root, _, files in os.walk(IMAGE_DIR):
            image_files = filter_for_jpeg(root, files)

            # go through each image
            for image_filename in image_files:
                image = Image.open(image_filename)
                image_info = pycococreatortools.create_image_info(
                    image_id, os.path.basename(image_filename), image.size)
                coco_output["images"].append(image_info)

                # filter for associated png annotations
                for root, _, files in os.walk(MASK_DIR):
                    annotation_files = filter_for_annotations(
                        root, files, image_filename)

                    # go through each associated annotation
                    for annotation_filename in annotation_files:

                        print(annotation_filename)
                        class_id = [
                            x['id'] for x in CATEGORIES
                            if x['name'] in annotation_filename
                        ][0]

                        category_info = {
                            'id': class_id,
                            'is_crowd': 'crowd' in image_filename
                        }
                        binary_mask = np.asarray(
                            Image.open(annotation_filename).convert(
                                '1')).astype(np.uint8)

                        annotation_info = pycococreatortools.create_annotation_info(
                            segmentation_id,
                            image_id,
                            category_info,
                            binary_mask,
                            image.size,
                            tolerance=2)

                        if annotation_info is not None:
                            coco_output["annotations"].append(annotation_info)

                        segmentation_id = segmentation_id + 1

                image_id = image_id + 1

        if IMAGE_DIR == TRAIN_DIR:
            with open('{}/instances_train2019.json'.format(ANNOTATION_DIR),
                      'w') as output_json_file:
                json.dump(coco_output, output_json_file)
        elif IMAGE_DIR == VAL_DIR:
            with open('{}/instances_val2019.json'.format(ANNOTATION_DIR),
                      'w') as output_json_file:
                json.dump(coco_output, output_json_file)
def main(args):
    INFO = {
        "description": args.split_name + " Dataset",
        "url": "",
        "version": "",
        "year": 2019,
        "contributor": "xyq",
        "date_created": datetime.datetime.utcnow().isoformat(' ')
    }

    LICENSES = [
        {
            "id": 1,
            "name": "",
            "url": ""
        }
    ]

    CATEGORIES = [
        {
            'id': 1,
            'name': 'person',
            'supercategory': 'person',
        },
    ]

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1
    segmentation_id = 1

    for image_name in os.listdir(args.train_img_dir):
        image = Image.open(os.path.join(args.train_img_dir, image_name))
        image_info = pycococreatortools.create_image_info(
            image_id, image_name, image.size
        )
        coco_output["images"].append(image_info)

        human_mask_name = os.path.splitext(image_name)[0] + '.png'
        human_mask = np.asarray(Image.open(os.path.join(args.train_anno_dir, human_mask_name)))
        human_gt_labels = np.unique(human_mask)

        for i in range(1, len(human_gt_labels)):
            category_info = {'id': 1, 'is_crowd': 0}
            binary_mask = np.uint8(human_mask == i)
            annotation_info = pycococreatortools.create_annotation_info(
                segmentation_id, image_id, category_info, binary_mask,
                image.size, tolerance=10
            )
            if annotation_info is not None:
                coco_output["annotations"].append(annotation_info)

            segmentation_id += 1
        image_id += 1

    if not os.path.exists(args.json_save_dir):
        os.makedirs(args.json_save_dir)
    if not args.use_val:
        with open('{}/{}_train.json'.format(args.json_save_dir, args.split_name), 'w') as output_json_file:
            json.dump(coco_output, output_json_file)
    else:
        for image_name in os.listdir(args.val_img_dir):
            image = Image.open(os.path.join(args.val_img_dir, image_name))
            image_info = pycococreatortools.create_image_info(
                image_id, image_name, image.size
            )
            coco_output["images"].append(image_info)

            human_mask_name = os.path.splitext(image_name)[0] + '.png'
            human_mask = np.asarray(Image.open(os.path.join(args.val_anno_dir, human_mask_name)))
            human_gt_labels = np.unique(human_mask)

            for i in range(1, len(human_gt_labels)):
                category_info = {'id': 1, 'is_crowd': 0}
                binary_mask = np.uint8(human_mask == i)
                annotation_info = pycococreatortools.create_annotation_info(
                    segmentation_id, image_id, category_info, binary_mask,
                    image.size, tolerance=10
                )
                if annotation_info is not None:
                    coco_output["annotations"].append(annotation_info)

                segmentation_id += 1
            image_id += 1

        with open('{}/{}_trainval.json'.format(args.json_save_dir, args.split_name), 'w') as output_json_file:
            json.dump(coco_output, output_json_file)

    coco_output_val = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id_val = 1
    segmentation_id_val = 1

    for image_name in os.listdir(args.val_img_dir):
        image = Image.open(os.path.join(args.val_img_dir, image_name))
        image_info = pycococreatortools.create_image_info(
            image_id_val, image_name, image.size
        )
        coco_output_val["images"].append(image_info)

        human_mask_name = os.path.splitext(image_name)[0] + '.png'
        human_mask = np.asarray(Image.open(os.path.join(args.val_anno_dir, human_mask_name)))
        human_gt_labels = np.unique(human_mask)

        for i in range(1, len(human_gt_labels)):
            category_info = {'id': 1, 'is_crowd': 0}
            binary_mask = np.uint8(human_mask == i)
            annotation_info = pycococreatortools.create_annotation_info(
                segmentation_id_val, image_id_val, category_info, binary_mask,
                image.size, tolerance=10
            )
            if annotation_info is not None:
                coco_output_val["annotations"].append(annotation_info)

            segmentation_id_val += 1
        image_id_val += 1

    with open('{}/{}_val.json'.format(args.json_save_dir, args.split_name), 'w') as output_json_file_val:
        json.dump(coco_output_val, output_json_file_val)
Пример #11
0
def convert():
    identity_lst = ['pedestrian', 'bicycle-group', 'person-group-far-away', 'scooter-group', 'co-rider', 'scooter', \
                    'motorbike', 'bicycle', 'rider', 'motorbike-group', 'rider+vehicle-group-far-away', None, \
                    'buggy-group', 'wheelchair-group', 'tricycle-group', 'buggy', 'wheelchair', 'tricycle']
    identity2index = dict(zip(identity_lst, range(0, len(identity_lst))))

    image_id = 1
    annotation_id = 1
    data_path = 'datasets/EuroCity/'  #check the path
    for day in ['day']:
        for phase in ['train', 'val', 'test']:
            coco_output = {
                "info": INFO,
                "licenses": LICENSES,
                "categories": CATEGORIES,
                "images": [],
                "annotations": []
            }
            box_num = 0
            ig_box_num = 0

            fold_path = '%s/ECP/%s/img/%s' % (data_path, day, phase)
            print(fold_path)
            fn_lst = glob.glob('%s/*/*.png' % fold_path)

            for img_name in fn_lst:
                image = Image.open(img_name)
                image_info = pycococreatortools.create_image_info(
                    image_id, img_name[img_name.index('ECP'):], image.size)
                coco_output["images"].append(image_info)

                if phase != 'test':
                    anno_fn = img_name.replace('img', 'labels').replace(
                        'png', 'json')

                    anno = json.load(open(anno_fn))

                    boxes = []
                    for each in anno['children']:
                        if len(each["children"]) > 0:
                            # print(each)
                            for each2 in each["children"]:
                                boxes.append([identity2index[each2["identity"]], float(each2['x0']), float(each2['y0']), \
                                              float(each2['x1']) - float(each2['x0']), float(each2['y1']) - float(each2['y0'])])
                                if "occluded>80" in each2['tags']:
                                    boxes[-1].append(1)
                                    # print('heavy occluded')
                                else:
                                    # print('normal')
                                    boxes[-1].append(0)

                        boxes.append([identity2index[each["identity"]], float(each['x0']), float(each['y0']), \
                                        float(each['x1']) - float(each['x0']), float(each['y1']) - float(each['y0'])])
                        if "occluded>80" in each['tags']:
                            boxes[-1].append(1)
                            # print('heavy occluded')
                        else:
                            boxes[-1].append(0)

                    boxes = np.array(boxes)
                    if len(boxes) > 0:
                        # slt = np.logical_and(boxes[:, 0]==0, boxes[:, 4]>20, boxes[:, 5]<1)
                        slt = (boxes[:, 0] == 0)
                        boxes_gt = boxes[slt, 1:5].tolist()
                        boxes_ig = boxes[~slt, 1:5].tolist()
                    else:
                        boxes_gt = []
                        boxes_ig = []
                    box_num += len(boxes_gt)
                    ig_box_num += len(boxes_ig)

                    for annotation in boxes_gt:
                        class_id = 1
                        category_info = {'id': class_id, 'is_crowd': False}
                        annotation_info = pycococreatortools.create_annotation_info(
                            annotation_id, image_id, category_info, annotation,
                            image.size)
                        if annotation_info is not None:
                            coco_output["annotations"].append(annotation_info)
                        annotation_id += 1

                    for annotation in boxes_ig:
                        category_info = {'id': 1, 'is_crowd': True}
                        annotation_info = pycococreatortools.create_annotation_info(
                            annotation_id, image_id, category_info, annotation,
                            image.size)
                        if annotation_info is not None:
                            coco_output["annotations"].append(annotation_info)
                        annotation_id += 1

                image_id = image_id + 1

            with open(data_path + day + '_' + phase + '_all.json',
                      'w') as output_json_file:
                json.dump(coco_output, output_json_file)
            print('box num: ', box_num)
            print('ignore box num: ', ig_box_num)
Пример #12
0
def main():

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id = 1
    segmentation_id = 1
    count = 1
    # filter for jpeg images
    for root, _, files in os.walk(IMAGE_DIR):
        image_files = filter_for_jpeg(root, files)
        # go through each image
        print(len(image_files))
        for image_filename in image_files:
            count = count + 1
            print(image_filename)
            print(count)
            image = Image.open(image_filename)
            image_info = pycococreatortools.create_image_info(
                image_id, os.path.basename(image_filename), image.size)
            coco_output["images"].append(image_info)

            # filter for associated png annotations
            for root, _, files in os.walk(ANNOTATION_DIR):
                annotation_files = filter_for_annotations(
                    root, files, image_filename)

                # go through each associated annotation
                print(len(annotation_files))
                for annotation_filename in annotation_files:
                    class_id = 1
                    print(annotation_filename)
                    # class_id = [x['id'] for x in CATEGORIES if x['name'] in annotation_filename][0]

                    category_info = {
                        'id': class_id,
                        'is_crowd': 'crowd' in image_filename
                    }
                    binary_mask = np.asarray(
                        Image.open(annotation_filename).convert('1')).astype(
                            np.uint8)

                    ###########
                    annotation_filename2 = os.path.join(
                        ANNOTATION_DIR2,
                        annotation_filename.split('/')[-1])
                    print(annotation_filename2)
                    binary_mask2 = np.asarray(
                        Image.open(annotation_filename2).convert('1')).astype(
                            np.uint8)
                    print(binary_mask2.shape)
                    ##########

                    annotation_info = pycococreatortools.create_annotation_info(
                        segmentation_id,
                        image_id,
                        category_info,
                        binary_mask,
                        binary_mask2,
                        image.size,
                        tolerance=0.1)

                    if annotation_info is not None:
                        coco_output["annotations"].append(annotation_info)

                    segmentation_id = segmentation_id + 1

            image_id = image_id + 1

    with open(
            '{}/annotations/instances_plants_validate2019.json'.format(
                ROOT_DIR), 'w') as output_json_file:
        json.dump(coco_output, output_json_file)
Пример #13
0
len(files)

# Get mask annotations info
mask_anno = []
for i, file in enumerate(files):
    img_id = file.rsplit('\\', 1)[1]
    #Change to your own needs if needed.
    # Important is only that ID between mask and image match, format of the ID is up to the user.
    i_id = str(re.findall(r'\d+',
                          img_id)).strip('[]').replace("'", "").replace(
                              ",", "").replace(" ", "_")
    with rasterio.open(file) as src:
        w = src.read(1)
    #if images with hedges havent been seperated yet then uncomment the following line and tab the other two over
    #if np.max(w)==1:
    m_info = coco.create_annotation_info(w, i_id, i, file, (320, 320))
    mask_anno.append(m_info)

# Get original satellite images from folder
train = in_img
val = r'C:\Users\ahls_st\Documents\MasterThesis\IKONOS\With_Hedges\FourBands\Splits\imgs\val'  #change

# combine original files with augmented files
files = getFiles(train, ending='.png')
files2 = getFiles(val, ending='.png')
files3 = getFiles(os.path.join(aug_img_dir, 'Cleaned'), ending='.png')
files_full = sorted(files + files2 + files3)
len(files_full)

#Get images annotation info
image_infos = []
Пример #14
0
def append_images_to_annotations(original_ann_filename, ROOT_DIR, IMAGE_DIR,
                                 ANNOTATION_DIR, output_filename):
    """
    args:
        ROOT_DIR: file where the anonation and the image folders are
        original_ann_filename: json file where we want to append images
        IMAGE_DIR: directory with the images we want to add
        ANNOTATION_DIR: directory with masks of the elements in the images (.png files)
    """
    print("creating anotations..")
    original_ann_filename = os.path.join(ROOT_DIR, original_ann_filename)
    IMAGE_DIR = os.path.join(ROOT_DIR, IMAGE_DIR)
    ANNOTATION_DIR = os.path.join(ROOT_DIR, ANNOTATION_DIR)

    #start with original json file
    with open(original_ann_filename) as json_file:
        coco_output = json.load(json_file)

    image_id = len(coco_output['images'])  #start with the last id
    segmentation_id = len(
        coco_output['annotations'])  #start with the last id of annotations
    CATEGORIES = coco_output['categories']

    # filter for jpeg images
    for root, _, files in os.walk(IMAGE_DIR):
        image_files = pycococreatortools.filter_for_jpeg(root, files)

        # go through each image
        for image_filename in image_files:
            image = Image.open(image_filename)
            image_filename_path = os.path.basename(
                IMAGE_DIR) + "/" + os.path.basename(image_filename)

            image_info = pycococreatortools.create_image_info(
                image_id, image_filename_path, image.size)
            coco_output["images"].append(image_info)

            # filter for associated png annotations
            for root, _, files in os.walk(ANNOTATION_DIR):
                annotation_files = pycococreatortools.filter_for_annotations(
                    root, files, image_filename)

                if len(annotation_files) == 0:
                    print("no ann files " + image_filename)

                # go through each associated annotation
                for annotation_filename in annotation_files:

                    #print(annotation_filename)
                    #skip BG
                    if "BG" not in annotation_filename:
                        s = os.path.basename(annotation_filename)
                        category_in_file = (
                            s.split("_"))[2].split("_")[0]  #extract category
                        class_id = [
                            x["id"] for x in CATEGORIES
                            if x["name"] == category_in_file
                        ][0]

                        category_info = {
                            "id": class_id,
                            "is_crowd": 'crowd' in image_filename
                        }
                        binary_mask = np.asarray(
                            Image.open(annotation_filename).convert(
                                'P')).astype(np.uint8)

                        annotation_info = pycococreatortools.create_annotation_info(
                            segmentation_id,
                            image_id,
                            category_info,
                            binary_mask,
                            image.size,
                            tolerance=2)

                        if annotation_info is not None:

                            coco_output["annotations"].append(annotation_info)
                        else:
                            print("No_anotation " + annotation_filename)

                        segmentation_id = segmentation_id + 1

            image_id = image_id + 1

    with open('{}/{}'.format(ROOT_DIR, output_filename),
              'w') as output_json_file:
        json.dump(coco_output, output_json_file)
    print("Finished creating anotations.")
Пример #15
0
def main():

    total_imagenum_count = 1

    for coco_type in total_coco:

        coco_output = {
            "info": INFO,
            "licenses": LICENSES,
            "categories": CATEGORIES,
            "images": [],
            "annotations": []
        }

        ROOT_DIR = os.path.join(maindir, 'data', mother_dataset_name)
        IMAGE_DIR = os.path.join(ROOT_DIR, coco_type)
        ANNOTATION_DIR = os.path.join(ROOT_DIR, coco_type + '_annotations')

        RENAME_IMAGE_DIR = os.path.join(ROOT_DIR, "rename_" + coco_type)
        RENAME_ANNOTATION_DIR = os.path.join(
            ROOT_DIR, "rename_" + coco_type + "_annotations")

        os.makedirs(RENAME_IMAGE_DIR, exist_ok=True)
        os.makedirs(RENAME_ANNOTATION_DIR, exist_ok=True)

        img_files = sorted(os.listdir(IMAGE_DIR))
        anno_files = sorted(os.listdir(ANNOTATION_DIR))

        for i in range(len(anno_types)):
            blank = [name for name in anno_files if anno_types[i] in name]
            if annotation_dict[i]['annotation_types'] == anno_types[i]:
                annotation_dict[i]['filenames'] = blank

        count = total_imagenum_count
        for num_ in range(len(img_files)):

            img_name = img_files[num_]
            img_ext = pathlib.PurePosixPath(img_name).suffix
            img_new_name = img_name.replace(
                img_name, DATASET_NAME_PREFIX + "_" + TRAIN_DATASET +
                "_{0:012d}".format(count)) + img_ext
            origin_img = cv2.imread(os.path.join(IMAGE_DIR, img_name))
            resize_img = resize_image(origin_img, width=780)
            cv2.imwrite(os.path.join(RENAME_IMAGE_DIR, img_new_name),
                        resize_img)

            for anno_num in range(len(anno_types)):

                anno_type = anno_types[anno_num]  #dog, cat
                anno_name = annotation_dict[anno_num]['filenames'][num_]

                anno_ext = pathlib.PurePosixPath(anno_name).suffix
                anno_new_name = anno_name.replace(
                    anno_name, DATASET_NAME_PREFIX + "_" + TRAIN_DATASET +
                    "_{0:012d}".format(count)) + "_" + anno_types[
                        anno_num] + anno_ext  # 분류될 이름을 반드시 넣을 것!!! cat 이부분에
                origin_anno = cv2.imread(
                    os.path.join(ANNOTATION_DIR, anno_name))

                resize_anno = resize_image(origin_anno, width=780)

                cv2.imwrite(os.path.join(RENAME_ANNOTATION_DIR, anno_new_name),
                            resize_anno)

            count += 1

        print("Renaming image file name is done!")
        print("# of Dataset : {}".format(count - 1))
        total_imagenum_count += count

        image_id = 1
        segmentation_id = 1

        view_count = 1
        for root, _, files in os.walk(RENAME_IMAGE_DIR):

            image_files = filter_for_images(root, files)  # image파일 골라내기

            for image_filename in image_files:  # rename 이미지 파일들 하나씩 불러오기

                image = Image.open(image_filename)
                image_info = pycococreatortools.create_image_info(
                    image_id, os.path.basename(image_filename), image.size)
                # coco데이터 셋에 정보를 넣기위해 이미지 번호,이름,이미지 크기등을 입력하여

                coco_output["images"].append(image_info)
                # 위 coco_output의 images 칸에 image_info를 넣기

                for root, _, files in os.walk(RENAME_ANNOTATION_DIR):
                    annotation_files = filter_for_annotations(
                        root, files, image_filename)

                    # go through each associated annotation
                    for annotation_filename in annotation_files:

                        class_id = [
                            x['id'] for x in CATEGORIES
                            if x['name'] in annotation_filename
                        ][0]
                        # CATEGORIES에서 목록을 뽑아내고 annotation_filename에
                        # x의 'name'부분과 일치하는 부분이 있는지 확인 후, 일치하는 id number를 가져오는 부분.

                        category_info = {
                            'id': class_id,
                            'is_crowd': 'crowd' in image_filename
                        }  # is_crowd가 뭔지 아직 모름

                        binary_mask = np.asarray(
                            Image.open(annotation_filename).convert(
                                '1')).astype(np.uint8)  #0과 1로 이루어진 마스크를 생성.

                        annotation_info = pycococreatortools.create_annotation_info(
                            segmentation_id,
                            image_id,
                            category_info,
                            binary_mask,
                            image.size,
                            tolerance=2)
                        # annotation의 정보를 만들어주는 듯. segmentation number를 저장하고 (위 image_id와 같은 세트이므로 같아야됨)
                        # 그리고 해당 annotation의 category 정보를 넣어주고, binary 마스크를 입력, 이미지 크기 또한 입력한다.
                        if annotation_info is not None:
                            coco_output["annotations"].append(annotation_info)

                        segmentation_id = segmentation_id + 1
                print("--------------------------")
                print("{}/{} is completed ! ".format(view_count,
                                                     len(image_files)))
                view_count += 1
                image_id = image_id + 1

        with open(
                os.path.join(RENAME_ANNOTATION_DIR,
                             coco_type + '2020.json').format(ROOT_DIR),
                'w') as output_json_file:
            json.dump(coco_output, output_json_file)
Пример #16
0
def main():
    coco_output = {
        'info': INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }
    image_id = 1
    segmentation_id = 1
    # filter for jpeg images
    image_files = os.listdir(IMAGE_DIR)
    annotation_filenames = os.listdir(ANNOTATION_DIR)
    # go through each image
    for image_filename, annotation_filename in zip(image_files,
                                                   annotation_filenames):
        image = Image.open(os.path.join(IMAGE_DIR, image_filename))
        image_info = pycococreatortools.create_image_info(
            image_id, image_filename, image.size)
        coco_output["images"].append(image_info)
        print(image_filename, annotation_filename)
        binary_mask = np.asarray(
            Image.open(os.path.join(ANNOTATION_DIR,
                                    annotation_filename)).convert('1')).astype(
                                        np.uint8)
        mask = np.unique(binary_mask)
        for i in mask:
            if i < 255:
                class_id = i
                category_info = {
                    "id": class_id,
                    "is_crowd": "crowd" in image_filename
                }
                annotation_info = pycococreatortools.create_annotation_info(
                    segmentation_id,
                    image_id,
                    category_info,
                    binary_mask,
                    image.size,
                    tolerance=2)
                coco_output["annotations"].append(annotation_info)
        segmentation_id = segmentation_id + 1
        image_id = image_id + 1

    class NumpyEncoder(json.JSONEncoder):
        """ Special json encoder for numpy types """
        def default(self, obj):
            if isinstance(
                    obj,
                (np.int_, np.intc, np.intp, np.int8, np.int16, np.int32,
                 np.int64, np.uint8, np.uint16, np.uint32, np.uint64)):
                return int(obj)
            elif isinstance(obj,
                            (np.float_, np.float16, np.float32, np.float64)):
                return float(obj)
            elif isinstance(obj, (np.ndarray, )):
                return obj.tolist()
            return json.JSONEncoder.default(self, obj)

    # operation = json.dumps(coco_output, cls=NumpyEncoder)
    with open("{}/instances_diangan_train2020.json".format(ROOT_DIR),
              "w",
              encoding="utf-8") as output_json_file:
        json.dump(coco_output,
                  output_json_file,
                  ensure_ascii=False,
                  indent=1,
                  cls=NumpyEncoder)
def main():
    """ Main entry point of the app """

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    segmentation_id = 1
    image_id = 1

    for root, directories, files in os.walk(IMAGE_DIR):
        file_types = ['*.jpeg', '*.jpg']
        file_types = r'|'.join([fnmatch.translate(x) for x in file_types])
        files = [os.path.join(root, f) for f in files]
        files = [f for f in files if re.match(file_types, f)]

        # go through each image
        for i, filename in enumerate(files):
            print(filename)
            parent_directory = root.split(os.path.sep)[-1]
            basename_no_extension = os.path.splitext(
                os.path.basename(filename))[0]
            image = Image.open(filename)
            image_info = pycococreatortools.create_image_info(
                image_id, os.path.basename(filename), image.size)
            coco_output["images"].append(image_info)

            # go through each associated annotation
            for root, directories, files in os.walk(ANNOTATION_DIR):
                file_types = ['*.png']
                file_types = r'|'.join([fnmatch.translate(x)
                                        for x in file_types])
                file_name_prefix = basename_no_extension + '.*'
                files = [os.path.join(root, f) for f in files]
                files = [f for f in files if re.match(file_types, f)]
                files = [f for f in files if re.match(
                    file_name_prefix, os.path.splitext(os.path.basename(f))[0])]

                for filename in files:
                    parent_directory = root.split(os.path.sep)[-1]
                    basename_no_extension = os.path.splitext(
                        os.path.basename(filename))[0]
                    annotation_array = np.array(Image.open(filename))
                    # load annotation information
                    annotation_metadata = get_metadata(filename)
                    annotation_data = AnnotationData(
                        annotation_array, annotation_metadata)
                    object_classes = annotation_data.get_classes()

                    # go through each class
                    for j, object_class in enumerate(object_classes):
                        if ANNOTATOR_CATEGORIES.get(object_class) == None:
                            print("missing: {}".format(object_class))
                            continue

                        # go through each object
                        for object_instance in range(object_classes[object_class]):
                            object_mask = annotation_data.get_mask(
                                object_class, object_instance)
                            if object_mask is not None:
                                object_mask = object_mask.astype(np.uint8)

                                annotation_info = pycococreatortools.create_annotation_info(segmentation_id, image_id,
                                                                                            ANNOTATOR_CATEGORIES.get(object_class), object_mask, image.size, tolerance=2)

                                if annotation_info is not None:
                                    coco_output["annotations"].append(
                                        annotation_info)
                                    segmentation_id = segmentation_id + 1

            image_id = image_id + 1

    with open('{}/coco.json'.format(DATA_DIR), 'w') as output_json_file:
        json.dump(coco_output, output_json_file)
Пример #18
0
def main(args):
    INFO = {
        "description": args.dataset + " Dataset",
        "url": "",
        "version": "",
        "year": 2020,
        "contributor": "qiu",
        "date_created": datetime.datetime.utcnow().isoformat(' ')
    }

    LICENSES = [{"id": 1, "name": "", "url": ""}]

    CATEGORIES = [
        {
            'id': 1,
            'name': 'person',
            'supercategory': 'person',
        },
    ]

    # coco_output = {
    #     "info": INFO,
    #     "licenses": LICENSES,
    #     "categories": CATEGORIES,
    #     "images": [],
    #     "annotations": []
    # }
    # image_id = 1
    # segmentation_id = 1
    # train_imgs = os.listdir(args.train_img_dir)
    # for idx, image_name in enumerate(train_imgs):
    #     if image_name.endswith(('jpg', 'png')):
    #         # print(image_name+'\n')
    #         try:
    #             image = Image.open(os.path.join(args.train_img_dir,
    #                                             image_name))
    #         except:
    #             print('corrupt img', image_name)
    #         image_info = pycococreatortools.create_image_info(
    #             image_id, image_name, image.size)
    #         coco_output["images"].append(image_info)
    #         mask_name_prefix = image_name.split('.')[0]
    #         mask_path = os.path.join(args.train_anno_dir,
    #                                  mask_name_prefix + '.png')
    #         mask = np.asarray(Image.open(mask_path))
    #         if len(mask.shape) == 3:
    #             mask = mask[:, :, 0]
    #         gt_labels = np.unique(mask)
    #         for k in range(1, len(gt_labels)):
    #             category_info = {'id': 1, 'is_crowd': 0}
    #             binary_mask = np.uint8(mask == k)
    #             annotation_info = pycococreatortools.create_annotation_info(
    #                 segmentation_id,
    #                 image_id,
    #                 category_info,
    #                 binary_mask,
    #                 image.size,
    #                 tolerance=0)
    #             if annotation_info is not None:
    #                 coco_output["annotations"].append(annotation_info)

    #             segmentation_id += 1
    #         image_id += 1
    #     if image_id % 100 == 0:
    #         print(image_id)
    # if not os.path.exists(args.json_save_dir):
    #     os.makedirs(args.json_save_dir)

    # if not args.use_val:
    #     with open('{}/{}_train.json'.format(args.json_save_dir, args.dataset),
    #               'w') as output_json_file:
    #         json.dump(coco_output, output_json_file)

    # val
    coco_output_val = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id_val = 1
    segmentation_id_val = 1
    val_imgs = os.listdir(args.val_img_dir)
    for idx, image_name in enumerate(val_imgs):
        if image_name.endswith(('jpg', 'png')):
            # print(image_name+'\n')
            try:
                image = Image.open(os.path.join(args.val_img_dir, image_name))
            except:
                print('corrupt img', image_name)
            image_info = pycococreatortools.create_image_info(
                image_id_val, image_name, image.size)
            coco_output_val["images"].append(image_info)
            mask_name_prefix = image_name.split('.')[0]
            mask_path = os.path.join(args.val_anno_dir,
                                     mask_name_prefix + '.png')
            mask = np.asarray(Image.open(mask_path))
            if len(mask.shape) == 3:
                mask = mask[:, :, 0]
            gt_labels = np.unique(mask)
            for k in range(1, len(gt_labels)):
                category_info = {'id': 1, 'is_crowd': 0}
                binary_mask = np.uint8(mask == k)
                annotation_info = pycococreatortools.create_annotation_info(
                    segmentation_id_val,
                    image_id_val,
                    category_info,
                    binary_mask,
                    image.size,
                    tolerance=0)
                if annotation_info is not None:
                    coco_output_val["annotations"].append(annotation_info)

                segmentation_id_val += 1
            image_id_val += 1
        if image_id_val % 100 == 0:
            print(image_id_val)
    if not os.path.exists(args.json_save_dir):
        os.makedirs(args.json_save_dir)

    if not args.use_val:
        with open('{}/{}_val.json'.format(args.json_save_dir, args.dataset),
                  'w') as output_json_file_val:
            json.dump(coco_output_val, output_json_file_val)
Пример #19
0
def main(args):
    INFO = {
        "description": args.dataset + " Dataset",
        "url": "",
        "version": "",
        "year": 2020,
        "contributor": "qiu",
        "date_created": datetime.datetime.utcnow().isoformat(' ')
    }

    LICENSES = [{"id": 1, "name": "", "url": ""}]

    CATEGORIES = [
        {
            'id': 1,
            'name': 'foot',
            'supercategory': 'person',
        },
    ]

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    # mhp2_foot_labels = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 33]

    image_id = 1
    segmentation_id = 1
    train_imgs = os.listdir(args.train_img_dir)
    for idx, image_name in enumerate(train_imgs):
        if image_name.endswith(('jpg', 'png')):
            # print(image_name+'\n')
            try:
                image = Image.open(os.path.join(args.train_img_dir,
                                                image_name))
            except:
                print('corrupt img', image_name)
            # image=cv2.imread(os.path.join(args.train_img_dir, image_name))
            image_info = pycococreatortools.create_image_info(
                image_id, image_name, image.size)
            coco_output["images"].append(image_info)
            mask_name_prefix = image_name.split('.')[0]
            # mask_temp = np.ones((image.size[1], image.size[0]),dtype=np.uint8)
            mask_temp = np.zeros((image.size[1], image.size[0]),
                                 dtype=np.uint8)

            count_str = ''
            ins_count = 0
            for i in range(1, 100):
                if i < 10:
                    count_str = '_0' + str(i)
                else:
                    count_str = '_' + str(i)

                if os.path.exists(
                        os.path.join(args.train_anno_dir, mask_name_prefix +
                                     count_str + '_01.png')):
                    ins_count = i
                    break
            temp_count = ''
            for i in range(ins_count):
                if i < 9:
                    temp_count = '_0' + str(i + 1)
                else:
                    temp_count = '_' + str(i + 1)
                mask_path = os.path.join(
                    args.train_anno_dir,
                    mask_name_prefix + count_str + temp_count + '.png')
                mask = np.asarray(Image.open(mask_path))
                # mask=cv2.imread(mask_path)
                if len(mask.shape) == 3:
                    mask = mask[:, :, 0]
                for j in range(2):
                    # # MHP v2 foot area
                    # mask_temp = np.where((mask >= 20) & (mask <= 33) &
                    #                      (mask != 30) & (mask != 31), 1, 0)
                    # MHP v1 foot area
                    mask_temp = np.where((mask == 9 + j), 1, 0)

                    # cv2.imshow("ss", mask_temp*255.0)
                    # cv2.waitKey(-1)
                    gt_labels = np.unique(mask_temp)
                    for k in range(1, len(gt_labels)):
                        category_info = {'id': 1, 'is_crowd': 0}
                        binary_mask = np.uint8(mask_temp)
                        annotation_info = pycococreatortools.create_annotation_info(
                            segmentation_id,
                            image_id,
                            category_info,
                            binary_mask,
                            image.size,
                            tolerance=0)
                        if annotation_info is not None:
                            coco_output["annotations"].append(annotation_info)

                        segmentation_id += 1

            image_id += 1
        if image_id % 100 == 0:
            print(image_id)
    if not os.path.exists(args.json_save_dir):
        os.makedirs(args.json_save_dir)

    if not args.use_val:
        with open('{}/{}_train.json'.format(args.json_save_dir, args.dataset),
                  'w') as output_json_file:
            json.dump(coco_output, output_json_file)

    coco_output_val = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "images": [],
        "annotations": []
    }

    image_id_val = 1
    segmentation_id_val = 1

    val_imgs = os.listdir(args.val_img_dir)
    for idx, image_name in enumerate(val_imgs):
        if image_name.endswith(('jpg', 'png')):
            try:
                image = Image.open(os.path.join(args.val_img_dir, image_name))
            except:
                print('corrupt img', image_name)
            # print(image_name+'\n')
            # image=cv2.imread(os.path.join(args.val_img_dir, image_name))
            image_info = pycococreatortools.create_image_info(
                image_id_val, image_name, image.size)
            coco_output_val["images"].append(image_info)
            mask_name_prefix = image_name.split('.')[0]
            # mask_temp = np.ones((image.size[1], image.size[0]))
            mask_temp = np.zeros((image.size[1], image.size[0]),
                                 dtype=np.uint8)
            count_str = ''
            ins_count = 0
            for i in range(1, 100):
                if i < 10:
                    count_str = '_0' + str(i)
                else:
                    count_str = '_' + str(i)

                if os.path.exists(
                        os.path.join(args.val_anno_dir, mask_name_prefix +
                                     count_str + '_01.png')):
                    ins_count = i
                    break
            temp_count = ''
            for i in range(ins_count):
                if i < 9:
                    temp_count = '_0' + str(i + 1)
                else:
                    temp_count = '_' + str(i + 1)
                mask_path = os.path.join(
                    args.val_anno_dir,
                    mask_name_prefix + count_str + temp_count + '.png')
                mask = np.asarray(Image.open(mask_path))
                # mask=cv2.imread(mask_path)
                if len(mask.shape) == 3:
                    mask = mask[:, :, 0]
                # # MHP v2 foot area
                mask_temp = np.where(
                    (mask >= 20) & (mask <= 33) & (mask != 30) & (mask != 31),
                    1, 0)
                # MHP v1 foot area
                # mask_temp = np.where((mask == 9) | (mask == 10), 1, 0)

                # cv2.imshow("ss", mask_temp*255.0)
                # cv2.waitKey(-1)
                gt_labels = np.unique(mask_temp)
                for i in range(1, len(gt_labels)):
                    category_info = {'id': 1, 'is_crowd': 0}
                    binary_mask = np.uint8(mask_temp)
                    annotation_info = pycococreatortools.create_annotation_info(
                        segmentation_id_val,
                        image_id_val,
                        category_info,
                        binary_mask,
                        image.size,
                        tolerance=0)
                    if annotation_info is not None:
                        coco_output_val["annotations"].append(annotation_info)

                    segmentation_id_val += 1

            image_id_val += 1
        if image_id_val % 100 == 0:
            print(image_id_val)

    with open('{}/{}_val.json'.format(args.json_save_dir, args.dataset),
              'w') as output_json_file_val:
        json.dump(coco_output_val, output_json_file_val)