Example #1
0
def test_relabel_darknet(data_dir, ):
    """
    Test for the cvdata.relabel.relabel_darknet() function

    :param data_dir: temporary directory into which test files will be loaded
    """
    darknet_file_name = "darknet_1.txt"
    darknet_file_path = str(data_dir.join(darknet_file_name))

    # confirm that a relabeling won't occur if the old value is not present
    relabel.relabel_darknet(darknet_file_path, 58, 59)
    expected_darknet_file_name = "expected_darknet_1.txt"
    expected_darknet_file_path = str(data_dir.join(expected_darknet_file_name))
    assert text_files_equal(
        darknet_file_path,
        expected_darknet_file_path,
    )

    # confirm that relabeling occurred as expected
    relabel.relabel_darknet(darknet_file_path, 3, 2)
    expected_darknet_file_name = "expected_darknet_2.txt"
    expected_darknet_file_path = str(data_dir.join(expected_darknet_file_name))
    assert text_files_equal(
        darknet_file_path,
        expected_darknet_file_path,
    )

    # confirm that various invalid arguments raise an error
    with pytest.raises(TypeError):
        relabel.relabel_darknet(darknet_file_path, None, 0)
        relabel.relabel_darknet(darknet_file_path, 0, None)
        relabel.relabel_darknet(1, 0, 1)
        relabel.relabel_darknet(None, 1, 0)
        relabel.relabel_darknet("/not/present", 0, 1)
        relabel.relabel_darknet(1.0, "strings won't work", 0)
        relabel.relabel_darknet(darknet_file_path, 1, "strings won't work")
        relabel.relabel_darknet(darknet_file_path, 1.0, 0)
        relabel.relabel_darknet(darknet_file_path, 2, 1.0)
        relabel.relabel_darknet(darknet_file_path, True, 0)
        relabel.relabel_darknet(darknet_file_path, 1, True)
    with pytest.raises(ValueError):
        relabel.relabel_darknet(darknet_file_path, -5, 1)
        relabel.relabel_darknet(darknet_file_path, 1, -4)
Example #2
0
def test_relabel_kitti(data_dir, ):
    """
    Test for the cvdata.relabel.relabel_kitti() function

    :param data_dir: temporary directory into which test files will be loaded
    """
    kitti_file_name = "kitti_1.txt"
    kitti_file_path = str(data_dir.join(kitti_file_name))

    # confirm that a relabeling won't occur if the old value is not present
    relabel.relabel_kitti(kitti_file_path, "NOT_PRESENT", "NOT_USED")
    expected_kitti_file_name = "expected_kitti_1.txt"
    expected_kitti_file_path = str(data_dir.join(expected_kitti_file_name))
    assert text_files_equal(
        kitti_file_path,
        expected_kitti_file_path,
    )

    # confirm that relabeling occurred as expected
    relabel.relabel_kitti(kitti_file_path, "pistol", "firearm")
    expected_kitti_file_name = "expected_kitti_2.txt"
    expected_kitti_file_path = str(data_dir.join(expected_kitti_file_name))
    assert text_files_equal(
        kitti_file_path,
        expected_kitti_file_path,
    )

    # confirm that various invalid arguments raise an error
    with pytest.raises(ValueError):
        relabel.relabel_kitti(None, "don't care", "don't care")
        relabel.relabel_kitti(kitti_file_path, None, "don't care")
        relabel.relabel_kitti(kitti_file_path, "don't care", None)
        relabel.relabel_kitti("/not/present", "don't care", "don't care")
        relabel.relabel_kitti(1, "don't care", "don't care")
        relabel.relabel_kitti(1.0, "don't care", "don't care")
        relabel.relabel_kitti(kitti_file_path, 1, "don't care")
        relabel.relabel_kitti(kitti_file_path, 1.0, "don't care")
        relabel.relabel_kitti(kitti_file_path, True, "don't care")
        relabel.relabel_kitti(kitti_file_path, "don't care", 1)
        relabel.relabel_kitti(kitti_file_path, "don't care", 1.0)
        relabel.relabel_kitti(kitti_file_path, "don't care", True)
Example #3
0
def test_clean_kitti(data_dir, ):
    """
    Test for the cvdata.clean.clean_kitti() function

    :param data_dir: temporary directory into which test files will be loaded
    """
    problems_dir = os.path.join(str(data_dir), 'problems')
    clean.clean_kitti(str(data_dir),
                      str(data_dir), {"pistol": "handgun"},
                      label_keep=["handgun", "weapon"],
                      problems_dir=problems_dir)

    # make sure that the test expected JPG image and KITTI label
    # files were correctly moved into the "problems" directory
    expected_image_path = os.path.join(problems_dir,
                                       'converted_handgun_00941.jpg')
    assert os.path.exists(expected_image_path)
    expected_kitti_path_1 = os.path.join(problems_dir,
                                         'expected_handgun_00941.txt')
    assert os.path.exists(expected_kitti_path_1)
    expected_kitti_path_2 = os.path.join(problems_dir,
                                         'expected_handgun_00929.txt')
    assert os.path.exists(expected_kitti_path_2)

    # make sure that the original PNG image was converted to JPG
    converted_image_path = os.path.join(str(data_dir), 'handgun_00941.jpg')
    assert os.path.exists(converted_image_path)

    # make sure the original PNG file was deleted
    assert not os.path.exists(os.path.join(str(data_dir), 'handgun_00941.png'))

    # make sure that the image was correctly converted to JPG
    assert images_equal(converted_image_path, expected_image_path)

    # make sure that the KITTI file was correctly updated
    assert text_files_equal(expected_kitti_path_1,
                            os.path.join(data_dir, 'handgun_00941.txt'))
    assert text_files_equal(expected_kitti_path_2,
                            os.path.join(data_dir, 'handgun_00929.txt'))
Example #4
0
def test_resize_image_label(
        data_dir,
):
    """
    Test for the cvdata.resize.resize_image_label() function

    :param data_dir: temporary directory into which test files will be loaded
    """
    file_id = "image"
    image_ext = ".jpg"
    image_file_name = f"{file_id}{image_ext}"
    kitti_ext = ".txt"
    kitti_file_name = f"{file_id}{kitti_ext}"
    pascal_ext = ".xml"
    pascal_file_name = f"{file_id}{pascal_ext}"

    # make a directory to hold our resized files
    resized_dir = os.path.join(str(data_dir), "resized")
    os.makedirs(resized_dir, exist_ok=True)

    new_width = 240
    new_height = 720
    expected_resized_file_id = f"{file_id}_w{new_width}_h{new_height}"
    expected_resized_image_file_name = f"{expected_resized_file_id}{image_ext}"
    expected_resized_kitti_file_name = f"{expected_resized_file_id}{kitti_ext}"
    expected_resized_pascal_file_name = f"{expected_resized_file_id}{pascal_ext}"
    expected_resized_image_file_path = os.path.join(str(data_dir), expected_resized_image_file_name)
    expected_resized_kitti_file_path = os.path.join(str(data_dir), expected_resized_kitti_file_name)
    expected_resized_pascal_file_path = os.path.join(str(data_dir), expected_resized_pascal_file_name)

    # confirm that we can resize as expected for a KITTI annotated image
    resize.resize_image_label(
        file_id,
        image_ext,
        kitti_ext,
        data_dir,
        data_dir,
        resized_dir,
        resized_dir,
        new_width,
        new_height,
        "kitti",
    )
    resized_image_file_path = os.path.join(resized_dir, image_file_name)
    resized_image = cv2.imread(resized_image_file_path)
    expected_resized_image = cv2.imread(expected_resized_image_file_path)
    np.testing.assert_equal(resized_image,
                            expected_resized_image,
                            err_msg="Image not resized as expected")
    resized_kitti_file_path = os.path.join(resized_dir, kitti_file_name)
    assert text_files_equal(resized_kitti_file_path, expected_resized_kitti_file_path)

    # confirm that we can resize as expected for a PASCAL annotated image
    resize.resize_image_label(
        file_id,
        image_ext,
        pascal_ext,
        data_dir,
        data_dir,
        resized_dir,
        resized_dir,
        new_width,
        new_height,
        "pascal",
    )
    resized_image_file_path = os.path.join(resized_dir, image_file_name)
    resized_image = cv2.imread(resized_image_file_path)
    expected_resized_image = cv2.imread(expected_resized_image_file_path)
    np.testing.assert_equal(resized_image,
                            expected_resized_image,
                            err_msg="Image not resized as expected")
    resized_pascal_file_path = os.path.join(resized_dir, pascal_file_name)
    assert xml_equal(resized_pascal_file_path, expected_resized_pascal_file_path)