def crop(image, boxes, classes): height, width, _ = image.shape while True: mode = random.choice(( None, (0.1, None), (0.3, None), (0.7, None), (0.9, None), (None, None), )) if mode is None: return image, boxes, classes min_iou, max_iou = mode if min_iou is None: min_iou = float('-inf') if max_iou is None: max_iou = float('inf') for _ in range(50): w = random.randrange(int(0.3 * width), width) h = random.randrange(int(0.3 * height), height) if h / w < 0.5 or 2 < h / w: continue rect = Rect.LTWH(random.randrange(width - w), random.randrange(height - h), w, h) iou = matrix_iou(boxes, np.array((rect, ))) if iou.min() < min_iou and max_iou < iou.max(): continue image = image[rect.top:rect.bottom, rect.left:rect.right] centers = (boxes[:, :2] + boxes[:, 2:]) / 2 mask = np.logical_and((rect.left, rect.top) < centers, centers < (rect.right, rect.bottom)).all(axis=1) boxes = boxes[mask].copy() classes = classes[mask] boxes[:, :2] = np.maximum(boxes[:, :2], (rect.left, rect.top)) boxes[:, :2] -= (rect.left, rect.top) boxes[:, 2:] = np.minimum(boxes[:, 2:], (rect.right, rect.bottom)) boxes[:, 2:] -= (rect.left, rect.top) return image, boxes, classes