r4d-bench-qa / scripts /_pieces.py
LiYacheng's picture
Fill unannotated_visible gaps: every query/frame has GT masks (part 24)
aa7e82c verified
Raw History Blame Contribute Delete
1.75 kB
"""Print per-piece centroid/area for (scene, frame, category) in COCO order."""
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 pieces(scene, fids, cats=None):
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"]
cname = {c["id"]: c["name"] for c in coco["categories"]}
for im in coco["images"]:
fid = int(frame_number_from_coco_image(im))
if fid not in fids:
continue
H, W = im["height"], im["width"]
cnt = {}
for a in coco["annotations"]:
if a["image_id"] != im["id"]:
continue
c = cname[a["category_id"]]
k = cnt.get(c, 0)
cnt[c] = k + 1
if cats and c not in cats:
continue
m = _segmentation_to_mask(a["segmentation"], H, W) > 0
ys, xs = np.nonzero(m)
print(f"f{fid} {c!r}#{k} ann={a['id']} -> {amap.get(str(a['id']))} "
f"cx={xs.mean():.0f} cy={ys.mean():.0f} area={len(xs)} bbox={[int(v) for v in a['bbox']]}")
if __name__ == "__main__":
scene = sys.argv[1]
fids = [int(x) for x in sys.argv[2].split(",")]
cats = sys.argv[3].split("|") if len(sys.argv) > 3 else None
pieces(scene, fids, cats)