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 test_toy_example_dets():
    dir_annots_dets = 'toyexample/dets_classid_rel_xcycwh'

    yolo_files = general_utils.get_files_recursively(dir_annots_dets)
    assert len(yolo_files) > 0
    for pascal_file in yolo_files:
        assert validations.is_yolo_format(pascal_file, bb_types=[BBType.DETECTED])
Пример #3
0
def _get_annotation_files(file_path):
    # Path can be a directory containing all files or a directory containing multiple files
    if file_path is None:
        return []
    annotation_files = []
    if os.path.isfile(file_path):
        annotation_files = [file_path]
    elif os.path.isdir(file_path):
        annotation_files = general_utils.get_files_recursively(file_path)
    return sorted(annotation_files)
def is_valid_cvat_dir(dir):
    bb_files = get_files_recursively(dir)
    if len(bb_files) != 1:
        return False
    for file_path in bb_files:
        return verify_format(file_path, FileFormat.CVAT)
Пример #5
0
def test_validation_formats():
    # Validate COCO format
    folder_annotations = 'data/database/dets/coco_format'
    assert validations.is_valid_coco_dir(folder_annotations)

    folder_annotations = 'data/database/gts/coco_format_v1'
    assert validations.is_valid_coco_dir(folder_annotations)

    folder_annotations = 'data/database/gts/coco_format_v2'
    assert validations.is_valid_coco_dir(folder_annotations)

    # Validate pascal format
    folder_annotations = 'data/database/gts/pascalvoc_format'
    bb_files = utils.get_files_recursively(folder_annotations)
    assert len(bb_files) > 0
    for file_path in bb_files:
        assert validations.is_pascal_format(file_path)

    # Validate absolute text formats for detections
    folders_annotations = [
        'data/database/dets/abs_xywh', 'data/database/dets/abs_xyx2y2'
    ]
    for folder_annotations in folders_annotations:
        bb_files = utils.get_files_recursively(folder_annotations)
        assert len(bb_files) > 0
        for file_path in bb_files:
            assert validations.is_specific_text_format(
                file_path, CoordinatesType.ABSOLUTE, BBType.DETECTED)

    # Validate relative text formats for detections
    folder_annotations = 'data/database/dets/rel_xywh'
    bb_files = utils.get_files_recursively(folder_annotations)
    assert len(bb_files) > 0
    for file_path in bb_files:
        assert validations.is_specific_text_format(file_path,
                                                   CoordinatesType.RELATIVE,
                                                   BBType.DETECTED)

    # Validate CVAT format
    folder_annotations = 'data/database/gts/cvat_format'
    assert validations.is_valid_cvat_dir(folder_annotations)

    # Validate OpenImageDataset (CSV)
    folder_annotations = 'data/database/gts/openimages_format'
    bb_files = utils.get_files_recursively(folder_annotations)
    assert len(bb_files) > 0
    for file_path in bb_files:
        assert validations.verify_format(file_path, FileFormat.OPENIMAGE)

    # Validate ImageNet (XML)
    folder_annotations = 'data/database/gts/imagenet_format/Annotations'
    bb_files = utils.get_files_recursively(folder_annotations)
    assert len(bb_files) > 0
    for file_path in bb_files:
        assert validations.verify_format(file_path, FileFormat.IMAGENET)

    # Validate LABEL ME format
    folder_annotations = 'data/database/gts/labelme_format'
    bb_files = utils.get_files_recursively(folder_annotations)
    assert len(bb_files) > 0
    for file_path in bb_files:
        assert validations.verify_format(file_path, FileFormat.LABEL_ME)

    # Validate voc pascal files
    folder_annotations = 'data/database/dets/abs_xywh'
    bb_files = utils.get_files_recursively(folder_annotations)
    assert len(bb_files) > 0
    for file_path in bb_files:
        assert validations.verify_format(
            file_path,
            FileFormat.ABSOLUTE_TEXT,
        )

    # Validate yolo files
    folder_annotations = 'data/database/gts/yolo_format/obj_train_data'
    bb_files = utils.get_files_recursively(folder_annotations)
    assert len(bb_files) > 0
    for file_path in bb_files:
        assert validations.verify_format(file_path, FileFormat.YOLO)
        r_Xin = int(xIn - int(thickness / 2))
        r_Yin = int(yin_bb - th - int(thickness / 2))
        # Draw filled rectangle to put the text in it
        cv2.rectangle(
            image, (r_Xin, r_Yin - thickness),
            (r_Xin + tw + thickness * 3, r_Yin + th + int(12.5 * fontScale)),
            (b, g, r), -1)
        cv2.putText(image, label, (xin_bb, yin_bb), font, fontScale, (0, 0, 0),
                    fontThickness, cv2.LINE_AA)
    return image


# Drawing bb into the images
green_color = [62, 235, 59]
red_color = [255, 0, 0]
for img_file in general_utils.get_files_recursively(dir_imgs):
    # Get corresponding GT bounding boxes
    gt_annotation_file = general_utils.find_file(dir_gts,
                                                 os.path.basename(img_file),
                                                 match_extension=False)
    if gt_annotation_file is None:
        continue
    gt_bbs = converter.vocpascal2bb(gt_annotation_file)
    # Get corresponding detections bounding boxes
    det_annotation_file = general_utils.find_file(dir_dets,
                                                  os.path.basename(img_file),
                                                  match_extension=False)
    if det_annotation_file is None:
        det_bbs = []
    else:
        det_bbs = converter.text2bb(det_annotation_file,