"""List polygons inside each annotation of multi-track categories (all scenes).""" import json import sys from collections import defaultdict from pathlib import Path import numpy as np ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from scripts.predictions_ground_truth import SCENES_CONFIG, frame_number_from_coco_image def poly_area(p): x, y = np.array(p[0::2]), np.array(p[1::2]) return 0.5 * abs(np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1))) def main(): only = sys.argv[1:] or None for cfg in SCENES_CONFIG: scene = cfg["folder"] if only and scene not in only: continue sd = ROOT / "data/scenes" / scene coco = json.loads((sd / cfg["annot_file"]).read_text(encoding="utf-8")) tracks = json.loads((sd / cfg["track_file"]).read_text(encoding="utf-8"))["tracks"] cnt = defaultdict(int) for t in tracks: cnt[t["category"]] += 1 multi = {c for c, n in cnt.items() if n > 1} cname = {c["id"]: c["name"] for c in coco["categories"]} fid_of = {im["id"]: int(frame_number_from_coco_image(im)) for im in coco["images"]} print(f"#### {scene} multi={sorted(multi)}") for a in sorted(coco["annotations"], key=lambda a: (fid_of[a["image_id"]], a["id"])): c = cname[a["category_id"]] seg = a["segmentation"] if c not in multi or not isinstance(seg, list) or len(seg) < 2: continue parts = [] for p in seg: xs, ys = p[0::2], p[1::2] parts.append(f"[x{min(xs):.0f}-{max(xs):.0f} y{min(ys):.0f}-{max(ys):.0f} A{poly_area(p):.0f}]") print(f" f{fid_of[a['image_id']]} {c!r} ann={a['id']} npoly={len(seg)} " + " ".join(parts)) if __name__ == "__main__": main()