table_line_generator.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. """
  2. 基于 OCR bbox 的表格线生成模块
  3. 自动分析无线表格的行列结构,生成表格线
  4. """
  5. import cv2
  6. import numpy as np
  7. from PIL import Image, ImageDraw
  8. from pathlib import Path
  9. from typing import List, Dict, Tuple, Optional, Union
  10. import json
  11. from bs4 import BeautifulSoup
  12. class TableLineGenerator:
  13. """表格线生成器"""
  14. def __init__(self, image: Union[str, Image.Image], ocr_data: Dict):
  15. """
  16. 初始化表格线生成器
  17. Args:
  18. image: 图片路径(str) 或 PIL.Image 对象
  19. ocr_data: OCR识别结果(包含bbox)
  20. """
  21. if isinstance(image, str):
  22. self.image_path = image
  23. self.image = Image.open(image)
  24. elif isinstance(image, Image.Image):
  25. self.image_path = None
  26. self.image = image
  27. else:
  28. raise TypeError(
  29. f"image 参数必须是 str (路径) 或 PIL.Image.Image 对象,"
  30. f"实际类型: {type(image)}"
  31. )
  32. self.ocr_data = ocr_data
  33. # 表格结构参数
  34. self.rows = []
  35. self.columns = []
  36. self.row_height = 0
  37. self.col_widths = []
  38. @staticmethod
  39. def parse_ocr_data(ocr_result: Dict, tool: str = "ppstructv3") -> Tuple[List[int], Dict]:
  40. """
  41. 统一的 OCR 数据解析接口(第一步:仅读取数据)
  42. Args:
  43. ocr_result: OCR 识别结果(完整 JSON)
  44. tool: 工具类型 ("ppstructv3" / "mineru")
  45. Returns:
  46. (table_bbox, ocr_data): 表格边界框和文本框列表
  47. """
  48. if tool.lower() == "mineru":
  49. return TableLineGenerator._parse_mineru_data(ocr_result)
  50. elif tool.lower() in ["ppstructv3", "ppstructure"]:
  51. return TableLineGenerator._parse_ppstructure_data(ocr_result)
  52. else:
  53. raise ValueError(f"不支持的工具类型: {tool}")
  54. @staticmethod
  55. def _parse_mineru_data(mineru_result: Union[Dict, List]) -> Tuple[List[int], Dict]:
  56. """
  57. 解析 MinerU 格式数据(仅提取数据,不分析结构)
  58. Args:
  59. mineru_result: MinerU 的完整 JSON 结果
  60. Returns:
  61. (table_bbox, ocr_data): 表格边界框和文本框列表
  62. """
  63. # 🔑 提取 table 数据
  64. table_data = _extract_table_data(mineru_result)
  65. if not table_data:
  66. raise ValueError("未找到 MinerU 格式的表格数据 (type='table')")
  67. # 验证必要字段
  68. if 'table_cells' not in table_data:
  69. raise ValueError("表格数据中未找到 table_cells 字段")
  70. table_cells = table_data['table_cells']
  71. if not table_cells:
  72. raise ValueError("table_cells 为空")
  73. # 🔑 优先使用 table_body 确定准确的行列数
  74. if 'table_body' in table_data:
  75. actual_rows, actual_cols = _parse_table_body_structure(table_data['table_body'])
  76. print(f"📋 从 table_body 解析: {actual_rows} 行 × {actual_cols} 列")
  77. else:
  78. # 回退:从 table_cells 推断
  79. actual_rows = max(cell.get('row', 0) for cell in table_cells if 'row' in cell)
  80. actual_cols = max(cell.get('col', 0) for cell in table_cells if 'col' in cell)
  81. print(f"📋 从 table_cells 推断: {actual_rows} 行 × {actual_cols} 列")
  82. if not table_data or 'table_cells' not in table_data:
  83. raise ValueError("未找到有效的 MinerU 表格数据")
  84. table_cells = table_data['table_cells']
  85. # 🔑 计算表格边界框
  86. all_bboxes = [cell['bbox'] for cell in table_cells if 'bbox' in cell]
  87. if all_bboxes:
  88. x_min = min(bbox[0] for bbox in all_bboxes)
  89. y_min = min(bbox[1] for bbox in all_bboxes)
  90. x_max = max(bbox[2] for bbox in all_bboxes)
  91. y_max = max(bbox[3] for bbox in all_bboxes)
  92. table_bbox = [x_min, y_min, x_max, y_max]
  93. else:
  94. table_bbox = table_data.get('bbox', [0, 0, 2000, 2000])
  95. # 按位置排序(从上到下,从左到右)
  96. table_cells.sort(key=lambda x: (x['bbox'][1], x['bbox'][0]))
  97. # 🔑 转换为统一的 ocr_data 格式
  98. ocr_data = {
  99. 'table_bbox': table_bbox,
  100. 'actual_rows': actual_rows,
  101. 'actual_cols': actual_cols,
  102. 'text_boxes': table_cells
  103. }
  104. print(f"📊 MinerU 数据解析完成: {len(table_cells)} 个文本框")
  105. return table_bbox, ocr_data
  106. @staticmethod
  107. def _parse_ppstructure_data(ocr_result: Dict) -> Tuple[List[int], Dict]:
  108. """
  109. 解析 PPStructure V3 格式数据
  110. Args:
  111. ocr_result: PPStructure V3 的完整 JSON 结果
  112. Returns:
  113. (table_bbox, ocr_data): 表格边界框和文本框列表
  114. """
  115. # 1. 从 parsing_res_list 中找到 table 区域
  116. table_bbox = None
  117. if 'parsing_res_list' in ocr_result:
  118. for block in ocr_result['parsing_res_list']:
  119. if block.get('block_label') == 'table':
  120. table_bbox = block.get('block_bbox')
  121. break
  122. if not table_bbox:
  123. raise ValueError("未找到表格区域 (block_label='table')")
  124. # 2. 从 overall_ocr_res 中提取文本框
  125. text_boxes = []
  126. if 'overall_ocr_res' in ocr_result:
  127. rec_boxes = ocr_result['overall_ocr_res'].get('rec_boxes', [])
  128. rec_texts = ocr_result['overall_ocr_res'].get('rec_texts', [])
  129. # 过滤出表格区域内的文本框
  130. for i, bbox in enumerate(rec_boxes):
  131. if len(bbox) >= 4:
  132. x1, y1, x2, y2 = bbox[:4]
  133. # 判断文本框是否在表格区域内
  134. if (x1 >= table_bbox[0] and y1 >= table_bbox[1] and
  135. x2 <= table_bbox[2] and y2 <= table_bbox[3]):
  136. text_boxes.append({
  137. 'bbox': [int(x1), int(y1), int(x2), int(y2)],
  138. 'text': rec_texts[i] if i < len(rec_texts) else ''
  139. })
  140. # 按位置排序
  141. text_boxes.sort(key=lambda x: (x['bbox'][1], x['bbox'][0]))
  142. print(f"📊 PPStructure 数据解析完成: {len(text_boxes)} 个文本框")
  143. ocr_data = {
  144. 'table_bbox': table_bbox,
  145. 'text_boxes': text_boxes
  146. }
  147. return table_bbox, ocr_data
  148. # ==================== 统一接口:第二步 - 分析结构 ====================
  149. def analyze_table_structure(self,
  150. y_tolerance: int = 5,
  151. x_tolerance: int = 10,
  152. min_row_height: int = 20,
  153. method: str = "auto",
  154. ) -> Dict:
  155. """
  156. 分析表格结构(支持多种算法)
  157. Args:
  158. y_tolerance: Y轴聚类容差(像素)
  159. x_tolerance: X轴聚类容差(像素)
  160. min_row_height: 最小行高(像素)
  161. method: 分析方法 ("auto" / "cluster" / "mineru")
  162. use_table_body: 是否使用 table_body(仅 mineru 方法有效)
  163. Returns:
  164. 表格结构信息
  165. """
  166. if not self.ocr_data:
  167. return {}
  168. # 🔑 自动选择方法
  169. if method == "auto":
  170. # 根据数据特征自动选择
  171. has_cell_index = any('row' in item and 'col' in item for item in self.ocr_data.get('text_boxes', []))
  172. method = "mineru" if has_cell_index else "cluster"
  173. print(f"🤖 自动选择分析方法: {method}")
  174. # 🔑 根据方法选择算法
  175. if method == "mineru":
  176. return self._analyze_by_cell_index()
  177. else:
  178. return self._analyze_by_clustering(y_tolerance, x_tolerance, min_row_height)
  179. def _analyze_by_cell_index(self) -> Dict:
  180. """
  181. 基于单元格的 row/col 索引分析(MinerU 专用)
  182. Args:
  183. use_table_body: 是否使用 table_body 确定准确的行列数
  184. Returns:
  185. 表格结构信息
  186. """
  187. if not self.ocr_data:
  188. return {}
  189. # 🔑 确定实际行列数
  190. actual_rows = self.ocr_data.get('actual_rows', 0)
  191. actual_cols = self.ocr_data.get('actual_cols', 0)
  192. print(f"📋 检测到: {actual_rows} 行 × {actual_cols} 列")
  193. ocr_data = self.ocr_data.get('text_boxes', [])
  194. # 🔑 按行列索引分组单元格
  195. cells_by_row = {}
  196. cells_by_col = {}
  197. for item in ocr_data:
  198. if 'row' not in item or 'col' not in item:
  199. continue
  200. row = item['row']
  201. col = item['col']
  202. bbox = item['bbox']
  203. if row <= actual_rows and col <= actual_cols:
  204. if row not in cells_by_row:
  205. cells_by_row[row] = []
  206. cells_by_row[row].append(bbox)
  207. if col not in cells_by_col:
  208. cells_by_col[col] = []
  209. cells_by_col[col].append(bbox)
  210. # 🔑 计算每行的 y 边界
  211. row_boundaries = {}
  212. for row_num in range(1, actual_rows + 1):
  213. if row_num in cells_by_row:
  214. bboxes = cells_by_row[row_num]
  215. y_min = min(bbox[1] for bbox in bboxes)
  216. y_max = max(bbox[3] for bbox in bboxes)
  217. row_boundaries[row_num] = (y_min, y_max)
  218. # 🔑 计算横线
  219. horizontal_lines = _calculate_horizontal_lines_with_spacing(row_boundaries)
  220. # 🔑 计算每列的 x 边界
  221. col_boundaries = {}
  222. for col_num in range(1, actual_cols + 1):
  223. if col_num in cells_by_col:
  224. bboxes = cells_by_col[col_num]
  225. x_min = min(bbox[0] for bbox in bboxes)
  226. x_max = max(bbox[2] for bbox in bboxes)
  227. col_boundaries[col_num] = (x_min, x_max)
  228. # 🔑 计算竖线
  229. vertical_lines = _calculate_vertical_lines_with_spacing(col_boundaries)
  230. # 🔑 生成行区间
  231. self.rows = []
  232. for row_num in sorted(row_boundaries.keys()):
  233. y_min, y_max = row_boundaries[row_num]
  234. self.rows.append({
  235. 'y_start': y_min,
  236. 'y_end': y_max,
  237. 'bboxes': cells_by_row.get(row_num, []),
  238. 'row_index': row_num
  239. })
  240. # 🔑 生成列区间
  241. self.columns = []
  242. for col_num in sorted(col_boundaries.keys()):
  243. x_min, x_max = col_boundaries[col_num]
  244. self.columns.append({
  245. 'x_start': x_min,
  246. 'x_end': x_max,
  247. 'col_index': col_num
  248. })
  249. # 计算行高和列宽
  250. self.row_height = int(np.median([r['y_end'] - r['y_start'] for r in self.rows])) if self.rows else 0
  251. self.col_widths = [c['x_end'] - c['x_start'] for c in self.columns]
  252. return {
  253. 'rows': self.rows,
  254. 'columns': self.columns,
  255. 'horizontal_lines': horizontal_lines,
  256. 'vertical_lines': vertical_lines,
  257. 'row_height': self.row_height,
  258. 'col_widths': self.col_widths,
  259. 'table_bbox': self._get_table_bbox(),
  260. 'total_rows': actual_rows,
  261. 'total_cols': actual_cols,
  262. 'method': 'mineru'
  263. }
  264. def _analyze_by_clustering(self, y_tolerance: int, x_tolerance: int, min_row_height: int) -> Dict:
  265. """
  266. 基于坐标聚类分析(通用方法)
  267. Args:
  268. y_tolerance: Y轴聚类容差
  269. x_tolerance: X轴聚类容差
  270. min_row_height: 最小行高
  271. Returns:
  272. 表格结构信息
  273. """
  274. if not self.ocr_data:
  275. return {}
  276. ocr_data = self.ocr_data.get('text_boxes', [])
  277. # 1. 提取所有bbox的Y坐标(用于行检测)
  278. y_coords = []
  279. for item in ocr_data:
  280. bbox = item.get('bbox', [])
  281. if len(bbox) >= 4:
  282. y1, y2 = bbox[1], bbox[3]
  283. y_coords.append((y1, y2, bbox))
  284. # 按Y坐标排序
  285. y_coords.sort(key=lambda x: x[0])
  286. # 2. 聚类检测行
  287. self.rows = self._cluster_rows(y_coords, y_tolerance, min_row_height)
  288. # 3. 计算标准行高
  289. row_heights = [row['y_end'] - row['y_start'] for row in self.rows]
  290. self.row_height = int(np.median(row_heights)) if row_heights else 30
  291. # 4. 提取所有bbox的X坐标(用于列检测)
  292. x_coords = []
  293. for item in self.ocr_data:
  294. bbox = item.get('bbox', [])
  295. if len(bbox) >= 4:
  296. x1, x2 = bbox[0], bbox[2]
  297. x_coords.append((x1, x2))
  298. # 5. 聚类检测列
  299. self.columns = self._cluster_columns(x_coords, x_tolerance)
  300. # 6. 计算列宽
  301. self.col_widths = [col['x_end'] - col['x_start'] for col in self.columns]
  302. # 7. 生成横线坐标
  303. horizontal_lines = []
  304. for row in self.rows:
  305. horizontal_lines.append(row['y_start'])
  306. if self.rows:
  307. horizontal_lines.append(self.rows[-1]['y_end'])
  308. # 8. 生成竖线坐标
  309. vertical_lines = []
  310. for col in self.columns:
  311. vertical_lines.append(col['x_start'])
  312. if self.columns:
  313. vertical_lines.append(self.columns[-1]['x_end'])
  314. return {
  315. 'rows': self.rows,
  316. 'columns': self.columns,
  317. 'horizontal_lines': horizontal_lines,
  318. 'vertical_lines': vertical_lines,
  319. 'row_height': self.row_height,
  320. 'col_widths': self.col_widths,
  321. 'table_bbox': self._get_table_bbox(),
  322. 'method': 'cluster'
  323. }
  324. @staticmethod
  325. def parse_mineru_table_result(mineru_result: Union[Dict, List], use_table_body: bool = True) -> Tuple[List[int], Dict]:
  326. """
  327. [已弃用] 建议使用 parse_ocr_data() + analyze_table_structure()
  328. 保留此方法是为了向后兼容
  329. """
  330. import warnings
  331. warnings.warn(
  332. "parse_mineru_table_result() 已弃用,请使用 "
  333. "parse_ocr_data() + analyze_table_structure()",
  334. DeprecationWarning
  335. )
  336. raise NotImplementedError( "parse_mineru_table_result() 已弃用,请使用 " "parse_ocr_data() + analyze_table_structure()")
  337. @staticmethod
  338. def parse_ppstructure_result(ocr_result: Dict) -> Tuple[List[int], Dict]:
  339. """
  340. [推荐] 解析 PPStructure V3 的 OCR 结果
  341. 这是第一步操作,建议继续使用
  342. """
  343. return TableLineGenerator._parse_ppstructure_data(ocr_result)
  344. def _cluster_rows(self, y_coords: List[Tuple], tolerance: int, min_height: int) -> List[Dict]:
  345. """聚类检测行"""
  346. if not y_coords:
  347. return []
  348. rows = []
  349. current_row = {
  350. 'y_start': y_coords[0][0],
  351. 'y_end': y_coords[0][1],
  352. 'bboxes': [y_coords[0][2]]
  353. }
  354. for i in range(1, len(y_coords)):
  355. y1, y2, bbox = y_coords[i]
  356. if abs(y1 - current_row['y_start']) <= tolerance:
  357. current_row['y_start'] = min(current_row['y_start'], y1)
  358. current_row['y_end'] = max(current_row['y_end'], y2)
  359. current_row['bboxes'].append(bbox)
  360. else:
  361. if current_row['y_end'] - current_row['y_start'] >= min_height:
  362. rows.append(current_row)
  363. current_row = {
  364. 'y_start': y1,
  365. 'y_end': y2,
  366. 'bboxes': [bbox]
  367. }
  368. if current_row['y_end'] - current_row['y_start'] >= min_height:
  369. rows.append(current_row)
  370. return rows
  371. def _cluster_columns(self, x_coords: List[Tuple], tolerance: int) -> List[Dict]:
  372. """聚类检测列"""
  373. if not x_coords:
  374. return []
  375. all_x = []
  376. for x1, x2 in x_coords:
  377. all_x.append(x1)
  378. all_x.append(x2)
  379. all_x = sorted(set(all_x))
  380. columns = []
  381. current_x = all_x[0]
  382. for x in all_x[1:]:
  383. if x - current_x > tolerance:
  384. columns.append(current_x)
  385. current_x = x
  386. columns.append(current_x)
  387. column_regions = []
  388. for i in range(len(columns) - 1):
  389. column_regions.append({
  390. 'x_start': columns[i],
  391. 'x_end': columns[i + 1]
  392. })
  393. return column_regions
  394. def _get_table_bbox(self) -> List[int]:
  395. """获取表格整体边界框"""
  396. if not self.rows or not self.columns:
  397. return [0, 0, self.image.width, self.image.height]
  398. y_min = min(row['y_start'] for row in self.rows)
  399. y_max = max(row['y_end'] for row in self.rows)
  400. x_min = min(col['x_start'] for col in self.columns)
  401. x_max = max(col['x_end'] for col in self.columns)
  402. return [x_min, y_min, x_max, y_max]
  403. def generate_table_lines(self,
  404. line_color: Tuple[int, int, int] = (0, 0, 255),
  405. line_width: int = 2) -> Image.Image:
  406. """在原图上绘制表格线"""
  407. img_with_lines = self.image.copy()
  408. draw = ImageDraw.Draw(img_with_lines)
  409. x_start = self.columns[0]['x_start'] if self.columns else 0
  410. x_end = self.columns[-1]['x_end'] if self.columns else img_with_lines.width
  411. y_start = self.rows[0]['y_start'] if self.rows else 0
  412. y_end = self.rows[-1]['y_end'] if self.rows else img_with_lines.height
  413. # 绘制横线
  414. for row in self.rows:
  415. y = row['y_start']
  416. draw.line([(x_start, y), (x_end, y)], fill=line_color, width=line_width)
  417. if self.rows:
  418. y = self.rows[-1]['y_end']
  419. draw.line([(x_start, y), (x_end, y)], fill=line_color, width=line_width)
  420. # 绘制竖线
  421. for col in self.columns:
  422. x = col['x_start']
  423. draw.line([(x, y_start), (x, y_end)], fill=line_color, width=line_width)
  424. if self.columns:
  425. x = self.columns[-1]['x_end']
  426. draw.line([(x, y_start), (x, y_end)], fill=line_color, width=line_width)
  427. return img_with_lines
  428. def _calculate_horizontal_lines_with_spacing(row_boundaries: Dict[int, Tuple[int, int]]) -> List[int]:
  429. """
  430. 计算横线位置(考虑行间距)
  431. Args:
  432. row_boundaries: {row_num: (y_min, y_max)}
  433. Returns:
  434. 横线 y 坐标列表
  435. """
  436. if not row_boundaries:
  437. return []
  438. sorted_rows = sorted(row_boundaries.items())
  439. # 🔑 分析相邻行之间的间隔
  440. gaps = []
  441. gap_info = [] # 保存详细信息用于调试
  442. for i in range(len(sorted_rows) - 1):
  443. row_num1, (y_min1, y_max1) = sorted_rows[i]
  444. row_num2, (y_min2, y_max2) = sorted_rows[i + 1]
  445. gap = y_min2 - y_max1 # 行间距(可能为负,表示重叠)
  446. gaps.append(gap)
  447. gap_info.append({
  448. 'row1': row_num1,
  449. 'row2': row_num2,
  450. 'gap': gap
  451. })
  452. print(f"📏 行间距详情:")
  453. for info in gap_info:
  454. status = "重叠" if info['gap'] < 0 else "正常"
  455. print(f" 行 {info['row1']} → {info['row2']}: {info['gap']:.1f}px ({status})")
  456. # 🔑 过滤掉负数 gap(重叠情况)和极小的 gap
  457. valid_gaps = [g for g in gaps if g > 2] # 至少 2px 间隔才算有效
  458. if valid_gaps:
  459. gap_median = np.median(valid_gaps)
  460. gap_std = np.std(valid_gaps)
  461. print(f"📏 行间距统计: 中位数={gap_median:.1f}px, 标准差={gap_std:.1f}px")
  462. print(f" 有效间隔数: {len(valid_gaps)}/{len(gaps)}")
  463. # 🔑 生成横线坐标(在相邻行中间)
  464. horizontal_lines = []
  465. for i, (row_num, (y_min, y_max)) in enumerate(sorted_rows):
  466. if i == 0:
  467. # 第一行的上边界
  468. horizontal_lines.append(y_min)
  469. if i < len(sorted_rows) - 1:
  470. next_row_num, (next_y_min, next_y_max) = sorted_rows[i + 1]
  471. gap = next_y_min - y_max
  472. if gap > 0:
  473. # 有间隔:在间隔中间画线
  474. # separator_y = int((y_max + next_y_min) / 2)
  475. # 有间隔:更靠近下一行的位置
  476. separator_y = int(next_y_min) - int(gap / 4)
  477. horizontal_lines.append(separator_y)
  478. else:
  479. # 重叠或紧贴:在当前行的下边界画线
  480. horizontal_lines.append(y_max)
  481. else:
  482. # 最后一行的下边界
  483. horizontal_lines.append(y_max)
  484. return sorted(set(horizontal_lines))
  485. def _calculate_vertical_lines_with_spacing(col_boundaries: Dict[int, Tuple[int, int]]) -> List[int]:
  486. """
  487. 计算竖线位置(考虑列间距和重叠)
  488. Args:
  489. col_boundaries: {col_num: (x_min, x_max)}
  490. Returns:
  491. 竖线 x 坐标列表
  492. """
  493. if not col_boundaries:
  494. return []
  495. sorted_cols = sorted(col_boundaries.items())
  496. # 🔑 分析相邻列之间的间隔
  497. gaps = []
  498. gap_info = []
  499. for i in range(len(sorted_cols) - 1):
  500. col_num1, (x_min1, x_max1) = sorted_cols[i]
  501. col_num2, (x_min2, x_max2) = sorted_cols[i + 1]
  502. gap = x_min2 - x_max1 # 列间距(可能为负)
  503. gaps.append(gap)
  504. gap_info.append({
  505. 'col1': col_num1,
  506. 'col2': col_num2,
  507. 'gap': gap
  508. })
  509. print(f"📏 列间距详情:")
  510. for info in gap_info:
  511. status = "重叠" if info['gap'] < 0 else "正常"
  512. print(f" 列 {info['col1']} → {info['col2']}: {info['gap']:.1f}px ({status})")
  513. # 🔑 过滤掉负数 gap
  514. valid_gaps = [g for g in gaps if g > 2]
  515. if valid_gaps:
  516. gap_median = np.median(valid_gaps)
  517. gap_std = np.std(valid_gaps)
  518. print(f"📏 列间距统计: 中位数={gap_median:.1f}px, 标准差={gap_std:.1f}px")
  519. # 🔑 生成竖线坐标(在相邻列中间)
  520. vertical_lines = []
  521. for i, (col_num, (x_min, x_max)) in enumerate(sorted_cols):
  522. if i == 0:
  523. # 第一列的左边界
  524. vertical_lines.append(x_min)
  525. if i < len(sorted_cols) - 1:
  526. next_col_num, (next_x_min, next_x_max) = sorted_cols[i + 1]
  527. gap = next_x_min - x_max
  528. if gap > 0:
  529. # 有间隔:在间隔中间画线
  530. separator_x = int((x_max + next_x_min) / 2)
  531. vertical_lines.append(separator_x)
  532. else:
  533. # 重叠或紧贴:在当前列的右边界画线
  534. vertical_lines.append(x_max)
  535. else:
  536. # 最后一列的右边界
  537. vertical_lines.append(x_max)
  538. return sorted(set(vertical_lines))
  539. def _extract_table_data(mineru_result: Union[Dict, List]) -> Optional[Dict]:
  540. """提取 table 数据"""
  541. if isinstance(mineru_result, list):
  542. for item in mineru_result:
  543. if isinstance(item, dict) and item.get('type') == 'table':
  544. return item
  545. elif isinstance(mineru_result, dict):
  546. if mineru_result.get('type') == 'table':
  547. return mineru_result
  548. # 递归查找
  549. for value in mineru_result.values():
  550. if isinstance(value, dict) and value.get('type') == 'table':
  551. return value
  552. elif isinstance(value, list):
  553. result = _extract_table_data(value)
  554. if result:
  555. return result
  556. return None
  557. def _parse_table_body_structure(table_body: str) -> Tuple[int, int]:
  558. """从 table_body HTML 中解析准确的行列数"""
  559. try:
  560. soup = BeautifulSoup(table_body, 'html.parser')
  561. table = soup.find('table')
  562. if not table:
  563. raise ValueError("未找到 <table> 标签")
  564. rows = table.find_all('tr')
  565. if not rows:
  566. raise ValueError("未找到 <tr> 标签")
  567. num_rows = len(rows)
  568. first_row = rows[0]
  569. num_cols = len(first_row.find_all(['td', 'th']))
  570. return num_rows, num_cols
  571. except Exception as e:
  572. print(f"⚠️ 解析 table_body 失败: {e}")
  573. return 0, 0