| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import streamlit as st
- import json
- # 当直接运行时
- import sys
- from pathlib import Path
- sys.path.insert(0, str(Path(__file__).parent.parent))
- from table_line_generator import TableLineGenerator # 上级目录
- def parse_ocr_data(ocr_data):
- """解析OCR数据,支持多种格式"""
- # 如果是字符串,尝试解析
- if isinstance(ocr_data, str):
- try:
- ocr_data = json.loads(ocr_data)
- except json.JSONDecodeError:
- st.error("❌ JSON 格式错误,无法解析")
- return []
-
- # 检查是否为 PPStructure V3 格式
- if isinstance(ocr_data, dict) and 'parsing_res_list' in ocr_data and 'overall_ocr_res' in ocr_data:
- st.info("🔍 检测到 PPStructure V3 格式")
-
- try:
- table_bbox, text_boxes = TableLineGenerator.parse_ppstructure_result(ocr_data)
- st.success(f"✅ 表格区域: {table_bbox}")
- st.success(f"✅ 表格内文本框: {len(text_boxes)} 个")
- return text_boxes
- except Exception as e:
- st.error(f"❌ 解析 PPStructure 结果失败: {e}")
- return []
-
- # 确保是列表
- if not isinstance(ocr_data, list):
- st.error(f"❌ OCR 数据应该是列表,实际类型: {type(ocr_data)}")
- return []
-
- if not ocr_data:
- st.warning("⚠️ OCR 数据为空")
- return []
-
- first_item = ocr_data[0]
- if not isinstance(first_item, dict):
- st.error(f"❌ OCR 数据项应该是字典,实际类型: {type(first_item)}")
- return []
-
- if 'bbox' not in first_item:
- st.error("❌ OCR 数据缺少 'bbox' 字段")
- st.info("💡 支持的格式示例:\n```json\n[\n {\n \"text\": \"文本\",\n \"bbox\": [x1, y1, x2, y2]\n }\n]\n```")
- return []
-
- return ocr_data
|