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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import zipfile
|
| 4 |
+
import uuid
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import numpy as np
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
from PIL import Image
|
| 11 |
+
from fpdf import FPDF
|
| 12 |
+
import plotly.graph_objects as go
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# =========================
|
| 16 |
+
# 1. μΈμ
ZIP λ‘λ©
|
| 17 |
+
# =========================
|
| 18 |
+
def load_session(zip_path: str):
|
| 19 |
+
"""
|
| 20 |
+
μ
λ‘λλ ZIPμ κ³ μ μΈμ
ν΄λμ νκ³
|
| 21 |
+
log.json, frames λλ ν 리λ₯Ό μ€λΉνλ€.
|
| 22 |
+
"""
|
| 23 |
+
base_dir = "uploaded_sessions"
|
| 24 |
+
os.makedirs(base_dir, exist_ok=True)
|
| 25 |
+
|
| 26 |
+
session_id = datetime.now().strftime("%Y%m%d_%H%M%S_") + str(uuid.uuid4())[:8]
|
| 27 |
+
session_dir = os.path.join(base_dir, session_id)
|
| 28 |
+
os.makedirs(session_dir, exist_ok=True)
|
| 29 |
+
|
| 30 |
+
with zipfile.ZipFile(zip_path, "r") as zf:
|
| 31 |
+
zf.extractall(session_dir)
|
| 32 |
+
|
| 33 |
+
log_path = os.path.join(session_dir, "log.json")
|
| 34 |
+
if not os.path.exists(log_path):
|
| 35 |
+
raise FileNotFoundError("log.json not found inside ZIP. Check collector.py output format.")
|
| 36 |
+
|
| 37 |
+
with open(log_path, "r", encoding="utf-8") as f:
|
| 38 |
+
data = json.load(f)
|
| 39 |
+
|
| 40 |
+
samples = data.get("samples", [])
|
| 41 |
+
events = data.get("events", [])
|
| 42 |
+
meta = data.get("meta", {})
|
| 43 |
+
|
| 44 |
+
return samples, events, meta, session_dir
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# =========================
|
| 48 |
+
# 2. 보쑰 ν¨μλ€
|
| 49 |
+
# =========================
|
| 50 |
+
def moving_average(arr, window=5):
|
| 51 |
+
arr = np.array(arr, dtype=float)
|
| 52 |
+
if len(arr) == 0:
|
| 53 |
+
return arr
|
| 54 |
+
if len(arr) < window:
|
| 55 |
+
return arr
|
| 56 |
+
cumsum = np.cumsum(np.insert(arr, 0, 0))
|
| 57 |
+
ma = (cumsum[window:] - cumsum[:-window]) / float(window)
|
| 58 |
+
# κΈΈμ΄ λ§μΆκΈ°: μλΆλΆμ κ·Έλ₯ μλ³Έ μ¬μ©
|
| 59 |
+
head = arr[:window-1]
|
| 60 |
+
return np.concatenate([head, ma])
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def normalize(arr):
|
| 64 |
+
arr = np.array(arr, dtype=float)
|
| 65 |
+
if arr.size == 0:
|
| 66 |
+
return arr
|
| 67 |
+
min_v = np.min(arr)
|
| 68 |
+
max_v = np.max(arr)
|
| 69 |
+
if max_v - min_v < 1e-8:
|
| 70 |
+
return np.zeros_like(arr)
|
| 71 |
+
return (arr - min_v) / (max_v - min_v)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# =========================
|
| 75 |
+
# 3. νμλΌμΈ λΆμ
|
| 76 |
+
# =========================
|
| 77 |
+
def analyze_timeline(samples, alpha: float, ma_window: int = 5):
|
| 78 |
+
"""
|
| 79 |
+
samplesμμ time, p_surprise, bpmμ μΆμΆνκ³
|
| 80 |
+
BPM smoothing + baseline + combined_score κ³μ°.
|
| 81 |
+
"""
|
| 82 |
+
if len(samples) == 0:
|
| 83 |
+
raise ValueError("No samples found in log.json.")
|
| 84 |
+
|
| 85 |
+
times = np.array([s.get("time", 0.0) for s in samples], dtype=float)
|
| 86 |
+
ps = np.array([s.get("p_surprise", 0.0) for s in samples], dtype=float)
|
| 87 |
+
|
| 88 |
+
raw_bpm = []
|
| 89 |
+
for s in samples:
|
| 90 |
+
b = s.get("bpm", None)
|
| 91 |
+
raw_bpm.append(0.0 if b is None else float(b))
|
| 92 |
+
raw_bpm = np.array(raw_bpm, dtype=float)
|
| 93 |
+
|
| 94 |
+
# BPM smoothing (moving average)
|
| 95 |
+
bpm_smooth = moving_average(raw_bpm, window=ma_window)
|
| 96 |
+
|
| 97 |
+
# baseline (μ ν¨ BPMλ§ median)
|
| 98 |
+
valid_bpm = bpm_smooth[bpm_smooth > 0]
|
| 99 |
+
if len(valid_bpm) > 0:
|
| 100 |
+
baseline = float(np.median(valid_bpm))
|
| 101 |
+
else:
|
| 102 |
+
baseline = 0.0
|
| 103 |
+
|
| 104 |
+
# spike component: baseline μ΄μ λΆλΆλ§ μ¬μ©
|
| 105 |
+
bpm_spike = np.where(bpm_smooth > baseline, bpm_smooth - baseline, 0.0)
|
| 106 |
+
|
| 107 |
+
# normalization
|
| 108 |
+
ps_norm = normalize(ps)
|
| 109 |
+
bpm_norm = normalize(bpm_spike)
|
| 110 |
+
|
| 111 |
+
beta = 1.0 - alpha
|
| 112 |
+
combined = alpha * ps_norm + beta * bpm_norm
|
| 113 |
+
|
| 114 |
+
return times, ps, bpm_smooth, combined, baseline
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
# =========================
|
| 118 |
+
# 4. Top3 + κ°κ²© 보μ + BPM κΈμμΉ νμ§
|
| 119 |
+
# =========================
|
| 120 |
+
def pick_top3_events(events, times, combined, min_gap_sec=1.0):
|
| 121 |
+
"""
|
| 122 |
+
κ° μ΄λ²€νΈμ combined_scoreλ₯Ό λ§€κΈ°κ³ ,
|
| 123 |
+
μ΅μ μκ° κ°κ²© >= min_gap_sec 쑰건μ λ§μ‘±νλ Top3 μ ν.
|
| 124 |
+
"""
|
| 125 |
+
if len(events) == 0:
|
| 126 |
+
return []
|
| 127 |
+
|
| 128 |
+
times = np.array(times, dtype=float)
|
| 129 |
+
enriched = []
|
| 130 |
+
|
| 131 |
+
for e in events:
|
| 132 |
+
t = float(e.get("time", 0.0))
|
| 133 |
+
idx = int(np.argmin(np.abs(times - t)))
|
| 134 |
+
score = float(combined[idx]) if 0 <= idx < len(combined) else 0.0
|
| 135 |
+
|
| 136 |
+
enriched.append({
|
| 137 |
+
"time": t,
|
| 138 |
+
"p_surprise": float(e.get("p_surprise", 0.0)),
|
| 139 |
+
"bpm": float(e["bpm"]) if e.get("bpm") is not None else None,
|
| 140 |
+
"frame_file": e.get("frame_file", ""),
|
| 141 |
+
"combined_score": score
|
| 142 |
+
})
|
| 143 |
+
|
| 144 |
+
# combined_score κΈ°μ€ μ λ ¬
|
| 145 |
+
enriched.sort(key=lambda x: x["combined_score"], reverse=True)
|
| 146 |
+
|
| 147 |
+
selected = []
|
| 148 |
+
for cand in enriched:
|
| 149 |
+
if len(selected) == 0:
|
| 150 |
+
selected.append(cand)
|
| 151 |
+
else:
|
| 152 |
+
too_close = any(abs(cand["time"] - ev["time"]) < min_gap_sec for ev in selected)
|
| 153 |
+
if not too_close:
|
| 154 |
+
selected.append(cand)
|
| 155 |
+
if len(selected) >= 3:
|
| 156 |
+
break
|
| 157 |
+
|
| 158 |
+
return selected
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def detect_bpm_spikes(times, bpm, baseline, spike_delta=10.0, min_gap_sec=1.0):
|
| 162 |
+
"""
|
| 163 |
+
BPMμ΄ μ§§μ μκ° μμ κΈμμΉνλ κ΅¬κ° νμ§.
|
| 164 |
+
κ°λ¨ν: bpm[i] - bpm[i-1] >= spike_delta μ΄κ³ ,
|
| 165 |
+
μ΄λ²€νΈ κ° μκ° κ°κ²© >= min_gap_sec.
|
| 166 |
+
"""
|
| 167 |
+
spikes = []
|
| 168 |
+
last_spike_t = -1e9
|
| 169 |
+
for i in range(1, len(bpm)):
|
| 170 |
+
if bpm[i-1] <= 0 or bpm[i] <= 0:
|
| 171 |
+
continue
|
| 172 |
+
diff = bpm[i] - bpm[i-1]
|
| 173 |
+
if diff >= spike_delta and bpm[i] > baseline:
|
| 174 |
+
t = times[i]
|
| 175 |
+
if t - last_spike_t >= min_gap_sec:
|
| 176 |
+
spikes.append(t)
|
| 177 |
+
last_spike_t = t
|
| 178 |
+
return spikes
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
# =========================
|
| 182 |
+
# 5. Plotly νμλΌμΈ + Heatmap
|
| 183 |
+
# =========================
|
| 184 |
+
def build_timeline_plot(times, ps, bpm, combined, top3, spike_times):
|
| 185 |
+
"""
|
| 186 |
+
Plotly figure (p_surprise, BPM, combined + top3 marker + spike lines)
|
| 187 |
+
"""
|
| 188 |
+
fig = go.Figure()
|
| 189 |
+
|
| 190 |
+
# p_surprise
|
| 191 |
+
fig.add_trace(
|
| 192 |
+
go.Scatter(
|
| 193 |
+
x=times,
|
| 194 |
+
y=ps,
|
| 195 |
+
mode="lines",
|
| 196 |
+
name="p_surprise",
|
| 197 |
+
line=dict(width=2)
|
| 198 |
+
)
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
# BPM (y2)
|
| 202 |
+
fig.add_trace(
|
| 203 |
+
go.Scatter(
|
| 204 |
+
x=times,
|
| 205 |
+
y=bpm,
|
| 206 |
+
mode="lines",
|
| 207 |
+
name="BPM (smoothed)",
|
| 208 |
+
line=dict(width=2, dash="dot"),
|
| 209 |
+
yaxis="y2"
|
| 210 |
+
)
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
# combined score
|
| 214 |
+
fig.add_trace(
|
| 215 |
+
go.Scatter(
|
| 216 |
+
x=times,
|
| 217 |
+
y=combined,
|
| 218 |
+
mode="lines",
|
| 219 |
+
name="combined_score",
|
| 220 |
+
line=dict(width=2)
|
| 221 |
+
)
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
# Top3 markers (combined score μμ νμ)
|
| 225 |
+
if top3:
|
| 226 |
+
top_times = [ev["time"] for ev in top3]
|
| 227 |
+
# κ° μκ°μμμ combined κ°
|
| 228 |
+
y_vals = []
|
| 229 |
+
for t in top_times:
|
| 230 |
+
idx = int(np.argmin(np.abs(times - t)))
|
| 231 |
+
if 0 <= idx < len(combined):
|
| 232 |
+
y_vals.append(combined[idx])
|
| 233 |
+
else:
|
| 234 |
+
y_vals.append(None)
|
| 235 |
+
|
| 236 |
+
fig.add_trace(
|
| 237 |
+
go.Scatter(
|
| 238 |
+
x=top_times,
|
| 239 |
+
y=y_vals,
|
| 240 |
+
mode="markers+text",
|
| 241 |
+
name="Top3",
|
| 242 |
+
marker=dict(size=12, symbol="star", color="gold"),
|
| 243 |
+
text=[f"Top{i+1}" for i in range(len(top3))],
|
| 244 |
+
textposition="top center"
|
| 245 |
+
)
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# μ¬λ° κΈμμΉ κ΅¬κ°: μΈλ‘μ
|
| 249 |
+
shapes = []
|
| 250 |
+
for t in spike_times:
|
| 251 |
+
shapes.append(
|
| 252 |
+
dict(
|
| 253 |
+
type="line",
|
| 254 |
+
x0=t,
|
| 255 |
+
x1=t,
|
| 256 |
+
y0=0,
|
| 257 |
+
y1=1,
|
| 258 |
+
xref="x",
|
| 259 |
+
yref="paper",
|
| 260 |
+
line=dict(color="red", width=1, dash="dot")
|
| 261 |
+
)
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
fig.update_layout(
|
| 265 |
+
title="Surprise Timeline (Expression + Heart-rate)",
|
| 266 |
+
xaxis=dict(title="Time (s)"),
|
| 267 |
+
yaxis=dict(title="p_surprise / combined_score", side="left"),
|
| 268 |
+
yaxis2=dict(
|
| 269 |
+
title="BPM (smoothed)",
|
| 270 |
+
overlaying="y",
|
| 271 |
+
side="right"
|
| 272 |
+
),
|
| 273 |
+
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
|
| 274 |
+
shapes=shapes,
|
| 275 |
+
margin=dict(l=60, r=60, t=80, b=40)
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
return fig
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
def build_heatmap(times, combined, bins=50):
|
| 282 |
+
"""
|
| 283 |
+
μκ°μ λ°λ₯Έ combined_score heatmap μμ±.
|
| 284 |
+
"""
|
| 285 |
+
if len(times) == 0:
|
| 286 |
+
fig = go.Figure()
|
| 287 |
+
fig.update_layout(title="Surprise Intensity Heatmap (no data)")
|
| 288 |
+
return fig
|
| 289 |
+
|
| 290 |
+
t_min, t_max = float(np.min(times)), float(np.max(times))
|
| 291 |
+
if t_max <= t_min:
|
| 292 |
+
t_max = t_min + 1e-6
|
| 293 |
+
|
| 294 |
+
edges = np.linspace(t_min, t_max, bins + 1)
|
| 295 |
+
centers = (edges[:-1] + edges[1:]) / 2.0
|
| 296 |
+
|
| 297 |
+
z_row = []
|
| 298 |
+
for i in range(bins):
|
| 299 |
+
mask = (times >= edges[i]) & (times < edges[i+1])
|
| 300 |
+
if np.any(mask):
|
| 301 |
+
z_row.append(float(np.max(combined[mask])))
|
| 302 |
+
else:
|
| 303 |
+
z_row.append(0.0)
|
| 304 |
+
|
| 305 |
+
z = np.array([z_row])
|
| 306 |
+
|
| 307 |
+
fig = go.Figure(data=go.Heatmap(
|
| 308 |
+
x=centers,
|
| 309 |
+
y=["surprise"],
|
| 310 |
+
z=z,
|
| 311 |
+
colorscale="YlOrRd",
|
| 312 |
+
colorbar=dict(title="Intensity")
|
| 313 |
+
))
|
| 314 |
+
|
| 315 |
+
fig.update_layout(
|
| 316 |
+
title="Surprise Intensity Heatmap (combined_score over time)",
|
| 317 |
+
xaxis=dict(title="Time (s)"),
|
| 318 |
+
yaxis=dict(showticklabels=False),
|
| 319 |
+
margin=dict(l=60, r=60, t=80, b=40)
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
return fig
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
# =========================
|
| 326 |
+
# 6. PDF μμ± (matplotlib μ΄μ©)
|
| 327 |
+
# =========================
|
| 328 |
+
def create_pdf(session_dir, meta, top3, times, ps, bpm, combined, alpha, beta, baseline):
|
| 329 |
+
reports_dir = os.path.join(session_dir, "reports")
|
| 330 |
+
os.makedirs(reports_dir, exist_ok=True)
|
| 331 |
+
|
| 332 |
+
# matplotlib νμλΌμΈ κ·Έλ¦Ό μμ±
|
| 333 |
+
fig, axes = plt.subplots(3, 1, figsize=(7, 10), sharex=True)
|
| 334 |
+
|
| 335 |
+
axes[0].plot(times, ps, linewidth=1.2)
|
| 336 |
+
axes[0].set_ylabel("p_surprise")
|
| 337 |
+
axes[0].set_title("Surprise Probability")
|
| 338 |
+
|
| 339 |
+
axes[1].plot(times, bpm, linewidth=1.2)
|
| 340 |
+
axes[1].set_ylabel("BPM")
|
| 341 |
+
axes[1].set_title("Heart-rate (smoothed)")
|
| 342 |
+
|
| 343 |
+
axes[2].plot(times, combined, linewidth=1.2)
|
| 344 |
+
axes[2].set_ylabel("combined")
|
| 345 |
+
axes[2].set_xlabel("Time (s)")
|
| 346 |
+
axes[2].set_title("Combined Score")
|
| 347 |
+
|
| 348 |
+
for ax in axes:
|
| 349 |
+
ax.grid(True, alpha=0.3)
|
| 350 |
+
|
| 351 |
+
fig.tight_layout()
|
| 352 |
+
|
| 353 |
+
timeline_path = os.path.join(reports_dir, "timeline.png")
|
| 354 |
+
fig.savefig(timeline_path, bbox_inches="tight")
|
| 355 |
+
plt.close(fig)
|
| 356 |
+
|
| 357 |
+
# Top3 μ΄λ―Έμ§ κ²½λ‘
|
| 358 |
+
img_paths = []
|
| 359 |
+
for ev in top3:
|
| 360 |
+
frame_rel = ev.get("frame_file", "")
|
| 361 |
+
frame_abs = os.path.join(session_dir, frame_rel)
|
| 362 |
+
if os.path.exists(frame_abs):
|
| 363 |
+
img_paths.append(frame_abs)
|
| 364 |
+
else:
|
| 365 |
+
img_paths.append(None)
|
| 366 |
+
|
| 367 |
+
pdf_path = os.path.join(reports_dir, "surprise_report.pdf")
|
| 368 |
+
|
| 369 |
+
pdf = FPDF()
|
| 370 |
+
pdf.add_page()
|
| 371 |
+
|
| 372 |
+
# Title
|
| 373 |
+
pdf.set_font("Arial", "B", 16)
|
| 374 |
+
pdf.cell(0, 10, "Surprise Analyzer Report", ln=1)
|
| 375 |
+
|
| 376 |
+
# Alpha/Beta/Baseline
|
| 377 |
+
pdf.set_font("Arial", "", 11)
|
| 378 |
+
pdf.cell(0, 8, f"Alpha (expression weight): {alpha:.2f}", ln=1)
|
| 379 |
+
pdf.cell(0, 8, f"Beta (heart-rate weight): {beta:.2f}", ln=1)
|
| 380 |
+
pdf.cell(0, 8, f"Baseline BPM (median of valid BPM): {baseline:.2f}", ln=1)
|
| 381 |
+
pdf.ln(2)
|
| 382 |
+
|
| 383 |
+
# Meta μ 보
|
| 384 |
+
if meta:
|
| 385 |
+
pdf.set_font("Arial", "B", 12)
|
| 386 |
+
pdf.cell(0, 8, "Session Meta", ln=1)
|
| 387 |
+
pdf.set_font("Arial", "", 11)
|
| 388 |
+
for k, v in meta.items():
|
| 389 |
+
pdf.cell(0, 6, f"- {k}: {v}", ln=1)
|
| 390 |
+
pdf.ln(2)
|
| 391 |
+
|
| 392 |
+
# Top3 summary
|
| 393 |
+
pdf.set_font("Arial", "B", 12)
|
| 394 |
+
pdf.cell(0, 8, "Top 3 Surprise Moments", ln=1)
|
| 395 |
+
pdf.set_font("Arial", "", 11)
|
| 396 |
+
if len(top3) == 0:
|
| 397 |
+
pdf.cell(0, 6, "No surprise events detected.", ln=1)
|
| 398 |
+
else:
|
| 399 |
+
for i, ev in enumerate(top3):
|
| 400 |
+
t = ev["time"]
|
| 401 |
+
ps_val = ev["p_surprise"]
|
| 402 |
+
bpm_val = ev["bpm"]
|
| 403 |
+
cs_val = ev["combined_score"]
|
| 404 |
+
pdf.multi_cell(
|
| 405 |
+
0, 6,
|
| 406 |
+
f"#{i+1} time = {t:.2f}s, "
|
| 407 |
+
f"p_surprise = {ps_val:.4f}, "
|
| 408 |
+
f"BPM = {bpm_val if bpm_val is not None else 'None'}, "
|
| 409 |
+
f"combined = {cs_val:.4f}"
|
| 410 |
+
)
|
| 411 |
+
pdf.ln(2)
|
| 412 |
+
|
| 413 |
+
# Timeline image
|
| 414 |
+
pdf.set_font("Arial", "B", 12)
|
| 415 |
+
pdf.cell(0, 8, "Surprise Timeline", ln=1)
|
| 416 |
+
pdf.image(timeline_path, w=180)
|
| 417 |
+
pdf.ln(4)
|
| 418 |
+
|
| 419 |
+
# Top3 images
|
| 420 |
+
pdf.set_font("Arial", "B", 12)
|
| 421 |
+
pdf.cell(0, 8, "Top 3 Frames", ln=1)
|
| 422 |
+
pdf.set_font("Arial", "", 11)
|
| 423 |
+
|
| 424 |
+
for i, p in enumerate(img_paths):
|
| 425 |
+
if p is None:
|
| 426 |
+
continue
|
| 427 |
+
pdf.cell(0, 6, f"Top {i+1}", ln=1)
|
| 428 |
+
pdf.image(p, w=80)
|
| 429 |
+
pdf.ln(3)
|
| 430 |
+
|
| 431 |
+
pdf.output(pdf_path)
|
| 432 |
+
return pdf_path
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
# =========================
|
| 436 |
+
# 7. Gradio λ©μΈ μ²λ¦¬ ν¨μ
|
| 437 |
+
# =========================
|
| 438 |
+
def process(zip_file, alpha):
|
| 439 |
+
"""
|
| 440 |
+
Gradioμμ νΈμΆλλ λ©μΈ ν¨μ.
|
| 441 |
+
zip_file: μ
λ‘λλ ZIP νμΌ κ²½λ‘
|
| 442 |
+
alpha: μ¬λΌμ΄λλ‘ λ°μ Ξ±κ°
|
| 443 |
+
"""
|
| 444 |
+
if zip_file is None:
|
| 445 |
+
return (
|
| 446 |
+
"Please upload a session ZIP created by collector.py.",
|
| 447 |
+
None, None, None,
|
| 448 |
+
None, None,
|
| 449 |
+
None
|
| 450 |
+
)
|
| 451 |
+
|
| 452 |
+
beta = 1.0 - alpha
|
| 453 |
+
|
| 454 |
+
# 1) μΈμ
λ‘λ
|
| 455 |
+
samples, events, meta, session_dir = load_session(zip_file)
|
| 456 |
+
|
| 457 |
+
# 2) νμλΌμΈ λΆμ (BPM smoothing + combined)
|
| 458 |
+
times, ps, bpm_smooth, combined, baseline = analyze_timeline(samples, alpha, ma_window=5)
|
| 459 |
+
|
| 460 |
+
# 3) BPM κΈμμΉ κ΅¬κ° νμ§
|
| 461 |
+
spike_times = detect_bpm_spikes(times, bpm_smooth, baseline, spike_delta=10.0, min_gap_sec=1.0)
|
| 462 |
+
|
| 463 |
+
# 4) Top3 μ΄λ²€νΈ μ μ (μ΅μ 1μ΄ κ°κ²©)
|
| 464 |
+
top3 = pick_top3_events(events, times, combined, min_gap_sec=1.0)
|
| 465 |
+
|
| 466 |
+
# 5) Plotly νμλΌμΈ + Heatmap μμ±
|
| 467 |
+
timeline_fig = build_timeline_plot(times, ps, bpm_smooth, combined, top3, spike_times)
|
| 468 |
+
heatmap_fig = build_heatmap(times, combined, bins=50)
|
| 469 |
+
|
| 470 |
+
# 6) PDF μμ±
|
| 471 |
+
pdf_path = create_pdf(session_dir, meta, top3, times, ps, bpm_smooth, combined, alpha, beta, baseline)
|
| 472 |
+
|
| 473 |
+
# 7) Top3 μ΄λ―Έμ§ λ‘λ©
|
| 474 |
+
img1 = img2 = img3 = None
|
| 475 |
+
if len(top3) >= 1:
|
| 476 |
+
p1 = os.path.join(session_dir, top3[0]["frame_file"])
|
| 477 |
+
if os.path.exists(p1):
|
| 478 |
+
img1 = Image.open(p1)
|
| 479 |
+
if len(top3) >= 2:
|
| 480 |
+
p2 = os.path.join(session_dir, top3[1]["frame_file"])
|
| 481 |
+
if os.path.exists(p2):
|
| 482 |
+
img2 = Image.open(p2)
|
| 483 |
+
if len(top3) >= 3:
|
| 484 |
+
p3 = os.path.join(session_dir, top3[2]["frame_file"])
|
| 485 |
+
if os.path.exists(p3):
|
| 486 |
+
img3 = Image.open(p3)
|
| 487 |
+
|
| 488 |
+
# 8) Summary ν
μ€νΈ
|
| 489 |
+
lines = [
|
| 490 |
+
f"Alpha (expression weight) = {alpha:.2f}",
|
| 491 |
+
f"Beta (heart-rate weight) = {beta:.2f}",
|
| 492 |
+
f"Baseline BPM (median of valid BPM) = {baseline:.2f}",
|
| 493 |
+
f"Number of samples = {len(samples)}",
|
| 494 |
+
f"Number of raw events (expression-based) = {len(events)}",
|
| 495 |
+
f"Number of detected BPM spikes = {len(spike_times)}",
|
| 496 |
+
""
|
| 497 |
+
]
|
| 498 |
+
|
| 499 |
+
if spike_times:
|
| 500 |
+
lines.append("BPM spike times (s):")
|
| 501 |
+
lines.append(", ".join([f"{t:.2f}" for t in spike_times]))
|
| 502 |
+
lines.append("")
|
| 503 |
+
|
| 504 |
+
if len(top3) == 0:
|
| 505 |
+
lines.append("No surprise events detected above the current settings.")
|
| 506 |
+
else:
|
| 507 |
+
lines.append("Top 3 surprise moments (time, p_surprise, BPM, combined_score):")
|
| 508 |
+
for i, ev in enumerate(top3):
|
| 509 |
+
t = ev["time"]
|
| 510 |
+
ps_val = ev["p_surprise"]
|
| 511 |
+
bpm_val = ev["bpm"]
|
| 512 |
+
cs_val = ev["combined_score"]
|
| 513 |
+
lines.append(
|
| 514 |
+
f"#{i+1} time = {t:.2f}s, "
|
| 515 |
+
f"p_surprise = {ps_val:.4f}, "
|
| 516 |
+
f"BPM = {bpm_val if bpm_val is not None else 'None'}, "
|
| 517 |
+
f"combined_score = {cs_val:.4f}"
|
| 518 |
+
)
|
| 519 |
+
|
| 520 |
+
summary = "\n".join(lines)
|
| 521 |
+
|
| 522 |
+
return summary, img1, img2, img3, timeline_fig, heatmap_fig, pdf_path
|
| 523 |
+
|
| 524 |
+
|
| 525 |
+
# =========================
|
| 526 |
+
# 8. Gradio UI
|
| 527 |
+
# =========================
|
| 528 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")) as demo:
|
| 529 |
+
gr.Markdown(
|
| 530 |
+
"""
|
| 531 |
+
# π Surprise Analyzer (Expression + Heart-rate, v2)
|
| 532 |
+
|
| 533 |
+
μ΄ μΉμ±μ **λ‘컬 μμ§κΈ°(collector.py)** λ‘ λ
Ήνν μΈμ
ZIPμ μ
λ‘λνλ©΄,
|
| 534 |
+
νμ κΈ°λ° λλ νλ₯ (`p_surprise`)κ³Ό μ¬λ°(`BPM`)μ κ²°ν©ν΄μ **Top 3 λλ μκ°**μ μ°Ύμμ€λλ€.
|
| 535 |
+
|
| 536 |
+
### π μ 체 νμ΄νλΌμΈ
|
| 537 |
+
1. λ‘컬μμ μΉμΊ + μλμ΄λ
Έ μ¬λ° μΌμλ‘ λ°μ΄ν° μμ§ (`collector.py` μ€ν)
|
| 538 |
+
2. μμ±λ `session_YYYYMMDD_XXXXXX.zip` νμΌμ μ΄κ³³μ μ
λ‘λ
|
| 539 |
+
3. μλ μ¬λΌμ΄λλ‘ **νμ vs μ¬λ° κ°μ€μΉ(Ξ±, Ξ²)** μ‘°μ
|
| 540 |
+
4. **νμλΌμΈ κ·Έλν + Heatmap + Top3 μ₯λ©΄ + PDF 리ν¬νΈ** νμΈ
|
| 541 |
+
|
| 542 |
+
- β
BPM κ·Έλνμ moving average smoothing μ μ©
|
| 543 |
+
- β
p_surprise / BPM / combined_scoreλ₯Ό **νλμ Plotly νμλΌμΈ**μ νμ
|
| 544 |
+
- β
Top3λ μλ‘ **1μ΄ μ΄μ κ°κ²©** μ μ§νλλ‘ λ³΄μ
|
| 545 |
+
- β
combined_score κΈ°λ° **λλ κ°λ Heatmap** μ 곡
|
| 546 |
+
- β
**μ¬λ° κΈμμΉ κ΅¬κ° μλ νμ§** ν νμλΌμΈμ νμ
|
| 547 |
+
---
|
| 548 |
+
"""
|
| 549 |
+
)
|
| 550 |
+
|
| 551 |
+
with gr.Row():
|
| 552 |
+
with gr.Column(scale=1):
|
| 553 |
+
zip_input = gr.File(
|
| 554 |
+
label="π Upload session ZIP (collector.py output)",
|
| 555 |
+
type="filepath"
|
| 556 |
+
)
|
| 557 |
+
alpha_slider = gr.Slider(
|
| 558 |
+
minimum=0.0,
|
| 559 |
+
maximum=1.0,
|
| 560 |
+
value=0.6,
|
| 561 |
+
step=0.05,
|
| 562 |
+
label="Ξ±: Expression weight (Ξ² = 1 β Ξ±, heart-rate weight)"
|
| 563 |
+
)
|
| 564 |
+
analyze_btn = gr.Button("π Run Analysis", variant="primary")
|
| 565 |
+
|
| 566 |
+
with gr.Column(scale=2):
|
| 567 |
+
summary_box = gr.Textbox(
|
| 568 |
+
label="π Analysis Summary",
|
| 569 |
+
lines=14
|
| 570 |
+
)
|
| 571 |
+
|
| 572 |
+
gr.Markdown("### π Top 3 Surprise Frames")
|
| 573 |
+
|
| 574 |
+
with gr.Row():
|
| 575 |
+
img1 = gr.Image(label="Top 1")
|
| 576 |
+
img2 = gr.Image(label="Top 2")
|
| 577 |
+
img3 = gr.Image(label="Top 3")
|
| 578 |
+
|
| 579 |
+
gr.Markdown("### π Timelines & Heatmap")
|
| 580 |
+
|
| 581 |
+
timeline_plot = gr.Plot(label="Surprise / BPM / Combined Timeline")
|
| 582 |
+
heatmap_plot = gr.Plot(label="Surprise Intensity Heatmap")
|
| 583 |
+
|
| 584 |
+
gr.Markdown("### π₯ Download Report (PDF)")
|
| 585 |
+
|
| 586 |
+
pdf_file = gr.File(label="Download PDF Report")
|
| 587 |
+
|
| 588 |
+
analyze_btn.click(
|
| 589 |
+
fn=process,
|
| 590 |
+
inputs=[zip_input, alpha_slider],
|
| 591 |
+
outputs=[summary_box, img1, img2, img3, timeline_plot, heatmap_plot, pdf_file]
|
| 592 |
+
)
|
| 593 |
+
|
| 594 |
+
if __name__ == "__main__":
|
| 595 |
+
demo.launch()
|