remove_rotate_bbox.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import math
  2. from pdf_tools.libs.boxbase import is_vbox_on_side
  3. def detect_non_horizontal_texts(result_dict):
  4. """
  5. This function detects watermarks and vertical margin notes in the document.
  6. Watermarks are identified by finding blocks with the same coordinates and frequently occurring identical texts across multiple pages.
  7. If these conditions are met, the blocks are highly likely to be watermarks, as opposed to headers or footers, which can change from page to page.
  8. If the direction of these blocks is not horizontal, they are definitely considered to be watermarks.
  9. Vertical margin notes are identified by finding blocks with the same coordinates and frequently occurring identical texts across multiple pages.
  10. If these conditions are met, the blocks are highly likely to be vertical margin notes, which typically appear on the left and right sides of the page.
  11. If the direction of these blocks is vertical, they are definitely considered to be vertical margin notes.
  12. Parameters
  13. ----------
  14. result_dict : dict
  15. The result dictionary.
  16. Returns
  17. -------
  18. result_dict : dict
  19. The updated result dictionary.
  20. """
  21. # Dictionary to store information about potential watermarks
  22. potential_watermarks = {}
  23. potential_margin_notes = {}
  24. for page_id, page_content in result_dict.items():
  25. if page_id.startswith("page_"):
  26. for block_id, block_data in page_content.items():
  27. if block_id.startswith("block_"):
  28. if "dir" in block_data:
  29. coordinates_text = (block_data["bbox"], block_data["text"]) # Tuple of coordinates and text
  30. angle = math.atan2(block_data["dir"][1], block_data["dir"][0])
  31. angle = abs(math.degrees(angle))
  32. if angle > 5 and angle < 85: # Check if direction is watermarks
  33. if coordinates_text in potential_watermarks:
  34. potential_watermarks[coordinates_text] += 1
  35. else:
  36. potential_watermarks[coordinates_text] = 1
  37. if angle > 85 and angle < 105: # Check if direction is vertical
  38. if coordinates_text in potential_margin_notes:
  39. potential_margin_notes[coordinates_text] += 1 # Increment count
  40. else:
  41. potential_margin_notes[coordinates_text] = 1 # Initialize count
  42. # Identify watermarks by finding entries with counts higher than a threshold (e.g., appearing on more than half of the pages)
  43. watermark_threshold = len(result_dict) // 2
  44. watermarks = {k: v for k, v in potential_watermarks.items() if v > watermark_threshold}
  45. # Identify margin notes by finding entries with counts higher than a threshold (e.g., appearing on more than half of the pages)
  46. margin_note_threshold = len(result_dict) // 2
  47. margin_notes = {k: v for k, v in potential_margin_notes.items() if v > margin_note_threshold}
  48. # Add watermark information to the result dictionary
  49. for page_id, blocks in result_dict.items():
  50. if page_id.startswith("page_"):
  51. for block_id, block_data in blocks.items():
  52. coordinates_text = (block_data["bbox"], block_data["text"])
  53. if coordinates_text in watermarks:
  54. block_data["is_watermark"] = 1
  55. else:
  56. block_data["is_watermark"] = 0
  57. if coordinates_text in margin_notes:
  58. block_data["is_vertical_margin_note"] = 1
  59. else:
  60. block_data["is_vertical_margin_note"] = 0
  61. return result_dict
  62. """
  63. 1. 当一个block里全部文字都不是dir=(1,0),这个block整体去掉
  64. 2. 当一个block里全部文字都是dir=(1,0),但是每行只有一个字,这个block整体去掉。这个block必须出现在页面的四周,否则不去掉
  65. """
  66. import re
  67. def __is_a_word(sentence):
  68. # 如果输入是中文并且长度为1,则返回True
  69. if re.fullmatch(r'[\u4e00-\u9fa5]', sentence):
  70. return True
  71. # 判断是否为单个英文单词或字符(包括ASCII标点)
  72. elif re.fullmatch(r'[a-zA-Z0-9]+', sentence) and len(sentence) <=2:
  73. return True
  74. else:
  75. return False
  76. def __get_text_color(num):
  77. """获取字体的颜色RGB值"""
  78. blue = num & 255
  79. green = (num >> 8) & 255
  80. red = (num >> 16) & 255
  81. return red, green, blue
  82. def __is_empty_side_box(text_block):
  83. """
  84. 是否是边缘上的空白没有任何内容的block
  85. """
  86. for line in text_block['lines']:
  87. for span in line['spans']:
  88. font_color = span['color']
  89. r,g,b = __get_text_color(font_color)
  90. if len(span['text'].strip())>0 and (r,g,b)!=(255,255,255):
  91. return False
  92. return True
  93. def remove_rotate_side_textblock(pymu_text_block, page_width, page_height):
  94. """
  95. 返回删除了垂直,水印,旋转的textblock
  96. 删除的内容打上tag返回
  97. """
  98. removed_text_block = []
  99. for i, block in enumerate(pymu_text_block): # 格式参考test/assets/papre/pymu_textblocks.json
  100. lines = block['lines']
  101. block_bbox = block['bbox']
  102. if not is_vbox_on_side(block_bbox, page_width, page_height, 0.2): # 保证这些box必须在页面的两边
  103. continue
  104. if all([__is_a_word(line['spans'][0]["text"]) for line in lines if len(line['spans'])>0]) and len(lines)>1 and all([len(line['spans'])==1 for line in lines]):
  105. is_box_valign = (len(set([int(line['spans'][0]['bbox'][0] ) for line in lines if len(line['spans'])>0]))==1) and (len([int(line['spans'][0]['bbox'][0] ) for line in lines if len(line['spans'])>0])>1) # 测试bbox在垂直方向是不是x0都相等,也就是在垂直方向排列.同时必须大于等于2个字
  106. if is_box_valign:
  107. block['tag'] = "vertical-text"
  108. removed_text_block.append(block)
  109. continue
  110. for line in lines:
  111. if line['dir']!=(1,0):
  112. block['tag'] = "rotate"
  113. removed_text_block.append(block) # 只要有一个line不是dir=(1,0),就把整个block都删掉
  114. break
  115. for block in removed_text_block:
  116. pymu_text_block.remove(block)
  117. return pymu_text_block, removed_text_block
  118. def get_side_boundry(rotate_bbox, page_width, page_height):
  119. """
  120. 根据rotate_bbox,返回页面的左右正文边界
  121. """
  122. left_x = 0
  123. right_x = page_width
  124. for x in rotate_bbox:
  125. box = x['bbox']
  126. if box[2]<page_width/2:
  127. left_x = max(left_x, box[2])
  128. else:
  129. right_x = min(right_x, box[0])
  130. return left_x+1, right_x-1
  131. def remove_side_blank_block(pymu_text_block, page_width, page_height):
  132. """
  133. 删除页面两侧的空白block
  134. """
  135. removed_text_block = []
  136. for i, block in enumerate(pymu_text_block): # 格式参考test/assets/papre/pymu_textblocks.json
  137. block_bbox = block['bbox']
  138. if not is_vbox_on_side(block_bbox, page_width, page_height, 0.2): # 保证这些box必须在页面的两边
  139. continue
  140. if __is_empty_side_box(block):
  141. block['tag'] = "empty-side-block"
  142. removed_text_block.append(block)
  143. continue
  144. for block in removed_text_block:
  145. pymu_text_block.remove(block)
  146. return pymu_text_block, removed_text_block