Coordinates OCR, processors, and XML output for text restoration. Default: Tesseract OCR + optional Pix2Text for formulas.
| 26 | |
| 27 | |
| 28 | class TextRestorer: |
| 29 | """ |
| 30 | Coordinates OCR, processors, and XML output for text restoration. |
| 31 | Default: Tesseract OCR + optional Pix2Text for formulas. |
| 32 | """ |
| 33 | |
| 34 | def __init__( |
| 35 | self, |
| 36 | formula_engine: str = "pix2text", |
| 37 | ocr_engine: str = "tesseract", |
| 38 | ): |
| 39 | """ |
| 40 | Args: |
| 41 | formula_engine: Formula engine ('pix2text', 'none'). |
| 42 | ocr_engine: Layout/text OCR engine ('tesseract', 'paddleocr'). PaddleOCR often better for mixed CN/EN. |
| 43 | """ |
| 44 | self.formula_engine = formula_engine |
| 45 | self._ocr_engine = (ocr_engine or "tesseract").strip().lower() |
| 46 | |
| 47 | self._layout_ocr = None |
| 48 | self._pix2text_ocr = None |
| 49 | |
| 50 | self.font_size_processor = FontSizeProcessor() |
| 51 | self.font_family_processor = FontFamilyProcessor() |
| 52 | self.style_processor = StyleProcessor() |
| 53 | self.formula_processor = FormulaProcessor() |
| 54 | |
| 55 | self.timing = { |
| 56 | "text_ocr": 0.0, |
| 57 | "pix2text_ocr": 0.0, |
| 58 | "processing": 0.0, |
| 59 | "total": 0.0, |
| 60 | } |
| 61 | |
| 62 | @property |
| 63 | def layout_ocr(self): |
| 64 | """Lazy-init layout OCR (tesseract or paddleocr); fallback to Tesseract if PaddleOCR fails.""" |
| 65 | if self._layout_ocr is None: |
| 66 | if self._ocr_engine == "paddleocr": |
| 67 | try: |
| 68 | from .ocr.paddle_ocr import PaddleOCRAdapter |
| 69 | self._layout_ocr = PaddleOCRAdapter() |
| 70 | except Exception as e: |
| 71 | import warnings |
| 72 | warnings.warn( |
| 73 | f"PaddleOCR unavailable ({e}), falling back to Tesseract. See README for compatible install.", |
| 74 | UserWarning, |
| 75 | stacklevel=2, |
| 76 | ) |
| 77 | self._layout_ocr = LocalOCR() |
| 78 | else: |
| 79 | self._layout_ocr = LocalOCR() |
| 80 | return self._layout_ocr |
| 81 | |
| 82 | @property |
| 83 | def pix2text_ocr(self): |
| 84 | """Lazy-init Pix2Text OCR (None if pix2text not installed).""" |
| 85 | from .ocr import Pix2TextOCR |
no outgoing calls
no test coverage detected