videoseam.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. # videoseam.py <frames.txt>... - loop seam statistics of grabbed videotex frame numbers ("<seconds> <frame>" lines from
  3. # grab_frames.py / grab_ours.py, the kit's test video counts 0..N-1 at 30 fps). Frame numbers are unwrapped at every
  4. # loop, fitted against time, and each seam is compared with the rest: the frames stepped across it, how long the
  5. # frame before it stayed up, and how far the video fell behind (residual after the seam minus before, in frames).
  6. import statistics, sys
  7. def runs(path):
  8. r = [(float(t), int(f)) for t, f in (l.split() for l in open(path))]
  9. out = []
  10. for t, f in r:
  11. if f < 0:
  12. continue
  13. if not out or out[-1][1] != f:
  14. out.append([t, f, t])
  15. else:
  16. out[-1][2] = t
  17. return out
  18. for path in sys.argv[1:]:
  19. seg = runs(path)
  20. period = max(f for _, f, _ in seg) + 1
  21. unwrapped, base, seams = [], 0, []
  22. for i, (t, f, _) in enumerate(seg):
  23. if i and f < seg[i - 1][1] - period // 2:
  24. base += period
  25. seams.append(i)
  26. unwrapped.append((t, f + base))
  27. ts = [t for t, _ in unwrapped]
  28. fs = [f for _, f in unwrapped]
  29. mt, mf = statistics.mean(ts), statistics.mean(fs)
  30. slope = sum((t - mt) * (f - mf) for t, f in unwrapped) / sum((t - mt) ** 2 for t in ts)
  31. res = [f - (mf + slope * (t - mt)) for t, f in unwrapped]
  32. steps = [fs[i] - fs[i - 1] for i in range(1, len(fs))]
  33. holds = [seg[i][0] - seg[i - 1][0] for i in range(1, len(seg))]
  34. seam_set = set(seams)
  35. normal_steps = [s for i, s in enumerate(steps, 1) if i not in seam_set]
  36. normal_holds = [h for i, h in enumerate(holds, 1) if i not in seam_set]
  37. print('%s: %d updates, %.3f video frames/s, %d seams, loop every %.3f s' % (
  38. path.split('/')[-1], len(seg), slope, len(seams), period / slope))
  39. print(' away from seams: step %s, hold median %.0f ms max %.0f ms' % (
  40. sorted(set(normal_steps)), statistics.median(normal_holds) * 1000, max(normal_holds) * 1000))
  41. for i in seams:
  42. before = statistics.mean(res[max(0, i - 6):i])
  43. after = statistics.mean(res[i:i + 6])
  44. print(' seam at %7.3f s: %3d -> %-3d step %d, held %3.0f ms, lost %+.2f frames' % (
  45. seg[i][0], seg[i - 1][1], seg[i][1], steps[i - 1], holds[i - 1] * 1000, before - after))