r4d-bench-qa / scripts /_audit_static_targets.py
LiYacheng's picture
Fill unannotated_visible gaps: every query/frame has GT masks (part 24)
aa7e82c verified
Raw History Blame Contribute Delete
2.74 kB
"""For every motionless/static query, report max centroid displacement of each target."""
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._rel_motion import masks # noqa: E402
from scripts.predictions_ground_truth import SCENES_CONFIG
gt = json.loads((ROOT / "scripts/new_predictions_ground_truth_all_queries_with_paths.json").read_text(encoding="utf-8"))
qs = json.loads((ROOT / "evaluation/R4D-Bench_queries.json").read_text(encoding="utf-8"))
STATIC_KEYS = (
"never move",
"motionless",
"stationary",
"completely still",
"do not change position",
"完全静止",
"从未移动",
"始终保持静止",
"不改变位置",
"physically motionless",
"physically stationary",
)
def scene_of(qid: str) -> str:
for c in SCENES_CONFIG:
for pref in (c["folder"], c["folder"].replace("_", "-")):
if qid.startswith(pref + "_"):
return c["folder"]
raise KeyError(qid)
def centroid(m: np.ndarray):
ys, xs = np.nonzero(m)
if len(xs) == 0:
return None
return float(xs.mean()), float(ys.mean()), int(len(xs))
def main():
static_qids = []
for q in qs:
blob = f"{q.get('text_en','')} {q.get('text_zh','')}".lower()
if any(k.lower() in blob for k in STATIC_KEYS):
static_qids.append(q["query_id"])
cache = {}
for qid in static_qids:
it = next(x for x in gt if x["query_id"] == qid)
targets = it["ground_truth"]["target_tracks"]
scene = scene_of(qid)
if scene not in cache:
cache[scene] = masks(scene)
m = cache[scene]
fids = sorted(m.keys())
print(f"\n==== {qid} targets={targets}")
for tid in targets:
cents = []
for fid in fids:
if tid not in m[fid]:
continue
c = centroid(m[fid][tid])
if c:
cents.append((fid, c[0], c[1], c[2]))
if not cents:
print(f" {tid}: NO MASKS")
continue
x0, y0 = cents[0][1], cents[0][2]
maxd = 0.0
maxfid = cents[0][0]
for fid, x, y, a in cents:
d = ((x - x0) ** 2 + (y - y0) ** 2) ** 0.5
if d > maxd:
maxd, maxfid = d, fid
areas = [c[3] for c in cents]
flag = " *** MOVE" if maxd >= 5.0 else ""
print(
f" {tid}: max_disp={maxd:.1f}px @f{maxfid} "
f"area[{min(areas)}..{max(areas)}] n={len(cents)}{flag}"
)
if __name__ == "__main__":
main()