ocr_detect_all_bboxes.py 9.1 KB

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