| |
| import streamlit as st |
| import json |
| from pathlib import Path |
| from PIL import Image |
| import numpy as np |
|
|
| st.set_page_config(layout="wide") |
|
|
| data_dir = Path("data/animations") |
|
|
|
|
| |
| json_files = sorted(data_dir.glob("sample-*.webp")) |
| sample_names = [f.stem for f in json_files] |
|
|
| |
| def find_next_unlabeled_sample(current=None): |
| for name in sample_names: |
| hf_path = data_dir / f"{name}.hf.json" |
| if not hf_path.exists(): |
| if current is None or name != current: |
| return name |
| return sample_names[0] if sample_names else None |
|
|
| |
| if "sample_choice" not in st.session_state: |
| st.session_state["sample_choice"] = find_next_unlabeled_sample() |
|
|
| sample_choice = st.selectbox("Select sample", sample_names, index=sample_names.index(st.session_state["sample_choice"]) if st.session_state["sample_choice"] in sample_names else 0) |
| st.session_state["sample_choice"] = sample_choice |
|
|
| json_path = data_dir / f"{sample_choice}.json" |
| webp_path = data_dir / f"{sample_choice}.webp" |
|
|
| |
| frames = [] |
| if webp_path.exists(): |
| im = Image.open(webp_path) |
| try: |
| while True: |
| frames.append(im.convert("RGB")) |
| im.seek(im.tell() + 1) |
| except EOFError: |
| pass |
| else: |
| st.error(f"WebP file not found: {webp_path}") |
|
|
|
|
|
|
| |
| hf_path = data_dir / f"{sample_choice}.hf.json" |
| if hf_path.exists(): |
| with open(hf_path) as f: |
| data = json.load(f) |
| scene_change_indices = data.get("scene_change_indices", []) |
| else: |
| with open(json_path) as f: |
| data = json.load(f) |
| scene_change_indices = data.get("scene_change_indices", []) |
|
|
| |
| if sorted(scene_change_indices) == [40, 80]: |
| out_path = data_dir / f"{sample_choice}.hf.json" |
| with open(out_path, "w") as f: |
| json.dump({"scene_change_indices": [40, 80]}, f, indent=2) |
| next_sample = find_next_unlabeled_sample(current=sample_choice) |
| st.info(f"Auto-saved human feedback for {sample_choice} (cuts at 40, 80). Moving to next sample...") |
| if next_sample and next_sample != sample_choice: |
| st.session_state["sample_choice"] = next_sample |
| st.experimental_set_query_params(sample=next_sample) |
| st.stop() |
|
|
| st.write(f"Total frames: {len(frames)}") |
|
|
| |
| selected = st.multiselect( |
| "Select transition frames (scene changes)", |
| options=list(range(len(frames))), |
| default=scene_change_indices, |
| format_func=lambda i: f"Frame {i}" |
| ) |
|
|
| |
|
|
| |
|
|
| import streamlit.components.v1 as components |
| import io |
| import base64 |
| thumb_size = 64 |
| slider_html = "<div style='overflow-x:auto; white-space:nowrap; padding:8px;'>" |
| for i, frame in enumerate(frames): |
| buf = io.BytesIO() |
| frame.resize((thumb_size, thumb_size)).save(buf, format='PNG') |
| img_b64 = base64.b64encode(buf.getvalue()).decode('utf-8') |
| highlight = i in selected |
| border = '3px solid red' if highlight else '1px solid #ccc' |
| slider_html += f"<span style='display:inline-block; margin:2px; border:{border}; border-radius:4px;'><img src='data:image/png;base64,{img_b64}' style='width:{thumb_size}px;height:{thumb_size}px;'><div style='text-align:center;font-size:10px;color:{'red' if highlight else '#333'}'>{i}</div></span>" |
| slider_html += "</div>" |
| components.html(slider_html, height=thumb_size+40) |
|
|
| |
|
|
|
|
| if st.button("Save human feedback"): |
| out_path = data_dir / f"{sample_choice}.hf.json" |
| with open(out_path, "w") as f: |
| json.dump({"scene_change_indices": sorted(selected)}, f, indent=2) |
| st.success(f"Saved to {out_path}") |
| next_sample = find_next_unlabeled_sample(current=sample_choice) |
| if next_sample and next_sample != sample_choice: |
| st.info(f"Next sample available: {next_sample}") |
| if st.button("Go to next sample"): |
| st.session_state["sample_choice"] = next_sample |
| |
| st.experimental_set_query_params(sample=next_sample) |
| st.stop() |
|
|