| from __future__ import annotations |
| import os, shutil, subprocess |
| from pathlib import Path |
| import pytesseract |
|
|
| class TesseractService: |
| def __init__(self): |
| custom = os.environ.get("TESSERACT_CMD") |
| if custom: |
| pytesseract.pytesseract.tesseract_cmd = custom |
|
|
| def ensure_available(self) -> str: |
| cmd = pytesseract.pytesseract.tesseract_cmd |
| resolved = shutil.which(cmd) if os.path.basename(cmd) == cmd else cmd |
| if not resolved or not Path(resolved).exists(): |
| raise RuntimeError( |
| "Tesseract not found. Install it locally and add to PATH, " |
| "or set the TESSERACT_CMD environment variable." |
| ) |
| return resolved |
|
|
| def version(self) -> str: |
| cmd = self.ensure_available() |
| out = subprocess.check_output([cmd, "--version"], text=True, errors="ignore") |
| return out.splitlines()[0].strip() |
|
|