Spaces:
Sleeping
Sleeping
Claude commited on
Commit ·
b3c6e2c
1
Parent(s): 410a4b8
Default to latest-in-time frame as the primary slide candidate
Browse filesAmong the 3 brightness-rank survivors from the second half of each
scene, promote the latest-in-time as the primary instead of the
brightest. Reviewers almost always want the fully-populated final
state of progressive-reveal slides; the brightest sample is often a
mid-scene animation flash. The peak still ships as an alternate so
the reviewer can switch back if they prefer it.
Files changed (2)
87 unmodified lines8889909192939495969798997 unmodified lines1071081091101111121134 unmodified lines118119120121122123124125126127128129130131132133134135160 unmodified lines29629729829930030130230330487 unmodified linesnum_candidates: int = 5,num_keep: int = 3,) -> list[Path]:"""Save up to `num_keep` candidate frames from the scene, brightest first.Sampling the last half of the scene biases toward the populated state ofprogressive-reveal slides; ranking by brightness skips dark transitionsand dim instructor cutaways. Returning multiple candidates lets thereviewer pick the best-looking frame in the editor."""duration = max(end_s - start_s, 0.0)if duration <= 0:7 unmodified linestimestamps = [t_start + (t_end - t_start) * (i / (n - 1)) for i in range(n)]cap = cv2.VideoCapture(str(video_path))scored: list[tuple[float, "np.ndarray"]] = []try:for t in timestamps:cap.set(cv2.CAP_PROP_POS_MSEC, max(t, 0) * 1000.0)4 unmodified lines# transient bright frame (e.g. instructor cutaway, fade flash);# we want the score to reflect post-correction visual quality.normalized = _normalize_brightness(frame)scored.append((_brightness_score(normalized), normalized))finally:cap.release()if not scored:return []scored.sort(key=lambda x: x[0], reverse=True)keep = scored[: max(1, num_keep)]saved: list[Path] = []out_dir.mkdir(parents=True, exist_ok=True)for i, (_, frame) in enumerate(keep):suffix = "" if i == 0 else f"_alt{i}"dest = out_dir / f"scene_{scene_index:04d}{suffix}.jpg"if cv2.imwrite(str(dest), frame):160 unmodified linesframes: list[SceneFrame] = []for i, (start_s, end_s) in enumerate(boundaries):# Save the top 3 brightest candidates from the second half of the# scene; the brightest becomes the primary, the next two are# alternates the reviewer can switch to in the editor.paths = _save_top_candidate_frames(video_path, start_s, end_s, out_dir, i)if paths:frames.append(87 unmodified lines8889909192939495969798991007 unmodified lines1081091101111121131144 unmodified lines119120121122123124125126127128129130131132133134135136137138139140141160 unmodified lines30230330430530630730830931087 unmodified linesnum_candidates: int = 5,num_keep: int = 3,) -> list[Path]:"""Save up to `num_keep` candidate frames from the scene, latest first.Sampling the last half of the scene biases toward the populated state ofprogressive-reveal slides; ranking by brightness skips dark transitionsand dim instructor cutaways. Among the brightness survivors we then pickthe latest-in-time as the primary, since the reviewer almost alwayswants the fully-populated final state of the slide."""duration = max(end_s - start_s, 0.0)if duration <= 0:7 unmodified linestimestamps = [t_start + (t_end - t_start) * (i / (n - 1)) for i in range(n)]cap = cv2.VideoCapture(str(video_path))scored: list[tuple[float, float, "np.ndarray"]] = []try:for t in timestamps:cap.set(cv2.CAP_PROP_POS_MSEC, max(t, 0) * 1000.0)4 unmodified lines# transient bright frame (e.g. instructor cutaway, fade flash);# we want the score to reflect post-correction visual quality.normalized = _normalize_brightness(frame)scored.append((_brightness_score(normalized), t, normalized))finally:cap.release()if not scored:return []# Brightness ranking filters out dark transitions and dim cutaways…scored.sort(key=lambda x: x[0], reverse=True)keep = scored[: max(1, num_keep)]# …but among the survivors, promote the latest-in-time as the primary.# Progressive-reveal slides reach their fully-populated state at the# end of the scene, and that's almost always what the reviewer wants.keep.sort(key=lambda x: x[1], reverse=True)saved: list[Path] = []out_dir.mkdir(parents=True, exist_ok=True)for i, (_, _, frame) in enumerate(keep):suffix = "" if i == 0 else f"_alt{i}"dest = out_dir / f"scene_{scene_index:04d}{suffix}.jpg"if cv2.imwrite(str(dest), frame):160 unmodified linesframes: list[SceneFrame] = []for i, (start_s, end_s) in enumerate(boundaries):# Save the top 3 brightness-survivors from the second half of the# scene, with the latest-in-time as the primary and the earlier# ones as alternates the reviewer can switch to in the editor.paths = _save_top_candidate_frames(video_path, start_s, end_s, out_dir, i)if paths:frames.append(
6 unmodified lines789101112240 unmodified lines2532542556 unmodified lines_cap_max_frames,_enforce_min_gap,_merge_duplicate_adjacent,)from app.pipeline.types import SceneFrame240 unmodified linesassert ap.exists()else:assert not ap.exists(), f"orphaned alt: {ap.name}"6 unmodified lines78910111213240 unmodified lines2542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383396 unmodified lines_cap_max_frames,_enforce_min_gap,_merge_duplicate_adjacent,_save_top_candidate_frames,)from app.pipeline.types import SceneFrame240 unmodified linesassert ap.exists()else:assert not ap.exists(), f"orphaned alt: {ap.name}"def _write_brightness_video(path: Path, duration_s: float, fps: int, brightness_at):"""Write a tiny video where each frame is a uniform gray of the valuereturned by `brightness_at(t_seconds)` (0..255). Used to control whichsampling timestamps inside _save_top_candidate_frames map to whichbrightness so we can verify candidate ordering deterministically."""import cv2 as _cv2fourcc = _cv2.VideoWriter_fourcc(*"mp4v")w, h = 64, 36writer = _cv2.VideoWriter(str(path), fourcc, fps, (w, h))try:n = int(duration_s * fps)for i in range(n):t = i / fpsv = int(max(0, min(255, brightness_at(t))))frame = np.full((h, w, 3), v, dtype=np.uint8)writer.write(frame)finally:writer.release()def test_save_top_candidate_frames_promotes_latest_among_brightness_survivors(tmp_path):"""The reviewer almost always wants the fully-populated final state ofa progressive-reveal slide. The candidate picker filters darktransitions by brightness rank, but among the survivors the primarymust be the latest-in-time, not the brightest. Without that, amid-scene peak (e.g. a brief animation flash) becomes the default."""video_path = tmp_path / "scene.mp4"# Sampling timestamps for start=0,end=10 are 5.0, 6.125, 7.25, 8.375, 9.5.# Brightness profile:# t=5.0 -> 60 (transition; drops out of top 3)# t=6.125 -> 220 (brightest peak — would be the old default primary)# t=7.25 -> 200# t=8.375 -> 195# t=9.5 -> 190# Top 3 by brightness: 220, 200, 195 -> timestamps 6.125, 7.25, 8.375.# Time-sort descending: 8.375, 7.25, 6.125 -> primary = t=8.375.def brightness_at(t):if t < 5.5:return 60if t < 6.5:return 220if t < 7.75:return 200if t < 9.0:return 195return 190_write_brightness_video(video_path, duration_s=10.0, fps=30, brightness_at=brightness_at)out_dir = tmp_path / "frames"paths = _save_top_candidate_frames(video_path, start_s=0.0, end_s=10.0, out_dir=out_dir, scene_index=0)assert len(paths) == 3, pathsimport cv2 as _cv2def mean_brightness(p):img = _cv2.imread(str(p))return float(_cv2.cvtColor(img, _cv2.COLOR_BGR2GRAY).mean())primary_b = mean_brightness(paths[0])# Primary must be the latest-time survivor (~195), NOT the brightness# peak (~220) that the old logic would have promoted.assert primary_b < 210, (f"primary should be the latest-in-time survivor (~195), not the "f"mid-scene brightness peak (~220); got {primary_b:.1f}")assert primary_b > 180, (f"primary should still be a brightness-survivor (>180), not the "f"dark transition frame (~60); got {primary_b:.1f}")# The peak should be retained as one of the alternates so the reviewer# can still pick it if they prefer that frame.alt_brights = sorted(mean_brightness(p) for p in paths[1:])assert alt_brights[-1] >= 215, (f"brightness peak should be kept as an alternate; got {alt_brights}")
app/pipeline/scenes.py+15-9
tests/test_scenes.py+84