Advertisement
Guest User

Untitled

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