Claude commited on
Commit
09ba151
·
1 Parent(s): 5edd431

Sync OCR text with the picked frame in the review editor

Browse files

Previously the on-screen-text panel kept showing OCR from the original
primary frame even after the reviewer switched to an alternate, so the
text and the visible slide could disagree.

The orchestrator now OCRs every alt frame alongside the primary; the
results travel through Segment / SegmentMetadata so they survive a
save/reload round-trip; each frame-picker button carries its own
data-frame-ocr; and the click handler swaps the [data-bind="ocr_text"]
panel content. The internal applyEditsToDom() call path is unchanged
(it doesn't go through the user-click handler), so restoring saved
state still keeps whatever ocr_text was persisted.

app/pipeline/html_gen.py CHANGED
@@ -34,6 +34,10 @@ class Segment:
34
  # scenes.detect_and_extract sampled multiple candidates.
35
  alt_image_filenames: list[str] = field(default_factory=list)
36
  alt_image_data_uris: list[str] = field(default_factory=list)
 
 
 
 
37
  # Optional sliced narration audio for this segment.
38
  audio_filename: str = ""
39
  audio_data_uri: str | None = None
@@ -85,6 +89,7 @@ def build_segments(
85
  narrations: list[list[str]],
86
  ocr_texts: list[str],
87
  inline_images: bool,
 
88
  ) -> list[Segment]:
89
  segments: list[Segment] = []
90
  for i, frame in enumerate(frames):
@@ -93,6 +98,13 @@ def build_segments(
93
  if isinstance(narration, str):
94
  narration = [narration] if narration else []
95
  alt_paths = list(getattr(frame, "alt_image_paths", []) or [])
 
 
 
 
 
 
 
96
  segments.append(
97
  Segment(
98
  index=i,
@@ -106,6 +118,7 @@ def build_segments(
106
  section_title=_default_section_title(i, frame.start_seconds),
107
  alt_image_filenames=[p.name for p in alt_paths],
108
  alt_image_data_uris=[_image_data_uri(p) for p in alt_paths] if inline_images else [],
 
109
  )
110
  )
111
  return segments
@@ -262,6 +275,7 @@ def metadata_to_segment(seg_meta: Any, frames_dir: Path, *, inline_images: bool)
262
  alt_image_data_uris=(
263
  [_image_data_uri(p) for p in alt_paths if p.exists()] if inline_images else []
264
  ),
 
265
  audio_filename=audio_filename,
266
  audio_data_uri=(
267
  _audio_data_uri(audio_path) if (inline_images and audio_path is not None) else None
 
34
  # scenes.detect_and_extract sampled multiple candidates.
35
  alt_image_filenames: list[str] = field(default_factory=list)
36
  alt_image_data_uris: list[str] = field(default_factory=list)
37
+ # OCR text for each alt frame, in the same order as alt_image_filenames.
38
+ # Lets the review editor swap the on-screen-text panel when the reviewer
39
+ # picks a different frame.
40
+ alt_ocr_texts: list[str] = field(default_factory=list)
41
  # Optional sliced narration audio for this segment.
42
  audio_filename: str = ""
43
  audio_data_uri: str | None = None
 
89
  narrations: list[list[str]],
90
  ocr_texts: list[str],
91
  inline_images: bool,
92
+ alt_ocr_texts: list[list[str]] | None = None,
93
  ) -> list[Segment]:
94
  segments: list[Segment] = []
95
  for i, frame in enumerate(frames):
 
98
  if isinstance(narration, str):
99
  narration = [narration] if narration else []
100
  alt_paths = list(getattr(frame, "alt_image_paths", []) or [])
101
+ per_alt_ocr = (
102
+ alt_ocr_texts[i] if alt_ocr_texts and i < len(alt_ocr_texts) else []
103
+ )
104
+ # Pad / truncate so the OCR list always lines up with the alt paths,
105
+ # even if a caller passed a shorter list.
106
+ per_alt_ocr = list(per_alt_ocr) + [""] * max(0, len(alt_paths) - len(per_alt_ocr))
107
+ per_alt_ocr = per_alt_ocr[: len(alt_paths)]
108
  segments.append(
109
  Segment(
110
  index=i,
 
118
  section_title=_default_section_title(i, frame.start_seconds),
119
  alt_image_filenames=[p.name for p in alt_paths],
120
  alt_image_data_uris=[_image_data_uri(p) for p in alt_paths] if inline_images else [],
121
+ alt_ocr_texts=per_alt_ocr,
122
  )
123
  )
124
  return segments
 
275
  alt_image_data_uris=(
276
  [_image_data_uri(p) for p in alt_paths if p.exists()] if inline_images else []
277
  ),
278
+ alt_ocr_texts=list(getattr(seg_meta, "alt_ocr_texts", []) or []),
279
  audio_filename=audio_filename,
280
  audio_data_uri=(
281
  _audio_data_uri(audio_path) if (inline_images and audio_path is not None) else None
app/pipeline/metadata.py CHANGED
@@ -43,6 +43,10 @@ class SegmentMetadata(BaseModel):
43
  # Additional candidate frames the reviewer can switch to; the active
44
  # choice is whatever's set as `image_filename`.
45
  alt_image_filenames: list[str] = Field(default_factory=list)
 
 
 
 
46
  # Sliced narration audio filename (sibling 'audio/' folder to frames_dir).
47
  audio_filename: str = ""
48
  # True once a human or LLM has authored real alt text. Gates the page
@@ -102,6 +106,7 @@ def migrate_v1_to_v2(raw: dict[str, Any]) -> dict[str, Any]:
102
  s.setdefault("math_equations", [])
103
  s.setdefault("include", True)
104
  s.setdefault("alt_image_filenames", [])
 
105
  s.setdefault("audio_filename", "")
106
  s.setdefault("alt_text_reviewed", False)
107
  narration = s.get("narration", [])
@@ -145,6 +150,7 @@ def segment_to_metadata(segment: Any) -> SegmentMetadata:
145
  ],
146
  include=segment.include,
147
  alt_image_filenames=list(getattr(segment, "alt_image_filenames", []) or []),
 
148
  audio_filename=getattr(segment, "audio_filename", "") or "",
149
  alt_text_reviewed=bool(getattr(segment, "alt_text_reviewed", False)),
150
  )
 
43
  # Additional candidate frames the reviewer can switch to; the active
44
  # choice is whatever's set as `image_filename`.
45
  alt_image_filenames: list[str] = Field(default_factory=list)
46
+ # OCR text per alt frame, parallel to alt_image_filenames. Survives a
47
+ # save/reload so the editor can swap the on-screen-text panel when the
48
+ # reviewer picks a different frame.
49
+ alt_ocr_texts: list[str] = Field(default_factory=list)
50
  # Sliced narration audio filename (sibling 'audio/' folder to frames_dir).
51
  audio_filename: str = ""
52
  # True once a human or LLM has authored real alt text. Gates the page
 
106
  s.setdefault("math_equations", [])
107
  s.setdefault("include", True)
108
  s.setdefault("alt_image_filenames", [])
109
+ s.setdefault("alt_ocr_texts", [])
110
  s.setdefault("audio_filename", "")
111
  s.setdefault("alt_text_reviewed", False)
112
  narration = s.get("narration", [])
 
150
  ],
151
  include=segment.include,
152
  alt_image_filenames=list(getattr(segment, "alt_image_filenames", []) or []),
153
+ alt_ocr_texts=list(getattr(segment, "alt_ocr_texts", []) or []),
154
  audio_filename=getattr(segment, "audio_filename", "") or "",
155
  alt_text_reviewed=bool(getattr(segment, "alt_text_reviewed", False)),
156
  )
app/pipeline/orchestrator.py CHANGED
@@ -140,13 +140,18 @@ def run_pipeline(
140
  log.info("Skipping OCR (--skip-ocr)")
141
  _emit(95, "ocr", "Skipping OCR.")
142
  ocr_texts = ["" for _ in kept]
 
143
  else:
144
  n = len(kept)
145
  log.info("Running OCR on %d frames", n)
146
  _emit(65, "ocr", f"Running OCR on {n} frames...")
147
  ocr_texts: list[str] = []
 
148
  for i, f in enumerate(kept):
149
  ocr_texts.append(ocr_frame(f.image_path))
 
 
 
150
  pct = 65 + ((i + 1) / max(n, 1)) * 30
151
  _emit(pct, "ocr", f"OCR frame {i + 1}/{n}")
152
 
@@ -192,7 +197,11 @@ def run_pipeline(
192
  log.info("Sliced %d/%d narration audio clips", len(audio_paths), len(kept))
193
 
194
  _emit(96, "render", "Building segments...")
195
- segments = build_segments(kept, narrations, ocr_texts, inline_images=inputs.inline_images)
 
 
 
 
196
  if extractions is not None:
197
  for seg, extracted in zip(segments, extractions):
198
  if inputs.llm_toggles.key_terms:
 
140
  log.info("Skipping OCR (--skip-ocr)")
141
  _emit(95, "ocr", "Skipping OCR.")
142
  ocr_texts = ["" for _ in kept]
143
+ alt_ocr_texts: list[list[str]] = [[] for _ in kept]
144
  else:
145
  n = len(kept)
146
  log.info("Running OCR on %d frames", n)
147
  _emit(65, "ocr", f"Running OCR on {n} frames...")
148
  ocr_texts: list[str] = []
149
+ alt_ocr_texts: list[list[str]] = []
150
  for i, f in enumerate(kept):
151
  ocr_texts.append(ocr_frame(f.image_path))
152
+ # Reviewers can swap to an alternate frame in the editor; OCR
153
+ # the alts up front so the on-screen-text panel updates instantly.
154
+ alt_ocr_texts.append([ocr_frame(p) for p in f.alt_image_paths])
155
  pct = 65 + ((i + 1) / max(n, 1)) * 30
156
  _emit(pct, "ocr", f"OCR frame {i + 1}/{n}")
157
 
 
197
  log.info("Sliced %d/%d narration audio clips", len(audio_paths), len(kept))
198
 
199
  _emit(96, "render", "Building segments...")
200
+ segments = build_segments(
201
+ kept, narrations, ocr_texts,
202
+ inline_images=inputs.inline_images,
203
+ alt_ocr_texts=alt_ocr_texts,
204
+ )
205
  if extractions is not None:
206
  for seg, extracted in zip(segments, extractions):
207
  if inputs.llm_toggles.key_terms:
app/templates/review.html CHANGED
@@ -318,16 +318,19 @@
318
  <button type="button" class="vgm-frame-picker__btn"
319
  aria-pressed="true"
320
  data-frame-uri="{{ seg.image_data_uri }}"
321
- data-frame-filename="{{ seg.image_filename }}">
 
322
  <img src="{{ seg.image_data_uri }}" alt="" loading="lazy">
323
  </button>
324
  {% for fn in seg.alt_image_filenames %}
325
  {% set inlined = seg.alt_image_data_uris[loop.index0] if seg.alt_image_data_uris else None %}
326
  {% set src = inlined if inlined else alts_url_template.replace('{filename}', fn) %}
 
327
  <button type="button" class="vgm-frame-picker__btn"
328
  aria-pressed="false"
329
  {% if inlined %}data-frame-uri="{{ inlined }}"{% else %}data-frame-url="{{ src }}"{% endif %}
330
- data-frame-filename="{{ fn }}">
 
331
  <img src="{{ src }}" alt="" loading="lazy">
332
  </button>
333
  {% endfor %}
@@ -694,6 +697,13 @@
694
  const btn = e.target.closest('.vgm-frame-picker__btn');
695
  if (!btn || !picker.contains(btn)) return;
696
  selectFrameButton(section, btn);
 
 
 
 
 
 
 
697
  });
698
  }
699
 
 
318
  <button type="button" class="vgm-frame-picker__btn"
319
  aria-pressed="true"
320
  data-frame-uri="{{ seg.image_data_uri }}"
321
+ data-frame-filename="{{ seg.image_filename }}"
322
+ data-frame-ocr="{{ seg.ocr_text }}">
323
  <img src="{{ seg.image_data_uri }}" alt="" loading="lazy">
324
  </button>
325
  {% for fn in seg.alt_image_filenames %}
326
  {% set inlined = seg.alt_image_data_uris[loop.index0] if seg.alt_image_data_uris else None %}
327
  {% set src = inlined if inlined else alts_url_template.replace('{filename}', fn) %}
328
+ {% set alt_ocr = seg.alt_ocr_texts[loop.index0] if seg.alt_ocr_texts and loop.index0 < seg.alt_ocr_texts|length else '' %}
329
  <button type="button" class="vgm-frame-picker__btn"
330
  aria-pressed="false"
331
  {% if inlined %}data-frame-uri="{{ inlined }}"{% else %}data-frame-url="{{ src }}"{% endif %}
332
+ data-frame-filename="{{ fn }}"
333
+ data-frame-ocr="{{ alt_ocr }}">
334
  <img src="{{ src }}" alt="" loading="lazy">
335
  </button>
336
  {% endfor %}
 
697
  const btn = e.target.closest('.vgm-frame-picker__btn');
698
  if (!btn || !picker.contains(btn)) return;
699
  selectFrameButton(section, btn);
700
+ // OCR for the previous frame is no longer accurate once a different
701
+ // frame is chosen. Swap in the new frame's OCR, but only on a real
702
+ // user click — applyEditsToDom() invokes selectFrameButton() during
703
+ // restore, and we don't want it clobbering the saved ocr_text it
704
+ // just wrote one line earlier.
705
+ const ocrEl = section.querySelector('[data-bind="ocr_text"]');
706
+ if (ocrEl) ocrEl.textContent = btn.getAttribute('data-frame-ocr') || '';
707
  });
708
  }
709