Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def extract_random_bbox(img, box_side=224):
- """
- Extracts a randomly places square bounding box in an image. If the image is too small to create the bounding box,
- it is scaled up so that it can be. If it is still invalid, it is skipped.
- :param img:
- :param box_side:
- :return: bbox or None
- """
- if img.shape[0] <= box_side or img.shape[1] <= box_side:
- LOG.debug("Image resized because it was smaller than the bounding box.")
- change = {"width": box_side * 2} if np.argmin(img.shape[:2]) == 1 else {"height": box_side * 2}
- img = resize(img, **change)
- x, y = np.random.randint(0, img.shape[0] - box_side, 2)
- bbox = img[y:y + box_side, x:x + box_side]
- if bbox.shape != (box_side, box_side, 3):
- LOG.warning("Image was not able to be coerced into a square. (BBox dims are %s)", bbox.shape)
- return None
- return bbox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement