|
13 | 13 | # limitations under the License.
|
14 | 14 | # ==============================================================================
|
15 | 15 | import numpy as np
|
16 |
| -from skimage.transform import resize |
| 16 | +try: |
| 17 | + import cv2 |
| 18 | +except ModuleNotFoundError: |
| 19 | + import skimage.transform |
17 | 20 |
|
18 |
| -MAP_INTERPOLATION_TO_ORDER = { |
19 |
| - "nearest": 0, |
20 |
| - "bilinear": 1, |
21 |
| - "biquadratic": 2, |
22 |
| - "bicubic": 3, |
23 |
| -} |
| 21 | + |
| 22 | +try: |
| 23 | + # OpenCV: Map interpolation string to OpenCV interpolation enum value |
| 24 | + INTERPOLATION_DICT = { |
| 25 | + "nearest": cv2.INTER_NEAREST, |
| 26 | + "bilinear": cv2.INTER_LINEAR, |
| 27 | + "bicubic": cv2.INTER_CUBIC, |
| 28 | + "laconzos": cv2.INTER_LANCZOS4, |
| 29 | + } |
| 30 | +except NameError: |
| 31 | + # scikit-image: Map interpolation string to interpolation order |
| 32 | + INTERPOLATION_DICT = { |
| 33 | + "nearest": 0, |
| 34 | + "bilinear": 1, |
| 35 | + "biquadratic": 2, |
| 36 | + "bicubic": 3, |
| 37 | + } |
24 | 38 |
|
25 | 39 |
|
26 | 40 | def center_crop_and_resize(image, image_size, crop_padding=32, interpolation="bicubic"):
|
27 | 41 | assert image.ndim in {2, 3}
|
28 |
| - assert interpolation in MAP_INTERPOLATION_TO_ORDER.keys() |
| 42 | + assert interpolation in INTERPOLATION_DICT.keys() |
29 | 43 |
|
30 | 44 | in_h, in_w = image.shape[:2]
|
31 | 45 |
|
@@ -55,11 +69,19 @@ def center_crop_and_resize(image, image_size, crop_padding=32, interpolation="bi
|
55 | 69 | offset_h : unpadded_center_crop_size_pre_scaling[0] + offset_h,
|
56 | 70 | offset_w : unpadded_center_crop_size_pre_scaling[1] + offset_w,
|
57 | 71 | ]
|
58 |
| - resized_image = resize( |
59 |
| - image_crop, |
60 |
| - (out_h, out_w), |
61 |
| - order=MAP_INTERPOLATION_TO_ORDER[interpolation], |
62 |
| - preserve_range=True, |
63 |
| - ) |
| 72 | + |
| 73 | + try: |
| 74 | + resized_image = cv2.resize( |
| 75 | + image_crop, |
| 76 | + (out_w, out_h), |
| 77 | + interpolation=INTERPOLATION_DICT[interpolation] if inv_scale < 1 else cv2.INTER_AREA, |
| 78 | + ) |
| 79 | + except NameError: |
| 80 | + resized_image = skimage.transform.resize( |
| 81 | + image_crop, |
| 82 | + (out_h, out_w), |
| 83 | + order=INTERPOLATION_DICT[interpolation], |
| 84 | + preserve_range=True, |
| 85 | + ) |
64 | 86 |
|
65 | 87 | return resized_image
|
0 commit comments