Browse Source

add and use khandy.is_numpy_image

quarrying 3 years ago
parent
commit
0fb3ddfb12

+ 4 - 3
khandy/image/crop_or_pad.py

@@ -1,5 +1,6 @@
 import numbers
 
+import khandy
 import numpy as np
 
 
@@ -11,7 +12,7 @@ def crop_or_pad(image, x_min, y_min, x_max, y_max, border_value=0):
     References:
         tf.image.resize_image_with_crop_or_pad
     """
-    assert image.ndim in [2, 3]
+    assert khandy.is_numpy_image(image)
     assert isinstance(x_min, numbers.Integral) and isinstance(y_min, numbers.Integral)
     assert isinstance(x_max, numbers.Integral) and isinstance(y_max, numbers.Integral)
     assert (x_min <= x_max) and (y_min <= y_max)
@@ -95,7 +96,7 @@ def center_crop(image, dst_width, dst_height, strict=True):
         when True, raise error if src size is less than dst size. 
         when False, remain unchanged if src size is less than dst size, otherwise center crop.
     """
-    assert image.ndim in [2, 3]
+    assert khandy.is_numpy_image(image)
     assert isinstance(dst_width, numbers.Integral) and isinstance(dst_height, numbers.Integral)
     src_height, src_width = image.shape[:2]
     if strict:
@@ -114,7 +115,7 @@ def center_pad(image, dst_width, dst_height, strict=True):
         when True, raise error if src size is greater than dst size. 
         when False, remain unchanged if src size is greater than dst size, otherwise center pad.
     """
-    assert image.ndim in [2, 3]
+    assert khandy.is_numpy_image(image)
     assert isinstance(dst_width, numbers.Integral) and isinstance(dst_height, numbers.Integral)
     
     src_height, src_width = image.shape[:2]

+ 4 - 0
khandy/image/flip.py

@@ -1,3 +1,4 @@
+import khandy
 import numpy as np
 
 
@@ -9,6 +10,7 @@ def flip_image(image, direction='h', copy=True):
         tf.image.flip_up_down
         tf.image.flip_left_right
     """
+    assert khandy.is_numpy_image(image)
     assert direction in ['x', 'h', 'horizontal',
                          'y', 'v', 'vertical', 
                          'o', 'b', 'both']
@@ -29,6 +31,7 @@ def transpose_image(image, copy=True):
         cv2.transpose
         tf.image.transpose
     """
+    assert khandy.is_numpy_image(image)
     if copy:
         image = image.copy()
     if image.ndim == 2:
@@ -47,6 +50,7 @@ def rot90_image(image, n=1, copy=True):
         cv2.rotate
         tf.image.rot90
     """
+    assert khandy.is_numpy_image(image)
     if copy:
         image = image.copy()
     if image.ndim == 2:

+ 4 - 0
khandy/image/image_hash.py

@@ -1,8 +1,10 @@
 import cv2
+import khandy
 import numpy as np
 
 
 def calc_image_ahash(image):
+    assert khandy.is_numpy_image(image)
     if image.ndim == 3:
         image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     resized = cv2.resize(image, (8, 8))
@@ -17,6 +19,7 @@ def calc_image_ahash(image):
 
     
 def calc_image_phash(image):
+    assert khandy.is_numpy_image(image)
     if image.ndim == 3:
         image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     resized = cv2.resize(image, (32, 32))
@@ -32,6 +35,7 @@ def calc_image_phash(image):
 
     
 def calc_image_dhash(image):
+    assert khandy.is_numpy_image(image)
     if image.ndim == 3:
         image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     resized = cv2.resize(image, (9, 8))

+ 7 - 2
khandy/image/misc.py

@@ -61,8 +61,13 @@ def stack_image_list(image_list, dtype=np.float32):
         blob = np.squeeze(blob, axis=-1)
     return blob
     
-    
+
+def is_numpy_image(image):
+    return isinstance(image, np.ndarray) and image.ndim in {2, 3}
+
+
 def is_gray_image(image, tol=3):
+    assert is_numpy_image(image)
     if image.ndim == 2:
         return True
     elif image.ndim == 3:
@@ -84,4 +89,4 @@ def is_gray_image(image, tol=3):
             return False
     else:
         return False
-        
+        

+ 7 - 0
khandy/image/resize.py

@@ -1,4 +1,5 @@
 import cv2
+import khandy
 import numpy as np
 
 
@@ -17,6 +18,7 @@ def scale_image(image, x_scale, y_scale, interpolation='bilinear'):
     Reference:
         mmcv.imrescale
     """
+    assert khandy.is_numpy_image(image)
     src_height, src_width = image.shape[:2]
     dst_width = int(round(x_scale * src_width))
     dst_height = int(round(y_scale * src_height))
@@ -43,6 +45,7 @@ def resize_image(image, dst_width, dst_height, return_scale=False, interpolation
     Reference:
         mmcv.imresize
     """
+    assert khandy.is_numpy_image(image)
     resized_image = cv2.resize(image, (dst_width, dst_height), 
                                interpolation=interp_codes[interpolation])
     if not return_scale:
@@ -61,6 +64,7 @@ def resize_image_short(image, dst_size, return_scale=False, interpolation='bilin
     References:
         `resize_min` 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 = max(dst_size / src_width, dst_size / src_height)
     dst_width = int(round(scale * src_width))
@@ -81,6 +85,7 @@ def resize_image_long(image, dst_size, return_scale=False, interpolation='biline
     References:
         `resize_max` 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_size / src_width, dst_size / src_height)
     dst_width = int(round(scale * src_width))
@@ -101,6 +106,7 @@ def letterbox_resize_image(image, dst_width, dst_height, border_value=0,
     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))
@@ -145,6 +151,7 @@ def resize_image_to_range(image, min_length, max_length, return_scale=False, int
         `prep_im_for_blob` in `py-faster-rcnn-master/lib/utils/blob.py`
         mmcv.imrescale
     """
+    assert khandy.is_numpy_image(image)
     assert min_length < max_length
     src_height, src_width = image.shape[:2]
     

+ 2 - 0
khandy/image/rotate.py

@@ -1,4 +1,5 @@
 import cv2
+import khandy
 import numpy as np
 
 
@@ -46,6 +47,7 @@ def rotate_image(image, angle, scale=1.0, center=None,
     References:
         mmcv.imrotate
     """
+    assert khandy.is_numpy_image(image)
     image_height, image_width = image.shape[:2]
     if auto_bound:
         center = None

+ 2 - 1
khandy/image/translate.py

@@ -1,5 +1,6 @@
 import numbers
 
+import khandy
 import numpy as np
 
 
@@ -21,7 +22,7 @@ def translate_image(image, x_shift, y_shift, border_value=0):
     See Also:
         crop_or_pad
     """
-    assert image.ndim in [2, 3]
+    assert khandy.is_numpy_image(image)
     assert isinstance(x_shift, numbers.Integral)
     assert isinstance(y_shift, numbers.Integral)
     image_height, image_width = image.shape[:2]