# Summarise one sidewalk clip's tracks: who is in it, and the longest-tracked people and objects.
# usage: python sidewalk_tracks.py roaddata-sidewalk-sample/clips/SW2878_tracks.json
import json, sys
from collections import Counter
t = json.load(open(sys.argv[1]))
first, last, cls = {}, {}, {}
for frame in t["frames"]:
    for track_id, class_index, *_box in frame["boxes"]:     # box: x0, y0, x1, y1, confidence (normalised 0-1)
        first.setdefault(track_id, frame["t"]); last[track_id] = frame["t"]; cls[track_id] = t["classes"][class_index]
print(t["clip"], f'| tracks {t["fps"]:.0f} times a second |', ", ".join(f"{k}: {n}" for k, n in Counter(cls.values()).most_common()))
for k in sorted(cls, key=lambda k: last[k] - first[k], reverse=True)[:3]:
    print(f"  track {k} ({cls[k]}) in view {first[k]:.1f} to {last[k]:.1f} s")
