import numpy as np # from icecream import ic def get_surrounding_bboxes(bboxes: np.ndarray, point: np.ndarray): is_surrounding = np.all(bboxes[:, :2] - point < 0, axis=1) * \ np.all(bboxes[:, :2] + bboxes[:, 2:] - point > 0, axis=1) return bboxes[is_surrounding] def get_bbox_by_point(bboxes: np.ndarray, point: np.ndarray): surrounding_bboxes = bboxes if len(surrounding_bboxes) < 1: bbox = None elif len(surrounding_bboxes) == 1: bbox = surrounding_bboxes[0] else: centers = surrounding_bboxes[:, :2] + surrounding_bboxes[:, 2:]/2 dist = np.linalg.norm(centers - point, axis=1) bbox = surrounding_bboxes[np.argmin(dist)] return bbox