summaryrefslogtreecommitdiff
path: root/anim/tools/motion.py
blob: 9bf0a393ed1ca2e277ce1d6e8073f48e4e9dde97 (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python3
"""Motion, made visible: a scene judged by how it moves, not by its stills.

    motion.py SCENE_EXE OUT_DIR [--size 960x540] [--fps 60]

Exports the scene twice (it is deterministic) and writes into OUT_DIR:

  change.png       how much of the picture changes from each frame to the
                   next: the whole frame on top (log scale), then a lane for
                   each panel, the head and the caption. Holds are gaps,
                   a metronome is evenly spaced spikes, overlapping action
                   is lanes that light one after another.
  change-NN.png    the same, ten seconds a picture, a frame about two pixels.
  sheet-NN.png     contact sheets: a row for each transition (a run of
                   frames in which something changes), six frames of it
                   side by side, cropped to where the change is, with its
                   length and the hold after it.
  transitions.txt  every transition: when, how long, how much, which panels
                   in what order, and the hold that follows.

and prints the summary: how many transitions, how long the holds are, how
regular the beat is.
"""
import os, subprocess, sys
import numpy as np
from PIL import Image, ImageDraw
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

args = [a for a in sys.argv[1:] if not a.startswith("--")]
opts = dict(zip(sys.argv[1:], sys.argv[2:]))
exe, out = args[0], args[1]
W, H = (int(v) for v in opts.get("--size", "960x540").split("x"))
FPS = int(opts.get("--fps", "60"))
os.makedirs(out, exist_ok=True)
for old in os.listdir(out):
    if old.startswith(("sheet-", "change")) or old == "transitions.txt":
        os.remove(os.path.join(out, old))

# A pixel has changed when any channel moved by more than this: above what
# antialiasing does to a still edge, below the faintest ember.
THRESHOLD = 10
# A frame is moving when this share of it changed; transitions closer than
# GAP frames are one (a strike, then its cooling).
ACTIVE = 0.0002
GAP = 4


def frames(want=None):
    """The scene's frames in order, as H x W x 3 arrays; with `want` (a set of indices), those alone."""
    env = dict(os.environ, CANIM_PANELS="1")
    # The scene says where its panels are, a line a panel a frame: kept aside, read once.
    log = open(os.path.join(out, "panels.log"), "w+")
    p = subprocess.Popen([exe, "--state", os.path.join(out, "motion.state"), "--export", "-", "--size", f"{W}x{H}", "--fps", str(FPS)],
                         stdout=subprocess.PIPE, stderr=log, env=env)
    size, i = W * H * 4, 0
    while True:
        raw = p.stdout.read(size)
        if len(raw) < size:
            break
        if want is None or i in want:
            yield i, np.frombuffer(raw, np.uint8).reshape(H, W, 4)[:, :, :3]
        i += 1
    p.wait()
    log.seek(0)
    frames.stderr = log.read()
    log.close()
    os.remove(os.path.join(out, "panels.log"))


# Pass one: what changes, frame by frame, in the whole picture and in each region.
regions = {"head": (0, 0, W, int(H * 112 / 1080)), "caption": (0, int(H * 896 / 1080), W, H)}
# The scene says where its panels are on frame 45, so each frame's change
# is kept small (a 96 x 54 map of where) and cut into lanes afterwards.
whole, maps, boxes, prev, n = [], [], [], None, 0
for i, frame in frames():
    n = i + 1
    # Every second pixel each way is enough to see what moves, four times sooner.
    now = frame[::2, ::2].copy()
    changed = np.zeros(now.shape[:2], bool) if prev is None else ((np.maximum(now, prev) - np.minimum(now, prev)).max(axis=2) > THRESHOLD)
    prev = now
    whole.append(float(changed.mean()))
    maps.append(changed.reshape(54, changed.shape[0] // 54, 96, changed.shape[1] // 96).mean(axis=(1, 3)).astype(np.float32))
    ys, xs = np.nonzero(changed)
    boxes.append((2 * xs.min(), 2 * ys.min(), 2 * xs.max() + 2, 2 * ys.max() + 2) if len(xs) else None)
whole = np.array(whole)

# Where each panel is when it first shows (before any camera move).
for line in frames.stderr.splitlines():
    if line.startswith("canim-panel "):
        x0, y0, x1, y1, name = line.split(" ", 5)[1:]
        if name not in regions:
            regions[name] = (max(0, int(float(x0) * W)), max(0, int(float(y0) * H)), min(W, int(float(x1) * W)), min(H, int(float(y1) * H)))
lanes = {}
for name, (x0, y0, x1, y1) in regions.items():
    cx0, cy0, cx1, cy1 = x0 * 96 // W, y0 * 54 // H, max(x1 * 96 // W, x0 * 96 // W + 1), max(y1 * 54 // H, y0 * 54 // H + 1)
    lanes[name] = np.array([m[cy0:cy1, cx0:cx1].mean() for m in maps])

# Transitions: runs of moving frames, bridged over short gaps.
moving = whole > ACTIVE
runs, i = [], 0
while i < n:
    if not moving[i]:
        i += 1
        continue
    j = i
    while j + 1 < n and (moving[j + 1] or moving[j + 1:j + 1 + GAP].any()):
        j += 1
    runs.append((i, j))
    i = j + 1

def order(a, b):
    """The panels that move in frames a..b, each with the frame it starts at after a."""
    firsts = []
    for name, lane in lanes.items():
        hit = np.nonzero(lane[a:b + 1] > ACTIVE * 4)[0]
        if len(hit):
            firsts.append((int(hit[0]), name))
    return sorted(firsts)

with open(os.path.join(out, "transitions.txt"), "w") as f:
    f.write(f"{exe}: {n} frames, {n / FPS:.2f} s at {FPS} fps; {len(runs)} transitions\n")
    f.write("  #   start      frames        length  peak   hold after   panels, in the order they start (frames after the first)\n")
    for k, (a, b) in enumerate(runs):
        hold = (runs[k + 1][0] if k + 1 < len(runs) else n) - b - 1
        who = "  ".join(f"{name}+{d}" for d, name in order(a, b))
        f.write(f"{k:3d} {a / FPS:7.2f}s  {a:5d}-{b:<5d} {(b - a + 1) / FPS:6.2f}s {whole[a:b + 1].max() * 100:5.1f}%  {hold / FPS:6.2f}s     {who}\n")

# The plot: the whole frame, then a lane each.
names = list(regions)
def plot(path, a, b):
    t = np.arange(a, b) / FPS
    fig, axes = plt.subplots(2, 1, figsize=(min(20, max(8, (b - a) / FPS * 1.8)), 2.4 + 0.32 * len(names)), sharex=True,
                             gridspec_kw={"height_ratios": [2.2, 0.32 * len(names) + 0.4]})
    ax = axes[0]
    ax.fill_between(t, np.maximum(whole[a:b] * 100, 0.005), 0.005, color="#ff6a4a", step="mid", linewidth=0)
    ax.set_yscale("log"); ax.set_ylim(0.005, 100); ax.set_ylabel("% of frame changed")
    for (s, e) in runs:
        if e >= a and s < b:
            ax.axvspan(s / FPS, (e + 1) / FPS, color="#e8c46a", alpha=0.18, linewidth=0)
    ax.grid(True, which="major", axis="both", alpha=0.25)
    grid = np.array([lanes[name][a:b] for name in names])
    axes[1].imshow(np.sqrt(grid), aspect="auto", cmap="magma", vmin=0, vmax=0.5, extent=(a / FPS, b / FPS, len(names), 0), interpolation="nearest")
    axes[1].set_yticks(np.arange(len(names)) + 0.5); axes[1].set_yticklabels(names)
    axes[1].set_xlabel("seconds")
    fig.tight_layout(); fig.savefig(path, dpi=110); plt.close(fig)

plot(os.path.join(out, "change.png"), 0, n)
for k, a in enumerate(range(0, n, 10 * FPS)):
    plot(os.path.join(out, f"change-{k:02d}.png"), a, min(n, a + 10 * FPS))

# Contact sheets: six frames of each transition, cropped to where it happens.
TILE, COLS, ROWS = (320, 180), 6, 5
picks = {}
for k, (a, b) in enumerate(runs):
    # From two frames before to where it has settled, evenly.
    span = np.linspace(max(a - 2, 0), min(b + 3, n - 1), COLS).round().astype(int)
    picks[k] = list(span)
want = {int(i) for span in picks.values() for i in span}
kept = {i: frame.copy() for i, frame in frames(want)}

def crop(a, b):
    """The box the change of frames a..b happens in, grown to the tile's shape, at least a quarter of the frame."""
    found = [bx for bx in boxes[a:b + 1] if bx is not None]
    if not found:
        return (0, 0, W, H)
    x0, y0 = min(bx[0] for bx in found), min(bx[1] for bx in found)
    x1, y1 = max(bx[2] for bx in found), max(bx[3] for bx in found)
    w = max(x1 - x0 + 40, W // 4, int((y1 - y0 + 40) * 16 / 9))
    w = min(W, w); h = min(H, int(w * 9 / 16))
    cx, cy = (x0 + x1) // 2, (y0 + y1) // 2
    left = min(max(cx - w // 2, 0), W - w); top = min(max(cy - h // 2, 0), H - h)
    return (left, top, left + w, top + h)

for s in range(0, len(runs), ROWS):
    sheet = Image.new("RGB", (COLS * TILE[0], min(ROWS, len(runs) - s) * (TILE[1] + 22)), "#000000")
    draw = ImageDraw.Draw(sheet)
    for r, k in enumerate(range(s, min(s + ROWS, len(runs)))):
        a, b = runs[k]
        box = crop(a, b)
        hold = (runs[k + 1][0] if k + 1 < len(runs) else n) - b - 1
        y = r * (TILE[1] + 22)
        draw.text((6, y + 4), f"#{k}  {a / FPS:.2f}s  frames {a}-{b}  {(b - a + 1) / FPS:.2f}s long, then still {hold / FPS:.2f}s   " + "  ".join(f"{name}+{d}" for d, name in order(a, b)), fill="#eee6d2")
        for c, i in enumerate(picks[k]):
            tile = Image.fromarray(kept[int(i)]).crop(box).resize(TILE, Image.LANCZOS)
            sheet.paste(tile, (c * TILE[0], y + 22))
            draw.text((c * TILE[0] + 6, y + 26), f"{int(i)}", fill="#e8c46a")
    sheet.save(os.path.join(out, f"sheet-{s // ROWS:02d}.png"))

# The summary: the beat, and whether it is a metronome.
starts = np.array([a for a, _ in runs]) / FPS
lengths = np.array([b - a + 1 for a, b in runs]) / FPS
holds = np.array([(runs[k + 1][0] if k + 1 < len(runs) else n) - runs[k][1] - 1 for k in range(len(runs))]) / FPS
gaps = np.diff(starts)
print(f"{os.path.basename(exe)}: {n / FPS:.1f} s, {len(runs)} transitions; moving {moving.mean() * 100:.0f}% of the time")
if len(runs) > 2:
    print(f"  transitions last {np.median(lengths):.2f} s (median), {lengths.min():.2f} to {lengths.max():.2f}")
    print(f"  holds: median {np.median(holds):.2f} s, shortest {holds.min():.2f} s; {int((holds < 0.25).sum())} under 0.25 s")
    same = np.abs(gaps[1:] - gaps[:-1]) < 0.02
    longest, cur = 0, 0
    for v in same:
        cur = cur + 1 if v else 0
        longest = max(longest, cur)
    print(f"  beat: {len(set(np.round(gaps, 2)))} different intervals between starts; longest run of equal ones: {longest + 1}")
print(f"  sheets and plots in {out}")
try:
    os.remove(os.path.join(out, "motion.state"))
except OSError:
    pass