-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
126 lines (99 loc) · 3.77 KB
/
Copy pathtest.py
File metadata and controls
126 lines (99 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# # import os
# # import glob
# # import numpy as np
# # from PIL import Image
# # # VOC Colormap
# # VOC_COLORMAP = [
# # (0, 0, 0), (128, 0, 0), (0, 128, 0), (128, 128, 0),
# # (0, 0, 128), (128, 0, 128), (0, 128, 128), (128, 128, 128),
# # (64, 0, 0), (192, 0, 0), (64, 128, 0), (192, 128, 0),
# # (64, 0, 128), (192, 0, 128), (64, 128, 128), (192, 128, 128),
# # (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0),
# # (0, 64, 128)
# # ]
# # def mask_to_class(mask):
# # mask = np.array(mask)
# # label = np.zeros(mask.shape[:2], dtype=np.int64)
# # for idx, color in enumerate(VOC_COLORMAP):
# # matches = np.all(mask == color, axis=-1)
# # label[matches] = idx
# # return label
# # # Path to extracted dataset
# # dataset_path = "./data/pascal-voc-2012-dataset/VOC2012_train_val/VOC2012_train_val"
# # mask_dir = os.path.join(dataset_path, "SegmentationClass")
# # mask_paths = glob.glob(os.path.join(mask_dir, "*.png"))
# # print(f"Found {len(mask_paths)} mask files.")
# # # Check only first 50 masks to avoid freezing
# # check_count = min(50, len(mask_paths))
# # invalid_masks = []
# # max_label = -1
# # for i, p in enumerate(mask_paths[:check_count]):
# # mask_class = mask_to_class(Image.open(p).convert("RGB"))
# # max_val = mask_class.max()
# # if max_val >= len(VOC_COLORMAP):
# # invalid_masks.append(p)
# # if max_val > max_label:
# # max_label = max_val
# # if (i + 1) % 10 == 0:
# # print(f"Checked {i + 1}/{check_count} masks...")
# # print("Max class label in first", check_count, "masks:", max_label)
# # if invalid_masks:
# # print("Masks with invalid labels:", invalid_masks)
# # else:
# # print("All checked masks are valid.")
# from pathlib import Path
# from PIL import Image
# import math
# # -------------------------------------------------
# # Config
# # -------------------------------------------------
# RESULT_DIR = Path(__file__).resolve().parent / "test/test_results"
# OUTPUT_PATH = RESULT_DIR / "all_results.png"
# IMAGE_EXTS = (".png", ".jpg", ".jpeg")
# # Grid layout (auto if None)
# N_COLS = None # e.g. 3, 4, 5 or None for auto
# RESIZE_TO = None # (width, height) or None to use first image size
# # -------------------------------------------------
# # Load images
# # -------------------------------------------------
# image_paths = sorted(
# [p for p in RESULT_DIR.iterdir() if p.suffix.lower() in IMAGE_EXTS]
# )
# if not image_paths:
# raise RuntimeError(f"No images found in {RESULT_DIR}")
# images = [Image.open(p).convert("RGB") for p in image_paths]
# # -------------------------------------------------
# # Resize images (optional)
# # -------------------------------------------------
# if RESIZE_TO is None:
# w, h = images[0].size
# else:
# w, h = RESIZE_TO
# images = [img.resize((w, h), Image.BILINEAR) for img in images]
# # -------------------------------------------------
# # Grid size
# # -------------------------------------------------
# n_images = len(images)
# if N_COLS is None:
# N_COLS = int(math.sqrt(n_images))
# N_ROWS = math.ceil(n_images / N_COLS)
# # -------------------------------------------------
# # Create canvas
# # -------------------------------------------------
# collage = Image.new(
# "RGB",
# (N_COLS * w, N_ROWS * h),
# color=(0, 0, 0)
# )
# # -------------------------------------------------
# # Paste images
# # -------------------------------------------------
# for idx, img in enumerate(images):
# row = idx // N_COLS
# col = idx % N_COLS
# collage.paste(img, (col * w, row * h))
# # -------------------------------------------------
# # Save
# # -------------------------------------------------
# collage.save(OUTPUT_PATH)
# print(f"Saved collage to: {OUTPUT_PATH}")