def test_toy_example_gts():
    ############################################################################
    # Verify if all files in the toy example follow their expected format
    ############################################################################

    # PASCAL VOC
    dir_annots_gts_pascal = 'toyexample/gts_vocpascal_format'
    files = general_utils.get_files_recursively(dir_annots_gts_pascal)
    assert len(files) > 0
    for f in files:
        assert validations.is_pascal_format(
            f), 'File {f} does not follow the expected format (PASCAL VOC)'

    # COCO
    dir_annots_gts_coco = 'toyexample/gts_coco_format'
    files = general_utils.get_files_recursively(dir_annots_gts_coco)
    assert len(files) > 0
    for f in files:
        assert validations.is_coco_format(f), 'File {f} does not follow the expected format (COCO)'

    ############################################################################
    # Compare if all bounding boxes are the same
    ############################################################################
    pascal_bbs = converter.vocpascal2bb(dir_annots_gts_pascal)
    coco_bbs = converter.coco2bb(dir_annots_gts_coco)

    coco_bbs.sort(key=lambda x: str(x), reverse=True)
    pascal_bbs.sort(key=lambda x: str(x), reverse=True)

    assert len(coco_bbs) == len(pascal_bbs)

    for coco_bb, pascal_bb in zip(coco_bbs, pascal_bbs):
        assert coco_bb == pascal_bb
def coco2bb(path, bb_type=BBType.GROUND_TRUTH):
    ret = []
    # Get annotation files in the path
    annotation_files = _get_annotation_files(path)
    # Loop through each file
    for file_path in annotation_files:
        if not validations.is_coco_format(file_path):
            continue

        with open(file_path, "r") as f:
            json_object = json.load(f)

        # COCO json file contains basically 3 lists:
        # categories: containing the classes
        # images: containing information of the images (width, height and filename)
        # annotations: containing information of the bounding boxes (x1, y1, bb_width, bb_height)
        classes = {}
        if 'categories' in json_object:
            classes = json_object['categories']
            # into dictionary
            classes = {c['id']: c['name'] for c in classes}
        images = {}
        # into dictionary
        for i in json_object['images']:
            images[i['id']] = {
                'file_name': i['file_name'],
                'img_size': (int(i['width']), int(i['height']))
            }
        annotations = []
        if 'annotations' in json_object:
            annotations = json_object['annotations']

        for annotation in annotations:
            img_id = annotation['image_id']
            x1, y1, bb_width, bb_height = annotation['bbox']
            if bb_type == BBType.DETECTED and 'score' not in annotation.keys():
                print('Warning: Confidence not found in the JSON file!')
                return ret
            confidence = annotation[
                'score'] if bb_type == BBType.DETECTED else None
            # Make image name only the filename, without extension
            img_name = images[img_id]['file_name']
            img_name = general_utils.get_file_name_only(img_name)
            # create BoundingBox object
            bb = BoundingBox(image_name=img_name,
                             class_id=classes[annotation['category_id']],
                             coordinates=(x1, y1, bb_width, bb_height),
                             type_coordinates=CoordinatesType.ABSOLUTE,
                             img_size=images[img_id]['img_size'],
                             confidence=confidence,
                             bb_type=bb_type,
                             format=BBFormat.XYWH)
            ret.append(bb)
    return ret