summaryrefslogtreecommitdiff
path: root/design/motion/motion.py
diff options
context:
space:
mode:
Diffstat (limited to 'design/motion/motion.py')
-rw-r--r--design/motion/motion.py37
1 files changed, 34 insertions, 3 deletions
diff --git a/design/motion/motion.py b/design/motion/motion.py
index d192295..66571be 100644
--- a/design/motion/motion.py
+++ b/design/motion/motion.py
@@ -189,6 +189,22 @@ def report(video, names, counts, soft, sizes, fps):
WORDS = ("head", "caption", "clock", "minimap") # regions that are not panels
+# One-frame changes that are right, by name: each a test of (region, frame, pixels moved there) that says
+# whether the POP at that frame is one of its kind. They are counted apart, never silently dropped.
+# `keyframe`: the encoder's, not the scene's: a few pixels of a still region re-quantized at an I frame.
+KEYFRAMES = set() # the video's I frames (cmd_chart reads them)
+EXEMPT = {"keyframe": lambda name, t, moved: t in KEYFRAMES and moved < 200}
+
+
+def keyframes(video):
+ out = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-skip_frame", "nokey", "-show_entries",
+ "frame=pts", "-of", "csv=p=0", video], capture_output=True, text=True, check=True)
+ fps, _ = probe(video)
+ tb = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=time_base",
+ "-of", "csv=p=0", video], capture_output=True, text=True, check=True).stdout.strip().split("/")
+ per = fps * int(tb[0]) / int(tb[1])
+ return {int(round(int(line) * per)) for line in out.stdout.split() if line.strip().lstrip("-").isdigit()}
+
def lint(names, counts, soft, sizes, fps, ev):
"""What a viewer would trip on, found by rule. Each line: what, how often, the first few times."""
@@ -200,17 +216,29 @@ def lint(names, counts, soft, sizes, fps, ev):
# POP: a region still for STILL frames changes in one frame and is still again, at any size: a label
# swapped, a box gone, a count appearing. Nothing eased it. (And, as before, a large change in two.)
- pops = {e["from"] for e in ev if e["moved"] <= 2 and e["frames"] <= 3 and e["peak"] / sizes[0] > 0.002}
+ pops, exempt = set(), {}
+ for e in ev:
+ if e["moved"] <= 2 and e["frames"] <= 3 and e["peak"] / sizes[0] > 0.002:
+ kind = next((n_ for n_, test in EXEMPT.items() if test("all", e["from"], e["peak"])), None)
+ exempt.setdefault(kind, set()).add(e["from"]) if kind else pops.add(e["from"])
for k in range(1, len(names)):
if names[k] == "clock": # its count changes in a frame by design: it is time, not a thing
continue
col = on[:, k]
for t in range(STILL, len(col) - 1):
if col[t] and not col[t + 1] and not col[t - STILL:t].any() and counts[t, k] >= MIN_PX:
- pops.add(t)
+ kind = next((n_ for n_, test in EXEMPT.items() if test(names[k], t, counts[t, k])), None)
+ if kind:
+ exempt.setdefault(kind, set()).add(t)
+ else:
+ pops.add(t)
pops = sorted(pops)
if pops:
out.append(f" POP {len(pops):3d} a change in one frame after stillness: {some([t / fps for t in pops])}")
+ for kind, ts in sorted(exempt.items()):
+ ts = sorted(ts - set(pops))
+ if ts:
+ out.append(f" POP exempt {len(ts):3d} {kind}: {some([t / fps for t in ts])}")
# CROWD: three or more panels begin to move within two frames of each other.
crowd = []
@@ -249,7 +277,8 @@ def lint(names, counts, soft, sizes, fps, ev):
longs.append(b / fps)
elif 6 < up < 90: # 6: a caption leaving and the next arriving
shorts.append(b / fps)
- later = np.nonzero(on[b + 1:, panels].any(axis=1))[0]
+ later = [i for i in np.nonzero(on[b + 1:, panels].any(axis=1))[0]
+ if not ({b + 1 + i, b + i} & KEYFRAMES and counts[b + 1 + i, panels].max() < 200)] # the encoder's (an I frame and the one after it), not the picture's
if len(later) and later[0] < 60 and up >= 90:
late.append(b / fps)
for what, times in (("up over 720 f (12 s)", longs), ("up under 90 f", shorts),
@@ -315,6 +344,8 @@ def chart(video, names, counts, soft, sizes, fps, ev, path, per_row=10.0):
def cmd_chart(a):
fps, _ = probe(a.video)
+ KEYFRAMES.clear()
+ KEYFRAMES.update(keyframes(a.video))
regions = regions_for(a.video, a.regions)
names, counts, soft, sizes = measure(a.video, regions)
ev, text = report(a.video, names, counts, soft, sizes, fps)