Skip to content

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

feat: introduce get_rotate_crop_image_for_text_rec function and update related calls for improved text recognition handling

myhloli26da5f25c41b
diff --git a/mineru/backend/pipeline/batch_analyze.py b/mineru/backend/pipeline/batch_analyze.py
index c8a82090..bbd2d0d2 100644
--- a/mineru/backend/pipeline/batch_analyze.py
+++ b/mineru/backend/pipeline/batch_analyze.py
@@ -23,7 +23,12 @@ from ...utils.config_reader import (
from ...utils.bbox_utils import normalize_to_int_bbox
from ...utils.model_utils import crop_img, get_res_list_from_layout_res, clean_vram
from ...utils.ocr_utils import merge_det_boxes, update_det_boxes, sorted_boxes
-from ...utils.ocr_utils import get_adjusted_mfdetrec_res, get_ocr_result_list, OcrConfidence, get_rotate_crop_image
+from ...utils.ocr_utils import (
+ get_adjusted_mfdetrec_res,
+ get_ocr_result_list,
+ OcrConfidence,
+ get_rotate_crop_image_for_text_rec,
+)
from ...utils.pdf_image_tools import get_crop_np_img
LAYOUT_BASE_BATCH_SIZE = 1
@@ -533,7 +538,7 @@ class BatchAnalyze:
for dt_box in dt_boxes_final:
rec_img_lang_group[table_res_dict["lang"]].append(
{
- "cropped_img": get_rotate_crop_image(
+ "cropped_img": get_rotate_crop_image_for_text_rec(
bgr_image, np.asarray(dt_box, dtype=np.float32)
),
"dt_box": np.asarray(dt_box, dtype=np.float32),
diff --git a/mineru/model/ocr/pytorch_paddle.py b/mineru/model/ocr/pytorch_paddle.py
index 23dc56e4..08db11ed 100644
--- a/mineru/model/ocr/pytorch_paddle.py
+++ b/mineru/model/ocr/pytorch_paddle.py
@@ -14,7 +14,14 @@ from mineru.model.ocr.seal_crop import CropByPolys, SortPolyBoxes
from mineru.utils.config_reader import get_device
from mineru.utils.enum_class import ModelPath
from mineru.utils.models_download_utils import auto_download_and_get_model_root_path
-from mineru.utils.ocr_utils import check_img, preprocess_image, sorted_boxes, merge_det_boxes, update_det_boxes, get_rotate_crop_image
+from mineru.utils.ocr_utils import (
+ check_img,
+ preprocess_image,
+ sorted_boxes,
+ merge_det_boxes,
+ update_det_boxes,
+ get_rotate_crop_image_for_text_rec,
+)
from mineru.model.utils.tools.infer.predict_system import TextSystem
from mineru.model.utils.tools.infer import pytorchocr_utility as utility
import argparse
@@ -366,9 +373,11 @@ class PytorchPaddleOCR(TextSystem):
if mfd_res:
dt_boxes = update_det_boxes(dt_boxes, mfd_res)
+ # Standard text OCR rotates tall crops before recognition.
+ # Seal OCR keeps its dedicated poly-crop path above.
for bno in range(len(dt_boxes)):
tmp_box = copy.deepcopy(dt_boxes[bno])
- img_crop = get_rotate_crop_image(ori_im, tmp_box)
+ img_crop = get_rotate_crop_image_for_text_rec(ori_im, tmp_box)
img_crop_list.append(img_crop)
rec_res, elapse = self.text_recognizer(img_crop_list)
diff --git a/mineru/utils/ocr_utils.py b/mineru/utils/ocr_utils.py
index eb332312..375b59e1 100644
--- a/mineru/utils/ocr_utils.py
+++ b/mineru/utils/ocr_utils.py
@@ -11,6 +11,7 @@ class OcrConfidence:
min_width = 3
LINE_WIDTH_TO_HEIGHT_RATIO_THRESHOLD = 4 # 一般情况下,行宽度超过高度4倍时才是一个正常的横向文本块
+TEXT_REC_ROTATE_RATIO = 1.5
def merge_spans_to_line(spans, threshold=0.6):
@@ -358,7 +359,7 @@ def get_ocr_result_list(
if ocr_enable:
tmp_box = copy.deepcopy(np.array([p1, p2, p3, p4]).astype('float32'))
- img_crop = get_rotate_crop_image(ori_im, tmp_box)
+ img_crop = get_rotate_crop_image_for_text_rec(ori_im, tmp_box)
need_ocr_rec = True
# average_angle_degrees = calculate_angle_degrees(box_ocr_res[0])
@@ -463,7 +464,24 @@ def get_rotate_crop_image(img, points):
borderMode=cv2.BORDER_REPLICATE,
flags=cv2.INTER_CUBIC)
dst_img_height, dst_img_width = dst_img.shape[0:2]
- rotate_radio = 2
- if dst_img_height * 1.0 / dst_img_width >= rotate_radio:
+ if dst_img_height * 1.0 / dst_img_width >= TEXT_REC_ROTATE_RATIO:
dst_img = np.rot90(dst_img)
return dst_img
+
+
+def rotate_vertical_crop_if_needed(crop_img, rotate_ratio=TEXT_REC_ROTATE_RATIO):
+ if crop_img is None or crop_img.size == 0:
+ return crop_img
+
+ crop_height, crop_width = crop_img.shape[:2]
+ if crop_width == 0:
+ return crop_img
+
+ if crop_height * 1.0 / crop_width >= rotate_ratio:
+ return np.rot90(crop_img)
+ return crop_img
+
+
+def get_rotate_crop_image_for_text_rec(img, points):
+ crop_img = get_rotate_crop_image(img, points)
+ return rotate_vertical_crop_if_needed(crop_img)