Lang_SAM

class Perception.Object_detection.Lang_SAM[source]

Class for performing object detection using LangSAM model.

__init__()[source]

Initializes the Lang_SAM model.

detect_obj(image: Type[Image], text: str | None = None, bbox: List[int] | None = None, save_box: bool = False, box_filename: str | None = None, save_mask: bool = False, mask_filename: str | None = None) Tuple[ndarray, List[int]][source]

Detects an object in the provided image using the LangSAM model.

Parameters:
  • image (Type[Image.Image]) – An image object on which object detection is performed.

  • text (str, optional) – Optional parameter for performing text-related object detection tasks. Defaults to None.

  • bbox (List[int], optional) – Optional parameter specifying an initial bounding box. Defaults to None.

  • save_box (bool, optional) – Optional parameter indicating whether to save bounding boxes. Defaults to False.

  • box_filename (str, optional) – Optional parameter specifying the filename to save the visualization of bounding boxes. Defaults to None.

  • save_mask (bool, optional) – Optional parameter indicating whether to save masks. Defaults to False.

  • mask_filename (str, optional) – Optional parameter specifying the filename to save the visualization of masks. Defaults to None.

Returns:

The segmentation mask and the bounding box coordinates of the detected object in the input image.

Return type:

Tuple[np.ndarray, List[int]]

draw_bounding_box(image: Type[Image], bbox: List[int], save_file: str | None = None) None[source]

Draws a bounding box on the image.

Parameters:
  • image (Type[Image.Image]) – The image on which to draw the bounding box.

  • bbox (List[int]) – The bounding box coordinates.

  • save_file (str, optional) – The filename to save the image with the bounding box. Defaults to None.

draw_bounding_boxes(image: Type[Image], bboxes: List[int], scores: List[int], max_box_ind: int = -1, save_file: str | None = None) None[source]

Draws multiple bounding boxes on the image.

Parameters:
  • image (Type[Image.Image]) – The image on which to draw the bounding boxes.

  • bboxes (List[int]) – The bounding box coordinates.

  • scores (List[int]) – The scores of the bounding boxes.

  • max_box_ind (int, optional) – The index of the maximum score bounding box. Defaults to -1.

  • save_file (str, optional) – The filename to save the image with the bounding boxes. Defaults to None.

draw_mask_on_image(image: Type[Image], seg_mask: ndarray, save_file: str | None = None) None[source]

Draws a segmentation mask on the image.

Parameters:
  • image (Type[Image.Image]) – The image on which to draw the segmentation mask.

  • seg_mask (np.ndarray) – The segmentation mask.

  • save_file (str, optional) – The filename to save the image with the segmentation mask. Defaults to None.

Perception.Object_detection.Lang_SAM.utils.draw_rectangle(image, bbox, width=5)[source]

Draw a bounding box on an image.

Parameters:
  • image (Image.Image) – The image on which to draw the bounding box.

  • bbox (list[int]) – The bounding box coordinates in the format [x1, y1, x2, y2].

  • width (int, optional) – The width of the bounding box outline. Defaults to 5.

Returns:

The ImageDraw object with the bounding box drawn.

Return type:

ImageDraw.Draw