Skip to content

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

feat: add blank cell result filtering for OCR to improve accuracy

myhloli2b8ee2df65c4
diff --git a/mineru/model/table/rec/unet_table/main.py b/mineru/model/table/rec/unet_table/main.py
index 3bb779b4..497bb6f6 100644
--- a/mineru/model/table/rec/unet_table/main.py
+++ b/mineru/model/table/rec/unet_table/main.py
@@ -28,6 +28,21 @@ from .utils_table_recover import (
gather_ocr_list_by_row,
)
+BLANK_CELL_REC_DROP_TEXTS = {
+ "1",
+ "一",
+ "—",
+ "口",
+ "■",
+ "(204号",
+ "(20",
+ "(2",
+ "(2号",
+ "(20号",
+ "号",
+ "(204",
+}
+
@dataclass
class WiredTableInput:
@@ -155,20 +170,18 @@ class WiredTableRecognition:
)
return res
- # def fill_blank_rec(
- # self,
- # img: np.ndarray,
- # sorted_polygons: np.ndarray,
- # cell_box_map: Dict[int, List[str]],
- # ) -> Dict[int, List[Any]]:
- # """找到poly对应为空的框,尝试将直接将poly框直接送到识别中"""
- # for i in range(sorted_polygons.shape[0]):
- # if cell_box_map.get(i):
- # continue
- # box = sorted_polygons[i]
- # cell_box_map[i] = [[box, "", 1]]
- # continue
- # return cell_box_map
+ @staticmethod
+ def _should_drop_blank_cell_rec_result(text: str, score) -> bool:
+ """判断空单元格二次 OCR-rec 结果是否应作为噪声过滤。"""
+ try:
+ if float(score) < 0.6:
+ return True
+ except (TypeError, ValueError):
+ return True
+
+ normalized_text = "" if text is None else str(text).strip()
+ return not normalized_text or normalized_text in BLANK_CELL_REC_DROP_TEXTS
+
def fill_blank_rec(
self,
img: np.ndarray,
@@ -209,13 +222,6 @@ class WiredTableRecognition:
if len(img_crop_list) > 0:
# 进行ocr识别
ocr_result = self.ocr_engine.ocr(img_crop_list, det=False)
- # ocr_result = [[]]
- # for crop_img in img_crop_list:
- # tmp_ocr_result = self.ocr_engine.ocr(crop_img)
- # if tmp_ocr_result[0] and len(tmp_ocr_result[0]) > 0 and isinstance(tmp_ocr_result[0], list) and len(tmp_ocr_result[0][0]) == 2:
- # ocr_result[0].append(tmp_ocr_result[0][0][1])
- # else:
- # ocr_result[0].append(("", 0.0))
if not ocr_result or not isinstance(ocr_result, list) or len(ocr_result) == 0:
logger.warning("OCR engine returned no results or invalid result for image crops.")
@@ -231,7 +237,7 @@ class WiredTableRecognition:
# 处理ocr结果
ocr_text, ocr_score = ocr_res
# logger.debug(f"OCR result for box {i}: {ocr_text} with score {ocr_score}")
- if ocr_score < 0.6 or ocr_text in ['1','口','■','(204号', '(20', '(2', '(2号', '(20号', '号', '(204']:
+ if self._should_drop_blank_cell_rec_result(ocr_text, ocr_score):
# logger.warning(f"Low confidence OCR result for box {i}: {ocr_text} with score {ocr_score}")
box = sorted_polygons[i]
cell_box_map[i] = [[box, "", 0.1]]