boxbase.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. from loguru import logger
  2. def _is_in_or_part_overlap(box1, box2) -> bool:
  3. """
  4. 两个bbox是否有部分重叠或者包含
  5. """
  6. if box1 is None or box2 is None:
  7. return False
  8. x0_1, y0_1, x1_1, y1_1 = box1
  9. x0_2, y0_2, x1_2, y1_2 = box2
  10. return not (x1_1 < x0_2 or # box1在box2的左边
  11. x0_1 > x1_2 or # box1在box2的右边
  12. y1_1 < y0_2 or # box1在box2的上边
  13. y0_1 > y1_2) # box1在box2的下边
  14. def _is_in(box1, box2) -> bool:
  15. """
  16. box1是否完全在box2里面
  17. """
  18. x0_1, y0_1, x1_1, y1_1 = box1
  19. x0_2, y0_2, x1_2, y1_2 = box2
  20. return (x0_1 >= x0_2 and # box1的左边界不在box2的左边外
  21. y0_1 >= y0_2 and # box1的上边界不在box2的上边外
  22. x1_1 <= x1_2 and # box1的右边界不在box2的右边外
  23. y1_1 <= y1_2) # box1的下边界不在box2的下边外
  24. def _is_part_overlap(box1, box2) -> bool:
  25. """
  26. 两个bbox是否有部分重叠,但不完全包含
  27. """
  28. if box1 is None or box2 is None:
  29. return False
  30. return _is_in_or_part_overlap(box1, box2) and not _is_in(box1, box2)
  31. def _left_intersect(left_box, right_box):
  32. "检查两个box的左边界是否有交集,也就是left_box的右边界是否在right_box的左边界内"
  33. if left_box is None or right_box is None:
  34. return False
  35. x0_1, y0_1, x1_1, y1_1 = left_box
  36. x0_2, y0_2, x1_2, y1_2 = right_box
  37. return x1_1>x0_2 and x0_1<x0_2 and (y0_1<=y0_2<=y1_1 or y0_1<=y1_2<=y1_1)
  38. def _right_intersect(left_box, right_box):
  39. """
  40. 检查box是否在右侧边界有交集,也就是left_box的左边界是否在right_box的右边界内
  41. """
  42. if left_box is None or right_box is None:
  43. return False
  44. x0_1, y0_1, x1_1, y1_1 = left_box
  45. x0_2, y0_2, x1_2, y1_2 = right_box
  46. return x0_1<x1_2 and x1_1>x1_2 and (y0_1<=y0_2<=y1_1 or y0_1<=y1_2<=y1_1)
  47. def _is_vertical_full_overlap(box1, box2, x_torlence=2):
  48. """
  49. x方向上:要么box1包含box2, 要么box2包含box1。不能部分包含
  50. y方向上:box1和box2有重叠
  51. """
  52. # 解析box的坐标
  53. x11, y11, x12, y12 = box1 # 左上角和右下角的坐标 (x1, y1, x2, y2)
  54. x21, y21, x22, y22 = box2
  55. # 在x轴方向上,box1是否包含box2 或 box2包含box1
  56. contains_in_x = (x11-x_torlence <= x21 and x12+x_torlence >= x22) or (x21-x_torlence <= x11 and x22+x_torlence >= x12)
  57. # 在y轴方向上,box1和box2是否有重叠
  58. overlap_in_y = not (y12 < y21 or y11 > y22)
  59. return contains_in_x and overlap_in_y
  60. def _is_bottom_full_overlap(box1, box2, y_tolerance=2):
  61. """
  62. 检查box1下方和box2的上方有轻微的重叠,轻微程度收到y_tolerance的限制
  63. 这个函数和_is_vertical-full_overlap的区别是,这个函数允许box1和box2在x方向上有轻微的重叠,允许一定的模糊度
  64. """
  65. if box1 is None or box2 is None:
  66. return False
  67. x0_1, y0_1, x1_1, y1_1 = box1
  68. x0_2, y0_2, x1_2, y1_2 = box2
  69. tolerance_margin = 2
  70. is_xdir_full_overlap = ((x0_1-tolerance_margin<=x0_2<=x1_1+tolerance_margin and x0_1-tolerance_margin<=x1_2<=x1_1+tolerance_margin) or (x0_2-tolerance_margin<=x0_1<=x1_2+tolerance_margin and x0_2-tolerance_margin<=x1_1<=x1_2+tolerance_margin))
  71. return y0_2<y1_1 and 0<(y1_1-y0_2)<y_tolerance and is_xdir_full_overlap
  72. def _is_left_overlap(box1, box2,):
  73. """
  74. 检查box1的左侧是否和box2有重叠
  75. 在Y方向上可以是部分重叠或者是完全重叠。不分box1和box2的上下关系,也就是无论box1在box2下方还是box2在box1下方,都可以检测到重叠。
  76. X方向上
  77. """
  78. def __overlap_y(Ay1, Ay2, By1, By2):
  79. return max(0, min(Ay2, By2) - max(Ay1, By1))
  80. if box1 is None or box2 is None:
  81. return False
  82. x0_1, y0_1, x1_1, y1_1 = box1
  83. x0_2, y0_2, x1_2, y1_2 = box2
  84. y_overlap_len = __overlap_y(y0_1, y1_1, y0_2, y1_2)
  85. ratio_1 = 1.0 * y_overlap_len / (y1_1 - y0_1) if y1_1-y0_1!=0 else 0
  86. ratio_2 = 1.0 * y_overlap_len / (y1_2 - y0_2) if y1_2-y0_2!=0 else 0
  87. vertical_overlap_cond = ratio_1 >= 0.5 or ratio_2 >= 0.5
  88. #vertical_overlap_cond = y0_1<=y0_2<=y1_1 or y0_1<=y1_2<=y1_1 or y0_2<=y0_1<=y1_2 or y0_2<=y1_1<=y1_2
  89. return x0_1<=x0_2<=x1_1 and vertical_overlap_cond
  90. def __is_overlaps_y_exceeds_threshold(bbox1, bbox2, overlap_ratio_threshold=0.8):
  91. """检查两个bbox在y轴上是否有重叠,并且该重叠区域的高度占两个bbox高度更低的那个超过80%"""
  92. _, y0_1, _, y1_1 = bbox1
  93. _, y0_2, _, y1_2 = bbox2
  94. overlap = max(0, min(y1_1, y1_2) - max(y0_1, y0_2))
  95. height1, height2 = y1_1 - y0_1, y1_2 - y0_2
  96. max_height = max(height1, height2)
  97. min_height = min(height1, height2)
  98. return (overlap / min_height) > overlap_ratio_threshold
  99. def calculate_iou(bbox1, bbox2):
  100. # Determine the coordinates of the intersection rectangle
  101. x_left = max(bbox1[0], bbox2[0])
  102. y_top = max(bbox1[1], bbox2[1])
  103. x_right = min(bbox1[2], bbox2[2])
  104. y_bottom = min(bbox1[3], bbox2[3])
  105. if x_right < x_left or y_bottom < y_top:
  106. return 0.0
  107. # The area of overlap area
  108. intersection_area = (x_right - x_left) * (y_bottom - y_top)
  109. # The area of both rectangles
  110. bbox1_area = (bbox1[2] - bbox1[0]) * (bbox1[3] - bbox1[1])
  111. bbox2_area = (bbox2[2] - bbox2[0]) * (bbox2[3] - bbox2[1])
  112. # Compute the intersection over union by taking the intersection area
  113. # and dividing it by the sum of both areas minus the intersection area
  114. iou = intersection_area / float(bbox1_area + bbox2_area - intersection_area)
  115. return iou
  116. def calculate_overlap_area_2_minbox_area_ratio(bbox1, bbox2):
  117. """
  118. 计算box1和box2的重叠面积占最小面积的box的比例
  119. """
  120. # Determine the coordinates of the intersection rectangle
  121. x_left = max(bbox1[0], bbox2[0])
  122. y_top = max(bbox1[1], bbox2[1])
  123. x_right = min(bbox1[2], bbox2[2])
  124. y_bottom = min(bbox1[3], bbox2[3])
  125. if x_right < x_left or y_bottom < y_top:
  126. return 0.0
  127. # The area of overlap area
  128. intersection_area = (x_right - x_left) * (y_bottom - y_top)
  129. min_box_area = min([(bbox1[2]-bbox1[0])*(bbox1[3]-bbox1[1]), (bbox2[3]-bbox2[1])*(bbox2[2]-bbox2[0])])
  130. if min_box_area==0:
  131. return 0
  132. else:
  133. return intersection_area / min_box_area
  134. def calculate_overlap_area_in_bbox1_area_ratio(bbox1, bbox2):
  135. """
  136. 计算box1和box2的重叠面积占bbox1的比例
  137. """
  138. # Determine the coordinates of the intersection rectangle
  139. x_left = max(bbox1[0], bbox2[0])
  140. y_top = max(bbox1[1], bbox2[1])
  141. x_right = min(bbox1[2], bbox2[2])
  142. y_bottom = min(bbox1[3], bbox2[3])
  143. if x_right < x_left or y_bottom < y_top:
  144. return 0.0
  145. # The area of overlap area
  146. intersection_area = (x_right - x_left) * (y_bottom - y_top)
  147. bbox1_area = (bbox1[2]-bbox1[0])*(bbox1[3]-bbox1[1])
  148. if bbox1_area == 0:
  149. return 0
  150. else:
  151. return intersection_area / bbox1_area
  152. def get_minbox_if_overlap_by_ratio(bbox1, bbox2, ratio):
  153. """
  154. 通过calculate_overlap_area_2_minbox_area_ratio计算两个bbox重叠的面积占最小面积的box的比例
  155. 如果比例大于ratio,则返回小的那个bbox,
  156. 否则返回None
  157. """
  158. x1_min, y1_min, x1_max, y1_max = bbox1
  159. x2_min, y2_min, x2_max, y2_max = bbox2
  160. area1 = (x1_max - x1_min) * (y1_max - y1_min)
  161. area2 = (x2_max - x2_min) * (y2_max - y2_min)
  162. overlap_ratio = calculate_overlap_area_2_minbox_area_ratio(bbox1, bbox2)
  163. if overlap_ratio > ratio:
  164. if area1 <= area2:
  165. return bbox1
  166. else:
  167. return bbox2
  168. else:
  169. return None
  170. def get_bbox_in_boundry(bboxes:list, boundry:tuple)-> list:
  171. x0, y0, x1, y1 = boundry
  172. new_boxes = [box for box in bboxes if box[0] >= x0 and box[1] >= y0 and box[2] <= x1 and box[3] <= y1]
  173. return new_boxes
  174. def is_vbox_on_side(bbox, width, height, side_threshold=0.2):
  175. """
  176. 判断一个bbox是否在pdf页面的边缘
  177. """
  178. x0, x1 = bbox[0], bbox[2]
  179. if x1<=width*side_threshold or x0>=width*(1-side_threshold):
  180. return True
  181. return False
  182. def find_top_nearest_text_bbox(pymu_blocks, obj_bbox):
  183. tolerance_margin = 4
  184. top_boxes = [box for box in pymu_blocks if obj_bbox[1]-box['bbox'][3] >=-tolerance_margin and not _is_in(box['bbox'], obj_bbox)]
  185. # 然后找到X方向上有互相重叠的
  186. top_boxes = [box for box in top_boxes if any([obj_bbox[0]-tolerance_margin <=box['bbox'][0]<=obj_bbox[2]+tolerance_margin,
  187. obj_bbox[0]-tolerance_margin <=box['bbox'][2]<=obj_bbox[2]+tolerance_margin,
  188. box['bbox'][0]-tolerance_margin <=obj_bbox[0]<=box['bbox'][2]+tolerance_margin,
  189. box['bbox'][0]-tolerance_margin <=obj_bbox[2]<=box['bbox'][2]+tolerance_margin
  190. ])]
  191. # 然后找到y1最大的那个
  192. if len(top_boxes)>0:
  193. top_boxes.sort(key=lambda x: x['bbox'][3], reverse=True)
  194. return top_boxes[0]
  195. else:
  196. return None
  197. def find_bottom_nearest_text_bbox(pymu_blocks, obj_bbox):
  198. bottom_boxes = [box for box in pymu_blocks if box['bbox'][1] - obj_bbox[3]>=-2 and not _is_in(box['bbox'], obj_bbox)]
  199. # 然后找到X方向上有互相重叠的
  200. bottom_boxes = [box for box in bottom_boxes if any([obj_bbox[0]-2 <=box['bbox'][0]<=obj_bbox[2]+2,
  201. obj_bbox[0]-2 <=box['bbox'][2]<=obj_bbox[2]+2,
  202. box['bbox'][0]-2 <=obj_bbox[0]<=box['bbox'][2]+2,
  203. box['bbox'][0]-2 <=obj_bbox[2]<=box['bbox'][2]+2
  204. ])]
  205. # 然后找到y0最小的那个
  206. if len(bottom_boxes)>0:
  207. bottom_boxes.sort(key=lambda x: x['bbox'][1], reverse=False)
  208. return bottom_boxes[0]
  209. else:
  210. return None
  211. def find_left_nearest_text_bbox(pymu_blocks, obj_bbox):
  212. """
  213. 寻找左侧最近的文本block
  214. """
  215. left_boxes = [box for box in pymu_blocks if obj_bbox[0]-box['bbox'][2]>=-2 and not _is_in(box['bbox'], obj_bbox)]
  216. # 然后找到X方向上有互相重叠的
  217. left_boxes = [box for box in left_boxes if any([obj_bbox[1]-2 <=box['bbox'][1]<=obj_bbox[3]+2,
  218. obj_bbox[1]-2 <=box['bbox'][3]<=obj_bbox[3]+2,
  219. box['bbox'][1]-2 <=obj_bbox[1]<=box['bbox'][3]+2,
  220. box['bbox'][1]-2 <=obj_bbox[3]<=box['bbox'][3]+2
  221. ])]
  222. # 然后找到x1最大的那个
  223. if len(left_boxes)>0:
  224. left_boxes.sort(key=lambda x: x['bbox'][2], reverse=True)
  225. return left_boxes[0]
  226. else:
  227. return None
  228. def find_right_nearest_text_bbox(pymu_blocks, obj_bbox):
  229. """
  230. 寻找右侧最近的文本block
  231. """
  232. right_boxes = [box for box in pymu_blocks if box['bbox'][0]-obj_bbox[2]>=-2 and not _is_in(box['bbox'], obj_bbox)]
  233. # 然后找到X方向上有互相重叠的
  234. right_boxes = [box for box in right_boxes if any([obj_bbox[1]-2 <=box['bbox'][1]<=obj_bbox[3]+2,
  235. obj_bbox[1]-2 <=box['bbox'][3]<=obj_bbox[3]+2,
  236. box['bbox'][1]-2 <=obj_bbox[1]<=box['bbox'][3]+2,
  237. box['bbox'][1]-2 <=obj_bbox[3]<=box['bbox'][3]+2
  238. ])]
  239. # 然后找到x0最小的那个
  240. if len(right_boxes)>0:
  241. right_boxes.sort(key=lambda x: x['bbox'][0], reverse=False)
  242. return right_boxes[0]
  243. else:
  244. return None