perf(kb):1.优化 PDF只对可疑页做 image box 分析避免整本重复扫描/重复 reopen;2.doc/.docx 不再因为“有图片”就强制 OCR;3.只在 intake 路由 明确要求 OCR 时才跑
This commit is contained in:
1 parent
a3123cd602
commit
c58ed37cc1
11 files changed
+717
-122
No files matched your search
@@ -6,6 +6,9 @@ import re
|
||||
|
||||
from edkb_system.contracts import Block, Context
|
||||
|
||||
_RAPID_OCR_ENGINE = None
|
||||
_PDF_PAGE_IMAGE_MAX_EDGE = 1800
|
||||
|
||||
|
||||
def _decode_data_uri_image(data_uri: str):
|
||||
m = re.match(r"^data:image/[^;]+;base64,(.+)$", data_uri or "", re.DOTALL)
|
||||
@@ -26,17 +29,66 @@ def _decode_data_uri_image(data_uri: str):
|
||||
return None
|
||||
|
||||
|
||||
def _get_ocr_engine():
|
||||
global _RAPID_OCR_ENGINE
|
||||
if _RAPID_OCR_ENGINE is not None:
|
||||
return _RAPID_OCR_ENGINE
|
||||
|
||||
from rapidocr_onnxruntime import RapidOCR # type: ignore
|
||||
|
||||
_RAPID_OCR_ENGINE = RapidOCR()
|
||||
return _RAPID_OCR_ENGINE
|
||||
|
||||
|
||||
def _prepare_image_for_ocr(image_obj, block: Block):
|
||||
meta = getattr(block, "meta", {}) or {}
|
||||
if not (
|
||||
meta.get("page_image")
|
||||
and str(meta.get("page_image_source") or "").lower() == "pdf"
|
||||
):
|
||||
return image_obj
|
||||
|
||||
try:
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
if not hasattr(image_obj, "shape") or len(image_obj.shape) < 2:
|
||||
return image_obj
|
||||
height = int(image_obj.shape[0])
|
||||
width = int(image_obj.shape[1])
|
||||
max_edge = max(width, height)
|
||||
if max_edge <= _PDF_PAGE_IMAGE_MAX_EDGE:
|
||||
return image_obj
|
||||
|
||||
scale = _PDF_PAGE_IMAGE_MAX_EDGE / float(max_edge)
|
||||
target_size = (
|
||||
max(1, int(round(width * scale))),
|
||||
max(1, int(round(height * scale))),
|
||||
)
|
||||
pil_image = Image.fromarray(np.asarray(image_obj, dtype=np.uint8))
|
||||
resized = pil_image.resize(target_size, Image.Resampling.LANCZOS)
|
||||
return np.asarray(resized)
|
||||
except Exception:
|
||||
return image_obj
|
||||
|
||||
|
||||
def _ocr_call_kwargs(block: Block) -> dict:
|
||||
meta = getattr(block, "meta", {}) or {}
|
||||
if meta.get("page_image") and str(meta.get("page_image_source") or "").lower() == "pdf":
|
||||
return {"use_det": True, "use_cls": False, "use_rec": True}
|
||||
return {}
|
||||
|
||||
|
||||
def run(
|
||||
input_data: list[Block], context: Context, min_confidence: float = 0.3
|
||||
) -> list[Block]:
|
||||
blocks = input_data
|
||||
try:
|
||||
from rapidocr_onnxruntime import RapidOCR # type: ignore
|
||||
engine = _get_ocr_engine()
|
||||
except Exception:
|
||||
context.warnings.append("RapidOCR unavailable; skip OCR")
|
||||
return blocks
|
||||
|
||||
engine = RapidOCR()
|
||||
out: list[Block] = []
|
||||
for b in blocks:
|
||||
out.append(b)
|
||||
@@ -46,6 +98,8 @@ def run(
|
||||
image_obj = b.meta.get("image")
|
||||
if image_obj is None and b.meta and b.meta.get("data_uri"):
|
||||
image_obj = _decode_data_uri_image(str(b.meta.get("data_uri")))
|
||||
if image_obj is not None:
|
||||
image_obj = _prepare_image_for_ocr(image_obj, b)
|
||||
if image_obj is None:
|
||||
out.append(
|
||||
Block(
|
||||
@@ -59,7 +113,7 @@ def run(
|
||||
continue
|
||||
|
||||
try:
|
||||
result, _ = engine(image_obj)
|
||||
result, _ = engine(image_obj, **_ocr_call_kwargs(b))
|
||||
lines = []
|
||||
for it in result or []:
|
||||
if len(it) < 3:
|
||||
|
||||
Reference in new issue
Block a user