Spaces:
Paused
Paused
Upload 2 files
Browse files
dofaker/transforms/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from .functional import center_crop, pad
|
dofaker/transforms/functional.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numbers
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
import cv2
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def center_crop(image: np.ndarray, output_size):
|
| 8 |
+
if isinstance(output_size, numbers.Number):
|
| 9 |
+
output_size = (int(output_size), int(output_size))
|
| 10 |
+
elif isinstance(output_size, (tuple, list)) and len(output_size) == 1:
|
| 11 |
+
output_size = (output_size[0], output_size[0])
|
| 12 |
+
|
| 13 |
+
image_height, image_width, c = image.shape
|
| 14 |
+
crop_height, crop_width = output_size
|
| 15 |
+
|
| 16 |
+
if crop_width > image_width or crop_height > image_height:
|
| 17 |
+
padding_ltrb = [
|
| 18 |
+
(crop_width - image_width) // 2 if crop_width > image_width else 0,
|
| 19 |
+
(crop_height - image_height) //
|
| 20 |
+
2 if crop_height > image_height else 0,
|
| 21 |
+
(crop_width - image_width + 1) //
|
| 22 |
+
2 if crop_width > image_width else 0,
|
| 23 |
+
(crop_height - image_height + 1) //
|
| 24 |
+
2 if crop_height > image_height else 0,
|
| 25 |
+
]
|
| 26 |
+
image = cv2.copyMakeBorder(image,
|
| 27 |
+
padding_ltrb[1],
|
| 28 |
+
padding_ltrb[3],
|
| 29 |
+
padding_ltrb[0],
|
| 30 |
+
padding_ltrb[2],
|
| 31 |
+
cv2.BORDER_CONSTANT,
|
| 32 |
+
value=(0, 0, 0))
|
| 33 |
+
image_height, image_width, c = image.shape
|
| 34 |
+
if crop_width == image_width and crop_height == image_height:
|
| 35 |
+
return image
|
| 36 |
+
|
| 37 |
+
crop_top = int(round((image_height - crop_height) / 2.0))
|
| 38 |
+
crop_left = int(round((image_width - crop_width) / 2.0))
|
| 39 |
+
return image[crop_top:crop_top + crop_height,
|
| 40 |
+
crop_left:crop_left + crop_width]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def pad(image,
|
| 44 |
+
left,
|
| 45 |
+
top,
|
| 46 |
+
right,
|
| 47 |
+
bottom,
|
| 48 |
+
fill: int = 0,
|
| 49 |
+
padding_mode: str = "constant"):
|
| 50 |
+
if padding_mode == 'constant':
|
| 51 |
+
return cv2.copyMakeBorder(image,
|
| 52 |
+
top,
|
| 53 |
+
bottom,
|
| 54 |
+
left,
|
| 55 |
+
right,
|
| 56 |
+
cv2.BORDER_CONSTANT,
|
| 57 |
+
value=(fill, fill, fill))
|
| 58 |
+
else:
|
| 59 |
+
raise UserWarning('padding mode {} not supported.'.format(padding_mode))
|