block_pre_proc.py 9.5 KB

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