MCPcopy
hub / github.com/landing-ai/vision-agent / overlay_segmentation_masks

Function overlay_segmentation_masks

vision_agent/tools/tools.py:3328–3442  ·  view source on GitHub ↗

overlay_segmentation_masks' is a utility function that displays segmentation masks. It will overlay a colored mask on the detected object with the label. Parameters: medias (Union[np.ndarray, List[np.ndarray]]): The image or frames to display the masks on. masks

(
    medias: Union[np.ndarray, List[np.ndarray]],
    masks: Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]],
    draw_label: bool = True,
    secondary_label_key: str = "tracking_label",
)

Source from the content-addressed store, hash-verified

3326
3327
3328def overlay_segmentation_masks(
3329 medias: Union[np.ndarray, List[np.ndarray]],
3330 masks: Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]],
3331 draw_label: bool = True,
3332 secondary_label_key: str = "tracking_label",
3333) -> Union[np.ndarray, List[np.ndarray]]:
3334 """'overlay_segmentation_masks' is a utility function that displays segmentation
3335 masks. It will overlay a colored mask on the detected object with the label.
3336
3337 Parameters:
3338 medias (Union[np.ndarray, List[np.ndarray]]): The image or frames to display
3339 the masks on.
3340 masks (Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]]): A list of
3341 dictionaries or a list of list of dictionaries containing the masks, labels
3342 and scores.
3343 draw_label (bool, optional): If True, the labels will be displayed on the image.
3344 secondary_label_key (str, optional): The key to use for the secondary
3345 tracking label which is needed in videos to display tracking information.
3346
3347 Returns:
3348 np.ndarray: The image with the masks displayed.
3349
3350 Example
3351 -------
3352 >>> image_with_masks = overlay_segmentation_masks(
3353 image,
3354 [{
3355 'score': 0.99,
3356 'label': 'dinosaur',
3357 'mask': array([[0, 0, 0, ..., 0, 0, 0],
3358 [0, 0, 0, ..., 0, 0, 0],
3359 ...,
3360 [0, 0, 0, ..., 0, 0, 0],
3361 [0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
3362 }],
3363 )
3364 """
3365 if not masks:
3366 return medias
3367
3368 medias_int: List[np.ndarray] = (
3369 [medias] if isinstance(medias, np.ndarray) else medias
3370 )
3371 masks_int = [masks] if isinstance(masks[0], dict) else masks
3372 masks_int = cast(List[List[Dict[str, Any]]], masks_int)
3373
3374 labels = set()
3375 for mask_i in masks_int:
3376 for mask_j in mask_i:
3377 labels.add(mask_j["label"])
3378
3379 use_tracking_label = False
3380 if all([":" in label for label in labels]):
3381 use_tracking_label = True
3382 unique_labels = set([label.split(":")[1].strip() for label in labels])
3383 colors = {
3384 label: COLORS[i % len(COLORS)] for i, label in enumerate(unique_labels)
3385 }

Callers

nothing calls this directly

Calls 3

getMethod · 0.80
textMethod · 0.80

Tested by

no test coverage detected