Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
refactor: remove remaining OpenCV calls and calibration branch
myhlolif232373fb319
diff --git a/docs/next/opencv-loading.md b/docs/next/opencv-loading.mdindex 4441771c..8b8dfbbf 100644--- a/docs/next/opencv-loading.md+++ b/docs/next/opencv-loading.md@@ -1,6 +1,41 @@# OpenCV 按需加载与使用验收-2026-10-08。第二轮已从 MinerU 基础直接依赖中移除 OpenCV;下方第一轮章节保留此前按需加载阶段的历史记录。+2026-10-08。第三轮已完全移除 MinerU 自身的 OpenCV 导入和调用。下方第二轮及第一轮章节保留历史验收记录。++## 第三轮:调用代码完全移除++生产目录 `mineru/` 中已无 `cv2` 标识、导入或调用;基础依赖不包含 OpenCV,Torch/full 等 extra+保持原样,其他依赖自行引入 OpenCV 的行为仍不在清理范围。DocVortex 公共 API 和内核无需继续修改。++正常印章裁剪一直显式使用 `mode="homography"`。本轮删除没有产品调用方的相机标定支路:+`PlanB`、虚拟相机、畸变投影、`Rodrigues`、`remap`、两处 `calibrateCamera` 以及损失阈值重试。+`CurveTextRectifier`、`AutoRectifier` 默认改为 homography,保留原分段展开、短多边形外接框裁图、+几何异常回退和诊断绘图。显式请求 calibration 模式会抛出 `ValueError`,不静默更换算法;+内部 `loss_thresh` 参数不再提供。公共 Parser、产品参数和九种输出契约保持原样。++同时移除未使用的 `base64_to_cv2` 内部辅助函数、`imresize` 的旧 `backend="cv2"` 别名,+修正三处过时模块注释。旧标定白名单改为全生产目录禁止 OpenCV 的静态守卫。++测试对照在删除前冻结为 `tests/fixtures/model_image_reference.json`:29 个模型数组来自原 OpenCV+对照,15 组印章裁图及诊断图来自原显式 homography 路径。记录基准提交 `e6d89a6a`、原测试+源码摘要及参考库版本。测试代码也不再导入或调用 OpenCV,只保留导入阻断、静态审计和元数据检查。++| 验收 | 结果 |+| --- | --- |+| 静态生产及测试调用审计 | OpenCV 导入和直接调用均为 0;生产源码 `cv2` 文本匹配为 0 |+| 针对数值与跨库边界 | 77 项通过;包括 Python/Rust 两后端的横竖曲线、四点、短多边形和退化回退 |+| 整组回归阻断 OpenCV | 同一 77 项通过,冻结参考测试无需安装 OpenCV |+| 普通宿主回归 | 3043 passed、4 skipped;范围为 `not remote and not full_stack` |+| 实际 Flash OCR | demo1 第 5–6 页,候选阻断 cv2;完整 ModelJson、MiddleJson、九种输出和实际模型输入摘要与第二轮候选相同 |+| Ruff / 补丁 | 核心印章模块与修改后边界测试 E/F/W/ANN、格式检查通过;`git diff --check` 通过 |++宿主回归有 63 条警告,包含此前出现过的 Gradio 页范围测试事件循环析构告警。+该文件按 `PytestUnraisableExceptionWarning` 作为错误重跑,59 passed、3 skipped,未复现;+原始告警和重跑日志均保留,不过滤它。++原始证据位于 `/tmp/mineru-opencv-zero-20261008`,归档为+[output/opencv-zero-20261008/evidence.tar.gz](../../output/opencv-zero-20261008/evidence.tar.gz)。+本轮修改提交并合并到本地 dev,未推送或发布。第二轮归档保持原样。## 第二轮:基础依赖移除diff --git a/mineru/model/_internal/pytorchocr/infer/pytorchocr_utility.py b/mineru/model/_internal/pytorchocr/infer/pytorchocr_utility.pyindex 21e9dad3..d3295fc6 100644--- a/mineru/model/_internal/pytorchocr/infer/pytorchocr_utility.py+++ b/mineru/model/_internal/pytorchocr/infer/pytorchocr_utility.py@@ -1,6 +1,6 @@# Copyright (c) Opendatalab. All rights reserved.-from docvortex.image import decode_image, resize_image+from docvortex.image import resize_imageimport osimport mathfrom pathlib import Path@@ -212,15 +212,6 @@ def str_count(s):return s_len - math.ceil(en_dg_count / 2)-def base64_to_cv2(b64str):-- import base64- data = base64.b64decode(b64str.encode('utf8'))- data = np.fromstring(data, np.uint8)- data = decode_image(data.tobytes(), color=True, apply_orientation=True)- return data--def get_arch_config(model_path):import yamlwith open(DEFAULT_CFG_PATH, encoding='utf-8') as f:diff --git a/mineru/model/layout/pp_doclayout_v2_base.py b/mineru/model/layout/pp_doclayout_v2_base.pyindex 58d1bcef..0f22a0c0 100644--- a/mineru/model/layout/pp_doclayout_v2_base.py+++ b/mineru/model/layout/pp_doclayout_v2_base.py@@ -1,7 +1,7 @@# Copyright (c) Opendatalab. All rights reserved."""PP-DocLayoutV2 的后处理与可视化逻辑(与推理引擎无关)。-本模块只依赖 numpy / cv2 / PIL,不依赖 torch / transformers,+本模块使用 numpy / PIL 和 DocVortex 公共几何接口,不依赖 torch / transformers,供 transformers 版(`pp_doclayoutv2.py`)和 ONNX 版(`pp_doclayout_v2_onnx.py`)共同复用,保证两个后端的输出后处理完全一致。"""diff --git a/mineru/model/layout/pp_doclayout_v2_onnx.py b/mineru/model/layout/pp_doclayout_v2_onnx.pyindex 2aae71f6..34fa97bd 100644--- a/mineru/model/layout/pp_doclayout_v2_onnx.py+++ b/mineru/model/layout/pp_doclayout_v2_onnx.py@@ -39,7 +39,7 @@ class PPDocLayoutV2LayoutModelONNX(PPDocLayoutV2PostProcessor):差异:- 构造函数接收 onnx 文件路径(单个 ``.onnx``),而非 HF 模型目录- - 不依赖 transformers / torch,仅用 onnxruntime + numpy + cv2+ - 不依赖 transformers / torch,使用 onnxruntime、numpy 及 DocVortex 公共图像接口- 预处理固定为 800x800 BICUBIC + /255(与 PaddlePaddle 官方 inference.yml 一致)- 阅读顺序直接来自 ONNX 输出的第 6/7 列(lexsort),无需 reading_order head"""diff --git a/mineru/model/mfr/pp_formulanet_plus_m_onnx.py b/mineru/model/mfr/pp_formulanet_plus_m_onnx.pyindex cb729abe..7a67c807 100644--- a/mineru/model/mfr/pp_formulanet_plus_m_onnx.py+++ b/mineru/model/mfr/pp_formulanet_plus_m_onnx.py@@ -38,7 +38,7 @@ class PPFormulaNetPlusMONNX:差异:- 构造函数接收 onnx 文件路径 + yml 配置路径,而非 HF 模型目录- - 不依赖 torch / transformers,仅用 onnxruntime + numpy + cv2+ - 不依赖 torch / transformers,使用 onnxruntime、numpy 及共享图像预处理- 模型输出 token IDs(非 logits),自回归循环已 bake 进 ONNX 图"""diff --git a/mineru/model/ocr/seal_det_warp.py b/mineru/model/ocr/seal_det_warp.pyindex c15b9684..b08201ab 100644--- a/mineru/model/ocr/seal_det_warp.py+++ b/mineru/model/ocr/seal_det_warp.py@@ -12,25 +12,25 @@# See the License for the specific language governing permissions and# limitations under the License.-from docvortex.image import perspective_matrix, warp_image-import copyimport numpy as np+from docvortex.image import perspective_matrix, warp_imagefrom loguru import logger+from numpy import sqrtfrom PIL import Image, ImageDraw-from numpy import arctan, cos, sin, sqrtdef Homography(- image,- img_points,- world_width,- world_height,- interpolation=None,- ratio_width=1.0,- ratio_height=1.0,-):+ image: np.ndarray,+ img_points: np.ndarray,+ world_width: float,+ world_height: float,+ interpolation: str | int | None = None,+ ratio_width: float = 1.0,+ ratio_height: float = 1.0,+) -> np.ndarray:+ """按既有四点单应矩阵采样,保留插值、扩边和目标尺寸规则。"""if interpolation is None:interpolation = "cubic"@@ -58,129 +58,11 @@ def Homography(return dst_img-class PlanB:- def __call__(- self,- image,- points,- curveTextRectifier,- interpolation=None,- ratio_width=1.0,- ratio_height=1.0,- loss_thresh=5.0,- square=False,- ):- if interpolation is None:- interpolation = "linear"- h, w = image.shape[:2]- _points = np.array(points).reshape(-1, 2).astype(np.float32)- x_min = int(np.min(_points[:, 0]))- y_min = int(np.min(_points[:, 1]))- x_max = int(np.max(_points[:, 0]))- y_max = int(np.max(_points[:, 1]))- dx = x_max - x_min- dy = y_max - y_min- max_d = max(dx, dy)- mean_pt = np.mean(_points, 0)-- expand_x = (ratio_width - 1.0) * 0.5 * max_d- expand_y = (ratio_height - 1.0) * 0.5 * max_d-- if square:- x_min = np.clip(int(mean_pt[0] - max_d - expand_x), 0, w - 1)- y_min = np.clip(int(mean_pt[1] - max_d - expand_y), 0, h - 1)- x_max = np.clip(int(mean_pt[0] + max_d + expand_x), 0, w - 1)- y_max = np.clip(int(mean_pt[1] + max_d + expand_y), 0, h - 1)- else:- x_min = np.clip(int(x_min - expand_x), 0, w - 1)- y_min = np.clip(int(y_min - expand_y), 0, h - 1)- x_max = np.clip(int(x_max + expand_x), 0, w - 1)- y_max = np.clip(int(y_max + expand_y), 0, h - 1)-- new_image = image[y_min:y_max, x_min:x_max, :].copy()- new_points = _points.copy()- new_points[:, 0] -= x_min- new_points[:, 1] -= y_min-- dst_img, loss = curveTextRectifier(- new_image,- new_points,- interpolation,- ratio_width,- ratio_height,- mode="calibration",- )-- return dst_img, loss--class CurveTextRectifier:- def __init__(self):- self.get_virtual_camera_parameter()-- def get_virtual_camera_parameter(self):- vcam_thz = 0- vcam_thx1 = 180- vcam_thy = 180- vcam_thx2 = 0-- vcam_x = 0- vcam_y = 0- vcam_z = 100-- radian = np.pi / 180-- angle_z = radian * vcam_thz- angle_x1 = radian * vcam_thx1- angle_y = radian * vcam_thy- angle_x2 = radian * vcam_thx2-- optic_x = vcam_x- optic_y = vcam_y- optic_z = vcam_z-- fu = 100- fv = 100-- matT = np.zeros((4, 4))- matT[0, 0] = cos(angle_z) * cos(angle_y) - sin(angle_z) * sin(angle_x1) * sin(angle_y)- matT[0, 1] = cos(angle_z) * sin(angle_y) * sin(angle_x2) - sin(angle_z) * (- cos(angle_x1) * cos(angle_x2) - sin(angle_x1) * cos(angle_y) * sin(angle_x2)- )- matT[0, 2] = cos(angle_z) * sin(angle_y) * cos(angle_x2) + sin(angle_z) * (- cos(angle_x1) * sin(angle_x2) + sin(angle_x1) * cos(angle_y) * cos(angle_x2)- )- matT[0, 3] = optic_x- matT[1, 0] = sin(angle_z) * cos(angle_y) + cos(angle_z) * sin(angle_x1) * sin(angle_y)- matT[1, 1] = sin(angle_z) * sin(angle_y) * sin(angle_x2) + cos(angle_z) * (- cos(angle_x1) * cos(angle_x2) - sin(angle_x1) * cos(angle_y) * sin(angle_x2)- )- matT[1, 2] = sin(angle_z) * sin(angle_y) * cos(angle_x2) - cos(angle_z) * (- cos(angle_x1) * sin(angle_x2) + sin(angle_x1) * cos(angle_y) * cos(angle_x2)- )- matT[1, 3] = optic_y- matT[2, 0] = -cos(angle_x1) * sin(angle_y)- matT[2, 1] = cos(angle_x1) * cos(angle_y) * sin(angle_x2) + sin(angle_x1) * cos(angle_x2)- matT[2, 2] = cos(angle_x1) * cos(angle_y) * cos(angle_x2) - sin(angle_x1) * sin(angle_x2)- matT[2, 3] = optic_z- matT[3, 0] = 0- matT[3, 1] = 0- matT[3, 2] = 0- matT[3, 3] = 1-- matS = np.zeros((4, 4))- matS[2, 3] = 0.5- matS[3, 2] = 0.5-- self.ifu = 1 / fu- self.ifv = 1 / fv-- self.matT = matT- self.matS = matS- self.K = np.dot(matT.T, matS)- self.K = np.dot(self.K, matT)-- def vertical_text_process(self, points, org_size):+ def vertical_text_process(+ self, points: np.ndarray | list[float] | list[list[float]], org_size: tuple[int, int]+ ) -> tuple[np.ndarray, np.ndarray, tuple[int, int]]:+ """竖排点序转入原横排展开逻辑,再恢复单应变换使用的坐标。"""org_w, org_h = org_size_points = np.array(points).reshape(-1).tolist()_points = np.array(_points[2:] + _points[:2]).reshape(-1, 2)@@ -200,7 +82,10 @@ class CurveTextRectifier:return image_coord, world_coord, new_image_size- def horizontal_text_process(self, points):+ def horizontal_text_process(+ self, points: np.ndarray | list[float] | list[list[float]]+ ) -> tuple[np.ndarray, np.ndarray, tuple[int, int]]:+ """沿上下边界的既有距离计算展开尺寸和对应平面坐标。"""poly = np.array(points).reshape(-1)dx_list = []@@ -258,7 +143,8 @@ class CurveTextRectifier:return image_coord, world_coord, new_image_size- def horizontal_text_estimate(self, points):+ def horizontal_text_estimate(self, points: np.ndarray | list[float] | list[list[float]]) -> bool:+ """沿用原外接框宽高比判定横竖方向。"""pts = np.array(points).reshape(-1, 2)x_min = int(np.min(pts[:, 0]))y_min = int(np.min(pts[:, 1]))@@ -271,196 +157,17 @@ class CurveTextRectifier:is_horizontal_text = Falsereturn is_horizontal_text- def virtual_camera_to_world(self, size):- ifu, ifv = self.ifu, self.ifv- K, matT = self.K, self.matT-- ppu = size[0] / 2 + 1e-6- ppv = size[1] / 2 + 1e-6-- P = np.zeros((size[1], size[0], 3))-- lu = np.array([i for i in range(size[0])])- lv = np.array([i for i in range(size[1])])- u, v = np.meshgrid(lu, lv)-- yp = (v - ppv) * ifv- xp = (u - ppu) * ifu- angle_a = arctan(sqrt(xp * xp + yp * yp))- angle_b = arctan(yp / xp)-- D0 = sin(angle_a) * cos(angle_b)- D1 = sin(angle_a) * sin(angle_b)- D2 = cos(angle_a)-- D0[xp <= 0] = -D0[xp <= 0]- D1[xp <= 0] = -D1[xp <= 0]-- ratio_a = (- K[0, 0] * D0 * D0- + K[1, 1] * D1 * D1- + K[2, 2] * D2 * D2- + (K[0, 1] + K[1, 0]) * D0 * D1- + (K[0, 2] + K[2, 0]) * D0 * D2- + (K[1, 2] + K[2, 1]) * D1 * D2- )- ratio_b = (K[0, 3] + K[3, 0]) * D0 + (K[1, 3] + K[3, 1]) * D1 + (K[2, 3] + K[3, 2]) * D2- ratio_c = K[3, 3] * np.ones(ratio_b.shape)-- delta = ratio_b * ratio_b - 4 * ratio_a * ratio_c- t = np.zeros(delta.shape)- t[ratio_a == 0] = -ratio_c[ratio_a == 0] / ratio_b[ratio_a == 0]- t[ratio_a != 0] = (-ratio_b[ratio_a != 0] + sqrt(delta[ratio_a != 0])) / (2 * ratio_a[ratio_a != 0])- t[delta < 0] = 0-- P[:, :, 0] = matT[0, 3] + t * (matT[0, 0] * D0 + matT[0, 1] * D1 + matT[0, 2] * D2)- P[:, :, 1] = matT[1, 3] + t * (matT[1, 0] * D0 + matT[1, 1] * D1 + matT[1, 2] * D2)- P[:, :, 2] = matT[2, 3] + t * (matT[2, 0] * D0 + matT[2, 1] * D1 + matT[2, 2] * D2)-- return P-- def world_to_image(self, image_size, world, intrinsic, distCoeffs, rotation, tvec):- r11 = rotation[0, 0]- r12 = rotation[0, 1]- r13 = rotation[0, 2]- r21 = rotation[1, 0]- r22 = rotation[1, 1]- r23 = rotation[1, 2]- r31 = rotation[2, 0]- r32 = rotation[2, 1]- r33 = rotation[2, 2]-- t1 = tvec[0]- t2 = tvec[1]- t3 = tvec[2]-- k1 = distCoeffs[0]- k2 = distCoeffs[1]- p1 = distCoeffs[2]- p2 = distCoeffs[3]- k3 = distCoeffs[4]- k4 = distCoeffs[5]- k5 = distCoeffs[6]- k6 = distCoeffs[7]-- if len(distCoeffs) > 8:- s1 = distCoeffs[8]- s2 = distCoeffs[9]- s3 = distCoeffs[10]- s4 = distCoeffs[11]- else:- s1 = s2 = s3 = s4 = 0-- if len(distCoeffs) > 12:- tx = distCoeffs[12]- ty = distCoeffs[13]- else:- tx = ty = 0-- fu = intrinsic[0, 0]- fv = intrinsic[1, 1]- ppu = intrinsic[0, 2]- ppv = intrinsic[1, 2]-- cos_tx = cos(tx)- cos_ty = cos(ty)- sin_tx = sin(tx)- sin_ty = sin(ty)-- tao11 = cos_ty * cos_tx * cos_ty + sin_ty * cos_tx * sin_ty- tao12 = cos_ty * cos_tx * sin_ty * sin_tx - sin_ty * cos_tx * cos_ty * sin_tx- tao13 = -cos_ty * cos_tx * sin_ty * cos_tx + sin_ty * cos_tx * cos_ty * cos_tx- tao21 = -sin_tx * sin_ty- tao22 = cos_ty * cos_tx * cos_tx + sin_tx * cos_ty * sin_tx- tao23 = cos_ty * cos_tx * sin_tx - sin_tx * cos_ty * cos_tx-- P = np.zeros((image_size[1], image_size[0], 2))-- c3 = r31 * world[:, :, 0] + r32 * world[:, :, 1] + r33 * world[:, :, 2] + t3- c1 = r11 * world[:, :, 0] + r12 * world[:, :, 1] + r13 * world[:, :, 2] + t1- c2 = r21 * world[:, :, 0] + r22 * world[:, :, 1] + r23 * world[:, :, 2] + t2-- x1 = c1 / c3- y1 = c2 / c3- x12 = x1 * x1- y12 = y1 * y1- x1y1 = 2 * x1 * y1- r2 = x12 + y12- r4 = r2 * r2- r6 = r2 * r4-- radial_distortion = (1 + k1 * r2 + k2 * r4 + k3 * r6) / (1 + k4 * r2 + k5 * r4 + k6 * r6)- x2 = x1 * radial_distortion + p1 * x1y1 + p2 * (r2 + 2 * x12) + s1 * r2 + s2 * r4- y2 = y1 * radial_distortion + p2 * x1y1 + p1 * (r2 + 2 * y12) + s3 * r2 + s4 * r4-- x3 = tao11 * x2 + tao12 * y2 + tao13- y3 = tao21 * x2 + tao22 * y2 + tao23-- P[:, :, 0] = fu * x3 + ppu- P[:, :, 1] = fv * y3 + ppv- P[c3 <= 0] = 0-- return P-- def spatial_transform(self, image_data, new_image_size, mtx, dist, rvecs, tvecs, interpolation):- import cv2-- rotation, _ = cv2.Rodrigues(rvecs)- world_map = self.virtual_camera_to_world(new_image_size)- image_map = self.world_to_image(new_image_size, world_map, mtx, dist, rotation, tvecs)- image_map = image_map.astype(np.float32)- if isinstance(interpolation, str):- interpolation = {"nearest": 0, "linear": 1, "cubic": 2, "area": 3, "lanczos4": 4}[interpolation]- dst = cv2.remap(image_data, image_map[:, :, 0], image_map[:, :, 1], interpolation)- return dst-- def calibrate(self, org_size, image_coord, world_coord):- """只在明确选择旧相机标定时按需使用 OpenCV,保留历史标定参数。"""- import cv2-- flag = cv2.CALIB_RATIONAL_MODEL- flag2 = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_TILTED_MODEL- flag3 = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_THIN_PRISM_MODEL- flag4 = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_ZERO_TANGENT_DIST | cv2.CALIB_FIX_ASPECT_RATIO- flag5 = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_TILTED_MODEL | cv2.CALIB_ZERO_TANGENT_DIST- flag6 = cv2.CALIB_RATIONAL_MODEL | cv2.CALIB_FIX_ASPECT_RATIO- flag_list = [flag2, flag3, flag4, flag5, flag6]-- ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(- world_coord.astype(np.float32),- image_coord.astype(np.float32),- org_size,- None,- None,- flags=flag,- )- if ret > 2:- min_ret = ret- for flag in flag_list:- _ret, _mtx, _dist, _rvecs, _tvecs = cv2.calibrateCamera(- world_coord.astype(np.float32),- image_coord.astype(np.float32),- org_size,- None,- None,- flags=flag,- )- if _ret < min_ret:- min_ret = _ret- ret, mtx, dist, rvecs, tvecs = _ret, _mtx, _dist, _rvecs, _tvecs-- return ret, mtx, dist, rvecs, tvecs-def dc_homo(self,- img,- img_points,- obj_points,- is_horizontal_text,- interpolation=None,- ratio_width=1.0,- ratio_height=1.0,- ):+ img: np.ndarray,+ img_points: np.ndarray,+ obj_points: np.ndarray,+ is_horizontal_text: bool,+ interpolation: str | int | None = None,+ ratio_width: float = 1.0,+ ratio_height: float = 1.0,+ ) -> np.ndarray:+ """逐段单应展开后按原高度拼接,竖排结果保留原旋转方向。"""if interpolation is None:interpolation = "linear"@@ -516,58 +223,50 @@ class CurveTextRectifier:def __call__(self,- image_data,- points,- interpolation=None,- ratio_width=1.0,- ratio_height=1.0,- mode="calibration",- ):+ image_data: np.ndarray,+ points: np.ndarray | list[float] | list[list[float]],+ interpolation: str | int | None = None,+ ratio_width: float = 1.0,+ ratio_height: float = 1.0,+ mode: str = "homography",+ ) -> tuple[np.ndarray, float]:+ """只执行既有分段单应变换,旧相机标定模式明确拒绝。"""+ if mode.lower() != "homography":+ raise ValueError(f'Only mode="homography" is supported, got {mode!r}')if interpolation is None:interpolation = "linear"-org_h, org_w = image_data.shape[:2]- org_size = (org_w, org_h)- self.image = image_data-is_horizontal_text = self.horizontal_text_estimate(points)if is_horizontal_text:- image_coord, world_coord, new_image_size = self.horizontal_text_process(points)+ image_coord, world_coord, _ = self.horizontal_text_process(points)else:- image_coord, world_coord, new_image_size = self.vertical_text_process(points, org_size)-- if mode.lower() == "calibration":- ret, mtx, dist, rvecs, tvecs = self.calibrate(org_size, image_coord, world_coord)-- st_size = (- int(new_image_size[0] * ratio_width),- int(new_image_size[1] * ratio_height),- )- dst = self.spatial_transform(image_data, st_size, mtx, dist[0], rvecs[0], tvecs[0], interpolation)- elif mode.lower() == "homography":- ret = 0.01- dst = self.dc_homo(- image_data,- image_coord,- world_coord,- is_horizontal_text,- interpolation=interpolation,- ratio_width=1.0,- ratio_height=1.0,- )- else:- raise ValueError('mode must be ["calibration", "homography"], but got {}'.format(mode))-- return dst, ret+ image_coord, world_coord, _ = self.vertical_text_process(points, (org_w, org_h))+ dst = self.dc_homo(+ image_data,+ image_coord,+ world_coord,+ is_horizontal_text,+ interpolation=interpolation,+ ratio_width=1.0,+ ratio_height=1.0,+ )+ return dst, 0.01class AutoRectifier:- def __init__(self):+ def __init__(self) -> None:+ """保留曲线文字点数阈值,初始化不再创建虚拟相机。"""self.npoints = 10- self.curveTextRectifier = CurveTextRectifier()@staticmethod- def get_rotate_crop_image(img, points, interpolation=None, ratio_width=1.0, ratio_height=1.0):+ def get_rotate_crop_image(+ img: np.ndarray,+ points: np.ndarray | list[float] | list[list[float]],+ interpolation: str | int | None = None,+ ratio_width: float = 1.0,+ ratio_height: float = 1.0,+ ) -> np.ndarray:+ """四点使用既有透视裁图,其他点数保留独立外接矩形裁片。"""if interpolation is None:interpolation = "cubic"h, w = img.shape[:2]@@ -614,7 +313,7 @@ class AutoRectifier:return dst_img- def visualize(self, image_data, points_list):+ def visualize(self, image_data: np.ndarray, points_list: list[list[float] | list[list[float]]]) -> np.ndarray:"""使用 Pillow 标注印章矫正点,返回与输入同通道顺序的诊断图。"""with Image.fromarray(image_data[:, :, ::-1]) as canvas:draw = ImageDraw.Draw(canvas)@@ -627,78 +326,40 @@ class AutoRectifier:def __call__(self,- image_data,- points,- interpolation=None,- ratio_width=1.0,- ratio_height=1.0,- loss_thresh=5.0,- mode="calibration",- ):+ image_data: np.ndarray,+ points: np.ndarray | list[float] | list[list[float]],+ interpolation: str | int | None = None,+ ratio_width: float = 1.0,+ ratio_height: float = 1.0,+ mode: str = "homography",+ ) -> np.ndarray:+ """默认使用曲线单应矫正,几何异常保留原有外接矩形裁图回退。"""+ if mode.lower() != "homography":+ raise ValueError(f'Only mode="homography" is supported, got {mode!r}')if interpolation is None:interpolation = "linear"_points = np.array(points).reshape(-1, 2)if len(_points) >= self.npoints and len(_points) % 2 == 0:try:- curveTextRectifier = CurveTextRectifier()-- dst_img, loss = curveTextRectifier(image_data, points, interpolation, ratio_width, ratio_height, mode)- if loss >= 2:- img_list, loss_list = [dst_img], [loss]- _dst_img, _loss = PlanB()(- image_data,- points,- curveTextRectifier,- interpolation,- ratio_width,- ratio_height,- loss_thresh=loss_thresh,- square=True,- )- img_list += [_dst_img]- loss_list += [_loss]-- _dst_img, _loss = PlanB()(- image_data,- points,- curveTextRectifier,- interpolation,- ratio_width,- ratio_height,- loss_thresh=loss_thresh,- square=False,- )- img_list += [_dst_img]- loss_list += [_loss]-- min_loss = min(loss_list)- dst_img = img_list[loss_list.index(min_loss)]-- if min_loss >= loss_thresh:- logger.warning(- "calibration loss: {} is too large for spatial transformer. It is failed. Using get_rotate_crop_image".format(- loss- )- )- dst_img = self.get_rotate_crop_image(image_data, points, interpolation, ratio_width, ratio_height)+ curve_text_rectifier = CurveTextRectifier()+ dst_img, _ = curve_text_rectifier(image_data, points, interpolation, ratio_width, ratio_height, mode)except Exception as e:logger.warning(f"Exception caught: {e}")dst_img = self.get_rotate_crop_image(image_data, points, interpolation, ratio_width, ratio_height)else:dst_img = self.get_rotate_crop_image(image_data, _points, interpolation, ratio_width, ratio_height)-return dst_imgdef run(self,- image_data,- points_list,- interpolation=None,- ratio_width=1.0,- ratio_height=1.0,- loss_thresh=5.0,- mode="calibration",- ):+ image_data: np.ndarray,+ points_list: list[list[float] | list[list[float]]],+ interpolation: str | int | None = None,+ ratio_width: float = 1.0,+ ratio_height: float = 1.0,+ mode: str = "homography",+ ) -> tuple[list[np.ndarray], np.ndarray]:+ """批量单应矫正并返回诊断图,不再提供相机标定或损失阈值参数。"""if image_data is None:raise ValueErrorif not isinstance(points_list, list):@@ -708,23 +369,19 @@ class AutoRectifier:raise ValueErrorif interpolation is None:interpolation = "linear"-if ratio_width < 1.0 or ratio_height < 1.0:raise ValueError("ratio_width and ratio_height cannot be smaller than 1, but got {}",(ratio_width, ratio_height),)-- if mode.lower() != "calibration" and mode.lower() != "homography":- raise ValueError('mode must be ["calibration", "homography"], but got {}'.format(mode))-+ if mode.lower() != "homography":+ raise ValueError(f'Only mode="homography" is supported, got {mode!r}')if mode.lower() == "homography" and ratio_width != 1.0 and ratio_height != 1.0:raise ValueError("ratio_width and ratio_height must be 1.0 when mode is homography, but got mode:{}, ratio:({},{})".format(mode, ratio_width, ratio_height))-res = []for points in points_list:rectified_img = self(@@ -733,11 +390,8 @@ class AutoRectifier:interpolation,ratio_width,ratio_height,- loss_thresh=loss_thresh,mode=mode,)res.append(rectified_img)-visualized_image = self.visualize(image_data, points_list)-return res, visualized_imagediff --git a/mineru/model/table/rec/unet_table/utils.py b/mineru/model/table/rec/unet_table/utils.pyindex c77e60c5..69cc731b 100644--- a/mineru/model/table/rec/unet_table/utils.py+++ b/mineru/model/table/rec/unet_table/utils.py@@ -229,12 +229,11 @@ def imresize(img, size, return_scale=False, interpolation="bilinear", out=None,size (tuple[int]): Target size (w, h).return_scale (bool): Whether to return `w_scale` and `h_scale`.interpolation (str): Interpolation method, accepted values are- "nearest", "bilinear", "bicubic", "area", "lanczos" for 'cv2'+ "nearest", "bilinear", "bicubic", "area", "lanczos" for 'numeric'backend, "nearest", "bilinear" for 'pillow' backend.out (ndarray): The output destination.- backend (str | None): The image resize backend type. Options are `cv2`,- `pillow`, `None`. If backend is None, the global imread_backend- specified by ``mmcv.use_backend()`` will be used. Default: None.+ backend (str | None): The image resize backend type. Options are+ `numeric`, `pillow`, `None`. None selects the shared numeric kernel.Returns:tuple | ndarray: (`resized_img`, `w_scale`, `h_scale`) or@@ -243,7 +242,7 @@ def imresize(img, size, return_scale=False, interpolation="bilinear", out=None,h, w = img.shape[:2]if backend is None:backend = "numeric"- if backend not in ["numeric", "cv2", "pillow"]:+ if backend not in ["numeric", "pillow"]:raise ValueError(f"backend: {backend} is not supported for resize.Supported backends are 'numeric', 'pillow'")if backend == "pillow":diff --git a/tests/fixtures/model_image_reference.json b/tests/fixtures/model_image_reference.jsonnew file mode 100644index 00000000..ee0ca5e2--- /dev/null+++ b/tests/fixtures/model_image_reference.json@@ -0,0 +1,1160 @@+{+ "provenance": {+ "baseline_commit": "e6d89a6af4d0ab930af285a24f4a157225dbc733",+ "source_sha256": "5d1a8b44bd7a05c9557bafc437fcce2001d11c74bdd553735b5079b68a1bd6a7",+ "opencv": "5.0.0.93",+ "numpy": "2.5.3",+ "note": "Model expectations computed by original OpenCV oracle tests; seal expectations from original explicit homography path"+ },+ "model_arrays": {+ "rgb:uint8:1": [+ {+ "shape": [+ 4,+ 5,+ 3+ ],+ "dtype": "uint8",+ "sha256": "effc4496e18ebf2f7303177f1f27c781614c132999a1d2e36a17310e76b25a7a"+ }+ ],+ "rgb:uint8:3": [+ {+ "shape": [+ 4,+ 5,+ 3+ ],+ "dtype": "uint8",+ "sha256": "31f9b2769cf290095cd08e7fd3be523fc8c7f56fa32ec0961be269c1ee065ecf"+ }+ ],+ "rgb:uint8:4": [+ {+ "shape": [+ 4,+ 5,+ 3+ ],+ "dtype": "uint8",+ "sha256": "33435c760dd105ee5bec40e62871b93b5bfe8294b6f1d2fa796293341f388468"+ }+ ],+ "rgb:uint16:1": [+ {+ "shape": [+ 4,+ 5,+ 3+ ],+ "dtype": "uint16",+ "sha256": "dbc7d5200c19d4484e623c4bc61439625701149a634f894db23860d4653b252b"+ }+ ],+ "rgb:uint16:3": [+ {+ "shape": [+ 4,+ 5,+ 3+ ],+ "dtype": "uint16",+ "sha256": "93ff30b8b398484f98654df175605667864b729591105206e804c38a79689726"+ }+ ],+ "rgb:uint16:4": [+ {+ "shape": [+ 4,+ 5,+ 3+ ],+ "dtype": "uint16",+ "sha256": "752b14c2db72e08364271cacfbd907079c9c27640884b8fa015f6ad508960eac"+ }+ ],+ "rgb:float32:1": [+ {+ "shape": [+ 4,+ 5,+ 3+ ],+ "dtype": "float32",+ "sha256": "5a8aae1e750fc29498e0f9ff4e3f6bcd117ee87a502fa5a96c68e58b5365128a"+ }+ ],+ "rgb:float32:3": [+ {+ "shape": [+ 4,+ 5,+ 3+ ],+ "dtype": "float32",+ "sha256": "087bd6c1b5908bcde5ce0132735eed2c0004b3d3248c80f5a10066b939368305"+ }+ ],+ "rgb:float32:4": [+ {+ "shape": [+ 4,+ 5,+ 3+ ],+ "dtype": "float32",+ "sha256": "f4f113784a7c5039a787fee2fec502f516fd8078334a64e6dea3967546f19946"+ }+ ],+ "gray_alpha": [+ {+ "shape": [+ 17,+ 13,+ 3+ ],+ "dtype": "uint8",+ "sha256": "671325297a2860befda321cb2f14a87e7d4bc845241123f31413ab4470ea9f02"+ },+ {+ "shape": [+ 17,+ 13,+ 3+ ],+ "dtype": "uint8",+ "sha256": "d3c64fde6b2d244a4d90cf0e968e154c63ec4c3ff4aa6d23118b90421f45560e"+ }+ ],+ "rotation:90": [+ {+ "shape": [+ 5,+ 4,+ 3+ ],+ "dtype": "uint8",+ "sha256": "1e5100ea3129f7356c33ea3bd12816b8f19c10e1365638e3541acceb29ac0c24"+ }+ ],+ "rotation:270": [+ {+ "shape": [+ 5,+ 4,+ 3+ ],+ "dtype": "uint8",+ "sha256": "09a48655dc13d06c0b8c9b4d1e0aeb51e3e89f27a22a4f6aee71ffa99ef921b5"+ }+ ],+ "rotation:0": [+ {+ "shape": [+ 4,+ 5,+ 3+ ],+ "dtype": "uint8",+ "sha256": "0af9cbd869052aee260bd2cf0e23e9c6f958a8c9d20edfc7cef6250270555ad2"+ }+ ],+ "perspective": [+ {+ "shape": [+ 12,+ 21,+ 3+ ],+ "dtype": "uint16",+ "sha256": "42244fdb72bb3d7338bbdafc6eb28f1a3e2176b615e52c518249eb0075088966"+ }+ ],+ "formula:False": [+ {+ "shape": [+ 17,+ 19,+ 3+ ],+ "dtype": "float32",+ "sha256": "8cee4bf0a38f9559709d3a4bf74c914508bdf160fc7383bcf8b94c40f1f43f53"+ }+ ],+ "formula:True": [+ {+ "shape": [+ 17,+ 19,+ 3+ ],+ "dtype": "float32",+ "sha256": "2a064df8b9316bc9d9a23ed2ee347e3720f9902910b3f930fbace104e211339a"+ }+ ],+ "margin:1:1": [],+ "margin:1:17": [+ {+ "shape": [+ 1,+ 17+ ],+ "dtype": "uint8",+ "sha256": "a97aa9c9e3554035ea0cbed4c6b5aeeca527849703098cc6c858116d98b78fa8"+ }+ ],+ "margin:19:1": [+ {+ "shape": [+ 19,+ 1+ ],+ "dtype": "uint8",+ "sha256": "9dd5d5924b297d7d3c0f405700da3a600ce690ab028b96668da15ec159f3cae7"+ }+ ],+ "margin:23:27": [+ {+ "shape": [+ 23,+ 27+ ],+ "dtype": "uint8",+ "sha256": "b381f8d0a12b4b0d28d5f31951215ea27b91757acf4192cde18229ebdb4caaf4"+ }+ ],+ "table_classifier": [+ {+ "shape": [+ 1,+ 3,+ 224,+ 224+ ],+ "dtype": "float32",+ "sha256": "a80f4d4ea376eb0aebd45225010204f92b3ab50fe55d77ba1bf248d1209574a8"+ },+ {+ "shape": [+ 1,+ 3,+ 224,+ 224+ ],+ "dtype": "float32",+ "sha256": "a80f4d4ea376eb0aebd45225010204f92b3ab50fe55d77ba1bf248d1209574a8"+ }+ ],+ "unimer:None": [+ {+ "shape": [+ 12,+ 19+ ],+ "dtype": "uint8",+ "sha256": "fb678f67aea5293efa9930a41b828fdfb475dc2b427628588640e080884f0e45"+ },+ {+ "shape": [+ 32,+ 64+ ],+ "dtype": "uint8",+ "sha256": "e5a00aa9991ac8a5ee3109844d84a55583bd20572ad3ffcd42792f3c36b183ad"+ }+ ],+ "unimer:1": [+ {+ "shape": [+ 12,+ 19,+ 1+ ],+ "dtype": "uint8",+ "sha256": "fb678f67aea5293efa9930a41b828fdfb475dc2b427628588640e080884f0e45"+ },+ {+ "shape": [+ 32,+ 64+ ],+ "dtype": "uint8",+ "sha256": "e5a00aa9991ac8a5ee3109844d84a55583bd20572ad3ffcd42792f3c36b183ad"+ }+ ],+ "unimer:3": [+ {+ "shape": [+ 12,+ 19,+ 3+ ],+ "dtype": "uint8",+ "sha256": "101bb8aeab946925b14006ee79fbfa56dcf5b2a65c44ca9b435abbd1abe3a22b"+ },+ {+ "shape": [+ 32,+ 64,+ 3+ ],+ "dtype": "uint8",+ "sha256": "fd9243e1ba57263ed469c3bdbd7ade6ec5254e7ed924a9f5737fa44749933cc0"+ }+ ],+ "unet": [+ {+ "shape": [+ 1,+ 3,+ 32,+ 52+ ],+ "dtype": "float32",+ "sha256": "0eca8260fd75612c33f5c1e679ee496bbc95eee1363cc0565695e677e7573f89"+ }+ ]+ },+ "seal_cases": [+ {+ "name": "horizontal",+ "interpolation": "nearest",+ "points": [+ [+ 10.0,+ 22.0+ ],+ [+ 26.0,+ 18.0+ ],+ [+ 42.0,+ 16.0+ ],+ [+ 58.0,+ 18.0+ ],+ [+ 75.0,+ 22.0+ ],+ [+ 75.0,+ 46.0+ ],+ [+ 58.0,+ 42.0+ ],+ [+ 42.0,+ 40.0+ ],+ [+ 26.0,+ 42.0+ ],+ [+ 10.0,+ 46.0+ ]+ ],+ "crop": {+ "shape": [+ 24,+ 65,+ 3+ ],+ "dtype": "uint8",+ "sha256": "4a41d5ee51afb59db0424e65f2052b9664c11cdf2cb3752192c7f023592f8190"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "03038ceea353c5f29692bdea16a50166a61ffd5eb99e74f97564c7543796983e"+ }+ },+ {+ "name": "horizontal",+ "interpolation": "linear",+ "points": [+ [+ 10.0,+ 22.0+ ],+ [+ 26.0,+ 18.0+ ],+ [+ 42.0,+ 16.0+ ],+ [+ 58.0,+ 18.0+ ],+ [+ 75.0,+ 22.0+ ],+ [+ 75.0,+ 46.0+ ],+ [+ 58.0,+ 42.0+ ],+ [+ 42.0,+ 40.0+ ],+ [+ 26.0,+ 42.0+ ],+ [+ 10.0,+ 46.0+ ]+ ],+ "crop": {+ "shape": [+ 24,+ 65,+ 3+ ],+ "dtype": "uint8",+ "sha256": "1c8e3bc851a4e00534b18c5827a62a751309552e329abf23659fa14051aa0997"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "03038ceea353c5f29692bdea16a50166a61ffd5eb99e74f97564c7543796983e"+ }+ },+ {+ "name": "horizontal",+ "interpolation": "cubic",+ "points": [+ [+ 10.0,+ 22.0+ ],+ [+ 26.0,+ 18.0+ ],+ [+ 42.0,+ 16.0+ ],+ [+ 58.0,+ 18.0+ ],+ [+ 75.0,+ 22.0+ ],+ [+ 75.0,+ 46.0+ ],+ [+ 58.0,+ 42.0+ ],+ [+ 42.0,+ 40.0+ ],+ [+ 26.0,+ 42.0+ ],+ [+ 10.0,+ 46.0+ ]+ ],+ "crop": {+ "shape": [+ 24,+ 65,+ 3+ ],+ "dtype": "uint8",+ "sha256": "9a0b7a3e429586d62af6f9307a478fec0779d3f49e67ca119c86b080515bd6f5"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "03038ceea353c5f29692bdea16a50166a61ffd5eb99e74f97564c7543796983e"+ }+ },+ {+ "name": "vertical",+ "interpolation": "nearest",+ "points": [+ [+ 22.0,+ 10.0+ ],+ [+ 18.0,+ 26.0+ ],+ [+ 16.0,+ 42.0+ ],+ [+ 18.0,+ 58.0+ ],+ [+ 22.0,+ 75.0+ ],+ [+ 46.0,+ 75.0+ ],+ [+ 42.0,+ 58.0+ ],+ [+ 40.0,+ 42.0+ ],+ [+ 42.0,+ 26.0+ ],+ [+ 46.0,+ 10.0+ ]+ ],+ "crop": {+ "shape": [+ 72,+ 31,+ 3+ ],+ "dtype": "uint8",+ "sha256": "97b2486105a0fbfe3ecab69dba04c907278c609079e5e6dc722fd0d0e873e61d"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "ec0a41722a43f203eb9326507454d61c5bc358af61bb60c8c8f7ba9824d7eac5"+ }+ },+ {+ "name": "vertical",+ "interpolation": "linear",+ "points": [+ [+ 22.0,+ 10.0+ ],+ [+ 18.0,+ 26.0+ ],+ [+ 16.0,+ 42.0+ ],+ [+ 18.0,+ 58.0+ ],+ [+ 22.0,+ 75.0+ ],+ [+ 46.0,+ 75.0+ ],+ [+ 42.0,+ 58.0+ ],+ [+ 40.0,+ 42.0+ ],+ [+ 42.0,+ 26.0+ ],+ [+ 46.0,+ 10.0+ ]+ ],+ "crop": {+ "shape": [+ 72,+ 31,+ 3+ ],+ "dtype": "uint8",+ "sha256": "2468967cf1721f2d0c0ce39922904ac2b8a874e7a4e0bb0934beaa74f2c1929b"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "ec0a41722a43f203eb9326507454d61c5bc358af61bb60c8c8f7ba9824d7eac5"+ }+ },+ {+ "name": "vertical",+ "interpolation": "cubic",+ "points": [+ [+ 22.0,+ 10.0+ ],+ [+ 18.0,+ 26.0+ ],+ [+ 16.0,+ 42.0+ ],+ [+ 18.0,+ 58.0+ ],+ [+ 22.0,+ 75.0+ ],+ [+ 46.0,+ 75.0+ ],+ [+ 42.0,+ 58.0+ ],+ [+ 40.0,+ 42.0+ ],+ [+ 42.0,+ 26.0+ ],+ [+ 46.0,+ 10.0+ ]+ ],+ "crop": {+ "shape": [+ 72,+ 31,+ 3+ ],+ "dtype": "uint8",+ "sha256": "b599b0f137d874a562e136ab259ecd7f6f3af4bd208414b39c41c219ce997d6d"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "ec0a41722a43f203eb9326507454d61c5bc358af61bb60c8c8f7ba9824d7eac5"+ }+ },+ {+ "name": "quadrilateral",+ "interpolation": "nearest",+ "points": [+ [+ 8.0,+ 8.0+ ],+ [+ 75.0,+ 10.0+ ],+ [+ 73.0,+ 65.0+ ],+ [+ 10.0,+ 63.0+ ]+ ],+ "crop": {+ "shape": [+ 55,+ 67,+ 3+ ],+ "dtype": "uint8",+ "sha256": "92e760ddfbe96151638278f2025b1a7c48ee8f9dc9dd42da91990d368373201f"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "699407a60fc44e994d01ad1fb0cdf8d88ec39af40c76db6cc2143b8054f32ea9"+ }+ },+ {+ "name": "quadrilateral",+ "interpolation": "linear",+ "points": [+ [+ 8.0,+ 8.0+ ],+ [+ 75.0,+ 10.0+ ],+ [+ 73.0,+ 65.0+ ],+ [+ 10.0,+ 63.0+ ]+ ],+ "crop": {+ "shape": [+ 55,+ 67,+ 3+ ],+ "dtype": "uint8",+ "sha256": "a1cb2cb5cfecfea6ca4b0b1a0e10f3feb302155d48303765dedad949b434e786"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "699407a60fc44e994d01ad1fb0cdf8d88ec39af40c76db6cc2143b8054f32ea9"+ }+ },+ {+ "name": "quadrilateral",+ "interpolation": "cubic",+ "points": [+ [+ 8.0,+ 8.0+ ],+ [+ 75.0,+ 10.0+ ],+ [+ 73.0,+ 65.0+ ],+ [+ 10.0,+ 63.0+ ]+ ],+ "crop": {+ "shape": [+ 55,+ 67,+ 3+ ],+ "dtype": "uint8",+ "sha256": "8cd975bd2817c9899c4f584a67b5a7e9e5d6eef5a637577f7e6dae9500c16c9b"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "699407a60fc44e994d01ad1fb0cdf8d88ec39af40c76db6cc2143b8054f32ea9"+ }+ },+ {+ "name": "short_polygon",+ "interpolation": "nearest",+ "points": [+ [+ 8.0,+ 8.0+ ],+ [+ 30.0,+ 6.0+ ],+ [+ 75.0,+ 10.0+ ],+ [+ 74.0,+ 64.0+ ],+ [+ 30.0,+ 68.0+ ],+ [+ 10.0,+ 63.0+ ]+ ],+ "crop": {+ "shape": [+ 62,+ 67,+ 3+ ],+ "dtype": "uint8",+ "sha256": "efbd014f9315576d9f79c9a00574a1671f99c815bf90d79a829bc8f1f780f2e4"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "3fb27595b653247546f699cb063a9742203121bf13fd5ae55a157f0d59ef283c"+ }+ },+ {+ "name": "short_polygon",+ "interpolation": "linear",+ "points": [+ [+ 8.0,+ 8.0+ ],+ [+ 30.0,+ 6.0+ ],+ [+ 75.0,+ 10.0+ ],+ [+ 74.0,+ 64.0+ ],+ [+ 30.0,+ 68.0+ ],+ [+ 10.0,+ 63.0+ ]+ ],+ "crop": {+ "shape": [+ 62,+ 67,+ 3+ ],+ "dtype": "uint8",+ "sha256": "efbd014f9315576d9f79c9a00574a1671f99c815bf90d79a829bc8f1f780f2e4"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "3fb27595b653247546f699cb063a9742203121bf13fd5ae55a157f0d59ef283c"+ }+ },+ {+ "name": "short_polygon",+ "interpolation": "cubic",+ "points": [+ [+ 8.0,+ 8.0+ ],+ [+ 30.0,+ 6.0+ ],+ [+ 75.0,+ 10.0+ ],+ [+ 74.0,+ 64.0+ ],+ [+ 30.0,+ 68.0+ ],+ [+ 10.0,+ 63.0+ ]+ ],+ "crop": {+ "shape": [+ 62,+ 67,+ 3+ ],+ "dtype": "uint8",+ "sha256": "efbd014f9315576d9f79c9a00574a1671f99c815bf90d79a829bc8f1f780f2e4"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "3fb27595b653247546f699cb063a9742203121bf13fd5ae55a157f0d59ef283c"+ }+ },+ {+ "name": "degenerate",+ "interpolation": "nearest",+ "points": [+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ]+ ],+ "crop": {+ "shape": [+ 0,+ 0,+ 3+ ],+ "dtype": "uint8",+ "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "ca5ad26dd79043a063bd995b319d85aa90baf4aa8c10024adcdc93009229fd21"+ }+ },+ {+ "name": "degenerate",+ "interpolation": "linear",+ "points": [+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ]+ ],+ "crop": {+ "shape": [+ 0,+ 0,+ 3+ ],+ "dtype": "uint8",+ "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "ca5ad26dd79043a063bd995b319d85aa90baf4aa8c10024adcdc93009229fd21"+ }+ },+ {+ "name": "degenerate",+ "interpolation": "cubic",+ "points": [+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ],+ [+ 20.0,+ 20.0+ ]+ ],+ "crop": {+ "shape": [+ 0,+ 0,+ 3+ ],+ "dtype": "uint8",+ "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"+ },+ "visual": {+ "shape": [+ 96,+ 96,+ 3+ ],+ "dtype": "uint8",+ "sha256": "ca5ad26dd79043a063bd995b319d85aa90baf4aa8c10024adcdc93009229fd21"+ }+ }+ ]+}diff --git a/tests/unittest/test_opencv_boundaries.py b/tests/unittest/test_opencv_boundaries.pyindex 5592cc72..98c2dd43 100644--- a/tests/unittest/test_opencv_boundaries.py+++ b/tests/unittest/test_opencv_boundaries.py@@ -2,9 +2,12 @@from __future__ import annotations+import hashlib+import jsonimport subprocessimport sysfrom pathlib import Path+from typing import Anyimport numpy as npimport pytest@@ -16,6 +19,7 @@ from mineru.model.ocr.image import alpha_to_color, check_img, get_rotate_crop_imfrom mineru.model.table.cls.mineru_table_ori_cls import MineruTableOrientationClsModel_ROOT = Path(__file__).resolve().parents[2]+_REFERENCE = json.loads((_ROOT / "tests/fixtures/model_image_reference.json").read_text())_BLOCK_CV2 = '''import importlib.abcimport sys@@ -81,6 +85,7 @@ def test_real_flash_pdf_parse_and_all_renderers_without_opencv(tmp_path: Path, a"""import asynciofrom pathlib import Path+from typing import Anyfrom mineru.config import configfrom mineru.parser import parse, parse_asyncfrom mineru.parser.writer import FileBasedDataWriter@@ -126,90 +131,64 @@ assert "cv2" not in sys.modules""")+def _assert_array_reference(actual: np.ndarray, reference: dict[str, object]) -> None:+ """按形状、位深和全部逻辑像素验证冻结参考,避免候选代码自证。"""+ assert list(actual.shape) == reference["shape"]+ assert str(actual.dtype) == reference["dtype"]+ assert hashlib.sha256(actual.tobytes()).hexdigest() == reference["sha256"]+++def _assert_model_reference(actual: np.ndarray, key: str, index: int = 0) -> None:+ """读取明确命名的既有模型参考结果。"""+ _assert_array_reference(actual, _REFERENCE["model_arrays"][key][index])++@pytest.mark.parametrize("dtype", [np.uint8, np.uint16, np.float32])@pytest.mark.parametrize("channels", [1, 3, 4])-def test_model_channel_conversion_matches_opencv(dtype: type, channels: int) -> None:- """各种实际位深和非连续数组逐像素一致,且转换结果拥有独立连续存储。"""- cv2 = pytest.importorskip("cv2")-+def test_model_channel_conversion_matches_reference(dtype: type, channels: int) -> None:+ """各种位深和非连续输入保留历史像素以及独立连续存储。"""image = np.arange(8 * 10 * channels).reshape(8, 10, channels).astype(dtype)[::2, ::2]actual = rgb_to_bgr(image)- np.testing.assert_array_equal(actual, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))+ _assert_model_reference(actual, f"rgb:{np.dtype(dtype)}:{channels}")assert actual.flags.c_contiguous and not np.shares_memory(actual, image)def test_grayscale_and_alpha_conversion_keep_rounding() -> None:- """灰度复制和非白色背景的 alpha 合成保持原来的逐通道截断顺序。"""- cv2 = pytest.importorskip("cv2")-+ """灰度复制及非白色背景 alpha 合成沿用冻结的逐通道截断结果。"""image = np.random.default_rng(7).integers(0, 256, (17, 13, 4), dtype=np.uint8)- np.testing.assert_array_equal(check_img(image[:, :, 0]), cv2.cvtColor(image[:, :, 0], cv2.COLOR_GRAY2BGR))- blue, green, red, alpha_channel = cv2.split(image)- alpha = alpha_channel / 255- expected = cv2.merge(- tuple(- (background * (1 - alpha) + channel * alpha).astype(np.uint8)- for channel, background in ((blue, 30), (green, 20), (red, 10))- )- )- np.testing.assert_array_equal(alpha_to_color(image, (10, 20, 30)), expected)-+ _assert_model_reference(check_img(image[:, :, 0]), "gray_alpha", 0)+ _assert_model_reference(alpha_to_color(image, (10, 20, 30)), "gray_alpha", 1)[email protected]("label,rotation", [("90", 2), ("270", 0), ("0", None)])-def test_orientation_rotation_keeps_pixels_and_ownership(label: str, rotation: int | None) -> None:- """方向评分使用的旋转保持像素、连续性和零角度副本行为。"""- cv2 = pytest.importorskip("cv2")[email protected]("label", ["90", "270", "0"])+def test_orientation_rotation_keeps_pixels_and_ownership(label: str) -> None:+ """方向评分保持历史旋转像素和零角度副本行为。"""image = np.arange(8 * 10 * 3, dtype=np.uint8).reshape(8, 10, 3)[::2, ::2]actual = MineruTableOrientationClsModel._rotate_image_by_label(image, label)- expected = image.copy() if rotation is None else cv2.rotate(image, rotation)- np.testing.assert_array_equal(actual, expected)+ _assert_model_reference(actual, f"rotation:{label}")assert actual.flags.c_contiguous and not np.shares_memory(actual, image)def test_perspective_crop_keeps_numeric_contract() -> None:- """共享内核的倾斜裁图保留透视矩阵、三次插值和边界复制。"""- cv2 = pytest.importorskip("cv2")-+ """高位深倾斜裁图保留历史透视矩阵、三次采样和边界复制结果。"""image = np.arange(20 * 30 * 3, dtype=np.uint16).reshape(20, 30, 3)points = np.array([[2, 2], [21, 4], [22, 15], [1, 14]], dtype=np.float32)- width = int(max(np.linalg.norm(points[0] - points[1]), np.linalg.norm(points[2] - points[3])))- height = int(max(np.linalg.norm(points[0] - points[3]), np.linalg.norm(points[1] - points[2])))- target = np.float32([[0, 0], [width, 0], [width, height], [0, height]])- expected = cv2.warpPerspective(- image,- cv2.getPerspectiveTransform(points, target),- (width, height),- flags=cv2.INTER_CUBIC,- borderMode=cv2.BORDER_REPLICATE,- )- np.testing.assert_array_equal(get_rotate_crop_image(image, points), expected)+ _assert_model_reference(get_rotate_crop_image(image, points), "perspective")@pytest.mark.parametrize("paddle_compatible", [False, True])def test_formula_tensor_keeps_original_gray_and_channel_values(paddle_compatible: bool) -> None:- """公式预处理仅替换 merge,灰度舍入和归一化计算顺序保持逐元素一致。"""- cv2 = pytest.importorskip("cv2")-+ """两种公式预处理保持冻结的灰度舍入和归一化模型张量。"""from mineru.model.mfr.pp_formulanet.processors import UniMERNetTestTransformimage = np.random.default_rng(11).integers(0, 256, (17, 19, 3), dtype=np.uint8)- mean = np.array([0.7931] * 3).reshape(1, 1, 3).astype("float32")- std = np.array([0.1738] * 3).reshape(1, 1, 3).astype("float32")- if paddle_compatible:- gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)- expected = (cv2.merge([gray] * 3).astype("float32") - mean * 255.0) * (1.0 / (std * 255.0))- else:- normalized = (image.astype("float32") * float(1 / 255.0) - mean) / std- expected = cv2.merge([np.squeeze(cv2.cvtColor(normalized, cv2.COLOR_BGR2GRAY))] * 3)- np.testing.assert_array_equal(UniMERNetTestTransform(paddle_compatible=paddle_compatible).transform(image), expected)+ actual = UniMERNetTestTransform(paddle_compatible=paddle_compatible).transform(image)+ _assert_model_reference(actual, f"formula:{paddle_compatible}")@pytest.mark.parametrize("shape", [(1, 1), (1, 17), (19, 1), (23, 27)])def test_formula_margin_crop_keeps_threshold_and_bounds(shape: tuple[int, int]) -> None:- """公式裁边覆盖空白、单行单列和边界像素,保持非零框的闭区间语义。"""- cv2 = pytest.importorskip("cv2")-+ """空白图、单行单列和边界像素保持冻结裁边及原对象语义。"""from mineru.model.mfr.pp_formulanet.processors import UniMERNetImgDecodeprocessor = UniMERNetImgDecode((192, 672))@@ -223,40 +202,23 @@ def test_formula_margin_crop_keeps_threshold_and_bounds(shape: tuple[int, int])if array.max() == array.min():assert actual is imagecontinue- normalized = (array - array.min()) / (array.max() - array.min()) * 255- x, y, width, height = cv2.boundingRect(cv2.findNonZero(255 * (normalized < 200).astype(np.uint8)))- with image.crop((x, y, x + width, y + height)) as expected:- assert actual.size == expected.size- np.testing.assert_array_equal(np.array(actual), np.array(expected))+ _assert_model_reference(np.array(actual), f"margin:{shape[0]}:{shape[1]}")actual.close()def test_table_classifier_tensor_keeps_channel_calculation_order() -> None:- """表格分类单张及批量预处理保持 OpenCV split/merge 的浮点计算顺序。"""- cv2 = pytest.importorskip("cv2")-+ """单张和批量表格分类使用同一独立冻结张量验证浮点计算顺序。"""from mineru.model.table.cls.paddle_table_cls import PaddleTableClsModelclassifier = object.__new__(PaddleTableClsModel)image = np.random.default_rng(13).integers(0, 256, (79, 137, 3), dtype=np.uint8)- scale = 256 / min(image.shape[:2])- resized = cv2.resize(image, (round(image.shape[1] * scale), round(image.shape[0] * scale)), interpolation=1)- y, x = (resized.shape[0] - 224) // 2, (resized.shape[1] - 224) // 2- split = list(cv2.split(resized[y : y + 224, x : x + 224]))- for channel in range(3):- split[channel] = split[channel].astype(np.float32)- split[channel] *= 0.00392156862745098 / [0.229, 0.224, 0.225][channel]- split[channel] += -[0.485, 0.456, 0.406][channel] / [0.229, 0.224, 0.225][channel]- expected = np.stack([cv2.merge(split).transpose(2, 0, 1)]).astype(np.float32)- np.testing.assert_array_equal(classifier.preprocess(image), expected)- np.testing.assert_array_equal(classifier.batch_preprocess([image]), expected)+ _assert_model_reference(classifier.preprocess(image), "table_classifier", 0)+ _assert_model_reference(classifier.batch_preprocess([image]), "table_classifier", 1)@pytest.mark.parametrize("channels", [None, 1, 3])def test_unimer_crop_and_zero_padding_keep_shape(channels: int | None) -> None:- """单通道和 RGB 公式图的裁边、零填充保持原形状及全部像素。"""- cv2 = pytest.importorskip("cv2")-+ """单通道和 RGB 公式图沿用冻结裁边、缩放和零填充像素。"""pytest.importorskip("torch")pytest.importorskip("torchvision")pytest.importorskip("transformers")@@ -265,47 +227,90 @@ def test_unimer_crop_and_zero_padding_keep_shape(channels: int | None) -> None:shape = (17, 29) if channels is None else (17, 29, channels)image = np.full(shape, 255, dtype=np.uint8)image[3:15, 5:24] = 0- gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) if channels == 3 else image.copy()- normalized = (((gray - gray.min()) / (gray.max() - gray.min())) * 255).astype(np.uint8)- binary = 255 * (normalized < 200).astype(np.uint8)- x, y, width, height = cv2.boundingRect(cv2.findNonZero(binary))- cropped = image[y : y + height, x : x + width]- np.testing.assert_array_equal(UnimerSwinImageProcessor.crop_margin_numpy(image), cropped)+ _assert_model_reference(UnimerSwinImageProcessor.crop_margin_numpy(image), f"unimer:{channels}", 0)processor = UnimerSwinImageProcessor(image_size=(32, 64))- scale = min(32 / height, 64 / width)- resized = cv2.resize(cropped, (int(width * scale), int(height * scale)))- pad_x, pad_y = (64 - resized.shape[1]) // 2, (32 - resized.shape[0]) // 2- expected = cv2.copyMakeBorder(- resized,- pad_y,- 32 - resized.shape[0] - pad_y,- pad_x,- 64 - resized.shape[1] - pad_x,- cv2.BORDER_CONSTANT,- value=[0, 0, 0],- )- np.testing.assert_array_equal(processor.prepare_input(image), expected)+ _assert_model_reference(processor.prepare_input(image), f"unimer:{channels}", 1)def test_unet_input_tensor_keeps_inplace_channel_exchange() -> None:- """UNet 预处理保留原位 RGB 交换及 OpenCV 浮点减乘顺序。"""- cv2 = pytest.importorskip("cv2")-+ """UNet 预处理使用独立冻结输入,验证通道交换和浮点减乘顺序。"""from mineru.model.table.rec.unet_table.table_structure_unet import TSRUnet- from mineru.model.table.rec.unet_table.utils import resize_imgmodel = object.__new__(TSRUnet)model.mean = np.array([123.675, 116.28, 103.53], dtype=np.float32)model.std = np.array([58.395, 57.12, 57.375], dtype=np.float32)model.inp_height, model.inp_width = 32, 64image = np.random.default_rng(17).integers(0, 256, (29, 47, 3), dtype=np.uint8)- resized, _, _ = resize_img(image, (32, 64), True)- expected = resized.copy().astype(np.float32)- cv2.cvtColor(expected, cv2.COLOR_BGR2RGB, expected)- cv2.subtract(expected, np.float64(model.mean.reshape(1, -1)), expected)- cv2.multiply(expected, 1 / np.float64(model.std.reshape(1, -1)), expected)- actual = model.preprocess(image)- np.testing.assert_array_equal(actual["img"], expected.transpose(2, 0, 1)[None, ...])+ _assert_model_reference(model.preprocess(image)["img"], "unet")++[email protected]("case", _REFERENCE["seal_cases"], ids=lambda case: case["name"] + "-" + case["interpolation"])[email protected]("backend", ["python", "rust"])+def test_seal_homography_keeps_frozen_pixels(case: dict[str, Any], backend: str, monkeypatch: pytest.MonkeyPatch) -> None:+ """两个后端验证横竖曲线、四边形、短多边形和退化回退的既有像素。"""+ from docvortex._compute_backend import get_native+ from mineru.model.ocr.seal_det_warp import AutoRectifier++ monkeypatch.setenv("DOCVORTEX_COMPUTE_BACKEND", backend)+ get_native.cache_clear()+ try:+ image = np.random.default_rng(29).integers(0, 256, (96, 96, 3), dtype=np.uint8)+ original = image.copy()+ points = np.float32(case["points"])+ rectifier = AutoRectifier()+ actual = rectifier(image, points, interpolation=case["interpolation"])+ _assert_array_reference(actual, case["crop"])+ outputs, visual = rectifier.run(image, [points.reshape(-1).tolist()], interpolation=case["interpolation"])+ _assert_array_reference(outputs[0], case["crop"])+ _assert_array_reference(visual, case["visual"])+ np.testing.assert_array_equal(image, original)+ assert not np.shares_memory(actual, image)+ finally:+ get_native.cache_clear()+++def test_default_seal_rectification_runs_without_opencv() -> None:+ """独立阻断进程验证默认单张、批量和曲线入口均执行原 homography 结果。"""+ _run_blocked(+ """+import hashlib+import json+from pathlib import Path+import numpy as np+from mineru.model.ocr.seal_det_warp import AutoRectifier, CurveTextRectifier++reference = json.loads(Path(sys.argv[1]).read_text())+image = np.random.default_rng(29).integers(0, 256, (96, 96, 3), dtype=np.uint8)+for case in reference['seal_cases']:+ if case['interpolation'] != 'linear' or case['name'] not in ('horizontal', 'vertical'):+ continue+ points = np.float32(case['points'])+ expected = case['crop']['sha256']+ rectifier = AutoRectifier()+ single = rectifier(image, points)+ outputs, _ = rectifier.run(image, [points.reshape(-1).tolist()])+ curve, _ = CurveTextRectifier()(image, points)+ for actual in (single, outputs[0], curve):+ assert hashlib.sha256(actual.tobytes()).hexdigest() == expected+assert 'cv2' not in sys.modules+""",+ str(_ROOT / "tests/fixtures/model_image_reference.json"),+ )++[email protected]("mode", ["calibration", "unknown"])+def test_seal_rejects_removed_mode(mode: str) -> None:+ """旧模式和拼写错误必须明确失败,不静默改变矫正算法。"""+ from mineru.model.ocr.seal_det_warp import AutoRectifier, CurveTextRectifier++ image = np.zeros((96, 96, 3), np.uint8)+ points = np.float32(_REFERENCE["seal_cases"][0]["points"])+ with pytest.raises(ValueError, match="homography"):+ CurveTextRectifier()(image, points, mode=mode)+ with pytest.raises(ValueError, match="homography"):+ AutoRectifier()(image, points, mode=mode)+ with pytest.raises(ValueError, match="homography"):+ AutoRectifier().run(image, [points.reshape(-1).tolist()], mode=mode)def test_base_dependencies_exclude_opencv_and_keep_product_extras() -> None:@@ -335,34 +340,26 @@ def test_base_dependencies_exclude_opencv_and_keep_product_extras() -> None:assert full_mineru.extras == {"torch"}-def test_production_opencv_is_limited_to_explicit_camera_calibration() -> None:- """静态守卫只允许旧相机标定的两个按需入口使用 cv2,不放宽整个模块。"""+def test_production_does_not_reference_opencv() -> None:+ """生产代码不允许导入、访问或动态请求 OpenCV,遗留白名单已移除。"""import ast- allowed = {- ("model/ocr/seal_det_warp.py", "CurveTextRectifier.spatial_transform"),- ("model/ocr/seal_det_warp.py", "CurveTextRectifier.calibrate"),- }- found: set[tuple[str, str]] = set()root = _ROOT / "mineru"+ errors = []for path in root.rglob("*.py"):tree = ast.parse(path.read_text())- parents = {child: parent for parent in ast.walk(tree) for child in ast.iter_child_nodes(parent)}for node in ast.walk(tree):- is_import = isinstance(node, ast.Import) and any(alias.name == "cv2" for alias in node.names)- is_attribute = isinstance(node, ast.Attribute) and isinstance(node.value, ast.Name) and node.value.id == "cv2"- if not (is_import or is_attribute):- continue- names = []- parent = node- while parent in parents:- parent = parents[parent]- if isinstance(parent, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):- names.append(parent.name)- location = (path.relative_to(root).as_posix(), ".".join(reversed(names)))- assert location in allowed, (location, node.lineno)- found.add(location)- assert found == allowed+ is_import = isinstance(node, ast.Import) and any(alias.name.split(".")[0] == "cv2" for alias in node.names)+ is_from = isinstance(node, ast.ImportFrom) and (node.module or "").split(".")[0] == "cv2"+ is_name = isinstance(node, ast.Name) and node.id == "cv2"+ is_string = (+ isinstance(node, ast.Constant)+ and isinstance(node.value, str)+ and (node.value == "cv2" or node.value.startswith("cv2."))+ )+ if is_import or is_from or is_name or is_string:+ errors.append(f"{path.relative_to(root)}:{node.lineno}")+ assert not errors, "\n".join(errors)def test_model_preprocessors_and_geometry_run_without_opencv() -> None: