Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

refactor: unify OCR inference functions by consolidating detection and recognition calls

myhloli2cf7a42453dd
diff --git a/mineru/backend/hybrid/hybrid_analyze.py b/mineru/backend/hybrid/hybrid_analyze.py
index 617a1193..095aa2d4 100644
--- a/mineru/backend/hybrid/hybrid_analyze.py
+++ b/mineru/backend/hybrid/hybrid_analyze.py
@@ -23,8 +23,7 @@ from mineru.backend.pipeline.model_init import (
HybridModelSingleton,
run_layout_inference,
run_mfr_inference,
- run_ocr_det_inference,
- run_ocr_rec_inference,
+ run_ocr_inference,
)
from mineru.backend.vlm.vlm_analyze import (
ModelSingleton,
@@ -125,7 +124,7 @@ def ocr_det(
page_mfd_res, useful_list
)
bgr_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)
- ocr_res = run_ocr_det_inference(
+ ocr_res = run_ocr_inference(
hybrid_pipeline_model.ocr_model.ocr,
bgr_image,
mfd_res=adjusted_mfdetrec_res,
@@ -202,7 +201,7 @@ def ocr_det(
# 批处理检测
det_batch_size = min(len(batch_images), batch_ratio * OCR_DET_BASE_BATCH_SIZE)
- batch_results = run_ocr_det_inference(
+ batch_results = run_ocr_inference(
hybrid_pipeline_model.ocr_model.text_detector.batch_predict,
batch_images,
det_batch_size,
@@ -488,7 +487,7 @@ def _process_ocr_and_formulas(
img_crop_list.append(ocr_res.pop('np_img'))
if len(img_crop_list) > 0:
# Process OCR
- ocr_result_list = run_ocr_rec_inference(
+ ocr_result_list = run_ocr_inference(
hybrid_pipeline_model.ocr_model.ocr,
img_crop_list,
det=False,
diff --git a/mineru/backend/hybrid/hybrid_model_output_to_middle_json.py b/mineru/backend/hybrid/hybrid_model_output_to_middle_json.py
index aa03e4cb..f1a46296 100644
--- a/mineru/backend/hybrid/hybrid_model_output_to_middle_json.py
+++ b/mineru/backend/hybrid/hybrid_model_output_to_middle_json.py
@@ -14,7 +14,7 @@ from mineru.backend.utils.para_block_utils import (
)
from mineru.backend.hybrid.hybrid_magic_model import MagicModel
from mineru.backend.utils.runtime_utils import cross_page_table_merge
-from mineru.backend.pipeline.model_init import run_ocr_rec_inference
+from mineru.backend.pipeline.model_init import run_ocr_inference
from mineru.utils.config_reader import get_table_enable
from mineru.utils.cut_image import cut_image_and_table
from mineru.utils.enum_class import ContentType, BlockType
@@ -139,7 +139,7 @@ def _apply_post_ocr(pdf_info_list, hybrid_pipeline_model):
img_crop_list.append(rotate_vertical_crop_if_needed(span['np_img']))
span.pop('np_img')
if len(img_crop_list) > 0:
- ocr_res_list = run_ocr_rec_inference(
+ ocr_res_list = run_ocr_inference(
hybrid_pipeline_model.ocr_model.ocr,
img_crop_list,
det=False,
diff --git a/mineru/backend/pipeline/batch_analyze.py b/mineru/backend/pipeline/batch_analyze.py
index 78f3f1a0..291a01da 100644
--- a/mineru/backend/pipeline/batch_analyze.py
+++ b/mineru/backend/pipeline/batch_analyze.py
@@ -13,8 +13,7 @@ from .model_init import (
AtomModelSingleton,
run_layout_inference,
run_mfr_inference,
- run_ocr_det_inference,
- run_ocr_rec_inference,
+ run_ocr_inference,
)
from .model_list import AtomicModel
from ...utils.config_reader import (
@@ -534,7 +533,7 @@ class BatchAnalyze:
if inline_mask_boxes
else bgr_image
)
- ocr_result = run_ocr_det_inference(
+ ocr_result = run_ocr_inference(
det_ocr_engine.ocr, det_image, rec=False
)[0]
if ocr_result and formula_mask_boxes:
@@ -565,7 +564,7 @@ class BatchAnalyze:
enable_merge_det_boxes=False,
)
cropped_img_list = [item["cropped_img"] for item in rec_img_list]
- ocr_res_list = run_ocr_rec_inference(
+ ocr_res_list = run_ocr_inference(
ocr_engine.ocr,
cropped_img_list,
det=False,
@@ -731,7 +730,7 @@ class BatchAnalyze:
# 批处理检测
det_batch_size = min(len(batch_images), self.batch_ratio * OCR_DET_BASE_BATCH_SIZE)
- batch_results = run_ocr_det_inference(
+ batch_results = run_ocr_inference(
ocr_model.text_detector.batch_predict, batch_images, det_batch_size
)
@@ -793,7 +792,7 @@ class BatchAnalyze:
bgr_image,
adjusted_mfdetrec_res,
)
- ocr_res = run_ocr_det_inference(
+ ocr_res = run_ocr_inference(
ocr_model.ocr,
det_image,
mfd_res=adjusted_mfdetrec_res,
@@ -852,7 +851,7 @@ class BatchAnalyze:
atom_model_name=AtomicModel.OCR,
lang=lang
)
- ocr_res_list = run_ocr_rec_inference(
+ ocr_res_list = run_ocr_inference(
ocr_model.ocr, img_crop_list, det=False, tqdm_enable=True
)[0]
@@ -923,7 +922,7 @@ class BatchAnalyze:
)
seal_crop_bgr = cv2.cvtColor(seal_crop_rgb, cv2.COLOR_RGB2BGR)
- seal_ocr_res = run_ocr_det_inference(
+ seal_ocr_res = run_ocr_inference(
seal_ocr_model.ocr, seal_crop_bgr, det=True, rec=True
)[0]
if not seal_ocr_res:
diff --git a/mineru/backend/pipeline/model_init.py b/mineru/backend/pipeline/model_init.py
index 12738f46..9b20c810 100644
--- a/mineru/backend/pipeline/model_init.py
+++ b/mineru/backend/pipeline/model_init.py
@@ -22,8 +22,7 @@ PIPELINE_MODEL_INIT_LOCK = threading.RLock()
# 这些锁保护 pipeline 与 hybrid 共享的 atom model/native 模型推理调用,避免多线程同时进入同一个模型对象。
PIPELINE_LAYOUT_INFERENCE_LOCK = threading.RLock()
PIPELINE_MFR_INFERENCE_LOCK = threading.RLock()
-PIPELINE_OCR_DET_INFERENCE_LOCK = threading.RLock()
-PIPELINE_OCR_REC_INFERENCE_LOCK = threading.RLock()
+PIPELINE_OCR_INFERENCE_LOCK = threading.RLock()
# 临时关闭 pipeline/hybrid 共享推理阶段锁;需要回滚实验时可通过环境变量重新打开。
PIPELINE_INFERENCE_LOCKS_ENABLED = os.getenv(
'MINERU_ENABLE_PIPELINE_INFERENCE_LOCKS', 'False'
@@ -53,17 +52,10 @@ def run_mfr_inference(inference_callable, *args, **kwargs):
)
-def run_ocr_det_inference(inference_callable, *args, **kwargs):
- """按实验开关执行共享 OCR det 模型调用。"""
+def run_ocr_inference(inference_callable, *args, **kwargs):
+ """按实验开关执行共享 OCR native 模型调用。"""
return _run_with_inference_lock(
- PIPELINE_OCR_DET_INFERENCE_LOCK, inference_callable, *args, **kwargs
- )
-
-
-def run_ocr_rec_inference(inference_callable, *args, **kwargs):
- """按实验开关执行共享 OCR rec 模型调用。"""
- return _run_with_inference_lock(
- PIPELINE_OCR_REC_INFERENCE_LOCK, inference_callable, *args, **kwargs
+ PIPELINE_OCR_INFERENCE_LOCK, inference_callable, *args, **kwargs
)
MFR_MODEL = os.getenv('MINERU_FORMULA_CH_SUPPORT', 'False')
diff --git a/mineru/backend/pipeline/model_json_to_middle_json.py b/mineru/backend/pipeline/model_json_to_middle_json.py
index 7ac043ff..ebf1212a 100644
--- a/mineru/backend/pipeline/model_json_to_middle_json.py
+++ b/mineru/backend/pipeline/model_json_to_middle_json.py
@@ -7,7 +7,7 @@ from mineru.backend.utils.html_image_utils import replace_inline_table_images
from mineru.backend.utils.runtime_utils import cross_page_table_merge
from mineru.backend.pipeline.model_init import (
AtomModelSingleton,
- run_ocr_rec_inference,
+ run_ocr_inference,
)
from mineru.backend.pipeline.para_split import para_split
from mineru.utils.char_utils import full_to_half
@@ -235,7 +235,7 @@ def _apply_post_ocr(pdf_info_list, lang=None):
det_db_box_thresh=0.3,
lang=lang
)
- ocr_res_list = run_ocr_rec_inference(
+ ocr_res_list = run_ocr_inference(
ocr_model.ocr, img_crop_list, det=False, tqdm_enable=True
)[0]
assert len(ocr_res_list) == len(