import torch


def rand_bbox_minmax(img_shape, minmax, count=1):
    """Min-Max CutMix bounding-box
    Inspired by Darknet cutmix impl, generates a random rectangular bbox
    based on min/max percent values applied to each dimension of the input image.

    Typical defaults for minmax are usually in the  .2-.3 for min and .8-.9 range for max.

    Args:
        img_shape (tuple): Image shape as tuple
        minmax (tuple or list): Min and max bbox ratios (as percent of image size)
        count (int): Number of bbox to generate
    """
    assert len(minmax) == 2
    img_h, img_w = img_shape[-2:]
    cut_h = torch.randint(int(img_h * minmax[0]), int(img_h * minmax[1]), (count,))
    cut_w = torch.randint(int(img_w * minmax[0]), int(img_w * minmax[1]), (count,))
    yl = torch.randint(0, img_h - cut_h, (count,))
    xl = torch.randint(0, img_w - cut_w, (count,))
    yu = yl + cut_h
    xu = xl + cut_w
    return yl, yu, xl, xu