|
32 | 32 | from ..backend.image import solarize |
33 | 33 | from ..backend.image import cutout |
34 | 34 | from ..backend.image import add_gaussian_noise |
35 | | -from ..backend.image import BILINEAR |
| 35 | +from ..backend.image import BILINEAR, CUBIC |
36 | 36 | from ..backend.image.tensorflow_image import imagenet_preprocess_input |
37 | 37 | from ..backend.image.opencv_image import convolve_image |
38 | 38 |
|
@@ -383,16 +383,35 @@ def __init__(self, background_paths): |
383 | 383 | raise ValueError('No paths given in ``background_paths``') |
384 | 384 | self.background_paths = background_paths |
385 | 385 |
|
| 386 | + def _random_shape_crop(self, image, shape, buffer=200): |
| 387 | + """Randomly crops an image of the given ``shape``. |
| 388 | +
|
| 389 | + # Arguments |
| 390 | + image: Numpy array. |
| 391 | + shape: List of two ints ''(H, W)''. |
| 392 | +
|
| 393 | + # Returns |
| 394 | + Numpy array of cropped image. |
| 395 | + """ |
| 396 | + H, W = image.shape[:2] |
| 397 | + image_copy = image.copy() |
| 398 | + if (shape[0] >= H) or (shape[1] >= W): |
| 399 | + image = resize_image(image_copy, (shape[0] + buffer, |
| 400 | + shape[1] + buffer), |
| 401 | + method=CUBIC) |
| 402 | + H, W = image.shape[:2] |
| 403 | + x_min = np.random.randint(0, W - shape[1]) |
| 404 | + y_min = np.random.randint(0, H - shape[0]) |
| 405 | + x_max = int(x_min + shape[1]) |
| 406 | + y_max = int(y_min + shape[0]) |
| 407 | + cropped_image = image[y_min:y_max, x_min:x_max] |
| 408 | + return cropped_image |
| 409 | + |
386 | 410 | def call(self, image): |
387 | 411 | random_arg = np.random.randint(0, len(self.background_paths)) |
388 | 412 | background_path = self.background_paths[random_arg] |
389 | 413 | background = load_image(background_path) |
390 | | - background = random_shape_crop(background, image.shape[:2]) |
391 | | - if background is None: |
392 | | - H, W, num_channels = image.shape |
393 | | - # background contains always a channel less |
394 | | - num_channels = num_channels - 1 |
395 | | - background = make_random_plain_image((H, W, num_channels)) |
| 414 | + background = self._random_shape_crop(background, image.shape[:2]) |
396 | 415 | return blend_alpha_channel(image, background) |
397 | 416 |
|
398 | 417 |
|
|
0 commit comments