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