Explorar o código

feat: 优化OCR文本框识别逻辑,优先使用多边形数据并增强错误日志信息

zhch158_admin hai 5 días
pai
achega
fe223cd19d
Modificáronse 1 ficheiros con 25 adicións e 3 borrados
  1. 25 3
      ocr_tools/universal_doc_parser/core/element_processors.py

+ 25 - 3
ocr_tools/universal_doc_parser/core/element_processors.py

@@ -217,8 +217,29 @@ class ElementProcessors:
         if pre_matched_spans and len(pre_matched_spans) > 0 and table_angle == 0:
             # 使用整页 OCR 的结果
             for idx, span in enumerate(pre_matched_spans):
+                # 优先使用 poly 数据,如果没有才使用 bbox
+                span_poly = span.get('poly', [])
                 span_bbox = span.get('bbox', [])
-                if span_bbox:
+                
+                if span_poly:
+                    # 如果有 poly 数据,直接使用(需要转换为相对坐标)
+                    if isinstance(span_poly[0], (list, tuple)) and len(span_poly) >= 4:
+                        # 转换为相对坐标(相对于表格区域)
+                        relative_poly = [
+                            [float(p[0]) - bbox[0], float(p[1]) - bbox[1]]
+                            for p in span_poly[:4]
+                        ]
+                        formatted_box = CoordinateUtils.convert_ocr_to_matcher_format(
+                            relative_poly,
+                            span.get('text', ''),
+                            span.get('confidence', 0.0),
+                            idx,
+                            table_bbox=None
+                        )
+                        if formatted_box:
+                            ocr_boxes.append(formatted_box)
+                elif span_bbox and len(span_bbox) >= 4:
+                    # 兜底:使用 bbox 数据
                     relative_bbox = [
                         span_bbox[0] - bbox[0],
                         span_bbox[1] - bbox[1],
@@ -245,7 +266,8 @@ class ElementProcessors:
                 ocr_results = self.ocr_recognizer.recognize_text(cropped_table)
                 if ocr_results:
                     for idx, item in enumerate(ocr_results):
-                        ocr_poly = item.get('bbox', [])
+                        # 优先使用 poly,没有才用 bbox
+                        ocr_poly = item.get('poly', item.get('bbox', []))
                         if ocr_poly:
                             formatted_box = CoordinateUtils.convert_ocr_to_matcher_format(
                                 ocr_poly, 
@@ -272,7 +294,7 @@ class ElementProcessors:
                 ocr_source = "cropped_ocr"
                 logger.info(f"📊 OCR detected {len(ocr_boxes)} text boxes in table (cropped)")
             except Exception as e:
-                logger.warning(f"Table OCR detection failed: {e}")
+                logger.warning(f"Table OCR failed: {e}")
         
         return cropped_table, ocr_boxes, table_angle, ocr_source