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
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
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
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
assert file_suffix_in(Path("davor.JA"), ["ja", "ti"])
assert not file_suffix_in(Path("davor.JA"), ["ti"])
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",
]
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