"""Track centroid relative to a reference track (camera-motion compensated), per frame.""" import json import sys from pathlib import Path import numpy as np ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from scripts.generate_instance_masks import _segmentation_to_mask from scripts.predictions_ground_truth import SCENES_CONFIG, frame_number_from_coco_image def masks(scene): cfg = next(c for c in SCENES_CONFIG if c["folder"] == scene) sd = ROOT / "data/scenes" / scene coco = json.loads((sd / cfg["annot_file"]).read_text(encoding="utf-8")) amap = json.loads((sd / "annotation_track_map.json").read_text(encoding="utf-8"))["assignments"] out = {} for im in coco["images"]: fid = int(frame_number_from_coco_image(im)) H, W = im["height"], im["width"] d = {} for a in coco["annotations"]: if a["image_id"] != im["id"]: continue t = amap.get(str(a["id"])) if t is None: continue m = _segmentation_to_mask(a["segmentation"], H, W) > 0 d[t] = d.get(t, np.zeros((H, W), bool)) | m out[fid] = d return out def main(): scene, tgt, ref = sys.argv[1], sys.argv[2], sys.argv[3] ms = masks(scene) for fid in sorted(ms): d = ms[fid] if tgt not in d or ref not in d: print(f"f{fid}: missing {tgt if tgt not in d else ref}") continue ys, xs = np.nonzero(d[ref]) rx, ry = xs.mean(), ys.mean() sw = xs.max() - xs.min() ys2, xs2 = np.nonzero(d[tgt]) print(f"f{fid:5d} rel=({(xs2.mean()-rx)/sw:+.3f},{(ys2.mean()-ry)/sw:+.3f}) " f"tip_y={(ys2.max()-ry)/sw:+.3f} area_ratio={len(xs2)/len(xs):.3f}") if __name__ == "__main__": main()