brails.processors.vlm_segmenter.segment_anything.automatic_mask_generator module

class brails.processors.vlm_segmenter.segment_anything.automatic_mask_generator.SamAutomaticMaskGenerator(model: Sam, points_per_side: int | None = 32, points_per_batch: int = 64, pred_iou_thresh: float = 0.88, stability_score_thresh: float = 0.95, stability_score_offset: float = 1.0, box_nms_thresh: float = 0.7, crop_n_layers: int = 0, crop_nms_thresh: float = 0.7, crop_overlap_ratio: float = 0.3413333333333333, crop_n_points_downscale_factor: int = 1, point_grids: List[ndarray] | None = None, min_mask_region_area: int = 0, output_mode: str = 'binary_mask')

Bases: object

generate(image: ndarray) List[Dict[str, Any]]

Generates masks for the given image.

Arguments:

image (np.ndarray): The image to generate masks for, in HWC uint8 format.

Returns:
list(dict(str, any)): A list over records for masks. Each record is
a dict containing the following keys:
segmentation (dict(str, any) or np.ndarray): The mask. If

output_mode=’binary_mask’, is an array of shape HW. Otherwise, is a dictionary containing the RLE.

bbox (list(float)): The box around the mask, in XYWH format. area (int): The area in pixels of the mask. predicted_iou (float): The model’s own prediction of the mask’s

quality. This is filtered by the pred_iou_thresh parameter.

point_coords (list(list(float))): The point coordinates input

to the model to generate this mask.

stability_score (float): A measure of the mask’s quality. This

is filtered on using the stability_score_thresh parameter.

crop_box (list(float)): The crop of the image used to generate

the mask, given in XYWH format.

static postprocess_small_regions(mask_data: MaskData, min_area: int, nms_thresh: float) MaskData

Removes small disconnected regions and holes in masks, then reruns box NMS to remove any new duplicates.

Edits mask_data in place.

Requires open-cv as a dependency.