Resizing dataset. This functions are not required for competitors but was developed to prepare the dataset for the competition.
fin_body_path = Path("../data/testing/fin_body_5/")

fin_body_jpeg_path = sorted(fin_body_path.glob("**/JPEGImages/*.jpg"))
fin_body_segmentation_classes_path = sorted(
    fin_body_path.glob("**/SegmentationClass/*.png")
)
fin_body_segmentation_objects_path = sorted(
    fin_body_path.glob("**/SegmentationObject/*.png")
)

assert fin_body_path.exists()
assert len(fin_body_jpeg_path) == 5
assert len(fin_body_segmentation_classes_path) == 5
assert len(fin_body_segmentation_objects_path) == 5

display_image[source]

display_image(img:Union[Image, Iterable[Image]], x_size=240)

Displays an image or images with smaller dimension equal to x_size, and the other dimension is calculated

imgs = [
    Image.open(fin_body_segmentation_classes_path[i])
    for i in range(len(fin_body_segmentation_classes_path))
]

display_image(imgs[0])
display_image(imgs)
class_test = Image.open(fin_body_segmentation_classes_path[0])

class_test = class_test.convert(
    "P", palette=Image.ADAPTIVE, colors=len(class_test.getcolors())
)

palette = _get_pallete(class_test)
assert len(palette) == 3
palette
[[250, 250, 55], [250, 50, 83], [0, 0, 0]]
instance_test = Image.open(fin_body_segmentation_objects_path[0])

instance_test = instance_test.convert(
    "P", palette=Image.ADAPTIVE, colors=len(instance_test.getcolors())
)

palette_instance = _get_pallete(instance_test)
assert len(palette_instance) >= len(palette)
palette_instance
[[128, 128, 0], [0, 128, 0], [128, 0, 0], [0, 0, 128], [0, 0, 0]]
display_image(_separate_img(instance_test, palette_instance))
for i in separate_masks(instance_test):
    display(i.resize((240, 180)))
resize_composite_multi(instance_test, (500, 500))
print(f"input shape: {instance_test.size}")
assert instance_test.size == (2664, 1274)

actual = calculate_image_size(instance_test, smaller_size=500)
expected = (1045, 500)
print(f"output shape {actual}")
assert actual == expected
input shape: (2664, 1274)
output shape (1045, 500)
assert file_suffix_in(Path("davor.JA"), ["ja", "ti"])
assert not file_suffix_in(Path("davor.JA"), ["ti"])

glob_suffixes[source]

glob_suffixes(root_path:Path, suffixes:Union[List[str], str])

Returns all suffixes located in the path and in the input variable suffixes

assert len(glob_suffixes(fin_body_path, ["jpg", "jpeg"])) == 5
assert len(glob_suffixes(fin_body_path, "png")) == 10
src_images, src_lbl_classes, src_lbl_instances = get_files_from_path(fin_body_path)
for p in [src_images, src_lbl_classes, src_lbl_instances]:
    assert len(p) == 5
    for f in p:
        assert f.exists()
        assert f.is_file()
# prazan dir i onda ga napravim
with TemporaryDirectory() as d:
    dst_path = Path(d)
    recreate_dst_dir(dst_path)
    assert dst_path.exists()
    assert dst_path.is_dir()
    dir_found = list(dst_path.glob("**/*"))
    assert len(dir_found) == 3
    for dst_subpath in dir_found:
        assert dst_subpath.is_dir()
        assert dst_subpath.name in [
            "JPEGImages",
            "SegmentationClass",
            "SegmentationObject",
        ]


# puni dir i onda ga izbrisem
with TemporaryDirectory() as d:
    dst_path = Path(d)
    (dst_path / "JPEGImages" / "random").mkdir(parents=True)

    recreate_dst_dir(dst_path)
    assert dst_path.exists()
    assert dst_path.is_dir()
    dir_found = list(dst_path.glob("**/*"))
    assert len(dir_found) == 3, f"{dir_found}"
    for dst_subpath in dir_found:
        assert dst_subpath.is_dir()
        assert dst_subpath.name in [
            "JPEGImages",
            "SegmentationClass",
            "SegmentationObject",
        ]

resize_dataset[source]

resize_dataset(src_path:"input directory containing images and annotations", dst_path:"output directory, will be deleted if not empy", dst_image_size:"min height or width"=1000, verbose:""=True)

Resizes the entire dataset (images and labels) and saves it

with TemporaryDirectory() as dst_path:
    dst_path = Path(dst_path)

    src_path = fin_body_path

    dst_image_size = 500

    resize_dataset(src_path, dst_path, dst_image_size)

    assert len(glob_suffixes(dst_path, ["jpg", "jpeg"])) == 5
    assert len(glob_suffixes(dst_path, "png")) == 10
Resizing images and annotation masks in: /tf/airt/data/testing/fin_body_5
 - [1/5] 0706_Dino_070618_1_2_0061.jpg...
 - [2/5] 140810_33_1_0224.jpg...
 - [3/5] 150828_98_1_0036.jpg...
 - [4/5] 170720_16_1_0069.jpg...
 - [5/5] 190809_35_1_0122.jpg...