boxbase.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. # Compute the intersection over union by taking the intersection area
  136. # and dividing it by the sum of both areas minus the intersection area
  137. iou = intersection_area / float(bbox1_area + bbox2_area -
  138. intersection_area)
  139. return iou
  140. def calculate_overlap_area_2_minbox_area_ratio(bbox1, bbox2):
  141. """计算box1和box2的重叠面积占最小面积的box的比例."""
  142. # Determine the coordinates of the intersection rectangle
  143. x_left = max(bbox1[0], bbox2[0])
  144. y_top = max(bbox1[1], bbox2[1])
  145. x_right = min(bbox1[2], bbox2[2])
  146. y_bottom = min(bbox1[3], bbox2[3])
  147. if x_right < x_left or y_bottom < y_top:
  148. return 0.0
  149. # The area of overlap area
  150. intersection_area = (x_right - x_left) * (y_bottom - y_top)
  151. min_box_area = min([(bbox1[2] - bbox1[0]) * (bbox1[3] - bbox1[1]),
  152. (bbox2[3] - bbox2[1]) * (bbox2[2] - bbox2[0])])
  153. if min_box_area == 0:
  154. return 0
  155. else:
  156. return intersection_area / min_box_area
  157. def calculate_overlap_area_in_bbox1_area_ratio(bbox1, bbox2):
  158. """计算box1和box2的重叠面积占bbox1的比例."""
  159. # Determine the coordinates of the intersection rectangle
  160. x_left = max(bbox1[0], bbox2[0])
  161. y_top = max(bbox1[1], bbox2[1])
  162. x_right = min(bbox1[2], bbox2[2])
  163. y_bottom = min(bbox1[3], bbox2[3])
  164. if x_right < x_left or y_bottom < y_top:
  165. return 0.0
  166. # The area of overlap area
  167. intersection_area = (x_right - x_left) * (y_bottom - y_top)
  168. bbox1_area = (bbox1[2] - bbox1[0]) * (bbox1[3] - bbox1[1])
  169. if bbox1_area == 0:
  170. return 0
  171. else:
  172. return intersection_area / bbox1_area
  173. def get_minbox_if_overlap_by_ratio(bbox1, bbox2, ratio):
  174. """通过calculate_overlap_area_2_minbox_area_ratio计算两个bbox重叠的面积占最小面积的box的比例
  175. 如果比例大于ratio,则返回小的那个bbox, 否则返回None."""
  176. x1_min, y1_min, x1_max, y1_max = bbox1
  177. x2_min, y2_min, x2_max, y2_max = bbox2
  178. area1 = (x1_max - x1_min) * (y1_max - y1_min)
  179. area2 = (x2_max - x2_min) * (y2_max - y2_min)
  180. overlap_ratio = calculate_overlap_area_2_minbox_area_ratio(bbox1, bbox2)
  181. if overlap_ratio > ratio:
  182. if area1 <= area2:
  183. return bbox1
  184. else:
  185. return bbox2
  186. else:
  187. return None
  188. def get_bbox_in_boundary(bboxes: list, boundary: tuple) -> list:
  189. x0, y0, x1, y1 = boundary
  190. new_boxes = [
  191. box for box in bboxes
  192. if box[0] >= x0 and box[1] >= y0 and box[2] <= x1 and box[3] <= y1
  193. ]
  194. return new_boxes
  195. def is_vbox_on_side(bbox, width, height, side_threshold=0.2):
  196. """判断一个bbox是否在pdf页面的边缘."""
  197. x0, x1 = bbox[0], bbox[2]
  198. if x1 <= width * side_threshold or x0 >= width * (1 - side_threshold):
  199. return True
  200. return False
  201. def find_top_nearest_text_bbox(pymu_blocks, obj_bbox):
  202. tolerance_margin = 4
  203. top_boxes = [
  204. box for box in pymu_blocks
  205. if obj_bbox[1] - box['bbox'][3] >= -tolerance_margin
  206. and not _is_in(box['bbox'], obj_bbox)
  207. ]
  208. # 然后找到X方向上有互相重叠的
  209. top_boxes = [
  210. box for box in top_boxes if any([
  211. obj_bbox[0] - tolerance_margin <= box['bbox'][0] <= obj_bbox[2] +
  212. tolerance_margin, obj_bbox[0] -
  213. tolerance_margin <= box['bbox'][2] <= obj_bbox[2] +
  214. tolerance_margin, box['bbox'][0] -
  215. tolerance_margin <= obj_bbox[0] <= box['bbox'][2] +
  216. tolerance_margin, box['bbox'][0] -
  217. tolerance_margin <= obj_bbox[2] <= box['bbox'][2] +
  218. tolerance_margin
  219. ])
  220. ]
  221. # 然后找到y1最大的那个
  222. if len(top_boxes) > 0:
  223. top_boxes.sort(key=lambda x: x['bbox'][3], reverse=True)
  224. return top_boxes[0]
  225. else:
  226. return None
  227. def find_bottom_nearest_text_bbox(pymu_blocks, obj_bbox):
  228. bottom_boxes = [
  229. box for box in pymu_blocks if box['bbox'][1] -
  230. obj_bbox[3] >= -2 and not _is_in(box['bbox'], obj_bbox)
  231. ]
  232. # 然后找到X方向上有互相重叠的
  233. bottom_boxes = [
  234. box for box in bottom_boxes if any([
  235. obj_bbox[0] - 2 <= box['bbox'][0] <= obj_bbox[2] + 2, obj_bbox[0] -
  236. 2 <= box['bbox'][2] <= obj_bbox[2] + 2, box['bbox'][0] -
  237. 2 <= obj_bbox[0] <= box['bbox'][2] + 2, box['bbox'][0] -
  238. 2 <= obj_bbox[2] <= box['bbox'][2] + 2
  239. ])
  240. ]
  241. # 然后找到y0最小的那个
  242. if len(bottom_boxes) > 0:
  243. bottom_boxes.sort(key=lambda x: x['bbox'][1], reverse=False)
  244. return bottom_boxes[0]
  245. else:
  246. return None
  247. def find_left_nearest_text_bbox(pymu_blocks, obj_bbox):
  248. """寻找左侧最近的文本block."""
  249. left_boxes = [
  250. box for box in pymu_blocks if obj_bbox[0] -
  251. box['bbox'][2] >= -2 and not _is_in(box['bbox'], obj_bbox)
  252. ]
  253. # 然后找到X方向上有互相重叠的
  254. left_boxes = [
  255. box for box in left_boxes if any([
  256. obj_bbox[1] - 2 <= box['bbox'][1] <= obj_bbox[3] + 2, obj_bbox[1] -
  257. 2 <= box['bbox'][3] <= obj_bbox[3] + 2, box['bbox'][1] -
  258. 2 <= obj_bbox[1] <= box['bbox'][3] + 2, box['bbox'][1] -
  259. 2 <= obj_bbox[3] <= box['bbox'][3] + 2
  260. ])
  261. ]
  262. # 然后找到x1最大的那个
  263. if len(left_boxes) > 0:
  264. left_boxes.sort(key=lambda x: x['bbox'][2], reverse=True)
  265. return left_boxes[0]
  266. else:
  267. return None
  268. def find_right_nearest_text_bbox(pymu_blocks, obj_bbox):
  269. """寻找右侧最近的文本block."""
  270. right_boxes = [
  271. box for box in pymu_blocks if box['bbox'][0] -
  272. obj_bbox[2] >= -2 and not _is_in(box['bbox'], obj_bbox)
  273. ]
  274. # 然后找到X方向上有互相重叠的
  275. right_boxes = [
  276. box for box in right_boxes if any([
  277. obj_bbox[1] - 2 <= box['bbox'][1] <= obj_bbox[3] + 2, obj_bbox[1] -
  278. 2 <= box['bbox'][3] <= obj_bbox[3] + 2, box['bbox'][1] -
  279. 2 <= obj_bbox[1] <= box['bbox'][3] + 2, box['bbox'][1] -
  280. 2 <= obj_bbox[3] <= box['bbox'][3] + 2
  281. ])
  282. ]
  283. # 然后找到x0最小的那个
  284. if len(right_boxes) > 0:
  285. right_boxes.sort(key=lambda x: x['bbox'][0], reverse=False)
  286. return right_boxes[0]
  287. else:
  288. return None
  289. def bbox_relative_pos(bbox1, bbox2):
  290. """判断两个矩形框的相对位置关系.
  291. Args:
  292. bbox1: 一个四元组,表示第一个矩形框的左上角和右下角的坐标,格式为(x1, y1, x1b, y1b)
  293. bbox2: 一个四元组,表示第二个矩形框的左上角和右下角的坐标,格式为(x2, y2, x2b, y2b)
  294. Returns:
  295. 一个四元组,表示矩形框1相对于矩形框2的位置关系,格式为(left, right, bottom, top)
  296. 其中,left表示矩形框1是否在矩形框2的左侧,right表示矩形框1是否在矩形框2的右侧,
  297. bottom表示矩形框1是否在矩形框2的下方,top表示矩形框1是否在矩形框2的上方
  298. """
  299. x1, y1, x1b, y1b = bbox1
  300. x2, y2, x2b, y2b = bbox2
  301. left = x2b < x1
  302. right = x1b < x2
  303. bottom = y2b < y1
  304. top = y1b < y2
  305. return left, right, bottom, top
  306. def bbox_distance(bbox1, bbox2):
  307. """计算两个矩形框的距离。
  308. Args:
  309. bbox1 (tuple): 第一个矩形框的坐标,格式为 (x1, y1, x2, y2),其中 (x1, y1) 为左上角坐标,(x2, y2) 为右下角坐标。
  310. bbox2 (tuple): 第二个矩形框的坐标,格式为 (x1, y1, x2, y2),其中 (x1, y1) 为左上角坐标,(x2, y2) 为右下角坐标。
  311. Returns:
  312. float: 矩形框之间的距离。
  313. """
  314. def dist(point1, point2):
  315. return math.sqrt((point1[0] - point2[0])**2 +
  316. (point1[1] - point2[1])**2)
  317. x1, y1, x1b, y1b = bbox1
  318. x2, y2, x2b, y2b = bbox2
  319. left, right, bottom, top = bbox_relative_pos(bbox1, bbox2)
  320. if top and left:
  321. return dist((x1, y1b), (x2b, y2))
  322. elif left and bottom:
  323. return dist((x1, y1), (x2b, y2b))
  324. elif bottom and right:
  325. return dist((x1b, y1), (x2, y2b))
  326. elif right and top:
  327. return dist((x1b, y1b), (x2, y2))
  328. elif left:
  329. return x1 - x2b
  330. elif right:
  331. return x2 - x1b
  332. elif bottom:
  333. return y1 - y2b
  334. elif top:
  335. return y2 - y1b
  336. return 0.0
  337. def box_area(bbox):
  338. return (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
  339. def get_overlap_area(bbox1, bbox2):
  340. """计算box1和box2的重叠面积占bbox1的比例."""
  341. # Determine the coordinates of the intersection rectangle
  342. x_left = max(bbox1[0], bbox2[0])
  343. y_top = max(bbox1[1], bbox2[1])
  344. x_right = min(bbox1[2], bbox2[2])
  345. y_bottom = min(bbox1[3], bbox2[3])
  346. if x_right < x_left or y_bottom < y_top:
  347. return 0.0
  348. # The area of overlap area
  349. return (x_right - x_left) * (y_bottom - y_top)
  350. def calculate_vertical_projection_overlap_ratio(block1, block2):
  351. """
  352. Calculate the proportion of the x-axis covered by the vertical projection of two blocks.
  353. Args:
  354. block1 (tuple): Coordinates of the first block (x0, y0, x1, y1).
  355. block2 (tuple): Coordinates of the second block (x0, y0, x1, y1).
  356. Returns:
  357. float: The proportion of the x-axis covered by the vertical projection of the two blocks.
  358. """
  359. x0_1, _, x1_1, _ = block1
  360. x0_2, _, x1_2, _ = block2
  361. # Calculate the intersection of the x-coordinates
  362. x_left = max(x0_1, x0_2)
  363. x_right = min(x1_1, x1_2)
  364. if x_right < x_left:
  365. return 0.0
  366. # Length of the intersection
  367. intersection_length = x_right - x_left
  368. # Length of the x-axis projection of the first block
  369. block1_length = x1_1 - x0_1
  370. if block1_length == 0:
  371. return 0.0
  372. # Proportion of the x-axis covered by the intersection
  373. # logger.info(f"intersection_length: {intersection_length}, block1_length: {block1_length}")
  374. return intersection_length / block1_length