diff --git a/anonymizer_core_refactored_onnx.py b/anonymizer_core_refactored_onnx.py index 0f8e7d8..f8cf181 100644 --- a/anonymizer_core_refactored_onnx.py +++ b/anonymizer_core_refactored_onnx.py @@ -1192,16 +1192,23 @@ def extract_text_with_fallback_ocr(pdf_path: Path) -> Tuple[List[str], List[List except Exception: pass - # --- Passe 3 : OCR docTR si PDF scanné (très peu de texte) --- + # --- Passe 3 : OCR docTR sur les pages pauvres en texte --- + # Pas de seuil global : on OCR uniquement les pages individuelles + # qui ont peu de texte (< 150 chars), puis on garde le meilleur résultat + # par page. Les pages déjà riches en texte ne sont pas touchées. + _OCR_PAGE_THRESHOLD = 150 # chars minimum pour considérer une page comme "texte OK" total_chars = sum(len(x or "") for x in pages_text) ocr_word_map: OcrWordMap = {} - if total_chars < 200 and _DOCTR_AVAILABLE and fitz is not None: + sparse_pages = [i for i, p in enumerate(pages_text) if len(p or "") < _OCR_PAGE_THRESHOLD] + if sparse_pages and _DOCTR_AVAILABLE and fitz is not None: try: model = _get_doctr_model() doc = fitz.open(str(pdf_path)) - ocr_pages: List[str] = [] import numpy as np - for i in range(len(doc)): + ocr_replaced = 0 + for i in sparse_pages: + if i >= len(doc): + continue pix = doc[i].get_pixmap(dpi=300) img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) result = model([np.array(img)]) @@ -1213,15 +1220,17 @@ def extract_text_with_fallback_ocr(pdf_path: Path) -> Tuple[List[str], List[List (x0, y0), (x1, y1) = w.geometry page_words.append((w.value, x0, y0, x1, y1)) page_text += " ".join(w.value for w in line.words) + "\n" - ocr_word_map[i] = page_words - ocr_pages.append(page_text) + # Remplacer seulement si l'OCR produit plus de texte + if len(page_text) > len(pages_text[i] or ""): + pages_text[i] = page_text + ocr_word_map[i] = page_words + ocr_replaced += 1 doc.close() - if sum(len(p) for p in ocr_pages) > total_chars: - pages_text = ocr_pages + if ocr_replaced > 0: ocr_used = True - else: - ocr_word_map = {} - except Exception: + log.info("OCR docTR : %d/%d pages remplacées", ocr_replaced, len(sparse_pages)) + except Exception as e: + log.warning("OCR docTR échoué : %s", e) ocr_word_map = {} return pages_text, tables_lines, ocr_used, ocr_word_map