Datasets:
File size: 1,583 Bytes
ada18d0 | 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 | 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()
|