|
@@ -1,10 +1,15 @@
|
|
import numpy as np
|
|
import numpy as np
|
|
import random
|
|
import random
|
|
|
|
+import typing as T
|
|
|
|
|
|
from PIL import Image
|
|
from PIL import Image
|
|
from functools import partial
|
|
from functools import partial
|
|
|
|
|
|
-def dimensions(im):
|
|
|
|
|
|
+def dimensions(im: T.Union[Image.Image, np.ndarray]) -> T.Tuple[int, int, int]:
|
|
|
|
+ """ Generic method that return the height, width and number of
|
|
|
|
+ channels for a given input. The input can be a numpy array
|
|
|
|
+ or a PIL.Image instance.
|
|
|
|
+ """
|
|
if isinstance(im, np.ndarray):
|
|
if isinstance(im, np.ndarray):
|
|
if im.ndim != 3:
|
|
if im.ndim != 3:
|
|
import pdb; pdb.set_trace()
|
|
import pdb; pdb.set_trace()
|
|
@@ -21,6 +26,21 @@ def dimensions(im):
|
|
raise ValueError("Unknown image instance ({})!".format(type(im)))
|
|
raise ValueError("Unknown image instance ({})!".format(type(im)))
|
|
|
|
|
|
def rescale(im, coords, rescale_size, center_cropped=True, no_offset=False):
|
|
def rescale(im, coords, rescale_size, center_cropped=True, no_offset=False):
|
|
|
|
+ """ Rescales coordinates for a given image.
|
|
|
|
+ The assumption is that the (pixel) coordinates were estimated
|
|
|
|
+ based on a different image resolution (rescale_size). This is
|
|
|
|
+ typically the input size of the CNN that was used for the
|
|
|
|
+ estimation of the coordinates (e.g. 224 or 448 for ResNets,
|
|
|
|
+ 299 or 427 for InceptionV3, or 227 for AlexNet).
|
|
|
|
+
|
|
|
|
+ The parameter "center_cropped" indicates whether the input image
|
|
|
|
+ was center cropped before resize or resized without preserving
|
|
|
|
+ the original aspect ratio.
|
|
|
|
+
|
|
|
|
+ If you want to rescale the width and height (e.g. of a bounding
|
|
|
|
+ box) then you should set "no_offset=True"!
|
|
|
|
+ """
|
|
|
|
+
|
|
h, w, c = dimensions(im)
|
|
h, w, c = dimensions(im)
|
|
|
|
|
|
if np.all(coords == -1):
|
|
if np.all(coords == -1):
|