File size: 588 Bytes
2edb151 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from __future__ import annotations
from pathlib import Path
from backends.base import OCRBackend, OCRResult
def maybe_ocr(backend: OCRBackend | None, path: Path) -> OCRResult | None:
if backend is None:
sidecar = path.with_suffix(".txt")
if sidecar.is_file():
return OCRResult(text=sidecar.read_text(encoding="utf-8", errors="replace"), engine="sidecar")
if path.suffix.lower() == ".txt":
return OCRResult(text=path.read_text(encoding="utf-8", errors="replace"), engine="plaintext")
return None
return backend.ocr(path)
|