span_block_fix.py 8.3 KB

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