File size: 12,364 Bytes
a781d1f | 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | import argparse
import json
from collections import defaultdict
from pathlib import Path
from typing import Optional
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from scipy.ndimage import label as nd_label
from shapely.geometry import Point, Polygon
def format2(value):
return f"{float(value):.2f}"
def normalize_label(label: str) -> str:
"""Normalize labels to lowercase snake_case."""
return label.strip().lower().replace(" ", "_")
def parse_args():
parser = argparse.ArgumentParser(
description="Convert InteriorGS annotations into 2D semantic maps."
)
parser.add_argument(
"--input-root",
type=Path,
help="Root directory of the InteriorGS dataset.",
)
parser.add_argument(
"--output-root",
type=Path,
help="Directory to store generated semantic maps.",
)
parser.add_argument(
"--overwrite",
action="store_true",
help="Overwrite existing semantic map files.",
)
parser.add_argument(
"--max-scenes",
type=int,
default=None,
help="Process at most this many scene folders (useful for quick tests).",
)
return parser.parse_args()
def build_semantic_maps(
input_root: Path, output_root: Path, overwrite: bool, max_scenes: Optional[int]
) -> None:
if not input_root.exists():
raise FileNotFoundError(f"Input root does not exist: {input_root}")
output_root.mkdir(parents=True, exist_ok=True)
scene_dirs = sorted(p for p in input_root.iterdir() if p.is_dir())
if max_scenes is not None:
scene_dirs = scene_dirs[:max_scenes]
if not scene_dirs:
print(f"[WARN] No scene directories found under {input_root}")
return
for scene_dir in scene_dirs:
scene_name = scene_dir.name
out_json = output_root / f"2D_Semantic_Map_{scene_name}_Complete.json"
out_png = output_root / f"2D_Semantic_Map_{scene_name}_Complete.png"
if out_json.exists() and not overwrite:
print(f"[SKIP] {out_json} already exists. Use --overwrite to regenerate.")
continue
occ_json_path = scene_dir / "occupancy.json"
labels_json_path = scene_dir / "labels.json"
occ_png_path = scene_dir / "occupancy.png"
if not (occ_json_path.is_file() and labels_json_path.is_file() and occ_png_path.is_file()):
print(f"[MISSING] {scene_name} lacks occupancy.json / occupancy.png / labels.json.")
continue
with occ_json_path.open("r", encoding="utf-8") as f:
meta = json.load(f)
scale = meta["scale"]
x_min, y_min = meta["min"][:2]
occ_img = Image.open(occ_png_path).convert("L")
occupancy = np.array(occ_img)
h, w = occupancy.shape
pixels, counts = np.unique(occupancy.reshape(-1), return_counts=True)
candidate_walls = [int(p) for p in pixels if 0 < p < 250]
if candidate_walls:
wall_value = int(
candidate_walls[
np.argmax([counts[np.where(pixels == v)[0][0]] for v in candidate_walls])
]
)
else:
wall_value = int(pixels[0])
print(f"[{scene_name}] wall pixel value = {wall_value}")
with labels_json_path.open("r", encoding="utf-8") as f:
labels = json.load(f)
predefined_classes = [
"door",
"window",
"chair",
"table",
"sofa",
"bed",
"wardrobe",
"plant",
"floor",
"wall",
"ceiling",
]
label2id = {cls: idx + 1 for idx, cls in enumerate(predefined_classes)}
cur_max_id = len(label2id) + 1
for obj in labels:
lbl = obj["label"]
if lbl not in label2id:
label2id[lbl] = cur_max_id
cur_max_id += 1
visual_map = np.zeros((h, w), dtype=np.int32)
result_list = []
item_counters = defaultdict(int)
for obj in labels:
if "bounding_box" not in obj:
continue
label = obj["label"]
cat_id = label2id[label]
poly3d = obj["bounding_box"]
z_values = [v["z"] for v in poly3d]
min_z = min(z_values)
max_z = max(z_values)
height = max_z - min_z
poly2d = [[v["x"], v["y"]] for v in poly3d[:4]]
poly = Polygon(poly2d)
xys = np.array(poly2d)
min_x_pixel = int(np.floor((np.min(xys[:, 0]) - x_min) / scale))
max_x_pixel = int(np.floor((np.max(xys[:, 0]) - x_min) / scale))
min_y_pixel = int(np.floor((np.min(xys[:, 1]) - y_min) / scale))
max_y_pixel = int(np.floor((np.max(xys[:, 1]) - y_min) / scale))
min_x_pixel = np.clip(min_x_pixel, 0, w - 1)
max_x_pixel = np.clip(max_x_pixel, 0, w - 1)
min_y_pixel = np.clip(min_y_pixel, 0, h - 1)
max_y_pixel = np.clip(max_y_pixel, 0, h - 1)
mask = np.zeros((h, w), dtype=bool)
for j in range(min_x_pixel, max_x_pixel + 1):
for i in range(min_y_pixel, max_y_pixel + 1):
i_flip = h - 1 - i
j_flip = w - 1 - j
cx = x_min + (j + 0.5) * scale
cy = y_min + (i + 0.5) * scale
if poly.covers(Point(cx, cy)):
mask[i_flip, j_flip] = True
visual_map[i_flip, j_flip] = cat_id
ys, xs = np.where(mask)
if xs.size == 0:
continue
xmin_pix, xmax_pix = xs.min(), xs.max()
ymin_pix, ymax_pix = ys.min(), ys.max()
x_left = x_min + xmin_pix * scale
x_right = x_min + (xmax_pix + 1) * scale
y_bottom = y_min + ymin_pix * scale
y_top = y_min + (ymax_pix + 1) * scale
w_box = x_right - x_left
h_box = y_top - y_bottom
bbox_m = [format2(x_left), format2(y_bottom), format2(x_right), format2(y_top)]
bbox_xywh_m = [format2(x_left), format2(y_bottom), format2(w_box), format2(h_box)]
mask_coords_m = [
[format2(y_min + (y + 0.5) * scale), format2(x_min + (x + 0.5) * scale)]
for y, x in zip(ys, xs)
]
item_counters[label] += 1
item_id = f"{normalize_label(label)}_{item_counters[label]}"
result_list.append(
{
"category_id": int(cat_id),
"category_label": label,
"instance_id": obj.get("ins_id", ""),
"item_id": item_id,
"bbox_m": bbox_m,
"bbox_xywh_m": bbox_xywh_m,
"area": int(mask.sum()),
"height_m": format2(height),
"min_z_m": format2(min_z),
"max_z_m": format2(max_z),
"mask_coords_m": mask_coords_m,
}
)
wall_cat_id = label2id["wall"]
wall_mask = occupancy == wall_value
wall_mask_flip = np.flipud(wall_mask)
visual_map[wall_mask_flip] = wall_cat_id
wall_label_mask, wall_count = nd_label(wall_mask_flip, structure=np.ones((3, 3), dtype=np.int32))
for idx in range(1, wall_count + 1):
block_mask = wall_label_mask == idx
ys, xs = np.where(block_mask)
if xs.size == 0 or ys.size == 0:
continue
xmin_pix, xmax_pix = xs.min(), xs.max()
ymin_pix, ymax_pix = ys.min(), ys.max()
x_left = x_min + xmin_pix * scale
x_right = x_min + (xmax_pix + 1) * scale
y_bottom = y_min + ymin_pix * scale
y_top = y_min + (ymax_pix + 1) * scale
w_box = x_right - x_left
h_box = y_top - y_bottom
bbox_m = [format2(x_left), format2(y_bottom), format2(x_right), format2(y_top)]
bbox_xywh_m = [format2(x_left), format2(y_bottom), format2(w_box), format2(h_box)]
mask_coords_m = [
[format2(y_min + (y + 0.5) * scale), format2(x_min + (x + 0.5) * scale)]
for y, x in zip(ys, xs)
]
label = "wall"
item_counters[label] += 1
item_id = f"{normalize_label(label)}_{item_counters[label]}"
result_list.append(
{
"category_id": int(wall_cat_id),
"category_label": label,
"instance_id": f"wall_{idx}",
"item_id": item_id,
"bbox_m": bbox_m,
"bbox_xywh_m": bbox_xywh_m,
"area": int(block_mask.sum()),
"height_m": format2(3.0),
"min_z_m": format2(0.0),
"max_z_m": format2(3.0),
"mask_coords_m": mask_coords_m,
}
)
unable_mask = occupancy == 0
unable_mask_flip = np.flipud(unable_mask)
labeled, num = nd_label(unable_mask_flip, structure=np.ones((3, 3)))
print(f"[{scene_name}] detected {num} unable-area clusters")
for idx in range(1, num + 1):
block = labeled == idx
area = block.sum()
if area < 5:
continue
ys, xs = np.where(block)
xmin_pix, xmax_pix = xs.min(), xs.max()
ymin_pix, ymax_pix = ys.min(), ys.max()
x_left = x_min + xmin_pix * scale
x_right = x_min + (xmax_pix + 1) * scale
y_bottom = y_min + ymin_pix * scale
y_top = y_min + (ymax_pix + 1) * scale
w_box = x_right - x_left
h_box = y_top - y_bottom
mask_coords_m = [
[format2(y_min + (y + 0.5) * scale), format2(x_min + (x + 0.5) * scale)]
for y, x in zip(ys, xs)
]
label = "Unable Area"
item_counters[label] += 1
item_id = f"{normalize_label(label)}_{item_counters[label]}"
result_list.append(
{
"category_id": -1,
"category_label": label,
"instance_id": f"unable_area_{idx}",
"item_id": item_id,
"bbox_m": [
format2(x_left),
format2(y_bottom),
format2(x_right),
format2(y_top),
],
"bbox_xywh_m": [
format2(x_left),
format2(y_bottom),
format2(w_box),
format2(h_box),
],
"area": int(area),
"height_m": format2(0.0),
"min_z_m": format2(0.0),
"max_z_m": format2(0.0),
"mask_coords_m": mask_coords_m,
}
)
with out_json.open("w", encoding="utf-8") as f:
json.dump(result_list, f, indent=2)
print(f"[WRITE] {out_json}")
extent = [float(x_min), float(x_min) + w * scale, float(y_min), float(y_min) + h * scale]
plt.figure(figsize=(12, 12))
bg_color = (31 / 255, 119 / 255, 180 / 255, 1.0) # deep blue
bg_img = np.zeros((h, w, 4), dtype=float)
bg_img[:, :] = bg_color
plt.imshow(bg_img, origin="lower", extent=extent)
overlay = np.zeros((h, w, 4), dtype=float)
overlay[unable_mask_flip] = [1.0, 128 / 255, 128 / 255, 1.0] # #FF8080
overlay[wall_mask_flip] = [158 / 255, 218 / 255, 229 / 255, 0.8] # light blue
plt.imshow(overlay, origin="lower", extent=extent)
plt.axis("off")
plt.savefig(out_png, bbox_inches="tight", dpi=300)
plt.close()
print(f"[WRITE] {out_png}")
print("Semantic map batch generation finished.")
def main():
args = parse_args()
build_semantic_maps(
args.input_root.expanduser(),
args.output_root.expanduser(),
args.overwrite,
args.max_scenes,
)
if __name__ == "__main__":
main() |