Coverage for src/bob/bio/face/database/sample_loaders.py: 85%

39 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-13 00:04 +0200

1#!/usr/bin/env python 

2# vim: set fileencoding=utf-8 : 

3 

4"""Sample and Metadata loaders""" 

5 

6 

7from sklearn.base import BaseEstimator, TransformerMixin 

8 

9from bob.pipelines import DelayedSample 

10 

11 

12def find_attribute(x, attribute): 

13 if hasattr(x, attribute): 

14 return getattr(x, attribute) 

15 else: 

16 raise ValueError(f"Attribute not found in the dataset: {attribute}") 

17 

18 

19class EyesAnnotations(TransformerMixin, BaseEstimator): 

20 def fit(self, X, y=None): 

21 return self 

22 

23 def _more_tags(self): 

24 return { 

25 "requires_fit": False, 

26 } 

27 

28 def transform(self, X): 

29 """ 

30 Converts leye_x, leye_y, reye_x, reye_y attributes to ``annotations = (leye, reye)`` 

31 """ 

32 

33 annotated_samples = [] 

34 for x in X: 

35 eyes = { 

36 "leye": ( 

37 float(find_attribute(x, "leye_y")), 

38 float(find_attribute(x, "leye_x")), 

39 ), 

40 "reye": ( 

41 float(find_attribute(x, "reye_y")), 

42 float(find_attribute(x, "reye_x")), 

43 ), 

44 } 

45 

46 sample = DelayedSample.from_sample(x, annotations=eyes) 

47 [ 

48 delattr(sample, a) 

49 for a in ["leye_x", "leye_y", "reye_x", "reye_y"] 

50 ] 

51 annotated_samples.append(sample) 

52 

53 return annotated_samples 

54 

55 

56class MultiposeAnnotations(TransformerMixin, BaseEstimator): 

57 def fit(self, X, y=None): 

58 return self 

59 

60 def _more_tags(self): 

61 return { 

62 "requires_fit": False, 

63 } 

64 

65 def transform(self, X): 

66 annotated_samples = [] 

67 for x in X: 

68 annotations = dict() 

69 if ( 

70 find_attribute(x, "leye_x") != "" 

71 and find_attribute(x, "reye_x") != "" 

72 ): 

73 # Normal profile 

74 annotations = { 

75 "leye": ( 

76 float(find_attribute(x, "leye_y")), 

77 float(find_attribute(x, "leye_x")), 

78 ), 

79 "reye": ( 

80 float(find_attribute(x, "reye_y")), 

81 float(find_attribute(x, "reye_x")), 

82 ), 

83 } 

84 elif ( 

85 find_attribute(x, "leye_x") != "" 

86 and find_attribute(x, "reye_x") == "" 

87 ): 

88 # Left profile 

89 annotations = { 

90 "leye": ( 

91 float(find_attribute(x, "leye_y")), 

92 float(find_attribute(x, "leye_x")), 

93 ), 

94 "mouth": ( 

95 float(find_attribute(x, "mouthl_y")), 

96 float(find_attribute(x, "mouthl_x")), 

97 ), 

98 } 

99 elif ( 

100 find_attribute(x, "leye_x") == "" 

101 and find_attribute(x, "reye_x") != "" 

102 ): 

103 # Right profile 

104 annotations = { 

105 "reye": ( 

106 float(find_attribute(x, "reye_y")), 

107 float(find_attribute(x, "reye_x")), 

108 ), 

109 "mouth": ( 

110 float(find_attribute(x, "mouthr_y")), 

111 float(find_attribute(x, "mouthr_x")), 

112 ), 

113 } 

114 else: 

115 raise ValueError("Annotations not available") 

116 

117 sample = DelayedSample.from_sample(x, annotations=annotations) 

118 [ 

119 delattr(sample, a) 

120 for a in [ 

121 "reye_x", 

122 "reye_y", 

123 "leye_x", 

124 "leye_y", 

125 "nose_x", 

126 "nose_y", 

127 "mouthr_x", 

128 "mouthr_y", 

129 "mouthl_x", 

130 "mouthl_y", 

131 "chin_x", 

132 "chin_y", 

133 ] 

134 ] 

135 

136 annotated_samples.append(sample) 

137 

138 return annotated_samples