span_block_fix.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_TEXT_RATIO_THRESHOLD = 2 # Threshold for determining vertical text blocks
  65. vertical_span_count = sum(
  66. 1 for span in block['spans']
  67. if (span['bbox'][3] - span['bbox'][1]) / (span['bbox'][2] - span['bbox'][0]) > VERTICAL_TEXT_RATIO_THRESHOLD
  68. )
  69. total_span_count = len(block['spans'])
  70. if total_span_count == 0:
  71. vertical_ratio = 0
  72. else:
  73. vertical_ratio = vertical_span_count / total_span_count
  74. if vertical_ratio > VERTICAL_TEXT_BLOCK_THRESHOLD:
  75. # 如果是纵向文本块,则按纵向lines处理
  76. block_lines = merge_spans_to_vertical_line(block['spans'])
  77. sort_block_lines = vertical_line_sort_spans_from_top_to_bottom(block_lines)
  78. else:
  79. block_lines = merge_spans_to_line(block['spans'])
  80. sort_block_lines = line_sort_spans_by_left_to_right(block_lines)
  81. block['lines'] = sort_block_lines
  82. del block['spans']
  83. return block
  84. def merge_spans_to_line(spans, threshold=0.6):
  85. if len(spans) == 0:
  86. return []
  87. else:
  88. # 按照y0坐标排序
  89. spans.sort(key=lambda span: span['bbox'][1])
  90. lines = []
  91. current_line = [spans[0]]
  92. for span in spans[1:]:
  93. # 如果当前的span类型为"interline_equation" 或者 当前行中已经有"interline_equation"
  94. # image和table类型,同上
  95. if span['type'] in [
  96. ContentType.INTERLINE_EQUATION, ContentType.IMAGE,
  97. ContentType.TABLE
  98. ] or any(s['type'] in [
  99. ContentType.INTERLINE_EQUATION, ContentType.IMAGE,
  100. ContentType.TABLE
  101. ] for s in current_line):
  102. # 则开始新行
  103. lines.append(current_line)
  104. current_line = [span]
  105. continue
  106. # 如果当前的span与当前行的最后一个span在y轴上重叠,则添加到当前行
  107. if __is_overlaps_y_exceeds_threshold(span['bbox'], current_line[-1]['bbox'], threshold):
  108. current_line.append(span)
  109. else:
  110. # 否则,开始新行
  111. lines.append(current_line)
  112. current_line = [span]
  113. # 添加最后一行
  114. if current_line:
  115. lines.append(current_line)
  116. return lines
  117. def merge_spans_to_vertical_line(spans, threshold=0.6):
  118. """将纵向文本的spans合并成纵向lines(从右向左阅读)"""
  119. if len(spans) == 0:
  120. return []
  121. else:
  122. # 按照x2坐标从大到小排序(从右向左)
  123. spans.sort(key=lambda span: span['bbox'][2], reverse=True)
  124. vertical_lines = []
  125. current_line = [spans[0]]
  126. for span in spans[1:]:
  127. # 特殊类型元素单独成列
  128. if span['type'] in [
  129. ContentType.INTERLINE_EQUATION, ContentType.IMAGE,
  130. ContentType.TABLE
  131. ] or any(s['type'] in [
  132. ContentType.INTERLINE_EQUATION, ContentType.IMAGE,
  133. ContentType.TABLE
  134. ] for s in current_line):
  135. vertical_lines.append(current_line)
  136. current_line = [span]
  137. continue
  138. # 如果当前的span与当前行的最后一个span在y轴上重叠,则添加到当前行
  139. if __is_overlaps_x_exceeds_threshold(span['bbox'], current_line[-1]['bbox'], threshold):
  140. current_line.append(span)
  141. else:
  142. vertical_lines.append(current_line)
  143. current_line = [span]
  144. # 添加最后一列
  145. if current_line:
  146. vertical_lines.append(current_line)
  147. return vertical_lines
  148. # 将每一个line中的span从左到右排序
  149. def line_sort_spans_by_left_to_right(lines):
  150. line_objects = []
  151. for line in lines:
  152. # 按照x0坐标排序
  153. line.sort(key=lambda span: span['bbox'][0])
  154. line_bbox = [
  155. min(span['bbox'][0] for span in line), # x0
  156. min(span['bbox'][1] for span in line), # y0
  157. max(span['bbox'][2] for span in line), # x1
  158. max(span['bbox'][3] for span in line), # y1
  159. ]
  160. line_objects.append({
  161. 'bbox': line_bbox,
  162. 'spans': line,
  163. })
  164. return line_objects
  165. def vertical_line_sort_spans_from_top_to_bottom(vertical_lines):
  166. line_objects = []
  167. for line in vertical_lines:
  168. # 按照y0坐标排序(从上到下)
  169. line.sort(key=lambda span: span['bbox'][1])
  170. # 计算整个列的边界框
  171. line_bbox = [
  172. min(span['bbox'][0] for span in line), # x0
  173. min(span['bbox'][1] for span in line), # y0
  174. max(span['bbox'][2] for span in line), # x1
  175. max(span['bbox'][3] for span in line), # y1
  176. ]
  177. # 组装结果
  178. line_objects.append({
  179. 'bbox': line_bbox,
  180. 'spans': line,
  181. })
  182. return line_objects
  183. def fix_block_spans(block_with_spans):
  184. fix_blocks = []
  185. for block in block_with_spans:
  186. block_type = block['type']
  187. if block_type in [BlockType.TEXT, BlockType.TITLE,
  188. BlockType.IMAGE_CAPTION, BlockType.IMAGE_CAPTION,
  189. BlockType.TABLE_CAPTION, BlockType.TABLE_FOOTNOTE
  190. ]:
  191. block = fix_text_block(block)
  192. elif block_type in [BlockType.INTERLINE_EQUATION, BlockType.IMAGE_BODY, BlockType.TABLE_BODY]:
  193. block = fix_interline_block(block)
  194. else:
  195. continue
  196. fix_blocks.append(block)
  197. return fix_blocks
  198. def fix_interline_block(block):
  199. block_lines = merge_spans_to_line(block['spans'])
  200. sort_block_lines = line_sort_spans_by_left_to_right(block_lines)
  201. block['lines'] = sort_block_lines
  202. del block['spans']
  203. return block