File size: 20,611 Bytes
ce847d4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
"""Smart OCR deduplication — multi-layer heuristic to avoid re-reading the same text.
Architecture (3 layers):
Layer 1 — **Per-Region Line Tracker**
Each capture region keeps a dict of known OCR lines (normalized text → metadata).
New OCR results are compared line-by-line; only genuinely new lines pass through.
Stale entries expire after ``line_ttl`` seconds.
Layer 2 — **Global Text History** (ring buffer)
After composing new lines into a text block, the block is fuzzy-matched against
a bounded history of recently emitted texts. TTL-based expiry allows the same
dialog to be read again after a configurable cooldown.
Layer 3 — **Semantic Change Detector**
Rejects composed text that is too short, has too few real words, or is mostly
non-alphanumeric (OCR garbage / UI artifacts).
Debounce (optional)
When text grows incrementally (typewriter effect), the emitter waits for
stabilization before yielding the final text.
Usage::
from src.services.ocr.dedup import SmartDedup
dedup = SmartDedup()
text = dedup.process(regions, ocr_results)
if text is not None:
translate_and_speak(text)
"""
from __future__ import annotations
import time
from collections import deque
from dataclasses import dataclass
from difflib import SequenceMatcher
from src.services.ocr.models import OcrResult
from src.utils.logger import logger
# ── Constants (sensible defaults) ────────────────────────────────
DEFAULT_LINE_TTL: float = 120.0
DEFAULT_LINE_SIMILARITY: float = 0.80
DEFAULT_HISTORY_SIZE: int = 30
DEFAULT_HISTORY_TTL: float = 90.0
DEFAULT_HISTORY_SIMILARITY: float = 0.82
DEFAULT_MIN_NEW_CHARS: int = 8
DEFAULT_MIN_NEW_WORDS: int = 2
DEFAULT_MIN_ALNUM_RATIO: float = 0.35
DEFAULT_DEBOUNCE_TIME: float = 0.0 # 0 = disabled
# ── Data classes ─────────────────────────────────────────────────
@dataclass
class KnownLine:
"""A line previously seen by a RegionLineTracker."""
text: str
first_seen: float
last_seen: float
hit_count: int = 1
@dataclass
class HistoryEntry:
"""An entry in the global text history ring buffer."""
norm_text: str
original_text: str
first_seen: float
last_seen: float
hit_count: int = 1
@dataclass
class DedupConfig:
"""All tunable knobs for the dedup system.
Attributes:
line_ttl: Seconds before a known line expires (Layer 1).
line_similarity: Fuzzy threshold for line-level dedup (0-1).
history_size: Max entries in global ring buffer (Layer 2).
history_ttl: Seconds before a global history entry expires.
history_similarity: Fuzzy threshold for global dedup (0-1).
min_new_chars: Minimum characters for a change to be significant (Layer 3).
min_new_words: Minimum word count for significance.
min_alnum_ratio: Minimum alphanumeric ratio for significance.
debounce_time: Seconds to wait for text stabilization (0 = off).
"""
line_ttl: float = DEFAULT_LINE_TTL
line_similarity: float = DEFAULT_LINE_SIMILARITY
history_size: int = DEFAULT_HISTORY_SIZE
history_ttl: float = DEFAULT_HISTORY_TTL
history_similarity: float = DEFAULT_HISTORY_SIMILARITY
min_new_chars: int = DEFAULT_MIN_NEW_CHARS
min_new_words: int = DEFAULT_MIN_NEW_WORDS
min_alnum_ratio: float = DEFAULT_MIN_ALNUM_RATIO
debounce_time: float = DEFAULT_DEBOUNCE_TIME
# ── Helpers ──────────────────────────────────────────────────────
def _normalize(text: str) -> str:
"""Collapse whitespace, strip, lowercase — for comparison only."""
return " ".join(text.split()).strip().lower()
# ── Layer 1: Per-Region Line Tracker ─────────────────────────────
class RegionLineTracker:
"""Track known lines for a single capture region.
Lines already seen (exact or fuzzy match) are filtered out.
Entries expire after ``line_ttl`` seconds so the same text
can be re-read after a cooldown.
"""
def __init__(
self,
similarity: float = DEFAULT_LINE_SIMILARITY,
line_ttl: float = DEFAULT_LINE_TTL,
) -> None:
self._known: dict[str, KnownLine] = {}
self._similarity = similarity
self._line_ttl = line_ttl
def extract_new_lines(self, ocr_result: OcrResult) -> list[str]:
"""Return only lines that are NOT already known.
Args:
ocr_result: OCR result with ``.lines`` populated.
Returns:
List of *original* (non-normalized) line texts that are new.
"""
now = time.monotonic()
self._gc(now)
new_lines: list[str] = []
for line in ocr_result.lines:
raw = line.text.strip()
if not raw:
continue
norm = _normalize(raw)
if len(norm) < 2:
continue
# Fast path: exact match
if norm in self._known:
self._known[norm].last_seen = now
self._known[norm].hit_count += 1
continue
# Slow path: fuzzy match (only short texts where OCR noise matters)
matched = False
if len(norm) < 60:
for key, entry in self._known.items():
# Skip candidates with very different length
if abs(len(norm) - len(key)) > max(5, len(key) * 0.2):
continue
ratio = SequenceMatcher(None, norm, key).ratio()
if ratio >= self._similarity:
entry.last_seen = now
entry.hit_count += 1
matched = True
break
if not matched:
self._known[norm] = KnownLine(
text=norm, first_seen=now, last_seen=now
)
new_lines.append(raw)
return new_lines
def reset(self) -> None:
"""Clear all known lines (e.g. on scene change)."""
self._known.clear()
@property
def known_count(self) -> int:
"""Number of tracked lines."""
return len(self._known)
def _gc(self, now: float) -> None:
"""Remove lines not seen for longer than TTL."""
expired = [
k for k, v in self._known.items() if now - v.last_seen > self._line_ttl
]
for k in expired:
del self._known[k]
# ── Layer 2: Global Text History ─────────────────────────────────
class GlobalTextHistory:
"""Ring buffer of recently emitted text blocks with TTL.
Prevents the same composed text from being processed twice
within the TTL window, even if it comes from different regions
or after a brief interruption.
"""
def __init__(
self,
max_size: int = DEFAULT_HISTORY_SIZE,
ttl: float = DEFAULT_HISTORY_TTL,
similarity: float = DEFAULT_HISTORY_SIMILARITY,
) -> None:
self._entries: deque[HistoryEntry] = deque(maxlen=max_size)
self._ttl = ttl
self._similarity = similarity
def is_duplicate(self, text: str) -> tuple[bool, float]:
"""Check whether *text* duplicates something in recent history.
Args:
text: Composed text block (already new-line joined).
Returns:
``(is_dup, best_similarity)`` — whether it matched and how closely.
"""
now = time.monotonic()
norm = _normalize(text)
if not norm:
return (True, 1.0) # empty → always "duplicate"
best_sim = 0.0
for entry in self._entries:
if now - entry.last_seen > self._ttl:
continue # expired
# Fast path: identical normalized text
if entry.norm_text == norm:
entry.last_seen = now
entry.hit_count += 1
return (True, 1.0)
# Fuzzy path
ratio = SequenceMatcher(None, norm, entry.norm_text).ratio()
best_sim = max(best_sim, ratio)
if ratio >= self._similarity:
entry.last_seen = now
entry.hit_count += 1
return (True, ratio)
return (False, best_sim)
def add(self, text: str) -> None:
"""Record a new text block in history."""
norm = _normalize(text)
now = time.monotonic()
self._entries.append(
HistoryEntry(
norm_text=norm,
original_text=text,
first_seen=now,
last_seen=now,
)
)
def reset(self) -> None:
"""Clear all history entries."""
self._entries.clear()
@property
def size(self) -> int:
return len(self._entries)
# ── Layer 3: Semantic Change Detector ────────────────────────────
class ChangeDetector:
"""Decide whether a set of new lines constitutes a meaningful change.
Rejects:
- Very short text (< ``min_chars`` printable characters)
- Too few words (< ``min_words``)
- Mostly non-alphanumeric (ratio < ``min_alnum_ratio``)
"""
def __init__(
self,
min_chars: int = DEFAULT_MIN_NEW_CHARS,
min_words: int = DEFAULT_MIN_NEW_WORDS,
min_alnum_ratio: float = DEFAULT_MIN_ALNUM_RATIO,
) -> None:
self._min_chars = min_chars
self._min_words = min_words
self._min_alnum_ratio = min_alnum_ratio
def is_significant(self, new_lines: list[str]) -> bool:
"""Return ``True`` if the new lines represent a real content change."""
text = " ".join(line.strip() for line in new_lines).strip()
if len(text) < self._min_chars:
return False
words = text.split()
if len(words) < self._min_words:
return False
alnum = sum(1 for c in text if c.isalnum())
ratio = alnum / len(text) if text else 0
if ratio < self._min_alnum_ratio:
return False
return True
# ── Debounce Emitter ─────────────────────────────────────────────
class DebouncedEmitter:
"""Buffer text and only yield it after stabilization.
Useful for typewriter-effect dialogs where text appears incrementally.
If ``stabilize_time`` is 0, debouncing is disabled (pass-through).
"""
def __init__(self, stabilize_time: float = DEFAULT_DEBOUNCE_TIME) -> None:
self._stabilize = stabilize_time
self._pending: str | None = None
self._pending_since: float = 0.0
def feed(self, text: str) -> str | None:
"""Feed new text. Returns the text once it has been stable long enough.
Args:
text: The candidate text to emit.
Returns:
The stabilized text, or ``None`` if still waiting.
"""
if self._stabilize <= 0:
return text # debounce disabled → immediate
now = time.monotonic()
if self._pending is None or _normalize(text) != _normalize(self._pending):
# New or changed text → reset timer
self._pending = text
self._pending_since = now
return None
# Text unchanged — check if stable long enough
if now - self._pending_since >= self._stabilize:
result = self._pending
self._pending = None
return result
return None # still waiting
def flush(self) -> str | None:
"""Force-emit whatever is pending (used on pipeline stop / force-read)."""
result = self._pending
self._pending = None
return result
def reset(self) -> None:
"""Discard pending text."""
self._pending = None
# ── Cross-Region Dedup Pool ──────────────────────────────────────
class CrossRegionPool:
"""Tracks lines across regions within a single tick to prevent cross-region duplication.
Within a single pipeline tick, if region A already yielded line X,
region B should skip it.
"""
def __init__(self, similarity: float = DEFAULT_LINE_SIMILARITY) -> None:
self._seen: dict[str, str] = {} # norm → original
self._similarity = similarity
def is_seen(self, line: str) -> bool:
"""Check if this line was already yielded by another region this tick."""
norm = _normalize(line)
if not norm:
return True
# Exact
if norm in self._seen:
return True
# Fuzzy (short lines only)
if len(norm) < 60:
for key in self._seen:
if abs(len(norm) - len(key)) > max(4, len(key) * 0.2):
continue
if SequenceMatcher(None, norm, key).ratio() >= self._similarity:
return True
return False
def mark(self, line: str) -> None:
"""Record a line as yielded this tick."""
norm = _normalize(line)
if norm:
self._seen[norm] = line
def clear(self) -> None:
"""Reset for next tick."""
self._seen.clear()
# ── Main Facade: SmartDedup ──────────────────────────────────────
class SmartDedup:
"""Three-layer OCR deduplication with debounce and cross-region awareness.
Replaces the old single-``_last_ocr_text`` comparison in ``bridge.py``.
Example::
dedup = SmartDedup()
# On each pipeline tick:
text = dedup.process(region_labels, ocr_results)
if text is not None:
await translate_and_speak(text)
# On pipeline stop or config change:
dedup.reset()
"""
def __init__(self, config: DedupConfig | None = None) -> None:
self._cfg = config or DedupConfig()
self._region_trackers: dict[str, RegionLineTracker] = {}
self._global_history = GlobalTextHistory(
max_size=self._cfg.history_size,
ttl=self._cfg.history_ttl,
similarity=self._cfg.history_similarity,
)
self._change_detector = ChangeDetector(
min_chars=self._cfg.min_new_chars,
min_words=self._cfg.min_new_words,
min_alnum_ratio=self._cfg.min_alnum_ratio,
)
self._debouncer = DebouncedEmitter(stabilize_time=self._cfg.debounce_time)
self._cross_pool = CrossRegionPool(similarity=self._cfg.line_similarity)
# ── Public API ───────────────────────────────────────────────
def process(
self,
region_labels: list[str],
ocr_results: list[OcrResult],
*,
force: bool = False,
) -> str | None:
"""Run all dedup layers on multi-region OCR results.
Args:
region_labels: Label/ID for each region (used as tracker key).
ocr_results: OCR result per region (same order as labels).
force: If ``True``, skip all dedup and return all text.
Returns:
Text to translate + speak, or ``None`` if dedup suppressed it.
"""
if force:
texts = [r.text.strip() for r in ocr_results if r.text.strip()]
combined = "\n".join(texts) if texts else None
if combined:
self._global_history.add(combined)
# Also update region trackers so we don't double-read next tick
for label, result in zip(region_labels, ocr_results):
tracker = self._get_tracker(label)
tracker.extract_new_lines(result) # just mark as known
flushed = self._debouncer.flush()
return combined
# Layer 1: Per-region line tracking + cross-region dedup
self._cross_pool.clear()
all_new_lines: list[str] = []
for label, result in zip(region_labels, ocr_results):
if result.error or result.is_empty:
continue
tracker = self._get_tracker(label)
region_new = tracker.extract_new_lines(result)
for line in region_new:
if not self._cross_pool.is_seen(line):
self._cross_pool.mark(line)
all_new_lines.append(line)
if not all_new_lines:
return None
# Layer 3: Semantic significance check
if not self._change_detector.is_significant(all_new_lines):
logger.debug(
"Dedup: new lines not significant (%d lines, %d chars)",
len(all_new_lines),
sum(len(l) for l in all_new_lines),
)
return None
composed = "\n".join(all_new_lines)
# Layer 2: Global history check
is_dup, sim = self._global_history.is_duplicate(composed)
if is_dup:
logger.debug("Dedup: global history match (sim=%.3f)", sim)
return None
# Debounce (typewriter effect protection)
stabilized = self._debouncer.feed(composed)
if stabilized is None:
logger.debug("Dedup: waiting for text stabilization")
return None
# ✅ New, significant, stabilized text — emit!
self._global_history.add(stabilized)
return stabilized
def force_flush(self) -> str | None:
"""Force-emit any debounced pending text."""
pending = self._debouncer.flush()
if pending:
self._global_history.add(pending)
return pending
def update_config(self, config: DedupConfig) -> None:
"""Apply new configuration. Recreates internal components."""
self._cfg = config
# Rebuild components with new settings
self._global_history = GlobalTextHistory(
max_size=config.history_size,
ttl=config.history_ttl,
similarity=config.history_similarity,
)
self._change_detector = ChangeDetector(
min_chars=config.min_new_chars,
min_words=config.min_new_words,
min_alnum_ratio=config.min_alnum_ratio,
)
self._debouncer = DebouncedEmitter(stabilize_time=config.debounce_time)
self._cross_pool = CrossRegionPool(similarity=config.line_similarity)
# Update existing region trackers
for tracker in self._region_trackers.values():
tracker._similarity = config.line_similarity
tracker._line_ttl = config.line_ttl
def reset(self) -> None:
"""Clear all state (e.g. on scene change or pipeline restart)."""
for tracker in self._region_trackers.values():
tracker.reset()
self._global_history.reset()
self._debouncer.reset()
self._cross_pool.clear()
logger.info("SmartDedup: all layers reset")
def reset_region(self, label: str) -> None:
"""Reset a specific region tracker."""
if label in self._region_trackers:
self._region_trackers[label].reset()
@property
def stats(self) -> dict[str, int]:
"""Return diagnostic stats."""
return {
"tracked_regions": len(self._region_trackers),
"total_known_lines": sum(
t.known_count for t in self._region_trackers.values()
),
"history_size": self._global_history.size,
}
# ── Internal ─────────────────────────────────────────────────
def _get_tracker(self, label: str) -> RegionLineTracker:
"""Get or create a line tracker for the given region label."""
if label not in self._region_trackers:
self._region_trackers[label] = RegionLineTracker(
similarity=self._cfg.line_similarity,
line_ttl=self._cfg.line_ttl,
)
return self._region_trackers[label]
|