File size: 920 Bytes
734b5b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()