Browse Source

remove normalize_boxes

quarrying 3 years ago
parent
commit
f0e072997c
2 changed files with 35 additions and 44 deletions
  1. 0 13
      khandy/boxes/boxes_utils.py
  2. 35 31
      khandy/image/resize.py

+ 0 - 13
khandy/boxes/boxes_utils.py

@@ -26,16 +26,3 @@ def assert_and_normalize_shape(x, length):
     else:
         raise ValueError('Incompatible ndim!')
         
-        
-def normalize_boxes(boxes, dtype=np.float32, copy=False, support_extra=True):
-    boxes = np.array(boxes, dtype=dtype, copy=copy)
-    assert boxes.ndim in [1, 2]
-    last_dimension = boxes.shape[-1]
-    if support_extra:
-        assert last_dimension >= 4
-    else:
-        assert last_dimension == 4
-        
-    if boxes.ndim == 1:
-        boxes = np.expand_dims(boxes, axis=0)
-    return boxes

+ 35 - 31
khandy/image/resize.py

@@ -1,3 +1,5 @@
+import warnings
+
 import cv2
 import khandy
 import numpy as np
@@ -99,36 +101,6 @@ def resize_image_long(image, dst_size, return_scale=False, interpolation='biline
         return resized_image, scale
         
         
-def letterbox_resize_image(image, dst_width, dst_height, border_value=0,
-                           return_scale=False, interpolation='bilinear'):
-    """Resize an image preserving the original aspect ratio using padding.
-    
-    References:
-        `letterbox_image` in `https://github.com/pjreddie/darknet/blob/master/src/image.c`
-    """
-    assert khandy.is_numpy_image(image)
-    src_height, src_width = image.shape[:2]
-    scale = min(dst_width / src_width, dst_height / src_height)
-    resize_w = int(round(scale * src_width))
-    resize_h = int(round(scale * src_height))
-
-    resized_image = cv2.resize(image, (resize_w, resize_h), 
-                               interpolation=interp_codes[interpolation])
-    padded_shape = list(resized_image.shape)
-    padded_shape[0] = dst_height
-    padded_shape[1] = dst_width
-    padded_image = np.full(padded_shape, border_value, image.dtype)
-
-    dw = (dst_width - resize_w) // 2
-    dh = (dst_height - resize_h) // 2
-    padded_image[dh: resize_h + dh, dw: resize_w + dw, ...] = resized_image
-    
-    if not return_scale:
-        return padded_image
-    else:
-        return padded_image, scale, dw, dh
-        
-        
 def resize_image_to_range(image, min_length, max_length, return_scale=False, interpolation='bilinear'):
     """Resizes an image so its dimensions are within the provided value.
     
@@ -170,4 +142,36 @@ def resize_image_to_range(image, min_length, max_length, return_scale=False, int
     else:
         return resized_image, scale
         
-        
+        
+def letterbox_image(image, dst_width, dst_height, border_value=0,
+                    return_scale=False, interpolation='bilinear'):
+    """Resize an image preserving the original aspect ratio using padding.
+    
+    References:
+        `letterbox_image` in `https://github.com/pjreddie/darknet/blob/master/src/image.c`
+    """
+    assert khandy.is_numpy_image(image)
+    src_height, src_width = image.shape[:2]
+    scale = min(dst_width / src_width, dst_height / src_height)
+    resize_w = int(round(scale * src_width))
+    resize_h = int(round(scale * src_height))
+
+    resized_image = cv2.resize(image, (resize_w, resize_h), 
+                               interpolation=interp_codes[interpolation])
+    pad_top = (dst_height - resize_h) // 2
+    pad_bottom = (dst_height - resize_h) - pad_top
+    pad_left = (dst_width - resize_w) // 2
+    pad_right = (dst_width - resize_w) - pad_left
+    padded_image = cv2.copyMakeBorder(resized_image, pad_top, pad_bottom, pad_left, pad_right, 
+                                      cv2.BORDER_CONSTANT, value=border_value)
+    if not return_scale:
+        return padded_image
+    else:
+        return padded_image, scale, pad_left, pad_top
+        
+
+def letterbox_resize_image(image, dst_width, dst_height, border_value=0,
+                           return_scale=False, interpolation='bilinear'):
+    warnings.warn('letterbox_resize_image will be deprecated, use letterbox_image instead!')
+    return letterbox_image(image, dst_width, dst_height, border_value,
+                           return_scale, interpolation)