ocr_dict_merge.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from magic_pdf.config.ocr_content_type import BlockType, ContentType
  2. from magic_pdf.libs.boxbase import __is_overlaps_y_exceeds_threshold, calculate_overlap_area_in_bbox1_area_ratio
  3. # 将每一个line中的span从左到右排序
  4. def line_sort_spans_by_left_to_right(lines):
  5. line_objects = []
  6. for line in lines:
  7. # 按照x0坐标排序
  8. line.sort(key=lambda span: span['bbox'][0])
  9. line_bbox = [
  10. min(span['bbox'][0] for span in line), # x0
  11. min(span['bbox'][1] for span in line), # y0
  12. max(span['bbox'][2] for span in line), # x1
  13. max(span['bbox'][3] for span in line), # y1
  14. ]
  15. line_objects.append({
  16. 'bbox': line_bbox,
  17. 'spans': line,
  18. })
  19. return line_objects
  20. def merge_spans_to_line(spans, threshold=0.6):
  21. if len(spans) == 0:
  22. return []
  23. else:
  24. # 按照y0坐标排序
  25. spans.sort(key=lambda span: span['bbox'][1])
  26. lines = []
  27. current_line = [spans[0]]
  28. for span in spans[1:]:
  29. # 如果当前的span类型为"interline_equation" 或者 当前行中已经有"interline_equation"
  30. # image和table类型,同上
  31. if span['type'] in [
  32. ContentType.InterlineEquation, ContentType.Image,
  33. ContentType.Table
  34. ] or any(s['type'] in [
  35. ContentType.InterlineEquation, ContentType.Image,
  36. ContentType.Table
  37. ] for s in current_line):
  38. # 则开始新行
  39. lines.append(current_line)
  40. current_line = [span]
  41. continue
  42. # 如果当前的span与当前行的最后一个span在y轴上重叠,则添加到当前行
  43. if __is_overlaps_y_exceeds_threshold(span['bbox'], current_line[-1]['bbox'], threshold):
  44. current_line.append(span)
  45. else:
  46. # 否则,开始新行
  47. lines.append(current_line)
  48. current_line = [span]
  49. # 添加最后一行
  50. if current_line:
  51. lines.append(current_line)
  52. return lines
  53. def span_block_type_compatible(span_type, block_type):
  54. if span_type in [ContentType.Text, ContentType.InlineEquation]:
  55. return block_type in [BlockType.Text, BlockType.Title, BlockType.ImageCaption, BlockType.ImageFootnote, BlockType.TableCaption, BlockType.TableFootnote]
  56. elif span_type == ContentType.InterlineEquation:
  57. return block_type in [BlockType.InterlineEquation]
  58. elif span_type == ContentType.Image:
  59. return block_type in [BlockType.ImageBody]
  60. elif span_type == ContentType.Table:
  61. return block_type in [BlockType.TableBody]
  62. else:
  63. return False
  64. def fill_spans_in_blocks(blocks, spans, radio):
  65. """将allspans中的span按位置关系,放入blocks中."""
  66. block_with_spans = []
  67. for block in blocks:
  68. block_type = block[7]
  69. block_bbox = block[0:4]
  70. block_dict = {
  71. 'type': block_type,
  72. 'bbox': block_bbox,
  73. }
  74. if block_type in [
  75. BlockType.ImageBody, BlockType.ImageCaption, BlockType.ImageFootnote,
  76. BlockType.TableBody, BlockType.TableCaption, BlockType.TableFootnote
  77. ]:
  78. block_dict['group_id'] = block[-1]
  79. block_spans = []
  80. for span in spans:
  81. span_bbox = span['bbox']
  82. if calculate_overlap_area_in_bbox1_area_ratio(span_bbox, block_bbox) > radio and span_block_type_compatible(span['type'], block_type):
  83. block_spans.append(span)
  84. block_dict['spans'] = block_spans
  85. block_with_spans.append(block_dict)
  86. # 从spans删除已经放入block_spans中的span
  87. if len(block_spans) > 0:
  88. for span in block_spans:
  89. spans.remove(span)
  90. return block_with_spans, spans
  91. def fix_block_spans_v2(block_with_spans):
  92. fix_blocks = []
  93. for block in block_with_spans:
  94. block_type = block['type']
  95. if block_type in [BlockType.Text, BlockType.Title,
  96. BlockType.ImageCaption, BlockType.ImageFootnote,
  97. BlockType.TableCaption, BlockType.TableFootnote
  98. ]:
  99. block = fix_text_block(block)
  100. elif block_type in [BlockType.InterlineEquation, BlockType.ImageBody, BlockType.TableBody]:
  101. block = fix_interline_block(block)
  102. else:
  103. continue
  104. fix_blocks.append(block)
  105. return fix_blocks
  106. def fix_discarded_block(discarded_block_with_spans):
  107. fix_discarded_blocks = []
  108. for block in discarded_block_with_spans:
  109. block = fix_text_block(block)
  110. fix_discarded_blocks.append(block)
  111. return fix_discarded_blocks
  112. def fix_text_block(block):
  113. # 文本block中的公式span都应该转换成行内type
  114. for span in block['spans']:
  115. if span['type'] == ContentType.InterlineEquation:
  116. span['type'] = ContentType.InlineEquation
  117. block_lines = merge_spans_to_line(block['spans'])
  118. sort_block_lines = line_sort_spans_by_left_to_right(block_lines)
  119. block['lines'] = sort_block_lines
  120. del block['spans']
  121. return block
  122. def fix_interline_block(block):
  123. block_lines = merge_spans_to_line(block['spans'])
  124. sort_block_lines = line_sort_spans_by_left_to_right(block_lines)
  125. block['lines'] = sort_block_lines
  126. del block['spans']
  127. return block