import torch


def cropToFrame(bbox, image_shape):
    """Checks if bounding boxes are inside frame. If not crop to border"""
    array_width = torch.ones_like(bbox[:, 1]) * image_shape[1] - 1
    array_height = torch.ones_like(bbox[:, 2]) * image_shape[0] - 1

    bbox[:, 1:3] = torch.max(bbox[:, 1:3], torch.zeros_like(bbox[:, 1:3]))
    bbox[:, 1] = torch.min(bbox[:, 1], array_width)
    bbox[:, 2] = torch.min(bbox[:, 2], array_height)

    bbox[:, 3] = torch.min(bbox[:, 3], array_width - bbox[:, 1])
    bbox[:, 4] = torch.min(bbox[:, 4], array_height - bbox[:, 2])

    return bbox