boxbase.py 17 KB

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