"""Link per-frame blobs into tracks, label them, and fill short gaps with marked estimates. Usage: python -I link_tracks.py [--maxgap 8] Track labels: 'own-airframe' (static large near-field blobs), 'plane', 'orb', 'other'. Each track point: frame, t, x, y, area, heat, circ, ellipse, est (true if filled in).""" import sys, json, math import numpy as np src, out = sys.argv[1], sys.argv[2] MAXGAP = int(sys.argv[sys.argv.index("--maxgap") + 1]) if "--maxgap" in sys.argv else 8 R = [json.loads(l) for l in open(src)] tracks = []; active = [] for r in R: blobs = [b for b in r["blobs"] if b["area"] >= 6] used = set(); pairs = [] for ti, tr in enumerate(active): last = tr["pts"][-1] gap = r["i"] - last["frame"] # predicted position from last two points if len(tr["pts"]) >= 2: p2 = tr["pts"][-2]; dt = max(last["frame"] - p2["frame"], 1) vx, vy = (last["x"] - p2["x"]) / dt, (last["y"] - p2["y"]) / dt else: vx = vy = 0 px, py = last["x"] + vx * gap, last["y"] + vy * gap for bi, b in enumerate(blobs): d = math.hypot(b["cx"] - px, b["cy"] - py) size = math.sqrt(max(b["area"], last["area"])) lim = 25 + 1.5 * size + 6 * gap ar = max(b["area"], last["area"]) / max(min(b["area"], last["area"]), 1) if d < lim and ar < 6: pairs.append((d / lim + 0.1 * math.log(ar), ti, bi)) pairs.sort(); taken_t = set() for cost, ti, bi in pairs: if ti in taken_t or bi in used: continue taken_t.add(ti); used.add(bi); b = blobs[bi] active[ti]["pts"].append({"frame": r["i"], "t": r["t"], "x": b["cx"], "y": b["cy"], "area": b["area"], "heat": b["heat_mean"], "heat_max": b["heat_max"], "hue": b.get("hue_mean"), "circ": b["circ"], "ellipse": b["ellipse"], "bbox": b["bbox"], "est": False}) for bi, b in enumerate(blobs): if bi not in used: active.append({"pts": [{"frame": r["i"], "t": r["t"], "x": b["cx"], "y": b["cy"], "area": b["area"], "heat": b["heat_mean"], "heat_max": b["heat_max"], "hue": b.get("hue_mean"), "circ": b["circ"], "ellipse": b["ellipse"], "bbox": b["bbox"], "est": False}]}) still = [] for tr in active: if r["i"] - tr["pts"][-1]["frame"] > MAXGAP: tracks.append(tr) else: still.append(tr) active = still tracks += active FPS = (R[-1]["i"]) / R[-1]["t"] if R[-1]["t"] else 24.0 out_tracks = [] for k, tr in enumerate(tracks): p = tr["pts"] if len(p) < 4: continue xs = np.array([q["x"] for q in p]); ys = np.array([q["y"] for q in p]) areas = np.array([q["area"] for q in p]); circ = np.median([q["circ"] for q in p]) motion = float(np.hypot(np.diff(xs), np.diff(ys)).mean()) if len(p) > 1 else 0 if np.median(areas) > 15000 and motion < 3: label = "own-airframe" elif circ >= 0.55: label = "orb" elif circ < 0.5 and np.median(areas) > 60: label = "plane" else: label = "other" # fill gaps with linear-in-time estimates (marked est), never beyond MAXGAP filled = [] for a, b in zip(p, p[1:]): filled.append(a) g = b["frame"] - a["frame"] for s in range(1, g): f = s / g filled.append({"frame": a["frame"] + s, "t": round((a["frame"] + s) / FPS, 4), "x": round(a["x"] + (b["x"] - a["x"]) * f, 2), "y": round(a["y"] + (b["y"] - a["y"]) * f, 2), "area": round(a["area"] + (b["area"] - a["area"]) * f), "heat": None, "circ": None, "ellipse": None, "est": True, "est_err_px": round(0.5 * math.hypot(b["x"] - a["x"], b["y"] - a["y"]) * min(f, 1 - f) * 2 + 1, 2)}) filled.append(p[-1]) out_tracks.append({"id": f"T{k:04d}", "label": label, "first": p[0]["frame"], "last": p[-1]["frame"], "n_measured": len(p), "n_estimated": len(filled) - len(p), "median_area": float(np.median(areas)), "median_circ": float(circ), "pts": filled}) out_tracks.sort(key=lambda t: t["first"]) json.dump({"source": src, "fps": FPS, "maxgap": MAXGAP, "tracks": out_tracks}, open(out, "w")) from collections import Counter print(len(out_tracks), "tracks", Counter(t["label"] for t in out_tracks)) for t in out_tracks: if t["n_measured"] >= 24: print(t["id"], t["label"], f'{t["first"]/FPS:6.2f}-{t["last"]/FPS:6.2f}s', "n", t["n_measured"], "est", t["n_estimated"], "area", round(t["median_area"]), "circ", round(t["median_circ"], 2))