Skip to content

Commit 602905a

Browse files
No public description
PiperOrigin-RevId: 604470925
1 parent 402038d commit 602905a

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

official/projects/waste_identification_ml/docker_solution/prediction_pipeline/mask_bbox_saver.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import os
2828
import sys
29-
from typing import Any
29+
from typing import Any, Dict
3030
import cv2
3131
import numpy as np
3232

@@ -35,11 +35,11 @@
3535

3636

3737
def save_bbox_masks_labels(
38-
result: dict[Any, np.ndarray],
38+
result: Dict[Any, np.ndarray],
3939
image: np.ndarray,
4040
file_name: str,
4141
folder: str,
42-
category_index: dict[int, dict[str, str]],
42+
category_index: Dict[int, Dict[str, str]],
4343
threshold: float,
4444
) -> None:
4545
"""Saves an image with visualized bounding boxes, labels, and masks.
@@ -86,7 +86,7 @@ def save_bbox_masks_labels(
8686

8787

8888
def save_binary_masks(
89-
result: dict[Any, np.ndarray], file_name: str, folder: str
89+
result: Dict[Any, np.ndarray], file_name: str, folder: str
9090
) -> None:
9191
"""Saves binary masks generated from object detection results.
9292

official/projects/waste_identification_ml/model_inference/color_and_property_extractor.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""Extract properties from each object mask and detect its color."""
1616

17-
from typing import Annotated, Literal, TypeVar, Union
17+
from typing import Dict, List, Tuple, TypeVar, Union
1818

1919
import numpy as np
2020
import numpy.typing as npt
@@ -30,7 +30,8 @@
3030
# Those values could be in different scales like
3131
# RGB ([0.0,255.0], [0.0,255.0], [0.0 to 255.0])
3232
# LAB ([0.0,100], [-128,127], [-128,127])
33-
NColor = Annotated[npt.NDArray[DType], Literal[3]][np.float64]
33+
# NColor = Annotated[npt.NDArray[DType], Literal[3]][np.float64]
34+
NColor = np.ndarray
3435

3536

3637
PROPERTIES = [
@@ -108,11 +109,11 @@
108109

109110

110111
def extract_properties_and_object_masks(
111-
final_result: dict[str, np.ndarray],
112+
final_result: Dict[str, np.ndarray],
112113
height: int,
113114
width: int,
114115
original_image: np.ndarray,
115-
) -> tuple[list[pd.DataFrame], list[np.ndarray]]:
116+
) -> Tuple[List[pd.DataFrame], List[np.ndarray]]:
116117
"""Extract specific properties from given detection masks.
117118
118119
Properties that will be computed includes the area of the masks, bbox
@@ -163,7 +164,7 @@ def extract_properties_and_object_masks(
163164

164165
def find_dominant_color(
165166
image: np.ndarray, black_threshold: int = 50
166-
) -> tuple[Union[int, str], Union[int, str], Union[int, str]]:
167+
) -> Tuple[Union[int, str], Union[int, str], Union[int, str]]:
167168
"""Determines the dominant color in a given image.
168169
169170
The function performs the following steps:
@@ -212,7 +213,7 @@ def color_difference(color1: int, color2: int) -> Union[float, int]:
212213
return (color1 - color2) ** 2
213214

214215

215-
def est_color(requested_color: tuple[int, int, int]) -> str:
216+
def est_color(requested_color: Tuple[int, int, int]) -> str:
216217
"""Estimates the closest named color for a given RGB color.
217218
218219
The function uses the Euclidean distance in the RGB space to find the closest
@@ -238,7 +239,7 @@ def est_color(requested_color: tuple[int, int, int]) -> str:
238239
return min_colors[min(min_colors.keys())]
239240

240241

241-
def get_color_name(rgb_color: tuple[int, int, int]) -> str | None:
242+
def get_color_name(rgb_color: Tuple[int, int, int]) -> str | None:
242243
"""Retrieves the name of a given RGB color.
243244
244245
If the RGB color exactly matches one of the CSS3 predefined colors, it returns
@@ -265,7 +266,7 @@ def get_color_name(rgb_color: tuple[int, int, int]) -> str | None:
265266
return None
266267

267268

268-
def rgb_int_to_lab(rgb_int_color: tuple[int, int, int]) -> NColor:
269+
def rgb_int_to_lab(rgb_int_color: Tuple[int, int, int]) -> NColor:
269270
"""Convert RGB color to LAB color space.
270271
271272
Args:
@@ -280,7 +281,7 @@ def rgb_int_to_lab(rgb_int_color: tuple[int, int, int]) -> NColor:
280281

281282

282283
def color_distance(
283-
a: tuple[int, int, int], b: tuple[int, int, int]
284+
a: Tuple[int, int, int], b: Tuple[int, int, int]
284285
) -> np.ndarray:
285286
"""The color distance following the ciede2000 formula.
286287
@@ -297,8 +298,8 @@ def color_distance(
297298

298299

299300
def build_color_lab_list(
300-
generic_colors: list[tuple[str, str]]
301-
) -> tuple[npt.NDArray[np.str_], list[NColor]]:
301+
generic_colors: List[Tuple[str, str]]
302+
) -> Tuple[npt.NDArray[np.str_], List[NColor]]:
302303
"""Get Simple colors names and lab values.
303304
304305
Args:
@@ -326,9 +327,9 @@ def build_color_lab_list(
326327

327328

328329
def get_generic_color_name(
329-
rgb_colors: list[tuple[int, int, int]],
330-
generic_colors: list[tuple[str, str]] | None = None,
331-
) -> list[str]:
330+
rgb_colors: List[Tuple[int, int, int]],
331+
generic_colors: List[Tuple[str, str]] | None = None,
332+
) -> List[str]:
332333
"""Retrieves generic names of given RGB colors.
333334
334335
Estimates the closest matching color name.

official/projects/waste_identification_ml/model_inference/postprocessing.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
object should not have more than a single bounding box.
2929
"""
3030
import copy
31-
from typing import Any, Optional, TypedDict
31+
from typing import Any, Optional, TypedDict, Dict, Tuple, List
3232
import numpy as np
3333
import tensorflow as tf, tf_keras
3434

@@ -128,8 +128,8 @@ def reframe_box_masks_to_image_masks_default():
128128

129129

130130
def reframing_masks(
131-
results: dict[str, np.ndarray], height: int, width: int
132-
) -> dict[str, np.ndarray]:
131+
results: Dict[str, np.ndarray], height: int, width: int
132+
) -> Dict[str, np.ndarray]:
133133
"""Processes the output from Mask RCNN model to create a full size mask.
134134
135135
Args:
@@ -155,7 +155,7 @@ def reframing_masks(
155155

156156

157157
def find_id_by_name(
158-
dictionary: dict[int, ItemDict], name: str
158+
dictionary: Dict[int, ItemDict], name: str
159159
) -> Optional[int]:
160160
"""Finds the id of a dictionary given its value.
161161
@@ -178,9 +178,9 @@ def find_id_by_name(
178178

179179

180180
def combine_bounding_boxes(
181-
box1: list[float],
182-
box2: list[float],
183-
) -> list[float]:
181+
box1: List[float],
182+
box2: List[float],
183+
) -> List[float]:
184184
"""Combines two bounding boxes.
185185
186186
Args:
@@ -207,9 +207,9 @@ def calculate_combined_scores_boxes_classes(
207207
j: int,
208208
results_1: DetectionResult,
209209
results_2: DetectionResult,
210-
category_indices: list[list[Any]],
211-
category_index_combined: dict[int, ItemDict],
212-
) -> tuple[Any, list[float], Any, Optional[int]]:
210+
category_indices: List[List[Any]],
211+
category_index_combined: Dict[int, ItemDict],
212+
) -> Tuple[Any, List[float], Any, Optional[int]]:
213213
"""Calculate combined scores, boxes, and classes for matched masks.
214214
215215
Args:
@@ -249,9 +249,9 @@ def calculate_combined_scores_boxes_classes(
249249
def calculate_single_result(
250250
index: int,
251251
result: DetectionResult,
252-
category_indices: list[list[Any]],
252+
category_indices: List[List[Any]],
253253
flag: Any | str,
254-
) -> tuple[float, tuple[float, float, float, float], str]:
254+
) -> Tuple[float, Tuple[float, float, float, float], str]:
255255
"""Calculate scores, boxes, and classes for non-matched masks.
256256
257257
Args:
@@ -279,7 +279,7 @@ def calculate_single_result(
279279

280280
def calculate_iou(
281281
mask1: np.ndarray, mask2: np.ndarray
282-
) -> tuple[float, np.ndarray]:
282+
) -> Tuple[float, np.ndarray]:
283283
"""Calculates the intersection over union (IoU) score for two masks.
284284
285285
Args:
@@ -305,11 +305,11 @@ def find_similar_masks(
305305
results_2: DetectionResult,
306306
num_detections: int,
307307
min_score_thresh: float,
308-
category_indices: list[list[Any]],
309-
category_index_combined: dict[int, ItemDict],
308+
category_indices: List[List[Any]],
309+
category_index_combined: Dict[int, ItemDict],
310310
area_threshold: float,
311311
iou_threshold: float = 0.8,
312-
) -> dict[str, np.ndarray]:
312+
) -> Dict[str, np.ndarray]:
313313
"""Aligns the masks of the detections in `results_1` and `results_2`.
314314
315315
Args:
@@ -437,10 +437,10 @@ def find_similar_masks(
437437

438438

439439
def filter_bounding_boxes(
440-
bounding_boxes: list[tuple[int, int, int, int]],
440+
bounding_boxes: List[Tuple[int, int, int, int]],
441441
iou_threshold: float = 0.5,
442442
area_ratio_threshold: float = 0.8,
443-
) -> tuple[list[tuple[int, int, int, int]], list[int]]:
443+
) -> Tuple[List[Tuple[int, int, int, int]], List[int]]:
444444
"""Filters overlapping bounding boxes based on IoU and area ratio criteria.
445445
446446
This function filters out overlapping bounding boxes from a given list based

0 commit comments

Comments
 (0)