Procházet zdrojové kódy

refactor(element_processors): update coordinate utilities to use TableCoordinateUtils

- Replaced instances of CoordinateUtils with TableCoordinateUtils for handling table coordinate transformations in the ElementProcessors class.
- Adjusted methods for converting OCR results to matcher format, inverse rotation of coordinates, and adding offsets to ensure consistency with the new utility module.
zhch158_admin před 6 hodinami
rodič
revize
364c2425cb

+ 16 - 15
ocr_tools/universal_doc_parser/core/element_processors.py

@@ -13,8 +13,9 @@ from typing import Dict, List, Any, Optional, Tuple
 import numpy as np
 from loguru import logger
 
-from .coordinate_utils import CoordinateUtils
-from .pdf_utils import PDFUtils
+from ocr_utils.coordinate_utils import CoordinateUtils
+from ocr_utils import PDFUtils
+from .table_coordinate_utils import TableCoordinateUtils
 
 # 导入 SpanMatcher(用于 spans 合并)
 try:
@@ -259,7 +260,7 @@ class ElementProcessors:
                             [float(p[0]) - cropped_offset_x, float(p[1]) - cropped_offset_y]
                             for p in span_poly[:4]
                         ]
-                        formatted_box = CoordinateUtils.convert_ocr_to_matcher_format(
+                        formatted_box = TableCoordinateUtils.convert_ocr_to_matcher_format(
                             relative_poly,
                             span.get('text', ''),
                             span.get('confidence', 0.0),
@@ -276,7 +277,7 @@ class ElementProcessors:
                         span_bbox[2] - cropped_offset_x,
                         span_bbox[3] - cropped_offset_y
                     ]
-                    formatted_box = CoordinateUtils.convert_ocr_to_matcher_format(
+                    formatted_box = TableCoordinateUtils.convert_ocr_to_matcher_format(
                         relative_bbox,
                         span.get('text', ''),
                         span.get('confidence', 0.0),
@@ -299,7 +300,7 @@ class ElementProcessors:
                         # 优先使用 poly,没有才用 bbox
                         ocr_poly = item.get('poly', item.get('bbox', []))
                         if ocr_poly:
-                            formatted_box = CoordinateUtils.convert_ocr_to_matcher_format(
+                            formatted_box = TableCoordinateUtils.convert_ocr_to_matcher_format(
                                 ocr_poly, 
                                 item.get('text', ''),
                                 item.get('confidence', 0.0),
@@ -403,14 +404,14 @@ class ElementProcessors:
         cropped_offset_bbox = [bbox[0] - crop_padding, bbox[1] - crop_padding, bbox[2] + crop_padding, bbox[3] + crop_padding]
         
         if table_angle != 0 and MERGER_AVAILABLE:
-            cells, enhanced_html = CoordinateUtils.inverse_rotate_table_coords(
+            cells, enhanced_html = TableCoordinateUtils.inverse_rotate_table_coords(
                 cells=cells,
                 html=enhanced_html,
                 rotation_angle=table_angle,
                 orig_table_size=orig_size_before_rotation,
                 table_bbox=cropped_offset_bbox
             )
-            ocr_boxes = CoordinateUtils.inverse_rotate_ocr_boxes(
+            ocr_boxes = TableCoordinateUtils.inverse_rotate_ocr_boxes(
                 ocr_boxes=ocr_boxes,
                 rotation_angle=table_angle,
                 orig_table_size=orig_size_before_rotation,
@@ -419,9 +420,9 @@ class ElementProcessors:
             logger.info(f"📐 Wired table coordinates transformed back to original image")
         else:
             # 没有旋转,使用正确的偏移量(考虑 padding)
-            cells = CoordinateUtils.add_table_offset_to_cells(cells, cropped_offset_bbox)
-            enhanced_html = CoordinateUtils.add_table_offset_to_html(enhanced_html, cropped_offset_bbox)
-            ocr_boxes = CoordinateUtils.add_table_offset_to_ocr_boxes(ocr_boxes, cropped_offset_bbox)
+            cells = TableCoordinateUtils.add_table_offset_to_cells(cells, cropped_offset_bbox)
+            enhanced_html = TableCoordinateUtils.add_table_offset_to_html(enhanced_html, cropped_offset_bbox)
+            ocr_boxes = TableCoordinateUtils.add_table_offset_to_ocr_boxes(ocr_boxes, cropped_offset_bbox)
         
         return {
             'type': 'table',
@@ -508,14 +509,14 @@ class ElementProcessors:
         cropped_offset_bbox = [bbox[0] - crop_padding, bbox[1] - crop_padding, bbox[2] + crop_padding, bbox[3] + crop_padding]
         
         if table_angle != 0 and MERGER_AVAILABLE:
-            cells, enhanced_html = CoordinateUtils.inverse_rotate_table_coords(
+            cells, enhanced_html = TableCoordinateUtils.inverse_rotate_table_coords(
                 cells=cells,
                 html=enhanced_html,
                 rotation_angle=table_angle,
                 orig_table_size=orig_size_before_rotation,
                 table_bbox=cropped_offset_bbox
             )
-            ocr_boxes = CoordinateUtils.inverse_rotate_ocr_boxes(
+            ocr_boxes = TableCoordinateUtils.inverse_rotate_ocr_boxes(
                 ocr_boxes=ocr_boxes,
                 rotation_angle=table_angle,
                 orig_table_size=orig_size_before_rotation,
@@ -524,9 +525,9 @@ class ElementProcessors:
             logger.info(f"📐 VLM table coordinates transformed back to original image")
         else:
             # 没有旋转,使用正确的偏移量(考虑 padding)
-            cells = CoordinateUtils.add_table_offset_to_cells(cells, cropped_offset_bbox)
-            enhanced_html = CoordinateUtils.add_table_offset_to_html(enhanced_html, cropped_offset_bbox)
-            ocr_boxes = CoordinateUtils.add_table_offset_to_ocr_boxes(ocr_boxes, cropped_offset_bbox)
+            cells = TableCoordinateUtils.add_table_offset_to_cells(cells, cropped_offset_bbox)
+            enhanced_html = TableCoordinateUtils.add_table_offset_to_html(enhanced_html, cropped_offset_bbox)
+            ocr_boxes = TableCoordinateUtils.add_table_offset_to_ocr_boxes(ocr_boxes, cropped_offset_bbox)
         
         return {
             'type': 'table',