Spaces:
Sleeping
Sleeping
Add 'Reveal video' tab: super-resolution reveal widget + downloadable MP4
Browse files- app.py +87 -6
- requirements.txt +2 -0
- superres_widget.html +206 -0
app.py
CHANGED
|
@@ -39,6 +39,7 @@ DEFAULT_MODEL = next(iter(MODELS))
|
|
| 39 |
|
| 40 |
TREE_HTML = open("tree_widget.html").read()
|
| 41 |
REVEAL_HTML = open("reveal_widget.html").read()
|
|
|
|
| 42 |
|
| 43 |
# single-slot cache: keep only the most-recently-used model (avoids OOM when switching)
|
| 44 |
_cur = {"repo": None, "model": None, "pre": None}
|
|
@@ -152,17 +153,86 @@ def iframe(widget_html, tree, img_b64, height):
|
|
| 152 |
f'style="width:100%;height:{height}px;border:none;background:#0f1117;border-radius:8px"></iframe>')
|
| 153 |
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
def process(image, model_label):
|
| 156 |
if image is None:
|
| 157 |
-
return "<p style='color:#999'>Upload an image first.</p>", ""
|
| 158 |
tree, img_b64 = build_tree(image, model_label)
|
| 159 |
-
return iframe(TREE_HTML, tree, img_b64, 660),
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
if gr is not None:
|
| 163 |
with gr.Blocks(title="Visual Tree", theme=gr.themes.Base()) as demo:
|
| 164 |
-
gr.Markdown("# 🌳 Visual Tree\nUpload an image → get its **condensed tree** (hover a node) "
|
| 165 |
-
"
|
|
|
|
|
|
|
| 166 |
with gr.Row():
|
| 167 |
inp = gr.Image(type="pil", label="Upload an image", height=300)
|
| 168 |
with gr.Column(scale=0):
|
|
@@ -178,8 +248,19 @@ if gr is not None:
|
|
| 178 |
out_tree = gr.HTML()
|
| 179 |
with gr.Tab("Reveal animation"):
|
| 180 |
out_rev = gr.HTML()
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
print("[app] launching gradio", flush=True)
|
| 185 |
demo.queue().launch() # module-level: HF Spaces imports this file, so no __main__ guard
|
|
|
|
| 39 |
|
| 40 |
TREE_HTML = open("tree_widget.html").read()
|
| 41 |
REVEAL_HTML = open("reveal_widget.html").read()
|
| 42 |
+
SUPERRES_HTML = open("superres_widget.html").read()
|
| 43 |
|
| 44 |
# single-slot cache: keep only the most-recently-used model (avoids OOM when switching)
|
| 45 |
_cur = {"repo": None, "model": None, "pre": None}
|
|
|
|
| 153 |
f'style="width:100%;height:{height}px;border:none;background:#0f1117;border-radius:8px"></iframe>')
|
| 154 |
|
| 155 |
|
| 156 |
+
def superres_video(tree, image, step=True):
|
| 157 |
+
"""Server-render the progressive super-resolution reveal (like *_reveal_superres_step.mp4).
|
| 158 |
+
The image starts blurry (root) and each region SNAPS/eases to full detail as the best-first
|
| 159 |
+
walk (mean CLS attention) descends the condensed tree. Returns an .mp4 filepath."""
|
| 160 |
+
import tempfile, imageio.v2 as imageio
|
| 161 |
+
from PIL import ImageDraw, ImageFilter
|
| 162 |
+
g = tree["g"]; nodes = tree["nodes"]; maxdepth = max(tree["maxdepth"], 1); P = g * g
|
| 163 |
+
CELL, R0, NB, FPS = 40, 32.0, 24, 14
|
| 164 |
+
kids = {n["id"]: [] for n in nodes}
|
| 165 |
+
for n in nodes:
|
| 166 |
+
if n["parent"] >= 0: kids[n["parent"]].append(n["id"])
|
| 167 |
+
pri = lambda i: (nodes[i]["attm"] if nodes[i].get("attm") is not None else nodes[i]["att"] / max(nodes[i]["n"], 1))
|
| 168 |
+
q, order = [0], []
|
| 169 |
+
while q: # best-first: pop highest mean-attention node
|
| 170 |
+
bi = max(range(len(q)), key=lambda k: pri(q[k])); j = q.pop(bi); order.append(j); q.extend(kids[j])
|
| 171 |
+
|
| 172 |
+
DISP = g * CELL
|
| 173 |
+
disp = image.convert("RGB").resize((DISP, DISP), Image.BICUBIC)
|
| 174 |
+
RADII = np.linspace(0, R0, NB)
|
| 175 |
+
stack = np.stack([np.asarray(disp if r < 0.3 else disp.filter(ImageFilter.GaussianBlur(float(r))), np.float32)
|
| 176 |
+
for r in RADII]) # NB,H,W,3
|
| 177 |
+
ROWS, COLS = np.indices((DISP, DISP))
|
| 178 |
+
tgt_r = lambda j: 0.0 if nodes[j]["leaf"] else R0 * (maxdepth - nodes[j]["depth"]) / maxdepth
|
| 179 |
+
cur = np.full(P, R0); frames = []
|
| 180 |
+
|
| 181 |
+
def frame(prog):
|
| 182 |
+
lvf = np.clip(cur / R0 * (NB - 1), 0, NB - 1); lo = np.floor(lvf).astype(int)
|
| 183 |
+
hi = np.minimum(lo + 1, NB - 1); fr = (lvf - lo)
|
| 184 |
+
loi = np.kron(lo.reshape(g, g), np.ones((CELL, CELL), int))
|
| 185 |
+
hii = np.kron(hi.reshape(g, g), np.ones((CELL, CELL), int))
|
| 186 |
+
fri = np.kron(fr.reshape(g, g), np.ones((CELL, CELL)))[..., None]
|
| 187 |
+
out = stack[loi, ROWS, COLS] * (1 - fri) + stack[hii, ROWS, COLS] * fri
|
| 188 |
+
im = Image.fromarray(out.clip(0, 255).astype(np.uint8))
|
| 189 |
+
ImageDraw.Draw(im).rectangle([0, 0, int(DISP * prog), 5], fill=(244, 162, 97))
|
| 190 |
+
frames.append(np.asarray(im))
|
| 191 |
+
|
| 192 |
+
if step: # one frame per look: snap, matches *_step.mp4
|
| 193 |
+
frame(0.0)
|
| 194 |
+
for k, j in enumerate(order, 1):
|
| 195 |
+
cur[nodes[j]["patches"]] = tgt_r(j); frame(k / len(order))
|
| 196 |
+
else: # eased settle (smooth variant)
|
| 197 |
+
EASE, FPL = 0.3, 3; tgt = np.full(P, R0)
|
| 198 |
+
for _ in range(8): frame(0.0)
|
| 199 |
+
for k, j in enumerate(order, 1):
|
| 200 |
+
tgt[nodes[j]["patches"]] = tgt_r(j)
|
| 201 |
+
for _ in range(FPL): cur += (tgt - cur) * EASE; frame(k / len(order))
|
| 202 |
+
for _ in range(50): cur += (tgt - cur) * EASE; frame(1.0)
|
| 203 |
+
|
| 204 |
+
fd, path = tempfile.mkstemp(suffix="_reveal_superres%s.mp4" % ("_step" if step else ""), dir="/tmp"); os.close(fd)
|
| 205 |
+
imageio.mimwrite(path, frames, fps=FPS, codec="libx264", quality=8, macro_block_size=1)
|
| 206 |
+
print(f"[app] superres video: {len(frames)} frames -> {path}", flush=True)
|
| 207 |
+
return path
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def render_video(state, image, model_label, mode):
|
| 211 |
+
if state is not None:
|
| 212 |
+
tree, pil = state # reuse the tree we already built (no re-run)
|
| 213 |
+
elif image is not None:
|
| 214 |
+
tree, _ = build_tree(image, model_label); pil = image
|
| 215 |
+
else:
|
| 216 |
+
return None
|
| 217 |
+
return superres_video(tree, pil, step=str(mode).startswith("Step"))
|
| 218 |
+
|
| 219 |
+
|
| 220 |
def process(image, model_label):
|
| 221 |
if image is None:
|
| 222 |
+
return "<p style='color:#999'>Upload an image first.</p>", "", "", None
|
| 223 |
tree, img_b64 = build_tree(image, model_label)
|
| 224 |
+
return (iframe(TREE_HTML, tree, img_b64, 660),
|
| 225 |
+
iframe(REVEAL_HTML, tree, img_b64, 700),
|
| 226 |
+
iframe(SUPERRES_HTML, tree, img_b64, 700),
|
| 227 |
+
(tree, image))
|
| 228 |
|
| 229 |
|
| 230 |
if gr is not None:
|
| 231 |
with gr.Blocks(title="Visual Tree", theme=gr.themes.Base()) as demo:
|
| 232 |
+
gr.Markdown("# 🌳 Visual Tree\nUpload an image → get its **condensed tree** (hover a node), "
|
| 233 |
+
"the **reveal animation**, and a **reveal video** (super-resolution) — all "
|
| 234 |
+
"best-first by CLS attention.")
|
| 235 |
+
st = gr.State() # (tree, PIL image) from the last build
|
| 236 |
with gr.Row():
|
| 237 |
inp = gr.Image(type="pil", label="Upload an image", height=300)
|
| 238 |
with gr.Column(scale=0):
|
|
|
|
| 248 |
out_tree = gr.HTML()
|
| 249 |
with gr.Tab("Reveal animation"):
|
| 250 |
out_rev = gr.HTML()
|
| 251 |
+
with gr.Tab("Reveal video"):
|
| 252 |
+
out_sr = gr.HTML()
|
| 253 |
+
with gr.Row():
|
| 254 |
+
video_mode = gr.Radio(["Step (snap per look)", "Smooth (eased)"],
|
| 255 |
+
value="Step (snap per look)", label="Video style", scale=1)
|
| 256 |
+
render_btn = gr.Button("🎬 Render MP4 (download)", variant="primary", scale=0)
|
| 257 |
+
video_out = gr.Video(label="Super-resolution reveal — use the ⬇ on the player to save to disk",
|
| 258 |
+
interactive=False, autoplay=True)
|
| 259 |
+
|
| 260 |
+
outs = [out_tree, out_rev, out_sr, st]
|
| 261 |
+
btn.click(process, [inp, model_sel], outs)
|
| 262 |
+
inp.upload(process, [inp, model_sel], outs)
|
| 263 |
+
render_btn.click(render_video, [st, inp, model_sel, video_mode], [video_out])
|
| 264 |
|
| 265 |
print("[app] launching gradio", flush=True)
|
| 266 |
demo.queue().launch() # module-level: HF Spaces imports this file, so no __main__ guard
|
requirements.txt
CHANGED
|
@@ -6,3 +6,5 @@ pillow
|
|
| 6 |
numpy
|
| 7 |
gradio
|
| 8 |
dreamsim
|
|
|
|
|
|
|
|
|
| 6 |
numpy
|
| 7 |
gradio
|
| 8 |
dreamsim
|
| 9 |
+
imageio
|
| 10 |
+
imageio-ffmpeg
|
superres_widget.html
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<style>
|
| 6 |
+
html,body{margin:0;height:100%;background:#0f1117;color:#e8eaf0;font-family:'Inter','Segoe UI',sans-serif;overflow:hidden}
|
| 7 |
+
#wrap{display:flex;height:100vh;gap:18px;padding:14px;box-sizing:border-box}
|
| 8 |
+
/* super-res video LEFT, tree RIGHT */
|
| 9 |
+
#left{flex:0 0 auto;display:flex;flex-direction:column;align-items:center;justify-content:center}
|
| 10 |
+
#right{flex:1 1 auto;position:relative}
|
| 11 |
+
svg{width:100%;height:100%}
|
| 12 |
+
.edge{stroke:#3a3f55;stroke-width:1}
|
| 13 |
+
.node{transition:fill .15s ease}
|
| 14 |
+
#imgbox{position:relative;border-radius:8px;overflow:hidden;box-shadow:0 0 0 1px #2a2d3e}
|
| 15 |
+
#cv{display:block}
|
| 16 |
+
#cap{font-size:12px;color:#7b8290;margin-top:8px;width:360px;text-align:center;line-height:1.35;
|
| 17 |
+
height:4.05em;overflow:hidden;display:flex;align-items:center;justify-content:center}
|
| 18 |
+
#ctrl{margin-top:10px;display:flex;gap:8px;align-items:center;flex-wrap:wrap;justify-content:center}
|
| 19 |
+
button{background:#1a2240;color:#e8eaf0;border:1px solid #5b9cf6;border-radius:6px;padding:5px 11px;font-size:13px;cursor:pointer}
|
| 20 |
+
button:hover{background:#24305a}
|
| 21 |
+
button:disabled{opacity:.5;cursor:default}
|
| 22 |
+
#dl{border-color:#3ee08a}
|
| 23 |
+
.legend{position:absolute;right:10px;top:8px;font-size:12px;color:#c8ccd8;line-height:1.7}
|
| 24 |
+
.dot{display:inline-block;width:11px;height:11px;border-radius:50%;margin-right:6px;vertical-align:middle;border:1px solid #0f1117}
|
| 25 |
+
</style>
|
| 26 |
+
</head>
|
| 27 |
+
<body>
|
| 28 |
+
<div id="wrap">
|
| 29 |
+
<div id="left">
|
| 30 |
+
<div id="imgbox"><canvas id="cv"></canvas><img id="img" style="display:none"></div>
|
| 31 |
+
<div id="cap"></div>
|
| 32 |
+
<div id="ctrl">
|
| 33 |
+
<button id="restart">⟲ Restart</button>
|
| 34 |
+
<button id="play">⏸ Pause</button>
|
| 35 |
+
<button id="back">◂ Back</button>
|
| 36 |
+
<button id="step">Forward ▸</button>
|
| 37 |
+
<button id="dl">⬇ Download</button>
|
| 38 |
+
</div>
|
| 39 |
+
</div>
|
| 40 |
+
<div id="right">
|
| 41 |
+
<svg id="svg"></svg>
|
| 42 |
+
<div class="legend">
|
| 43 |
+
<div><span class="dot" style="background:#ffffff"></span>not reached yet</div>
|
| 44 |
+
<div><span class="dot" style="background:#f4a261"></span>in the frontier (queued)</div>
|
| 45 |
+
<div><span class="dot" style="background:#5b9cf6"></span>now sharpening</div>
|
| 46 |
+
<div><span class="dot" style="background:#b00018"></span>already looked at</div>
|
| 47 |
+
<div style="margin-top:6px;border-top:1px solid #2a2d3e;padding-top:6px">image starts blurry (root) → each region <b style="color:#3ee08a">snaps sharp</b> at its leaf</div>
|
| 48 |
+
</div>
|
| 49 |
+
</div>
|
| 50 |
+
</div>
|
| 51 |
+
<script>
|
| 52 |
+
const WHITE='#ffffff', QUEUED='#f4a261', PRESENT='#5b9cf6', PAST='#b00018';
|
| 53 |
+
let T, G, P, NODES, ORDER, children, N, FMAX, maxdepth;
|
| 54 |
+
let f, timer, playing=true;
|
| 55 |
+
let S=440, R0=26, NB=16, blurLv=[]; // blur pyramid (level 0 = sharp)
|
| 56 |
+
|
| 57 |
+
const img=document.getElementById('img'), cv=document.getElementById('cv'), ctx=cv.getContext('2d');
|
| 58 |
+
const svg=document.getElementById('svg'), right=document.getElementById('right'), cap=document.getElementById('cap');
|
| 59 |
+
|
| 60 |
+
function sizeImage(){
|
| 61 |
+
S=Math.round(Math.min(window.innerHeight*0.58, window.innerWidth*0.34, 440));
|
| 62 |
+
cv.width=S; cv.height=S; cv.style.width=S+'px'; cv.style.height=S+'px';
|
| 63 |
+
R0=Math.max(8, Math.round(S*0.06)); // max blur radius scales with canvas
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
// NB full-image blur levels: draw the sharp image, then the blurred image over it
|
| 67 |
+
// (the sharp base keeps borders from darkening where the blur samples off-canvas).
|
| 68 |
+
function buildBlur(){
|
| 69 |
+
blurLv=[];
|
| 70 |
+
for(let i=0;i<NB;i++){
|
| 71 |
+
const r=R0*i/(NB-1);
|
| 72 |
+
const c=document.createElement('canvas'); c.width=S; c.height=S;
|
| 73 |
+
const cx=c.getContext('2d');
|
| 74 |
+
cx.drawImage(img,0,0,S,S);
|
| 75 |
+
if(r>=0.3){ cx.filter='blur('+r.toFixed(2)+'px)'; cx.drawImage(img,0,0,S,S); cx.filter='none'; }
|
| 76 |
+
blurLv.push(c);
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
// priority = MEAN CLS-attention over the segment's patches (favors small salient parts).
|
| 81 |
+
const PRI = n => (n.attm!=null ? n.attm : n.att/Math.max(n.n,1));
|
| 82 |
+
function buildOrder(){
|
| 83 |
+
children=NODES.map(()=>[]);
|
| 84 |
+
NODES.forEach(n=>{ if(n.parent>=0) children[n.parent].push(n.id); });
|
| 85 |
+
const q=[0], steps=[];
|
| 86 |
+
while(q.length){
|
| 87 |
+
let bi=0; for(let i=1;i<q.length;i++) if(PRI(NODES[q[i]])>PRI(NODES[q[bi]])) bi=i;
|
| 88 |
+
const id=q.splice(bi,1)[0]; children[id].forEach(c=>q.push(c)); steps.push(id);
|
| 89 |
+
}
|
| 90 |
+
return steps;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// leaf -> 0 (fully sharp); else coarser, linear in depth (root = fully blurred).
|
| 94 |
+
function targetRadius(j){ return NODES[j].leaf ? 0 : R0*(maxdepth-NODES[j].depth)/Math.max(maxdepth,1); }
|
| 95 |
+
|
| 96 |
+
// per-patch blur radius after looking at ORDER[0..looked-1] (deepest looked segment wins).
|
| 97 |
+
function curRadius(f){
|
| 98 |
+
const c=new Float32Array(P).fill(R0), looked=Math.min(f,N);
|
| 99 |
+
for(let i=0;i<looked;i++){ const j=ORDER[i], tr=targetRadius(j); NODES[j].patches.forEach(p=>{ c[p]=tr; }); }
|
| 100 |
+
return c;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
function renderImage(f){
|
| 104 |
+
const cell=S/G, cur=curRadius(f);
|
| 105 |
+
ctx.clearRect(0,0,S,S);
|
| 106 |
+
for(let p=0;p<P;p++){
|
| 107 |
+
const lvf=Math.max(0,Math.min(NB-1, cur[p]/R0*(NB-1)));
|
| 108 |
+
const lo=Math.floor(lvf), hi=Math.min(lo+1,NB-1), fr=lvf-lo;
|
| 109 |
+
const c=(p%G)*cell, r=Math.floor(p/G)*cell;
|
| 110 |
+
ctx.globalAlpha=1; ctx.drawImage(blurLv[lo], c,r,cell,cell, c,r,cell,cell);
|
| 111 |
+
if(fr>0.002){ ctx.globalAlpha=fr; ctx.drawImage(blurLv[hi], c,r,cell,cell, c,r,cell,cell); }
|
| 112 |
+
}
|
| 113 |
+
ctx.globalAlpha=1;
|
| 114 |
+
// blue outline of the segment currently being sharpened (ties the video to the tree)
|
| 115 |
+
const present=(f>=1&&f<=N)?ORDER[f-1]:null;
|
| 116 |
+
if(present!=null){ const ps=NODES[present].patches, set=new Set(ps);
|
| 117 |
+
ctx.strokeStyle='rgba(91,156,246,0.95)'; ctx.lineWidth=2;
|
| 118 |
+
ps.forEach(p=>{ const rr0=Math.floor(p/G),cc0=p%G;
|
| 119 |
+
[[rr0-1,cc0],[rr0+1,cc0],[rr0,cc0-1],[rr0,cc0+1]].forEach(([rr,cc])=>{
|
| 120 |
+
if(rr<0||rr>=G||cc<0||cc>=G||!set.has(rr*G+cc)){
|
| 121 |
+
ctx.beginPath();
|
| 122 |
+
if(rr<rr0){ctx.moveTo(cc0*cell,rr0*cell);ctx.lineTo((cc0+1)*cell,rr0*cell);}
|
| 123 |
+
else if(rr>rr0){ctx.moveTo(cc0*cell,(rr0+1)*cell);ctx.lineTo((cc0+1)*cell,(rr0+1)*cell);}
|
| 124 |
+
else if(cc<cc0){ctx.moveTo(cc0*cell,rr0*cell);ctx.lineTo(cc0*cell,(rr0+1)*cell);}
|
| 125 |
+
else{ctx.moveTo((cc0+1)*cell,rr0*cell);ctx.lineTo((cc0+1)*cell,(rr0+1)*cell);}
|
| 126 |
+
ctx.stroke();
|
| 127 |
+
}});
|
| 128 |
+
});
|
| 129 |
+
}
|
| 130 |
+
// progress bar (matches the orange bar in the exported mp4)
|
| 131 |
+
ctx.fillStyle='#f4a261'; ctx.fillRect(0,0,S*Math.min(f,N)/Math.max(N,1),5);
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
let circles=[];
|
| 135 |
+
function drawTree(){
|
| 136 |
+
const W=right.clientWidth, H=right.clientHeight, pad=28;
|
| 137 |
+
svg.setAttribute('viewBox',`0 0 ${W} ${H}`);
|
| 138 |
+
const xs=x=>pad+(x/Math.max(T.nleaves-1,1))*(W-2*pad);
|
| 139 |
+
const ys=d=>pad+(d/Math.max(T.maxdepth,1))*(H-2*pad);
|
| 140 |
+
let s='';
|
| 141 |
+
NODES.forEach(n=>{ if(n.parent>=0){const p=NODES[n.parent];
|
| 142 |
+
s+=`<line class="edge" x1="${xs(p.x)}" y1="${ys(p.depth)}" x2="${xs(n.x)}" y2="${ys(n.depth)}"/>`;}});
|
| 143 |
+
NODES.forEach(n=>{ const r=Math.min(20,2.5+1.9*Math.sqrt(n.att));
|
| 144 |
+
s+=`<circle class="node" data-id="${n.id}" cx="${xs(n.x)}" cy="${ys(n.depth)}" r="${r}"
|
| 145 |
+
fill="${WHITE}" stroke="#0f1117" stroke-width="1"/>`;});
|
| 146 |
+
svg.innerHTML=s; circles=[...svg.querySelectorAll('.node')];
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
function render(){
|
| 150 |
+
const st=NODES.map(()=>WHITE), looked=Math.min(f,N);
|
| 151 |
+
for(let i=0;i<looked;i++) st[ORDER[i]]=PAST;
|
| 152 |
+
const pushed=new Set([0]);
|
| 153 |
+
for(let i=0;i<looked;i++) children[ORDER[i]].forEach(c=>pushed.add(c));
|
| 154 |
+
pushed.forEach(id=>{ if(st[id]===WHITE) st[id]=QUEUED; });
|
| 155 |
+
const present=(f>=1 && f<=N)? ORDER[f-1] : null;
|
| 156 |
+
if(present!=null) st[present]=PRESENT;
|
| 157 |
+
circles.forEach(el=>{ el.setAttribute('fill', st[+el.dataset.id]); });
|
| 158 |
+
renderImage(f);
|
| 159 |
+
if(f===0) cap.textContent='Fully blurred = root segment. The highest-attention segment sharpens next.';
|
| 160 |
+
else if(f<=N){ const id=ORDER[f-1], nch=children[id].length;
|
| 161 |
+
cap.textContent=`Step ${f}/${N}: sharpen node #${id} (mean ${PRI(NODES[id]).toFixed(2)}% attn/patch, ${NODES[id].n} patches)`+
|
| 162 |
+
(nch?` → push ${nch} child segment${nch>1?'s':''}.`:` → leaf: this region snaps to full detail.`); }
|
| 163 |
+
else cap.textContent='Done — every region has snapped to full resolution.';
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
function goTo(nf){ f=Math.max(0,Math.min(FMAX,nf)); render(); }
|
| 167 |
+
function stop(){ playing=false; clearInterval(timer); document.getElementById('play').textContent='▶ Play'; }
|
| 168 |
+
function start(){ playing=true; document.getElementById('play').textContent='⏸ Pause';
|
| 169 |
+
clearInterval(timer); timer=setInterval(()=>{ if(f>=FMAX){ stop(); return; } goTo(f+1); },700); }
|
| 170 |
+
|
| 171 |
+
document.getElementById('back').onclick=()=>{ stop(); goTo(f-1); };
|
| 172 |
+
document.getElementById('step').onclick=()=>{ stop(); goTo(f+1); };
|
| 173 |
+
document.getElementById('play').onclick=()=>{ if(f>=FMAX) goTo(0); playing?stop():start(); };
|
| 174 |
+
document.getElementById('restart').onclick=()=>{ goTo(0); start(); };
|
| 175 |
+
|
| 176 |
+
// ---- client-side download: record the canvas play-through as a .webm ----
|
| 177 |
+
const dlBtn=document.getElementById('dl');
|
| 178 |
+
if(typeof MediaRecorder==='undefined' || !cv.captureStream){ dlBtn.style.display='none'; }
|
| 179 |
+
dlBtn.onclick=()=>{
|
| 180 |
+
stop(); const stream=cv.captureStream(30);
|
| 181 |
+
let mime='video/webm;codecs=vp9'; if(!MediaRecorder.isTypeSupported(mime)) mime='video/webm';
|
| 182 |
+
const rec=new MediaRecorder(stream,{mimeType:mime, videoBitsPerSecond:6e6}), chunks=[];
|
| 183 |
+
rec.ondataavailable=e=>{ if(e.data.size) chunks.push(e.data); };
|
| 184 |
+
rec.onstop=()=>{ const blob=new Blob(chunks,{type:mime}); const url=URL.createObjectURL(blob);
|
| 185 |
+
const a=document.createElement('a'); a.href=url; a.download='reveal_superres.webm';
|
| 186 |
+
document.body.appendChild(a); a.click(); a.remove(); URL.revokeObjectURL(url);
|
| 187 |
+
dlBtn.textContent='⬇ Download'; dlBtn.disabled=false; start(); };
|
| 188 |
+
dlBtn.textContent='● recording…'; dlBtn.disabled=true;
|
| 189 |
+
rec.start(); goTo(0);
|
| 190 |
+
let k=0; const HOLD=4, tick=setInterval(()=>{ // hold each look a few frames, then advance
|
| 191 |
+
if(k>=(N+2)*HOLD){ clearInterval(tick); rec.stop(); return; }
|
| 192 |
+
goTo(Math.floor(k/HOLD)); k++;
|
| 193 |
+
}, 1000/30);
|
| 194 |
+
};
|
| 195 |
+
|
| 196 |
+
window.addEventListener('resize',()=>{ if(T){ sizeImage(); buildBlur(); drawTree(); render(); } });
|
| 197 |
+
|
| 198 |
+
let dataReady=false, imgReady=false;
|
| 199 |
+
function go(){ if(!(dataReady&&imgReady)) return;
|
| 200 |
+
sizeImage(); P=G*G; buildBlur(); ORDER=buildOrder(); N=ORDER.length; FMAX=N+1; drawTree(); goTo(0); start(); }
|
| 201 |
+
img.onload=()=>{ imgReady=true; go(); };
|
| 202 |
+
T=window.TREE; G=T.g; NODES=T.nodes; maxdepth=T.maxdepth; dataReady=true;
|
| 203 |
+
img.src=window.IMG_SRC; // injected data URI
|
| 204 |
+
</script>
|
| 205 |
+
</body>
|
| 206 |
+
</html>
|