import numpy as np
import json
import matplotlib.pyplot as plt

# 1. 读取2D map json
with open('semantic_map_2d.json') as f:
    result_list = json.load(f)

# 2. 自动确定画布长宽 (有mask_coords则比bbox更可靠)
h = max([max((p[0] for p in inst['mask_coords']), default=0) for inst in result_list]) + 1
w = max([max((p[1] for p in inst['mask_coords']), default=0) for inst in result_list]) + 1
semantic_map = np.zeros((h, w), dtype=int)

# 3. 遍历所有实例，按mask_coords涂色
for inst in result_list:
    cat_id = int(inst['category_id'])
    for y, x in inst['mask_coords']:
        semantic_map[y, x] = cat_id

plt.figure(figsize=(10, 10))
im = plt.imshow(semantic_map, cmap='tab20')
plt.colorbar(im, label='Category ID')
plt.title('Semantic Occupancy Map (reconstructed from json)')
plt.show()