File size: 2,874 Bytes
4eecd03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""Tracker quality on REAL handheld video, which has no ground truth.

Two things are measured instead:
  disagreement -- two keyframes are tracked independently (separate chains). A scene point seen from keyframe A and the
                  same point seen from keyframe B must land on the same pixel in every frame; their distance is the
                  tracking error, with no labels. Reported as a share of the frame width (mean / p95).
  jitter       -- the frame-to-frame shake of a tracked point: distance from its 5-sample moving average
                  (a hand-held camera's real motion is smooth at 8 Hz; shake is the tracker's).

    python eval_tracking_real.py clip.mp4 [clip2.mov ...]
"""
from __future__ import annotations

import sys

import numpy as np

import video_demo as vd

GRID = [(x, y) for x in (0.25, 0.5, 0.75) for y in (0.25, 0.5, 0.75)]


def _apply(M, x, y):
    p = np.asarray(M).reshape(3, 3) @ [x, y, 1.0]
    return p[:2] / p[2]


def evaluate(path, ta=None, tb=None, track_fn=None):
    track_fn = track_fn or vd.track_keyframes
    dur = vd.duration_of(path)
    ta = 0.2 * dur if ta is None else ta
    tb = 0.8 * dur if tb is None else tb
    A, B = track_fn(path, [ta]), track_fn(path, [tb])
    byB = {round(e["t"], 3): e for e in B}
    # A -> B coordinates at B's own keyframe time
    eB = min(A, key=lambda e: abs(e["t"] - tb))
    if eB["lost"]:
        return {"clip": str(path), "error": "tracker lost the wall before the second keyframe"}
    dis, jit, lost = [], [], 0
    traj = {g: [] for g in GRID}
    for e in A:
        f = byB.get(round(e["t"], 3))
        if not f or e["lost"] or f["lost"]:
            lost += 1
            continue
        for g in GRID:
            pa = _apply(e["m"], *g)                             # keyframe-A point g, now
            q = _apply(eB["m"], *g)                             # the same point in keyframe B's frame
            pb = _apply(f["m"], *q)                             # ... carried to now by B's chain
            dis.append(float(np.hypot(*(pa - pb))))
            traj[g].append(pa)
    for g, pts in traj.items():
        p = np.array(pts)
        if len(p) < 7:
            continue
        k = np.ones(5) / 5
        sm = np.column_stack([np.convolve(p[:, d], k, "valid") for d in (0, 1)])
        jit += list(np.hypot(*(p[2:-2] - sm).T))
    return {"clip": str(path).split("/")[-1], "frames": len(A), "unusable": lost,
            "disagree_mean": round(float(np.mean(dis)), 4) if dis else None,
            "disagree_p95": round(float(np.percentile(dis, 95)), 4) if dis else None,
            "jitter_mean": round(float(np.mean(jit)), 5) if jit else None,
            "jitter_p95": round(float(np.percentile(jit, 95)), 5) if jit else None}


if __name__ == "__main__":
    for p in sys.argv[1:]:
        print(evaluate(p), flush=True)