Skip to content

Commit d953b36

Browse files
No public description
PiperOrigin-RevId: 741084800
1 parent 6cd3ebc commit d953b36

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

Diff for: official/projects/waste_identification_ml/model_inference/color_and_property_extractor.py

+5-15
Original file line numberDiff line numberDiff line change
@@ -167,39 +167,29 @@ def extract_properties_and_object_masks(
167167

168168
def find_dominant_color(
169169
image: np.ndarray, black_threshold: int = 50
170-
) -> Tuple[Union[int, str], Union[int, str], Union[int, str]]:
170+
) -> Tuple[int, int, int]:
171171
"""Determines the dominant color in a given image.
172172
173-
The function performs the following steps:
174-
Filters out black or near-black pixels based on a threshold.
175-
Uses k-means clustering to identify the dominant color among the remaining
176-
pixels.
177-
178173
Args:
179174
image: An array representation of the image.
180-
black_threshold: pixel value of black color
181-
182-
shape is (height, width, 3) for RGB channels.
183175
black_threshold: The intensity threshold below which pixels
184-
are considered 'black' or near-black. Default is 50.
176+
are considered 'black' or near-black.
185177
186178
Returns:
187-
The dominant RGB color in the format (R, G, B). If no non-black
188-
pixels are found, returns ('Na', 'Na', 'Na').
179+
The dominant RGB color in the format (R, G, B).
189180
"""
190181
pixels = image.reshape(-1, 3)
191182

192183
# Filter out black pixels based on the threshold
193184
non_black_pixels = pixels[(pixels > black_threshold).any(axis=1)]
194185

195-
if non_black_pixels.size != 0:
186+
if non_black_pixels.size:
196187
kmeans = sklearn_cluster.KMeans(
197188
n_clusters=1, n_init=10, random_state=0
198189
).fit(non_black_pixels)
199190
dominant_color = kmeans.cluster_centers_[0].astype(int)
200-
201191
else:
202-
dominant_color = ['Na', 'Na', 'Na']
192+
dominant_color = np.array([0, 0, 0], dtype=int)
203193
return tuple(dominant_color)
204194

205195

Diff for: official/projects/waste_identification_ml/model_inference/color_and_property_extractor_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_find_dominant_color_black(self):
105105
original_image, black_threshold=50
106106
)
107107

108-
self.assertEqual(result, ('Na', 'Na', 'Na'))
108+
self.assertEqual(result, (0, 0, 0))
109109

110110
def test_est_color(self):
111111
result = color_and_property_extractor.est_color((255, 0, 0))
@@ -114,9 +114,9 @@ def test_est_color(self):
114114

115115
def test_generic_color(self):
116116
test_colors = np.array(
117-
[(255, 0, 0), (55, 118, 171), (73, 128, 41), (231, 112, 13)]
117+
[(255, 0, 0), (55, 118, 171), (73, 128, 41), (231, 112, 13), (0, 0, 0)]
118118
)
119-
expected_colors = ['red', 'blue', 'green', 'orange']
119+
expected_colors = ['red', 'blue', 'green', 'orange', 'black']
120120

121121
result = color_and_property_extractor.get_generic_color_name(test_colors)
122122

0 commit comments

Comments
 (0)