span_block_fix.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # Copyright (c) Opendatalab. All rights reserved.
  2. from mineru.utils.boxbase import calculate_overlap_area_in_bbox1_area_ratio
  3. from mineru.utils.enum_class import BlockType, ContentType
  4. from mineru.utils.ocr_utils import _is_overlaps_y_exceeds_threshold, _is_overlaps_x_exceeds_threshold
  5. VERTICAL_SPAN_HEIGHT_TO_WIDTH_RATIO_THRESHOLD = 2
  6. VERTICAL_SPAN_IN_BLOCK_THRESHOLD = 0.8
  7. def fill_spans_in_blocks(blocks, spans, radio):
  8. """将allspans中的span按位置关系,放入blocks中."""
  9. block_with_spans = []
  10. for block in blocks:
  11. block_type = block[7]
  12. block_bbox = block[0:4]
  13. block_dict = {
  14. 'type': block_type,
  15. 'bbox': block_bbox,
  16. }
  17. if block_type in [
  18. BlockType.IMAGE_BODY, BlockType.IMAGE_CAPTION, BlockType.IMAGE_FOOTNOTE,
  19. BlockType.TABLE_BODY, BlockType.TABLE_CAPTION, BlockType.TABLE_FOOTNOTE
  20. ]:
  21. block_dict['group_id'] = block[-1]
  22. block_spans = []
  23. for span in spans:
  24. span_bbox = span['bbox']
  25. if calculate_overlap_area_in_bbox1_area_ratio(span_bbox, block_bbox) > radio and span_block_type_compatible(
  26. span['type'], block_type):
  27. block_spans.append(span)
  28. block_dict['spans'] = block_spans
  29. block_with_spans.append(block_dict)
  30. # 从spans删除已经放入block_spans中的span
  31. if len(block_spans) > 0:
  32. for span in block_spans:
  33. spans.remove(span)
  34. return block_with_spans, spans
  35. def span_block_type_compatible(span_type, block_type):
  36. if span_type in [ContentType.TEXT, ContentType.INLINE_EQUATION]:
  37. return block_type in [
  38. BlockType.TEXT,
  39. BlockType.TITLE,
  40. BlockType.IMAGE_CAPTION,
  41. BlockType.IMAGE_FOOTNOTE,
  42. BlockType.TABLE_CAPTION,
  43. BlockType.TABLE_FOOTNOTE,
  44. BlockType.DISCARDED
  45. ]
  46. elif span_type == ContentType.INTERLINE_EQUATION:
  47. return block_type in [BlockType.INTERLINE_EQUATION, BlockType.TEXT]
  48. elif span_type == ContentType.IMAGE:
  49. return block_type in [BlockType.IMAGE_BODY]
  50. elif span_type == ContentType.TABLE:
  51. return block_type in [BlockType.TABLE_BODY]
  52. else:
  53. return False
  54. def fix_discarded_block(discarded_block_with_spans):
  55. fix_discarded_blocks = []
  56. for block in discarded_block_with_spans:
  57. block = fix_text_block(block)
  58. fix_discarded_blocks.append(block)
  59. return fix_discarded_blocks
  60. def fix_text_block(block):
  61. # 文本block中的公式span都应该转换成行内type
  62. for span in block['spans']:
  63. if span['type'] == ContentType.INTERLINE_EQUATION:
  64. span['type'] = ContentType.INLINE_EQUATION
  65. # 假设block中的span超过80%的数量高度是宽度的两倍以上,则认为是纵向文本块
  66. vertical_span_count = sum(
  67. 1 for span in block['spans']
  68. if (span['bbox'][3] - span['bbox'][1]) / (span['bbox'][2] - span['bbox'][0]) > VERTICAL_SPAN_HEIGHT_TO_WIDTH_RATIO_THRESHOLD
  69. )
  70. total_span_count = len(block['spans'])
  71. if total_span_count == 0:
  72. vertical_ratio = 0
  73. else:
  74. vertical_ratio = vertical_span_count / total_span_count
  75. if vertical_ratio > VERTICAL_SPAN_IN_BLOCK_THRESHOLD:
  76. # 如果是纵向文本块,则按纵向lines处理
  77. block_lines = merge_spans_to_vertical_line(block['spans'])
  78. sort_block_lines = vertical_line_sort_spans_from_top_to_bottom(block_lines)
  79. else:
  80. block_lines = merge_spans_to_line(block['spans'])
  81. sort_block_lines = line_sort_spans_by_left_to_right(block_lines)
  82. block['lines'] = sort_block_lines
  83. del block['spans']
  84. return block
  85. def merge_spans_to_line(spans, threshold=0.6):
  86. if len(spans) == 0:
  87. return []
  88. else:
  89. # 按照y0坐标排序
  90. spans.sort(key=lambda span: span['bbox'][1])
  91. lines = []
  92. current_line = [spans[0]]
  93. for span in spans[1:]:
  94. # 如果当前的span类型为"interline_equation" 或者 当前行中已经有"interline_equation"
  95. # image和table类型,同上
  96. if span['type'] in [
  97. ContentType.INTERLINE_EQUATION, ContentType.IMAGE,
  98. ContentType.TABLE
  99. ] or any(s['type'] in [
  100. ContentType.INTERLINE_EQUATION, ContentType.IMAGE,
  101. ContentType.TABLE
  102. ] for s in current_line):
  103. # 则开始新行
  104. lines.append(current_line)
  105. current_line = [span]
  106. continue
  107. # 如果当前的span与当前行的最后一个span在y轴上重叠,则添加到当前行
  108. if _is_overlaps_y_exceeds_threshold(span['bbox'], current_line[-1]['bbox'], threshold):
  109. current_line.append(span)
  110. else:
  111. # 否则,开始新行
  112. lines.append(current_line)
  113. current_line = [span]
  114. # 添加最后一行
  115. if current_line:
  116. lines.append(current_line)
  117. return lines
  118. def merge_spans_to_vertical_line(spans, threshold=0.6):
  119. """将纵向文本的spans合并成纵向lines(从右向左阅读)"""
  120. if len(spans) == 0:
  121. return []
  122. else:
  123. # 按照x2坐标从大到小排序(从右向左)
  124. spans.sort(key=lambda span: span['bbox'][2], reverse=True)
  125. vertical_lines = []
  126. current_line = [spans[0]]
  127. for span in spans[1:]:
  128. # 特殊类型元素单独成列
  129. if span['type'] in [
  130. ContentType.INTERLINE_EQUATION, ContentType.IMAGE,
  131. ContentType.TABLE
  132. ] or any(s['type'] in [
  133. ContentType.INTERLINE_EQUATION, ContentType.IMAGE,
  134. ContentType.TABLE
  135. ] for s in current_line):
  136. vertical_lines.append(current_line)
  137. current_line = [span]
  138. continue
  139. # 如果当前的span与当前行的最后一个span在y轴上重叠,则添加到当前行
  140. if _is_overlaps_x_exceeds_threshold(span['bbox'], current_line[-1]['bbox'], threshold):
  141. current_line.append(span)
  142. else:
  143. vertical_lines.append(current_line)
  144. current_line = [span]
  145. # 添加最后一列
  146. if current_line:
  147. vertical_lines.append(current_line)
  148. return vertical_lines
  149. # 将每一个line中的span从左到右排序
  150. def line_sort_spans_by_left_to_right(lines):
  151. line_objects = []
  152. for line in lines:
  153. # 按照x0坐标排序
  154. line.sort(key=lambda span: span['bbox'][0])
  155. line_bbox = [
  156. min(span['bbox'][0] for span in line), # x0
  157. min(span['bbox'][1] for span in line), # y0
  158. max(span['bbox'][2] for span in line), # x1
  159. max(span['bbox'][3] for span in line), # y1
  160. ]
  161. line_objects.append({
  162. 'bbox': line_bbox,
  163. 'spans': line,
  164. })
  165. return line_objects
  166. def vertical_line_sort_spans_from_top_to_bottom(vertical_lines):
  167. line_objects = []
  168. for line in vertical_lines:
  169. # 按照y0坐标排序(从上到下)
  170. line.sort(key=lambda span: span['bbox'][1])
  171. # 计算整个列的边界框
  172. line_bbox = [
  173. min(span['bbox'][0] for span in line), # x0
  174. min(span['bbox'][1] for span in line), # y0
  175. max(span['bbox'][2] for span in line), # x1
  176. max(span['bbox'][3] for span in line), # y1
  177. ]
  178. # 组装结果
  179. line_objects.append({
  180. 'bbox': line_bbox,
  181. 'spans': line,
  182. })
  183. return line_objects
  184. def fix_block_spans(block_with_spans):
  185. fix_blocks = []
  186. for block in block_with_spans:
  187. block_type = block['type']
  188. if block_type in [BlockType.TEXT, BlockType.TITLE,
  189. BlockType.IMAGE_CAPTION, BlockType.IMAGE_CAPTION,
  190. BlockType.TABLE_CAPTION, BlockType.TABLE_FOOTNOTE
  191. ]:
  192. block = fix_text_block(block)
  193. elif block_type in [BlockType.INTERLINE_EQUATION, BlockType.IMAGE_BODY, BlockType.TABLE_BODY]:
  194. block = fix_interline_block(block)
  195. else:
  196. continue
  197. fix_blocks.append(block)
  198. return fix_blocks
  199. def fix_interline_block(block):
  200. block_lines = merge_spans_to_line(block['spans'])
  201. sort_block_lines = line_sort_spans_by_left_to_right(block_lines)
  202. block['lines'] = sort_block_lines
  203. del block['spans']
  204. return block