drowzeys's picture
v1.0 alpha: keys-Auto Receipts Studio (iPhone / may add Autonomous Lamp Skill)
2edb151 verified
Raw
History Blame Contribute Delete
588 Bytes
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)