RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(ocr): prevent OpenCV remap crashes with oversized images by clamping dimensions and adding tests (#19419)
lucasoffchain26ce0ed544ae
diff --git a/deepdoc/parser/pdf_parser.py b/deepdoc/parser/pdf_parser.pyindex 26d779ff9b..ae565352cd 100644--- a/deepdoc/parser/pdf_parser.py+++ b/deepdoc/parser/pdf_parser.py@@ -859,15 +859,17 @@ class RAGFlowPdfParser:# logging.info(f"__ocr sorting {len(chars)} chars cost {timer() - start}s")# start = timer()boxes_to_reg = []- img_np = None+ crop_boxes = []for b in bxs:if not b["text"]:- if img_np is None:- img_np = np.asarray(img)left, right, top, bott = b["x0"] * ZM, b["x1"] * ZM, b["top"] * ZM, b["bottom"] * ZM- b["box_image"] = self.ocr.get_rotate_crop_image(img_np, np.array([[left, top], [right, top], [right, bott], [left, bott]], dtype=np.float32))+ crop_boxes.append(np.array([[left, top], [right, top], [right, bott], [left, bott]], dtype=np.float32))boxes_to_reg.append(b)del b["txt"]+ if boxes_to_reg:+ crops = self.ocr.get_rotate_crop_images(np.asarray(img), crop_boxes)+ for box, crop in zip(boxes_to_reg, crops):+ box["box_image"] = croptexts = self.ocr.recognize_batch([b["box_image"] for b in boxes_to_reg], device_id)for i in range(len(boxes_to_reg)):boxes_to_reg[i]["text"] = texts[i]diff --git a/deepdoc/vision/ocr.py b/deepdoc/vision/ocr.pyindex 1cec1563c8..8430218ba9 100644--- a/deepdoc/vision/ocr.py+++ b/deepdoc/vision/ocr.py@@ -15,7 +15,6 @@#import gcimport logging-import copyimport timeimport os@@ -35,6 +34,11 @@ from .postprocess import build_post_processloaded_models = {}+# OpenCV remap (used by cv2.warpPerspective) asserts src/dst width and height+# are strictly less than SHRT_MAX (32767). Oversized PDF page renders can+# exceed that and crash chunking.+_OPENCV_REMAP_MAX_DIM = 32766+def transform(data, ops=None):"""transform"""@@ -558,7 +562,19 @@ class OCR:self.drop_score = 0.5self.crop_image_res_index = 0- def get_rotate_crop_image(self, img, points):+ @staticmethod+ def _prepare_crop_source(img):+ img_h, img_w = img.shape[:2]+ max_src_side = max(img_w, img_h)+ if max_src_side <= _OPENCV_REMAP_MAX_DIM:+ return img, 1.0++ scale = _OPENCV_REMAP_MAX_DIM / max_src_side+ new_w = max(1, int(img_w * scale))+ new_h = max(1, int(img_h * scale))+ return cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA), scale++ def _get_rotate_crop_image(self, img, points, scale=1.0):"""img_height, img_width = img.shape[0:2]left = int(np.min(points[:, 0]))@@ -570,8 +586,23 @@ class OCR:points[:, 1] = points[:, 1] - top"""assert len(points) == 4, "shape of points must be 4*2"+ points = np.asarray(points, dtype=np.float32).copy()+ points *= scale+ img_h, img_w = img.shape[:2]+ if img_h <= 0 or img_w <= 0:+ channels = img.shape[2] if img.ndim == 3 else 1+ return np.zeros((1, 1, channels), dtype=img.dtype) if img.ndim == 3 else np.zeros((1, 1), dtype=img.dtype)++ # Clamp detector quads to the source image so out-of-range coordinates+ # cannot inflate the warp destination.+ points[:, 0] = np.clip(points[:, 0], 0, max(img_w - 1, 0))+ points[:, 1] = np.clip(points[:, 1], 0, max(img_h - 1, 0))+img_crop_width = int(max(np.linalg.norm(points[0] - points[1]), np.linalg.norm(points[2] - points[3])))img_crop_height = int(max(np.linalg.norm(points[0] - points[3]), np.linalg.norm(points[1] - points[2])))+ img_crop_width = max(img_crop_width, 1)+ img_crop_height = max(img_crop_height, 1)+pts_std = np.float32([[0, 0], [img_crop_width, 0], [img_crop_width, img_crop_height], [0, img_crop_height]])M = cv2.getPerspectiveTransform(points, pts_std)dst_img = cv2.warpPerspective(img, M, (img_crop_width, img_crop_height), borderMode=cv2.BORDER_REPLICATE, flags=cv2.INTER_CUBIC)@@ -602,6 +633,14 @@ class OCR:dst_img = best_imgreturn dst_img+ def get_rotate_crop_image(self, img, points):+ src, scale = self._prepare_crop_source(img)+ return self._get_rotate_crop_image(src, points, scale)++ def get_rotate_crop_images(self, img, boxes):+ src, scale = self._prepare_crop_source(img)+ return [self._get_rotate_crop_image(src, points, scale) for points in boxes]+def sorted_boxes(self, dt_boxes):"""Sort text boxes in order from top to bottom, left to right@@ -701,14 +740,8 @@ class OCR:time_dict["all"] = end - startreturn None, None, time_dict- img_crop_list = []-dt_boxes = self.sorted_boxes(dt_boxes)-- for bno in range(len(dt_boxes)):- tmp_box = copy.deepcopy(dt_boxes[bno])- img_crop = self.get_rotate_crop_image(ori_im, tmp_box)- img_crop_list.append(img_crop)+ img_crop_list = self.get_rotate_crop_images(ori_im, dt_boxes)rec_res, elapse = self.text_recognizer[device_id](img_crop_list)diff --git a/test/unit_test/deepdoc/vision/test_ocr_rotate_crop_limit.py b/test/unit_test/deepdoc/vision/test_ocr_rotate_crop_limit.pynew file mode 100644index 0000000000..4584ac9933--- /dev/null+++ b/test/unit_test/deepdoc/vision/test_ocr_rotate_crop_limit.py@@ -0,0 +1,109 @@+"""+Regression: OpenCV warpPerspective/remap rejects src/dst dims >= SHRT_MAX.++Oversized PDF page renders (e.g. wide infographics at zoomin=3) used to crash+chunking inside OCR.get_rotate_crop_image with:++ error: (-215:Assertion failed) dst.cols < SHRT_MAX && ... in function remap+"""++from unittest.mock import MagicMock, patch++import cv2+import numpy as np+import pytest++from deepdoc.vision.ocr import _OPENCV_REMAP_MAX_DIM, OCR+++def _cv2_runtime_available() -> bool:+ try:+ cv2.resize(np.zeros((2, 2, 3), dtype=np.uint8), (1, 1))+ return True+ except Exception:+ return False+++# Several unit test modules replace sys.modules["cv2"] with a stub that raises on+# every call when the real OpenCV wheel cannot be imported, and never restore it.+# Those modules are collected before this one, so the geometry checks below need+# a live OpenCV runtime rather than whatever "cv2" currently resolves to.+requires_cv2 = pytest.mark.skipif(+ not _cv2_runtime_available(),+ reason="OpenCV runtime is unavailable or stubbed out",+)+++def _ocr_stub() -> OCR:+ """Bypass model loading; only exercise get_rotate_crop_image geometry."""+ ocr = object.__new__(OCR)+ ocr.text_recognizer = [MagicMock()]+ return ocr+++def test_opencv_remap_max_dim_below_shrt_max():+ assert _OPENCV_REMAP_MAX_DIM < 32767+++@requires_cv2+def test_get_rotate_crop_image_handles_oversized_source():+ """Source wider than SHRT_MAX must not raise; crop stays under the limit."""+ ocr = _ocr_stub()+ # ~10MB strip; wide enough to trip OpenCV remap without allocating a square.+ width = _OPENCV_REMAP_MAX_DIM + 100+ height = 64+ img = np.zeros((height, width, 3), dtype=np.uint8)+ img[:, :] = (40, 80, 120)+ points = np.float32([[10, 10], [200, 10], [200, 50], [10, 50]])++ cropped = ocr.get_rotate_crop_image(img, points)++ assert cropped is not None+ assert cropped.ndim == 3+ assert cropped.shape[0] <= _OPENCV_REMAP_MAX_DIM+ assert cropped.shape[1] <= _OPENCV_REMAP_MAX_DIM+ assert cropped.shape[0] > 0 and cropped.shape[1] > 0+++@requires_cv2+def test_get_rotate_crop_images_resizes_oversized_source_once():+ ocr = _ocr_stub()+ width = _OPENCV_REMAP_MAX_DIM + 100+ img = np.zeros((64, width, 3), dtype=np.uint8)+ boxes = [+ np.float32([[10, 10], [200, 10], [200, 40], [10, 40]]),+ np.float32([[300, 10], [500, 10], [500, 40], [300, 40]]),+ np.float32([[600, 10], [800, 10], [800, 40], [600, 40]]),+ ]++ with patch("deepdoc.vision.ocr.cv2.resize", wraps=cv2.resize) as resize:+ crops = ocr.get_rotate_crop_images(img, boxes)++ assert len(crops) == len(boxes)+ resize.assert_called_once()+++@requires_cv2+def test_get_rotate_crop_image_normal_page_unchanged_size():+ """In-bounds crops on normal pages keep the expected output geometry."""+ ocr = _ocr_stub()+ img = np.zeros((200, 400, 3), dtype=np.uint8)+ points = np.float32([[10, 20], [110, 20], [110, 60], [10, 60]])++ cropped = ocr.get_rotate_crop_image(img, points)++ assert cropped.shape[1] == 100+ assert cropped.shape[0] == 40+++@requires_cv2+def test_get_rotate_crop_image_clamps_out_of_bounds_quad():+ ocr = _ocr_stub()+ img = np.zeros((100, 100, 3), dtype=np.uint8)+ points = np.float32([[-50, -50], [500, -50], [500, 500], [-50, 500]])++ cropped = ocr.get_rotate_crop_image(img, points)++ assert cropped.shape[0] <= _OPENCV_REMAP_MAX_DIM+ assert cropped.shape[1] <= _OPENCV_REMAP_MAX_DIM+ assert cropped.shape[0] >= 1 and cropped.shape[1] >= 1