-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_masks_script.py
More file actions
68 lines (56 loc) · 2.02 KB
/
Copy pathcreate_masks_script.py
File metadata and controls
68 lines (56 loc) · 2.02 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
import json
import numpy as np
import cv2
from shapely import wkt
from shapely.geometry import Polygon, MultiPolygon
from pathlib import Path
from PIL import Image
from glob import glob
from tqdm import tqdm
post_json_paths = glob("test/labels/*_post_disaster.json")
output_dir = Path("test/masks")
output_dir.mkdir(parents=True, exist_ok=True)
# Class mapping for xView2 damage subtypes to integer labels
DAMAGE_CLASSES = {
"no-damage": 1,
"minor-damage": 2,
"major-damage": 3,
"destroyed": 4,
}
def create_damage_mask_from_post_json(post_json_path, image_size=(1024, 1024)):
"""
Generates a 5-class damage classification mask from a post-disaster GeoJSON.
0 = background
1 = no-damage
2 = minor-damage
3 = major-damage
4 = destroyed
"""
with open(post_json_path, 'r') as f:
data = json.load(f)
mask = np.zeros(image_size, dtype=np.uint8)
for feature in data["features"]["xy"]:
props = feature["properties"]
subtype = props.get("subtype", "no-damage") # Default to no-damage if missing
class_id = DAMAGE_CLASSES.get(subtype, 1)
poly = wkt.loads(feature["wkt"])
if isinstance(poly, Polygon):
polygons = [poly]
elif isinstance(poly, MultiPolygon):
polygons = list(poly.geoms)
else:
continue
for p in polygons:
coords = np.array(p.exterior.coords).round().astype(np.int32)
cv2.fillPoly(mask, [coords], class_id)
return mask
# Example usage
# post_json_path = Path("test/labels/guatemala-volcano_00000003_post_disaster.json")
# mask = create_damage_mask_from_post_json(post_json_path)
# Image.fromarray(mask).save("guatemala-volcano_00000003_mask.png")
for post_json_path in tqdm(post_json_paths):
post_json_path = Path(post_json_path)
mask = create_damage_mask_from_post_json(post_json_path)
base_name = post_json_path.stem.replace("_post_disaster", "") + "_mask.png"
mask_path = output_dir / base_name
Image.fromarray(mask).save(mask_path)