Coverage for src/bob/bio/face/preprocessor/Scale.py: 83%
18 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-13 00:04 +0200
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-13 00:04 +0200
1from skimage.transform import resize
2from sklearn.preprocessing import FunctionTransformer
3from sklearn.utils import check_array
5from bob.io.image import to_bob, to_matplotlib
8def scale(images, target_img_size):
9 """Scales a list of images to the target size
11 Parameters
12 ----------
13 images : array_like
14 A list of images (in Bob format) to be scaled to the target size
15 target_img_size : int or tuple
16 A tuple of size 2 as (H, W) or an integer where H==W
18 Returns
19 -------
20 numpy.ndarray
21 Scaled images
22 """
23 if isinstance(target_img_size, int):
24 target_img_size = (target_img_size, target_img_size)
26 # images are always batched
27 images = check_array(images, allow_nd=True)
29 output_shape = tuple(target_img_size)
30 output_shape = tuple(images.shape[0:1]) + output_shape
32 # If it's Bob batched RGB images
33 if images.ndim > 3:
34 images = to_matplotlib(images)
35 images = resize(images, output_shape=output_shape)
36 return to_bob(images) * 255
37 else:
38 # If it's Bob batched gray scaled images
39 images = resize(images, output_shape=output_shape)
40 return images * 255
43def Scale(target_img_size):
44 """
45 A transformer that scales images.
46 It accepts a list of inputs
48 Parameters
49 -----------
51 target_img_size: tuple
52 Target image size, specified as a tuple of (H, W)
55 """
56 return FunctionTransformer(
57 func=scale,
58 validate=False,
59 kw_args=dict(target_img_size=target_img_size),
60 )