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

1from skimage.transform import resize 

2from sklearn.preprocessing import FunctionTransformer 

3from sklearn.utils import check_array 

4 

5from bob.io.image import to_bob, to_matplotlib 

6 

7 

8def scale(images, target_img_size): 

9 """Scales a list of images to the target size 

10 

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 

17 

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) 

25 

26 # images are always batched 

27 images = check_array(images, allow_nd=True) 

28 

29 output_shape = tuple(target_img_size) 

30 output_shape = tuple(images.shape[0:1]) + output_shape 

31 

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 

41 

42 

43def Scale(target_img_size): 

44 """ 

45 A transformer that scales images. 

46 It accepts a list of inputs 

47 

48 Parameters 

49 ----------- 

50 

51 target_img_size: tuple 

52 Target image size, specified as a tuple of (H, W) 

53 

54 

55 """ 

56 return FunctionTransformer( 

57 func=scale, 

58 validate=False, 

59 kw_args=dict(target_img_size=target_img_size), 

60 )