Pārlūkot izejas kodu

feat: 新增表格识别管线,允许控制单元格内是否拆分重识别以保留折行文本

zhch158_admin 1 mēnesi atpakaļ
vecāks
revīzija
dee3f31187
1 mainītis faili ar 117 papildinājumiem un 0 dzēšanām
  1. 117 0
      zhch/table_recognition_v2.md

+ 117 - 0
zhch/table_recognition_v2.md

@@ -0,0 +1,117 @@
+# 交易流水,单栏列表,有线表格
+使用table_recognition_v2产线,关键参数use_wired_table_cells_trans_to_html:
+use_wired_table_cells_trans_to_html:启用有线表单元格直转HTML模式,启用时直接基于有线表单元格检测模型预测的各单元格位置排布与几何关系构建HTML结构。此模式在不同场景下效果不同,请根据实际情况选择。一般来说,建议在有线表单元格检测非常准确的场景下开启此模式,以获得更加准确的端到端表格识别结果。
+
+不启用表格结构识别模块预测得到表格的HTML结构
+
+去掉其他干扰模型
+
+```python
+outputs = pipeline.predict(
+	img_path,
+	use_doc_orientation_classify=False,
+	use_doc_unwarping=False,
+	use_layout_detection=True,
+	use_ocr_results_with_table_cells=True,
+	use_table_orientation_classify=False,
+	use_wired_table_cells_trans_to_html=True,
+	# 新增:关闭单元格内拆分,整格识别以保留折行文本, 
+	# 修改paddlex/inference/pipelines/table_recognition/pipeline_v2.py
+	# get_table_recognition_res传入参数self.cells_split_ocr=False,保证单元格内换行不被拆分
+	use_table_cells_split_ocr=False,
+)
+```
+
+## 折行文本被丢弃
+正在收集工作区信息正在筛选到最相关的信息看起来不是“表格结构定位”问题,而是表格内容填充阶段把同一单元格里的折行文本做了“分段重识别/分裂”,只保留了其中一段(常见为最后一行“款”),导致“扫二维码付”被丢弃。
+
+原因定位
+- 你走的是 table_recognition_v2,且传了 use_ocr_results_with_table_cells=True、use_wired_table_cells_trans_to_html=True。
+- 在 v2 管线中,默认启用了“单元格内再切分并重识别”的策略,见函数:`_TableRecognitionPipelineV2.predict` 和单元格内容合成逻辑:`get_table_recognition_res`。
+- 该切分策略在遇到“纵向换行”的短词时,可能只选择到其中一个分段,从而丢字。
+
+关闭“单元格内拆分重识别”,直接对整格做一次OCR,再把同格内多行按阅读顺序拼接。为此给 table_recognition_v2 暴露一个开关 use_table_cells_split_ocr,并传到后处理。
+
+修改1:在管线里增加开关并传给后处理
+
+````python
+# ...existing code...
+class _TableRecognitionPipelineV2(BasePipeline):
+    # ...existing code...
+    def predict(
+        self,
+        input: Union[str, List[str], np.ndarray, List[np.ndarray]],
+        use_doc_orientation_classify: Optional[bool] = None,
+        use_doc_unwarping: Optional[bool] = None,
+        use_layout_detection: Optional[bool] = None,
+        use_ocr_model: Optional[bool] = None,
+        overall_ocr_res: Optional[OCRResult] = None,
+        layout_det_res: Optional[DetResult] = None,
+        text_det_limit_side_len: Optional[int] = None,
+        text_det_limit_type: Optional[str] = None,
+        text_det_thresh: Optional[float] = None,
+        text_det_box_thresh: Optional[float] = None,
+        text_det_unclip_ratio: Optional[float] = None,
+        text_rec_score_thresh: Optional[float] = None,
+        use_e2e_wired_table_rec_model: bool = False,
+        use_e2e_wireless_table_rec_model: bool = False,
+        use_wired_table_cells_trans_to_html: bool = False,
+        use_wireless_table_cells_trans_to_html: bool = False,
+        use_table_orientation_classify: bool = True,
+        use_ocr_results_with_table_cells: bool = True,
+        **kwargs,
+    ) -> TableRecognitionResult:
+        # 新增:允许外部控制单元格内是否拆分重识别(默认保持兼容 True)
+        use_table_cells_split_ocr = kwargs.get("use_table_cells_split_ocr", None)
+        self.cells_split_ocr = (
+            bool(use_table_cells_split_ocr) if use_table_cells_split_ocr is not None else True
+        )
+        # ...existing code...
+
+    def predict_single_table_recognition_res(
+        self,
+        image_array: np.ndarray,
+        overall_ocr_res: OCRResult,
+        table_box: list,
+        use_e2e_wired_table_rec_model: bool = False,
+        use_e2e_wireless_table_rec_model: bool = False,
+        use_wired_table_cells_trans_to_html: bool = False,
+        use_wireless_table_cells_trans_to_html: bool = False,
+        use_ocr_results_with_table_cells: bool = True,
+        flag_find_nei_text: bool = True,
+    ) -> SingleTableRecognitionResult:
+        # ...existing code,完成 table_structure_result / table_cells_result / table_ocr_pred / cells_texts_list 的产生后...
+        single_table_recognition_res = get_table_recognition_res(
+            table_box,
+            table_structure_result,
+            table_cells_result,
+            overall_ocr_res,
+            table_ocr_pred,
+            cells_texts_list,
+            use_ocr_results_with_table_cells,
+            self.cells_split_ocr,  # 传递新增开关
+        )
+        # ...existing code...
+````
+
+说明
+- `get_table_recognition_res` 已有 use_table_cells_split_ocr 参数,传 True 时走“拆分重识别”,传 False 时用整格OCR结果,不会丢掉折行。
+- 以上改动保持默认行为不变(兼容老调用)。
+
+修改2:调用处传入开关(你的脚本)
+
+````python
+# ...existing code...
+                outputs = pipeline.predict(
+                    img_path,
+                    use_doc_orientation_classify=False,
+                    use_doc_unwarping=False,
+                    use_layout_detection=True,
+                    use_ocr_results_with_table_cells=True,
+                    use_table_orientation_classify=False,
+                    use_wired_table_cells_trans_to_html=True,
+                    # 新增:关闭单元格内拆分,整格识别以保留折行文本
+                    use_table_cells_split_ocr=False,
+                )
+# ...existing code...
+````