def perturb_BB(image_shape, bb, max_pertub_pixel,
        rng=None, max_aspect_ratio_diff=0.3,
        max_try=100):
    """
    Perturb a bounding box.
    :param image_shape: [h, w]
    :param bb: a `Rect` instance
    :param max_pertub_pixel: pertubation on each coordinate
    :param max_aspect_ratio_diff: result can't have an aspect ratio too different from the original
    :param max_try: if cannot find a valid bounding box, return the original
    :returns: new bounding box
    """
    orig_ratio = bb.h * 1.0 / bb.w
    if rng is None:
        rng = np.random.RandomState()
    for _ in range(max_try):
        p = rng.randint(-max_pertub_pixel, max_pertub_pixel, [4])
        newbb = bb.copy()
        newbb.x += p[0]
        newbb.y += p[1]
        newx1 = bb.x1 + p[2]
        newy1 = bb.y1 + p[3]
        newbb.w = newx1 - newbb.x
        newbb.h = newy1 - newbb.y
        if not newbb.validate(image_shape):
            continue
        new_ratio = newbb.h * 1.0 / newbb.w
        diff = abs(new_ratio - orig_ratio)
        if diff / orig_ratio > max_aspect_ratio_diff:
            continue
        return newbb
    return bb
dataset/ETHPy150Open ppwwyyxx/tensorpack/tensorpack/dataflow/imgaug/crop.py/perturb_BB/original