ocr_span_list_modify.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. from loguru import logger
  2. from magic_pdf.libs.boxbase import calculate_overlap_area_in_bbox1_area_ratio, get_minbox_if_overlap_by_ratio, \
  3. __is_overlaps_y_exceeds_threshold
  4. from magic_pdf.libs.ocr_content_type import ContentType
  5. def remove_overlaps_min_spans(spans):
  6. # 删除重叠spans中较小的那些
  7. for span1 in spans.copy():
  8. for span2 in spans.copy():
  9. if span1 != span2:
  10. overlap_box = get_minbox_if_overlap_by_ratio(span1['bbox'], span2['bbox'], 0.65)
  11. if overlap_box is not None:
  12. bbox_to_remove = next((span for span in spans if span['bbox'] == overlap_box), None)
  13. if bbox_to_remove is not None:
  14. spans.remove(bbox_to_remove)
  15. return spans
  16. def remove_spans_by_bboxes(spans, need_remove_spans_bboxes):
  17. # 遍历spans, 判断是否在removed_span_block_bboxes中
  18. # 如果是, 则删除该span 否则, 保留该span
  19. need_remove_spans = []
  20. for span in spans:
  21. for removed_bbox in need_remove_spans_bboxes:
  22. if calculate_overlap_area_in_bbox1_area_ratio(span['bbox'], removed_bbox) > 0.5:
  23. need_remove_spans.append(span)
  24. break
  25. for span in need_remove_spans:
  26. spans.remove(span)
  27. return spans
  28. def remove_spans_by_bboxes_dict(spans, need_remove_spans_bboxes_dict):
  29. dropped_text_block = []
  30. dropped_image_block = []
  31. dropped_table_block = []
  32. for drop_tag, removed_bboxes in need_remove_spans_bboxes_dict.items():
  33. # logger.info(f"remove spans by bbox dict, drop_tag: {drop_tag}, removed_bboxes: {removed_bboxes}")
  34. need_remove_spans = []
  35. for span in spans:
  36. for removed_bbox in removed_bboxes:
  37. if calculate_overlap_area_in_bbox1_area_ratio(span['bbox'], removed_bbox) > 0.5:
  38. need_remove_spans.append(span)
  39. break
  40. for span in need_remove_spans:
  41. spans.remove(span)
  42. span['tag'] = drop_tag
  43. if span['type'] in [ContentType.Text, ContentType.InlineEquation, ContentType.InterlineEquation]:
  44. dropped_text_block.append(span)
  45. elif span['type'] == ContentType.Image:
  46. dropped_image_block.append(span)
  47. elif span['type'] == ContentType.Table:
  48. dropped_table_block.append(span)
  49. return spans, dropped_text_block, dropped_image_block, dropped_table_block
  50. def adjust_bbox_for_standalone_block(spans):
  51. # 对tpye=["interline_equation", "image", "table"]进行额外处理,如果左边有字的话,将该span的bbox中y0调整至不高于文字的y0
  52. for sb_span in spans:
  53. if sb_span['type'] in [ContentType.InterlineEquation, ContentType.Image, ContentType.Table]:
  54. for text_span in spans:
  55. if text_span['type'] in [ContentType.Text, ContentType.InlineEquation]:
  56. # 判断span2的纵向高度是否被span所覆盖
  57. if sb_span['bbox'][1] < text_span['bbox'][1] and sb_span['bbox'][3] > text_span['bbox'][3]:
  58. # 判断span2是否在span左边
  59. if text_span['bbox'][0] < sb_span['bbox'][0]:
  60. # 调整span的y0和span2的y0一致
  61. sb_span['bbox'][1] = text_span['bbox'][1]
  62. return spans
  63. def modify_y_axis(spans: list, displayed_list: list, text_inline_lines: list):
  64. # displayed_list = []
  65. # 如果spans为空,则不处理
  66. if len(spans) == 0:
  67. pass
  68. else:
  69. spans.sort(key=lambda span: span['bbox'][1])
  70. lines = []
  71. current_line = [spans[0]]
  72. if spans[0]["type"] in [ContentType.InterlineEquation, ContentType.Image, ContentType.Table]:
  73. displayed_list.append(spans[0])
  74. line_first_y0 = spans[0]["bbox"][1]
  75. line_first_y = spans[0]["bbox"][3]
  76. # 用于给行间公式搜索
  77. # text_inline_lines = []
  78. for span in spans[1:]:
  79. # if span.get("content","") == "78.":
  80. # print("debug")
  81. # 如果当前的span类型为"interline_equation" 或者 当前行中已经有"interline_equation"
  82. # image和table类型,同上
  83. if span['type'] in [ContentType.InterlineEquation, ContentType.Image, ContentType.Table] or any(
  84. s['type'] in [ContentType.InterlineEquation, ContentType.Image, ContentType.Table] for s in current_line):
  85. # 传入
  86. if span["type"] in [ContentType.InterlineEquation, ContentType.Image, ContentType.Table]:
  87. displayed_list.append(span)
  88. # 则开始新行
  89. lines.append(current_line)
  90. if len(current_line) > 1 or current_line[0]["type"] in [ContentType.Text, ContentType.InlineEquation]:
  91. text_inline_lines.append((current_line, (line_first_y0, line_first_y)))
  92. current_line = [span]
  93. line_first_y0 = span["bbox"][1]
  94. line_first_y = span["bbox"][3]
  95. continue
  96. # 如果当前的span与当前行的最后一个span在y轴上重叠,则添加到当前行
  97. if __is_overlaps_y_exceeds_threshold(span['bbox'], current_line[-1]['bbox']):
  98. if span["type"] == "text":
  99. line_first_y0 = span["bbox"][1]
  100. line_first_y = span["bbox"][3]
  101. current_line.append(span)
  102. else:
  103. # 否则,开始新行
  104. lines.append(current_line)
  105. text_inline_lines.append((current_line, (line_first_y0, line_first_y)))
  106. current_line = [span]
  107. line_first_y0 = span["bbox"][1]
  108. line_first_y = span["bbox"][3]
  109. # 添加最后一行
  110. if current_line:
  111. lines.append(current_line)
  112. if len(current_line) > 1 or current_line[0]["type"] in [ContentType.Text, ContentType.InlineEquation]:
  113. text_inline_lines.append((current_line, (line_first_y0, line_first_y)))
  114. for line in text_inline_lines:
  115. # 按照x0坐标排序
  116. current_line = line[0]
  117. current_line.sort(key=lambda span: span['bbox'][0])
  118. # 调整每一个文字行内bbox统一
  119. for line in text_inline_lines:
  120. current_line, (line_first_y0, line_first_y) = line
  121. for span in current_line:
  122. span["bbox"][1] = line_first_y0
  123. span["bbox"][3] = line_first_y
  124. # return spans, displayed_list, text_inline_lines
  125. def modify_inline_equation(spans: list, displayed_list: list, text_inline_lines: list):
  126. # 错误行间公式转行内公式
  127. j = 0
  128. for i in range(len(displayed_list)):
  129. # if i == 8:
  130. # print("debug")
  131. span = displayed_list[i]
  132. span_y0, span_y = span["bbox"][1], span["bbox"][3]
  133. while j < len(text_inline_lines):
  134. text_line = text_inline_lines[j]
  135. y0, y1 = text_line[1]
  136. if (
  137. span_y0 < y0 and span_y > y0 or span_y0 < y1 and span_y > y1 or span_y0 < y0 and span_y > y1) and __is_overlaps_y_exceeds_threshold(
  138. span['bbox'], (0, y0, 0, y1)):
  139. # 调整公式类型
  140. if span["type"] == ContentType.InterlineEquation:
  141. # 最后一行是行间公式
  142. if j + 1 >= len(text_inline_lines):
  143. span["type"] = ContentType.InlineEquation
  144. span["bbox"][1] = y0
  145. span["bbox"][3] = y1
  146. else:
  147. # 行间公式旁边有多行文字或者行间公式比文字高3倍则不转换
  148. y0_next, y1_next = text_inline_lines[j + 1][1]
  149. if not __is_overlaps_y_exceeds_threshold(span['bbox'], (0, y0_next, 0, y1_next)) and 3 * (
  150. y1 - y0) > span_y - span_y0:
  151. span["type"] = ContentType.InlineEquation
  152. span["bbox"][1] = y0
  153. span["bbox"][3] = y1
  154. break
  155. elif span_y < y0 or span_y0 < y0 and span_y > y0 and not __is_overlaps_y_exceeds_threshold(span['bbox'],
  156. (0, y0, 0, y1)):
  157. break
  158. else:
  159. j += 1
  160. return spans
  161. def get_qa_need_list(blocks):
  162. # 创建 images, tables, interline_equations, inline_equations 的副本
  163. images = []
  164. tables = []
  165. interline_equations = []
  166. inline_equations = []
  167. for block in blocks:
  168. for line in block["lines"]:
  169. for span in line["spans"]:
  170. if span["type"] == ContentType.Image:
  171. images.append(span)
  172. elif span["type"] == ContentType.Table:
  173. tables.append(span)
  174. elif span["type"] == ContentType.InlineEquation:
  175. inline_equations.append(span)
  176. elif span["type"] == ContentType.InterlineEquation:
  177. interline_equations.append(span)
  178. else:
  179. continue
  180. return images, tables, interline_equations, inline_equations