import json import math import os import random import sys import matplotlib.pyplot as plt from matplotlib.patches import Polygon from PIL import Image multiannotation_path = "instance_segmentation/multiannotator_15imgs.json" img_folder = "images" if __name__ == "__main__": with open(multiannotation_path) as f: multiannotations = json.load(f) # Optional image name as argument, otherwise pick a random image name = sys.argv[1] if len(sys.argv) > 1 else random.choice(sorted(multiannotations)) annotators = multiannotations[name] print(f"{name}: {len(annotators)} annotators, " f"{sum(len(a['polygons']) for a in annotators)} polygons") img = Image.open(os.path.join(img_folder, name)) cols = min(4, len(annotators)) rows = math.ceil(len(annotators) / cols) fig, axes = plt.subplots(rows, cols, figsize=(5 * cols, 3 * rows), squeeze=False) for ax in axes.flat: ax.axis("off") cmap = plt.get_cmap("tab20") for ax, annotator in zip(axes.flat, annotators): ax.imshow(img) for i, poly in enumerate(annotator["polygons"]): pts = [(p["x"], p["y"]) for p in poly["polygon_pts"]] color = cmap(i % cmap.N) ax.add_patch(Polygon(pts, closed=True, alpha=0.4, facecolor=color, edgecolor=color, linewidth=1.5)) ax.set_title(f"{annotator['user_id']} ({len(annotator['polygons'])} polygons)", fontsize=9) fig.suptitle(name) fig.tight_layout() fig.savefig("example_image_annotations.png", dpi=100) plt.show()