block_pre_proc.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # Copyright (c) Opendatalab. All rights reserved.
  2. from mineru.utils.boxbase import (
  3. calculate_iou,
  4. calculate_overlap_area_in_bbox1_area_ratio,
  5. calculate_vertical_projection_overlap_ratio,
  6. get_minbox_if_overlap_by_ratio
  7. )
  8. from mineru.utils.enum_class import BlockType
  9. def process_groups(groups, body_key, caption_key, footnote_key):
  10. body_blocks = []
  11. caption_blocks = []
  12. footnote_blocks = []
  13. for i, group in enumerate(groups):
  14. group[body_key]['group_id'] = i
  15. body_blocks.append(group[body_key])
  16. for caption_block in group[caption_key]:
  17. caption_block['group_id'] = i
  18. caption_blocks.append(caption_block)
  19. for footnote_block in group[footnote_key]:
  20. footnote_block['group_id'] = i
  21. footnote_blocks.append(footnote_block)
  22. return body_blocks, caption_blocks, footnote_blocks
  23. def prepare_block_bboxes(
  24. img_body_blocks,
  25. img_caption_blocks,
  26. img_footnote_blocks,
  27. table_body_blocks,
  28. table_caption_blocks,
  29. table_footnote_blocks,
  30. discarded_blocks,
  31. text_blocks,
  32. title_blocks,
  33. interline_equation_blocks,
  34. page_w,
  35. page_h,
  36. ):
  37. all_bboxes = []
  38. add_bboxes(img_body_blocks, BlockType.IMAGE_BODY, all_bboxes)
  39. add_bboxes(img_caption_blocks, BlockType.IMAGE_CAPTION, all_bboxes)
  40. add_bboxes(img_footnote_blocks, BlockType.IMAGE_CAPTION, all_bboxes)
  41. add_bboxes(table_body_blocks, BlockType.TABLE_BODY, all_bboxes)
  42. add_bboxes(table_caption_blocks, BlockType.TABLE_CAPTION, all_bboxes)
  43. add_bboxes(table_footnote_blocks, BlockType.TABLE_FOOTNOTE, all_bboxes)
  44. add_bboxes(text_blocks, BlockType.TEXT, all_bboxes)
  45. add_bboxes(title_blocks, BlockType.TITLE, all_bboxes)
  46. add_bboxes(interline_equation_blocks, BlockType.INTERLINE_EQUATION, all_bboxes)
  47. """block嵌套问题解决"""
  48. """文本框与标题框重叠,优先信任文本框"""
  49. all_bboxes = fix_text_overlap_title_blocks(all_bboxes)
  50. """任何框体与舍弃框重叠,优先信任舍弃框"""
  51. all_bboxes = remove_need_drop_blocks(all_bboxes, discarded_blocks)
  52. # interline_equation 与title或text框冲突的情况,分两种情况处理
  53. """interline_equation框与文本类型框iou比较接近1的时候,信任行间公式框"""
  54. all_bboxes = fix_interline_equation_overlap_text_blocks_with_hi_iou(all_bboxes)
  55. """interline_equation框被包含在文本类型框内,且interline_equation比文本区块小很多时信任文本框,这时需要舍弃公式框"""
  56. # 通过后续大框套小框逻辑删除
  57. """discarded_blocks"""
  58. all_discarded_blocks = []
  59. add_bboxes(discarded_blocks, BlockType.DISCARDED, all_discarded_blocks)
  60. """footnote识别:宽度超过1/3页面宽度的,高度超过10的,处于页面下半30%区域的"""
  61. footnote_blocks = []
  62. for discarded in discarded_blocks:
  63. x0, y0, x1, y1 = discarded['bbox']
  64. if (x1 - x0) > (page_w / 3) and (y1 - y0) > 10 and y0 > (page_h * 0.7):
  65. footnote_blocks.append([x0, y0, x1, y1])
  66. """移除在footnote下面的任何框"""
  67. need_remove_blocks = find_blocks_under_footnote(all_bboxes, footnote_blocks)
  68. if len(need_remove_blocks) > 0:
  69. for block in need_remove_blocks:
  70. all_bboxes.remove(block)
  71. all_discarded_blocks.append(block)
  72. """经过以上处理后,还存在大框套小框的情况,则删除小框"""
  73. all_bboxes = remove_overlaps_min_blocks(all_bboxes)
  74. all_discarded_blocks = remove_overlaps_min_blocks(all_discarded_blocks)
  75. """将剩余的bbox做分离处理,防止后面分layout时出错"""
  76. # all_bboxes, drop_reasons = remove_overlap_between_bbox_for_block(all_bboxes)
  77. all_bboxes.sort(key=lambda x: x[0]+x[1])
  78. return all_bboxes, all_discarded_blocks, footnote_blocks
  79. def add_bboxes(blocks, block_type, bboxes):
  80. for block in blocks:
  81. x0, y0, x1, y1 = block['bbox']
  82. if block_type in [
  83. BlockType.IMAGE_BODY,
  84. BlockType.IMAGE_CAPTION,
  85. BlockType.IMAGE_FOOTNOTE,
  86. BlockType.TABLE_BODY,
  87. BlockType.TABLE_CAPTION,
  88. BlockType.TABLE_FOOTNOTE,
  89. ]:
  90. bboxes.append([x0, y0, x1, y1, None, None, None, block_type, None, None, None, None, block['score'], block['group_id']])
  91. else:
  92. bboxes.append([x0, y0, x1, y1, None, None, None, block_type, None, None, None, None, block['score']])
  93. def fix_text_overlap_title_blocks(all_bboxes):
  94. # 先提取所有text和title block
  95. text_blocks = []
  96. for block in all_bboxes:
  97. if block[7] == BlockType.TEXT:
  98. text_blocks.append(block)
  99. title_blocks = []
  100. for block in all_bboxes:
  101. if block[7] == BlockType.TITLE:
  102. title_blocks.append(block)
  103. need_remove = []
  104. for text_block in text_blocks:
  105. for title_block in title_blocks:
  106. text_block_bbox = text_block[:4]
  107. title_block_bbox = title_block[:4]
  108. if calculate_iou(text_block_bbox, title_block_bbox) > 0.8:
  109. if title_block not in need_remove:
  110. need_remove.append(title_block)
  111. if len(need_remove) > 0:
  112. for block in need_remove:
  113. all_bboxes.remove(block)
  114. return all_bboxes
  115. def remove_need_drop_blocks(all_bboxes, discarded_blocks):
  116. need_remove = []
  117. for block in all_bboxes:
  118. for discarded_block in discarded_blocks:
  119. block_bbox = block[:4]
  120. if (
  121. calculate_overlap_area_in_bbox1_area_ratio(
  122. block_bbox, discarded_block['bbox']
  123. )
  124. > 0.6
  125. ):
  126. if block not in need_remove:
  127. need_remove.append(block)
  128. break
  129. if len(need_remove) > 0:
  130. for block in need_remove:
  131. all_bboxes.remove(block)
  132. return all_bboxes
  133. def fix_interline_equation_overlap_text_blocks_with_hi_iou(all_bboxes):
  134. # 先提取所有text和interline block
  135. text_blocks = []
  136. for block in all_bboxes:
  137. if block[7] == BlockType.TEXT:
  138. text_blocks.append(block)
  139. interline_equation_blocks = []
  140. for block in all_bboxes:
  141. if block[7] == BlockType.INTERLINE_EQUATION:
  142. interline_equation_blocks.append(block)
  143. need_remove = []
  144. for interline_equation_block in interline_equation_blocks:
  145. for text_block in text_blocks:
  146. interline_equation_block_bbox = interline_equation_block[:4]
  147. text_block_bbox = text_block[:4]
  148. if calculate_iou(interline_equation_block_bbox, text_block_bbox) > 0.8:
  149. if text_block not in need_remove:
  150. need_remove.append(text_block)
  151. if len(need_remove) > 0:
  152. for block in need_remove:
  153. all_bboxes.remove(block)
  154. return all_bboxes
  155. def find_blocks_under_footnote(all_bboxes, footnote_blocks):
  156. need_remove_blocks = []
  157. for block in all_bboxes:
  158. block_x0, block_y0, block_x1, block_y1 = block[:4]
  159. for footnote_bbox in footnote_blocks:
  160. footnote_x0, footnote_y0, footnote_x1, footnote_y1 = footnote_bbox
  161. # 如果footnote的纵向投影覆盖了block的纵向投影的80%且block的y0大于等于footnote的y1
  162. if (
  163. block_y0 >= footnote_y1
  164. and calculate_vertical_projection_overlap_ratio(
  165. (block_x0, block_y0, block_x1, block_y1), footnote_bbox
  166. )
  167. >= 0.8
  168. ):
  169. if block not in need_remove_blocks:
  170. need_remove_blocks.append(block)
  171. break
  172. return need_remove_blocks
  173. def remove_overlaps_min_blocks(all_bboxes):
  174. # 重叠block,小的不能直接删除,需要和大的那个合并成一个更大的。
  175. # 删除重叠blocks中较小的那些
  176. need_remove = []
  177. for block1 in all_bboxes:
  178. for block2 in all_bboxes:
  179. if block1 != block2:
  180. block1_bbox = block1[:4]
  181. block2_bbox = block2[:4]
  182. overlap_box = get_minbox_if_overlap_by_ratio(
  183. block1_bbox, block2_bbox, 0.8
  184. )
  185. if overlap_box is not None:
  186. block_to_remove = next(
  187. (block for block in all_bboxes if block[:4] == overlap_box),
  188. None,
  189. )
  190. if (
  191. block_to_remove is not None
  192. and block_to_remove not in need_remove
  193. ):
  194. large_block = block1 if block1 != block_to_remove else block2
  195. x1, y1, x2, y2 = large_block[:4]
  196. sx1, sy1, sx2, sy2 = block_to_remove[:4]
  197. x1 = min(x1, sx1)
  198. y1 = min(y1, sy1)
  199. x2 = max(x2, sx2)
  200. y2 = max(y2, sy2)
  201. large_block[:4] = [x1, y1, x2, y2]
  202. need_remove.append(block_to_remove)
  203. if len(need_remove) > 0:
  204. for block in need_remove:
  205. all_bboxes.remove(block)
  206. return all_bboxes