ocr_span_list_modify.py 12 KB

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