ocr_span_list_modify.py 10 KB

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