Files
OCR/openspec/changes/extract-table-cell-boxes/proposal.md
egg 801ee9c4b6 feat: create extract-table-cell-boxes proposal and archive old proposal
- Archive unify-image-scaling proposal to archive/2025-11-28
- Create new extract-table-cell-boxes proposal for supplementing PPStructureV3
  with direct SLANeXt model calls to extract table cell bounding boxes
- Add debug logging to pp_structure_enhanced.py for table cell boxes investigation
- Discovered that PPStructureV3 high-level API filters out cell bbox data,
  but paddlex.create_model() can directly invoke underlying models

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 12:15:06 +08:00

5.0 KiB
Raw Blame History

Change: Extract Table Cell Boxes via Direct Model Invocation

Why

PPStructureV3 (PaddleX 3.x) 的高層 API 在處理表格時,只輸出 HTML 格式的表格內容,不返回每個 cell 的座標 (bbox)

問題分析

經過測試確認:

# PPStructureV3 輸出 (parsing_res_list)
{
    'block_label': 'table',
    'block_content': '<html>...</html>',  # 只有 HTML
    'block_bbox': [84, 269, 1174, 1508],  # 只有整個表格的 bbox
    # ❌ 沒有 cell boxes
}

但底層模型 (SLANeXt) 實際上有輸出 cell boxes

# 直接調用 SLANeXt 模型
from paddlex import create_model
table_model = create_model('SLANeXt_wired')
result = table_model.predict(table_img)
# result.json['res']['bbox'] → 29 個 cell 座標 (8點多邊形)

影響

缺少 cell boxes 導致:

  • OCR Track 的 PDF 版面還原表格渲染不準確
  • 無法精確定位每個 cell 的位置
  • 表格內容可能重疊或錯位

What Changes

方案:補充調用底層 SLANeXt 模型

pp_structure_enhanced.py 處理表格時,補充調用 PaddleX 底層模型獲取 cell boxes

┌─────────────────────────────────────────────────────────────┐
│                    修改後的流程                              │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  PPStructureV3.predict()                                     │
│       │                                                      │
│       ▼                                                      │
│  parsing_res_list (HTML only)                               │
│       │                                                      │
│       ▼ (對於 TABLE 類型)                                    │
│  ┌─────────────────────────────────────┐                    │
│  │ 補充調用底層模型                      │                    │
│  │ 1. 裁切表格區域                       │                    │
│  │ 2. 調用 SLANeXt 獲取 cell boxes      │                    │
│  │ 3. 轉換座標到全域座標                 │                    │
│  │ 4. 存入 element['cell_boxes']        │                    │
│  └─────────────────────────────────────┘                    │
│       │                                                      │
│       ▼                                                      │
│  完整的表格元素 (HTML + cell_boxes)                          │
│                                                              │
└─────────────────────────────────────────────────────────────┘

模型選擇邏輯

根據表格類型選擇對應的 SLANeXt 模型:

表格類型 判斷方式 使用模型
有線表格 (wired) PP-LCNet 分類 SLANeXt_wired
無線表格 (wireless) PP-LCNet 分類 SLANeXt_wireless

Cell Boxes 格式

SLANeXt 輸出的 bbox 是 8 點多邊形格式:

[x1, y1, x2, y2, x3, y3, x4, y4]  # 四個角點座標
# 例如: [11, 4, 692, 5, 675, 57, 10, 56]

需要轉換為全域座標(加上表格偏移量)。

Impact

Affected Specs

  • ocr-processing - 表格處理增強

Affected Code

  • backend/app/services/pp_structure_enhanced.py

    • 添加底層模型緩存機制
    • 修改 _process_parsing_res_list 中的 TABLE 處理邏輯
    • 添加 cell boxes 提取和座標轉換
  • backend/app/services/pdf_generator_service.py

    • 利用 cell_boxes 改進表格渲染

Quality Impact

項目 改進前 改進後
Cell 座標 有 (8點多邊形)
表格渲染 平均分配行列 精確定位
版面還原 內容可能重疊 準確對應

Performance Impact

  • 額外模型調用:每個表格需要額外調用一次 SLANeXt
  • 緩存優化:模型實例可緩存,避免重複載入
  • 預估開銷:每表格增加 ~0.5-1 秒

Risks

  1. 性能開銷

    • 風險:額外模型調用增加處理時間
    • 緩解:緩存模型實例,僅在需要時調用
  2. 模型不一致

    • 風險PPStructureV3 內部可能已使用不同參數的模型
    • 緩解:使用相同的模型配置
  3. 座標轉換錯誤

    • 風險bbox 座標系可能有差異
    • 緩解:充分測試,確保座標正確轉換

Not Included

  • 完全繞過 PPStructureV3保留用於 Layout 分析)
  • RT-DETR cell detection可作為後續增強
  • 其他元素的增強處理