boxbase.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. from loguru import logger
  2. import math
  3. def _is_in_or_part_overlap(box1, box2) -> bool:
  4. """
  5. 两个bbox是否有部分重叠或者包含
  6. """
  7. if box1 is None or box2 is None:
  8. return False
  9. x0_1, y0_1, x1_1, y1_1 = box1
  10. x0_2, y0_2, x1_2, y1_2 = box2
  11. return not (x1_1 < x0_2 or # box1在box2的左边
  12. x0_1 > x1_2 or # box1在box2的右边
  13. y1_1 < y0_2 or # box1在box2的上边
  14. y0_1 > y1_2) # box1在box2的下边
  15. def _is_in_or_part_overlap_with_area_ratio(box1, box2, area_ratio_threshold=0.6):
  16. """
  17. 判断box1是否在box2里面,或者box1和box2有部分重叠,且重叠面积占box1的比例超过area_ratio_threshold
  18. """
  19. if box1 is None or box2 is None:
  20. return False
  21. x0_1, y0_1, x1_1, y1_1 = box1
  22. x0_2, y0_2, x1_2, y1_2 = box2
  23. if not _is_in_or_part_overlap(box1, box2):
  24. return False
  25. # 计算重叠面积
  26. x_left = max(x0_1, x0_2)
  27. y_top = max(y0_1, y0_2)
  28. x_right = min(x1_1, x1_2)
  29. y_bottom = min(y1_1, y1_2)
  30. overlap_area = (x_right - x_left) * (y_bottom - y_top)
  31. # 计算box1的面积
  32. box1_area = (x1_1 - x0_1) * (y1_1 - y0_1)
  33. return overlap_area / box1_area > area_ratio_threshold
  34. def _is_in(box1, box2) -> bool:
  35. """
  36. box1是否完全在box2里面
  37. """
  38. x0_1, y0_1, x1_1, y1_1 = box1
  39. x0_2, y0_2, x1_2, y1_2 = box2
  40. return (x0_1 >= x0_2 and # box1的左边界不在box2的左边外
  41. y0_1 >= y0_2 and # box1的上边界不在box2的上边外
  42. x1_1 <= x1_2 and # box1的右边界不在box2的右边外
  43. y1_1 <= y1_2) # box1的下边界不在box2的下边外
  44. def _is_part_overlap(box1, box2) -> bool:
  45. """
  46. 两个bbox是否有部分重叠,但不完全包含
  47. """
  48. if box1 is None or box2 is None:
  49. return False
  50. return _is_in_or_part_overlap(box1, box2) and not _is_in(box1, box2)
  51. def _left_intersect(left_box, right_box):
  52. "检查两个box的左边界是否有交集,也就是left_box的右边界是否在right_box的左边界内"
  53. if left_box is None or right_box is None:
  54. return False
  55. x0_1, y0_1, x1_1, y1_1 = left_box
  56. x0_2, y0_2, x1_2, y1_2 = right_box
  57. return x1_1>x0_2 and x0_1<x0_2 and (y0_1<=y0_2<=y1_1 or y0_1<=y1_2<=y1_1)
  58. def _right_intersect(left_box, right_box):
  59. """
  60. 检查box是否在右侧边界有交集,也就是left_box的左边界是否在right_box的右边界内
  61. """
  62. if left_box is None or right_box is None:
  63. return False
  64. x0_1, y0_1, x1_1, y1_1 = left_box
  65. x0_2, y0_2, x1_2, y1_2 = right_box
  66. return x0_1<x1_2 and x1_1>x1_2 and (y0_1<=y0_2<=y1_1 or y0_1<=y1_2<=y1_1)
  67. def _is_vertical_full_overlap(box1, box2, x_torlence=2):
  68. """
  69. x方向上:要么box1包含box2, 要么box2包含box1。不能部分包含
  70. y方向上:box1和box2有重叠
  71. """
  72. # 解析box的坐标
  73. x11, y11, x12, y12 = box1 # 左上角和右下角的坐标 (x1, y1, x2, y2)
  74. x21, y21, x22, y22 = box2
  75. # 在x轴方向上,box1是否包含box2 或 box2包含box1
  76. contains_in_x = (x11-x_torlence <= x21 and x12+x_torlence >= x22) or (x21-x_torlence <= x11 and x22+x_torlence >= x12)
  77. # 在y轴方向上,box1和box2是否有重叠
  78. overlap_in_y = not (y12 < y21 or y11 > y22)
  79. return contains_in_x and overlap_in_y
  80. def _is_bottom_full_overlap(box1, box2, y_tolerance=2):
  81. """
  82. 检查box1下方和box2的上方有轻微的重叠,轻微程度收到y_tolerance的限制
  83. 这个函数和_is_vertical-full_overlap的区别是,这个函数允许box1和box2在x方向上有轻微的重叠,允许一定的模糊度
  84. """
  85. if box1 is None or box2 is None:
  86. return False
  87. x0_1, y0_1, x1_1, y1_1 = box1
  88. x0_2, y0_2, x1_2, y1_2 = box2
  89. tolerance_margin = 2
  90. 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))
  91. return y0_2<y1_1 and 0<(y1_1-y0_2)<y_tolerance and is_xdir_full_overlap
  92. def _is_left_overlap(box1, box2,):
  93. """
  94. 检查box1的左侧是否和box2有重叠
  95. 在Y方向上可以是部分重叠或者是完全重叠。不分box1和box2的上下关系,也就是无论box1在box2下方还是box2在box1下方,都可以检测到重叠。
  96. X方向上
  97. """
  98. def __overlap_y(Ay1, Ay2, By1, By2):
  99. return max(0, min(Ay2, By2) - max(Ay1, By1))
  100. if box1 is None or box2 is None:
  101. return False
  102. x0_1, y0_1, x1_1, y1_1 = box1
  103. x0_2, y0_2, x1_2, y1_2 = box2
  104. y_overlap_len = __overlap_y(y0_1, y1_1, y0_2, y1_2)
  105. ratio_1 = 1.0 * y_overlap_len / (y1_1 - y0_1) if y1_1-y0_1!=0 else 0
  106. ratio_2 = 1.0 * y_overlap_len / (y1_2 - y0_2) if y1_2-y0_2!=0 else 0
  107. vertical_overlap_cond = ratio_1 >= 0.5 or ratio_2 >= 0.5
  108. #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
  109. return x0_1<=x0_2<=x1_1 and vertical_overlap_cond
  110. def __is_overlaps_y_exceeds_threshold(bbox1, bbox2, overlap_ratio_threshold=0.8):
  111. """检查两个bbox在y轴上是否有重叠,并且该重叠区域的高度占两个bbox高度更低的那个超过80%"""
  112. _, y0_1, _, y1_1 = bbox1
  113. _, y0_2, _, y1_2 = bbox2
  114. overlap = max(0, min(y1_1, y1_2) - max(y0_1, y0_2))
  115. height1, height2 = y1_1 - y0_1, y1_2 - y0_2
  116. max_height = max(height1, height2)
  117. min_height = min(height1, height2)
  118. return (overlap / min_height) > overlap_ratio_threshold
  119. def calculate_iou(bbox1, bbox2):
  120. """
  121. 计算两个边界框的交并比(IOU)。
  122. Args:
  123. bbox1 (list[float]): 第一个边界框的坐标,格式为 [x1, y1, x2, y2],其中 (x1, y1) 为左上角坐标,(x2, y2) 为右下角坐标。
  124. bbox2 (list[float]): 第二个边界框的坐标,格式与 `bbox1` 相同。
  125. Returns:
  126. float: 两个边界框的交并比(IOU),取值范围为 [0, 1]。
  127. """
  128. # Determine the coordinates of the intersection rectangle
  129. x_left = max(bbox1[0], bbox2[0])
  130. y_top = max(bbox1[1], bbox2[1])
  131. x_right = min(bbox1[2], bbox2[2])
  132. y_bottom = min(bbox1[3], bbox2[3])
  133. if x_right < x_left or y_bottom < y_top:
  134. return 0.0
  135. # The area of overlap area
  136. intersection_area = (x_right - x_left) * (y_bottom - y_top)
  137. # The area of both rectangles
  138. bbox1_area = (bbox1[2] - bbox1[0]) * (bbox1[3] - bbox1[1])
  139. bbox2_area = (bbox2[2] - bbox2[0]) * (bbox2[3] - bbox2[1])
  140. # Compute the intersection over union by taking the intersection area
  141. # and dividing it by the sum of both areas minus the intersection area
  142. iou = intersection_area / float(bbox1_area + bbox2_area - intersection_area)
  143. return iou
  144. def calculate_overlap_area_2_minbox_area_ratio(bbox1, bbox2):
  145. """
  146. 计算box1和box2的重叠面积占最小面积的box的比例
  147. """
  148. # Determine the coordinates of the intersection rectangle
  149. x_left = max(bbox1[0], bbox2[0])
  150. y_top = max(bbox1[1], bbox2[1])
  151. x_right = min(bbox1[2], bbox2[2])
  152. y_bottom = min(bbox1[3], bbox2[3])
  153. if x_right < x_left or y_bottom < y_top:
  154. return 0.0
  155. # The area of overlap area
  156. intersection_area = (x_right - x_left) * (y_bottom - y_top)
  157. min_box_area = min([(bbox1[2]-bbox1[0])*(bbox1[3]-bbox1[1]), (bbox2[3]-bbox2[1])*(bbox2[2]-bbox2[0])])
  158. if min_box_area==0:
  159. return 0
  160. else:
  161. return intersection_area / min_box_area
  162. def calculate_overlap_area_in_bbox1_area_ratio(bbox1, bbox2):
  163. """
  164. 计算box1和box2的重叠面积占bbox1的比例
  165. """
  166. # Determine the coordinates of the intersection rectangle
  167. x_left = max(bbox1[0], bbox2[0])
  168. y_top = max(bbox1[1], bbox2[1])
  169. x_right = min(bbox1[2], bbox2[2])
  170. y_bottom = min(bbox1[3], bbox2[3])
  171. if x_right < x_left or y_bottom < y_top:
  172. return 0.0
  173. # The area of overlap area
  174. intersection_area = (x_right - x_left) * (y_bottom - y_top)
  175. bbox1_area = (bbox1[2]-bbox1[0])*(bbox1[3]-bbox1[1])
  176. if bbox1_area == 0:
  177. return 0
  178. else:
  179. return intersection_area / bbox1_area
  180. def get_minbox_if_overlap_by_ratio(bbox1, bbox2, ratio):
  181. """
  182. 通过calculate_overlap_area_2_minbox_area_ratio计算两个bbox重叠的面积占最小面积的box的比例
  183. 如果比例大于ratio,则返回小的那个bbox,
  184. 否则返回None
  185. """
  186. x1_min, y1_min, x1_max, y1_max = bbox1
  187. x2_min, y2_min, x2_max, y2_max = bbox2
  188. area1 = (x1_max - x1_min) * (y1_max - y1_min)
  189. area2 = (x2_max - x2_min) * (y2_max - y2_min)
  190. overlap_ratio = calculate_overlap_area_2_minbox_area_ratio(bbox1, bbox2)
  191. if overlap_ratio > ratio:
  192. if area1 <= area2:
  193. return bbox1
  194. else:
  195. return bbox2
  196. else:
  197. return None
  198. def get_bbox_in_boundry(bboxes:list, boundry:tuple)-> list:
  199. x0, y0, x1, y1 = boundry
  200. new_boxes = [box for box in bboxes if box[0] >= x0 and box[1] >= y0 and box[2] <= x1 and box[3] <= y1]
  201. return new_boxes
  202. def is_vbox_on_side(bbox, width, height, side_threshold=0.2):
  203. """
  204. 判断一个bbox是否在pdf页面的边缘
  205. """
  206. x0, x1 = bbox[0], bbox[2]
  207. if x1<=width*side_threshold or x0>=width*(1-side_threshold):
  208. return True
  209. return False
  210. def find_top_nearest_text_bbox(pymu_blocks, obj_bbox):
  211. tolerance_margin = 4
  212. 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)]
  213. # 然后找到X方向上有互相重叠的
  214. top_boxes = [box for box in top_boxes if any([obj_bbox[0]-tolerance_margin <=box['bbox'][0]<=obj_bbox[2]+tolerance_margin,
  215. obj_bbox[0]-tolerance_margin <=box['bbox'][2]<=obj_bbox[2]+tolerance_margin,
  216. box['bbox'][0]-tolerance_margin <=obj_bbox[0]<=box['bbox'][2]+tolerance_margin,
  217. box['bbox'][0]-tolerance_margin <=obj_bbox[2]<=box['bbox'][2]+tolerance_margin
  218. ])]
  219. # 然后找到y1最大的那个
  220. if len(top_boxes)>0:
  221. top_boxes.sort(key=lambda x: x['bbox'][3], reverse=True)
  222. return top_boxes[0]
  223. else:
  224. return None
  225. def find_bottom_nearest_text_bbox(pymu_blocks, obj_bbox):
  226. bottom_boxes = [box for box in pymu_blocks if box['bbox'][1] - obj_bbox[3]>=-2 and not _is_in(box['bbox'], obj_bbox)]
  227. # 然后找到X方向上有互相重叠的
  228. bottom_boxes = [box for box in bottom_boxes if any([obj_bbox[0]-2 <=box['bbox'][0]<=obj_bbox[2]+2,
  229. obj_bbox[0]-2 <=box['bbox'][2]<=obj_bbox[2]+2,
  230. box['bbox'][0]-2 <=obj_bbox[0]<=box['bbox'][2]+2,
  231. box['bbox'][0]-2 <=obj_bbox[2]<=box['bbox'][2]+2
  232. ])]
  233. # 然后找到y0最小的那个
  234. if len(bottom_boxes)>0:
  235. bottom_boxes.sort(key=lambda x: x['bbox'][1], reverse=False)
  236. return bottom_boxes[0]
  237. else:
  238. return None
  239. def find_left_nearest_text_bbox(pymu_blocks, obj_bbox):
  240. """
  241. 寻找左侧最近的文本block
  242. """
  243. left_boxes = [box for box in pymu_blocks if obj_bbox[0]-box['bbox'][2]>=-2 and not _is_in(box['bbox'], obj_bbox)]
  244. # 然后找到X方向上有互相重叠的
  245. left_boxes = [box for box in left_boxes if any([obj_bbox[1]-2 <=box['bbox'][1]<=obj_bbox[3]+2,
  246. obj_bbox[1]-2 <=box['bbox'][3]<=obj_bbox[3]+2,
  247. box['bbox'][1]-2 <=obj_bbox[1]<=box['bbox'][3]+2,
  248. box['bbox'][1]-2 <=obj_bbox[3]<=box['bbox'][3]+2
  249. ])]
  250. # 然后找到x1最大的那个
  251. if len(left_boxes)>0:
  252. left_boxes.sort(key=lambda x: x['bbox'][2], reverse=True)
  253. return left_boxes[0]
  254. else:
  255. return None
  256. def find_right_nearest_text_bbox(pymu_blocks, obj_bbox):
  257. """
  258. 寻找右侧最近的文本block
  259. """
  260. right_boxes = [box for box in pymu_blocks if box['bbox'][0]-obj_bbox[2]>=-2 and not _is_in(box['bbox'], obj_bbox)]
  261. # 然后找到X方向上有互相重叠的
  262. right_boxes = [box for box in right_boxes if any([obj_bbox[1]-2 <=box['bbox'][1]<=obj_bbox[3]+2,
  263. obj_bbox[1]-2 <=box['bbox'][3]<=obj_bbox[3]+2,
  264. box['bbox'][1]-2 <=obj_bbox[1]<=box['bbox'][3]+2,
  265. box['bbox'][1]-2 <=obj_bbox[3]<=box['bbox'][3]+2
  266. ])]
  267. # 然后找到x0最小的那个
  268. if len(right_boxes)>0:
  269. right_boxes.sort(key=lambda x: x['bbox'][0], reverse=False)
  270. return right_boxes[0]
  271. else:
  272. return None
  273. def bbox_relative_pos(bbox1, bbox2):
  274. """
  275. 判断两个矩形框的相对位置关系
  276. Args:
  277. bbox1: 一个四元组,表示第一个矩形框的左上角和右下角的坐标,格式为(x1, y1, x1b, y1b)
  278. bbox2: 一个四元组,表示第二个矩形框的左上角和右下角的坐标,格式为(x2, y2, x2b, y2b)
  279. Returns:
  280. 一个四元组,表示矩形框1相对于矩形框2的位置关系,格式为(left, right, bottom, top)
  281. 其中,left表示矩形框1是否在矩形框2的左侧,right表示矩形框1是否在矩形框2的右侧,
  282. bottom表示矩形框1是否在矩形框2的下方,top表示矩形框1是否在矩形框2的上方
  283. """
  284. x1, y1, x1b, y1b = bbox1
  285. x2, y2, x2b, y2b = bbox2
  286. left = x2b < x1
  287. right = x1b < x2
  288. bottom = y2b < y1
  289. top = y1b < y2
  290. return left, right, bottom, top
  291. def bbox_distance(bbox1, bbox2):
  292. """
  293. 计算两个矩形框的距离。
  294. Args:
  295. bbox1 (tuple): 第一个矩形框的坐标,格式为 (x1, y1, x2, y2),其中 (x1, y1) 为左上角坐标,(x2, y2) 为右下角坐标。
  296. bbox2 (tuple): 第二个矩形框的坐标,格式为 (x1, y1, x2, y2),其中 (x1, y1) 为左上角坐标,(x2, y2) 为右下角坐标。
  297. Returns:
  298. float: 矩形框之间的距离。
  299. """
  300. def dist(point1, point2):
  301. return math.sqrt((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2)
  302. x1, y1, x1b, y1b = bbox1
  303. x2, y2, x2b, y2b = bbox2
  304. left, right, bottom, top = bbox_relative_pos(bbox1, bbox2)
  305. if top and left:
  306. return dist((x1, y1b), (x2b, y2))
  307. elif left and bottom:
  308. return dist((x1, y1), (x2b, y2b))
  309. elif bottom and right:
  310. return dist((x1b, y1), (x2, y2b))
  311. elif right and top:
  312. return dist((x1b, y1b), (x2, y2))
  313. elif left:
  314. return x1 - x2b
  315. elif right:
  316. return x2 - x1b
  317. elif bottom:
  318. return y1 - y2b
  319. elif top:
  320. return y2 - y1b
  321. else: # rectangles intersect
  322. return 0