mirror of
https://github.com/opendatalab/MinerU.git
synced 2026-09-21 12:42:22 +08:00
feat: add blank cell result filtering for OCR to improve accuracy
This commit is contained in:
@@ -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]]
|
||||
|
||||
Reference in New Issue
Block a user