ocr_dict_merge.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 [
  56. BlockType.Text,
  57. BlockType.Title,
  58. BlockType.ImageCaption,
  59. BlockType.ImageFootnote,
  60. BlockType.TableCaption,
  61. BlockType.TableFootnote,
  62. BlockType.Discarded
  63. ]
  64. elif span_type == ContentType.InterlineEquation:
  65. return block_type in [BlockType.InterlineEquation, BlockType.Text]
  66. elif span_type == ContentType.Image:
  67. return block_type in [BlockType.ImageBody]
  68. elif span_type == ContentType.Table:
  69. return block_type in [BlockType.TableBody]
  70. else:
  71. return False
  72. def fill_spans_in_blocks(blocks, spans, radio):
  73. """将allspans中的span按位置关系,放入blocks中."""
  74. block_with_spans = []
  75. for block in blocks:
  76. block_type = block[7]
  77. block_bbox = block[0:4]
  78. block_dict = {
  79. 'type': block_type,
  80. 'bbox': block_bbox,
  81. }
  82. if block_type in [
  83. BlockType.ImageBody, BlockType.ImageCaption, BlockType.ImageFootnote,
  84. BlockType.TableBody, BlockType.TableCaption, BlockType.TableFootnote
  85. ]:
  86. block_dict['group_id'] = block[-1]
  87. block_spans = []
  88. for span in spans:
  89. span_bbox = span['bbox']
  90. if calculate_overlap_area_in_bbox1_area_ratio(span_bbox, block_bbox) > radio and span_block_type_compatible(span['type'], block_type):
  91. block_spans.append(span)
  92. block_dict['spans'] = block_spans
  93. block_with_spans.append(block_dict)
  94. # 从spans删除已经放入block_spans中的span
  95. if len(block_spans) > 0:
  96. for span in block_spans:
  97. spans.remove(span)
  98. return block_with_spans, spans
  99. def fix_block_spans_v2(block_with_spans):
  100. fix_blocks = []
  101. for block in block_with_spans:
  102. block_type = block['type']
  103. if block_type in [BlockType.Text, BlockType.Title,
  104. BlockType.ImageCaption, BlockType.ImageFootnote,
  105. BlockType.TableCaption, BlockType.TableFootnote
  106. ]:
  107. block = fix_text_block(block)
  108. elif block_type in [BlockType.InterlineEquation, BlockType.ImageBody, BlockType.TableBody]:
  109. block = fix_interline_block(block)
  110. else:
  111. continue
  112. fix_blocks.append(block)
  113. return fix_blocks
  114. def fix_discarded_block(discarded_block_with_spans):
  115. fix_discarded_blocks = []
  116. for block in discarded_block_with_spans:
  117. block = fix_text_block(block)
  118. fix_discarded_blocks.append(block)
  119. return fix_discarded_blocks
  120. def fix_text_block(block):
  121. # 文本block中的公式span都应该转换成行内type
  122. for span in block['spans']:
  123. if span['type'] == ContentType.InterlineEquation:
  124. span['type'] = ContentType.InlineEquation
  125. block_lines = merge_spans_to_line(block['spans'])
  126. sort_block_lines = line_sort_spans_by_left_to_right(block_lines)
  127. block['lines'] = sort_block_lines
  128. del block['spans']
  129. return block
  130. def fix_interline_block(block):
  131. block_lines = merge_spans_to_line(block['spans'])
  132. sort_block_lines = line_sort_spans_by_left_to_right(block_lines)
  133. block['lines'] = sort_block_lines
  134. del block['spans']
  135. return block