Coverage for /scratch/builds/bob/bob.ip.binseg/miniconda/conda-bld/bob.ip.binseg_1673966692152/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_p/lib/python3.10/site-packages/bob/ip/common/data/loader.py: 70%

20 statements  

« prev     ^ index     » next       coverage.py v7.0.5, created at 2023-01-17 15:03 +0000

1#!/usr/bin/env python 

2# coding=utf-8 

3 

4 

5"""Data loading code""" 

6 

7 

8import functools 

9import os 

10 

11import numpy 

12import PIL.Image 

13import skimage.exposure 

14 

15from .sample import DelayedSample 

16 

17 

18def load_pil_raw_12bit_jsrt(path, width): 

19 """Loads a raw 16-bit sample data 

20 

21 This method was designed to handle the raw images from the JSRT_ dataset. 

22 It reads the data file and applies a simple histogram equalization to the 

23 8-bit representation of the image to obtain something along the lines of 

24 the PNG (unofficial) version distributed at `JSRT-Kaggle`_. 

25 

26 

27 Parameters 

28 ---------- 

29 

30 path : str 

31 The full path leading to the image to be loaded 

32 

33 width : int 

34 The desired width of the output image 

35 

36 

37 Returns 

38 ------- 

39 

40 image : PIL.Image.Image 

41 A PIL image in RGB mode, with `width`x`width` pixels 

42 

43 

44 .. include:: ../../links.rst 

45 

46 """ 

47 

48 raw_image = numpy.fromfile(path, numpy.dtype(">u2")).reshape(2048, 2048) 

49 raw_image[raw_image > 4095] = 4095 

50 raw_image = 4095 - raw_image # invert colors 

51 raw_image = (raw_image >> 4).astype(numpy.uint8) # 8-bit uint 

52 raw_image = skimage.exposure.equalize_hist(raw_image) 

53 return ( 

54 PIL.Image.fromarray((raw_image * 255).astype(numpy.uint8)) 

55 .resize((width, width)) 

56 .convert("RGB") 

57 ) 

58 

59 

60def load_pil_rgb(path): 

61 """Loads a sample data 

62 

63 Parameters 

64 ---------- 

65 

66 path : str 

67 The full path leading to the image to be loaded 

68 

69 

70 Returns 

71 ------- 

72 

73 image : PIL.Image.Image 

74 A PIL image in RGB mode 

75 

76 """ 

77 

78 return PIL.Image.open(path).convert("RGB") 

79 

80 

81def load_pil_1(path): 

82 """Loads a sample binary label or mask 

83 

84 Parameters 

85 ---------- 

86 

87 path : str 

88 The full path leading to the image to be loaded 

89 

90 

91 Returns 

92 ------- 

93 

94 image : PIL.Image.Image 

95 A PIL image in mode "1" 

96 

97 """ 

98 

99 return PIL.Image.open(path).convert(mode="1", dither=None) 

100 

101 

102def make_delayed(sample, loader, key=None): 

103 """Returns a delayed-loading Sample object 

104 

105 Parameters 

106 ---------- 

107 

108 sample : dict 

109 A dictionary that maps field names to sample data values (e.g. paths) 

110 

111 loader : object 

112 A function that inputs ``sample`` dictionaries and returns the loaded 

113 data. 

114 

115 key : str 

116 A unique key identifier for this sample. If not provided, assumes 

117 ``sample`` is a dictionary with a ``data`` entry and uses its path as 

118 key. 

119 

120 

121 Returns 

122 ------- 

123 

124 sample : bob.ip.common.data.sample.DelayedSample 

125 In which ``key`` is as provided and ``data`` can be accessed to trigger 

126 sample loading. 

127 

128 """ 

129 

130 return DelayedSample( 

131 functools.partial(loader, sample), 

132 key=key or os.path.splitext(sample["data"])[0], 

133 )