o
    Wh                     @   s0   d dl mZ d dlmZmZ G dd deZdS )    )Results)BaseSolutionSolutionResultsc                       s(   e Zd ZdZ fddZdd Z  ZS )InstanceSegmentationa  
    A class to manage instance segmentation in images or video streams.

    This class extends the BaseSolution class and provides functionality for performing instance segmentation, including
    drawing segmented masks with bounding boxes and labels.

    Attributes:
        model (str): The segmentation model to use for inference.
        line_width (int): Width of the bounding box and text lines.
        names (Dict[int, str]): Dictionary mapping class indices to class names.
        clss (List[int]): List of detected class indices.
        track_ids (List[int]): List of track IDs for detected instances.
        masks (List[numpy.ndarray]): List of segmentation masks for detected instances.

    Methods:
        process: Process the input image to perform instance segmentation and annotate results.
        extract_tracks: Extract tracks including bounding boxes, classes, and masks from model predictions.

    Examples:
        >>> segmenter = InstanceSegmentation()
        >>> frame = cv2.imread("frame.jpg")
        >>> results = segmenter.segment(frame)
        >>> print(f"Total segmented instances: {results['total_tracks']}")
    c                    sV   | dd|d< t jdi | | j dd| _| j dd| _| j dd| _dS )	a"  
        Initialize the InstanceSegmentation class for detecting and annotating segmented instances.

        Args:
            **kwargs (Any): Keyword arguments passed to the BaseSolution parent class.
                model (str): Model name or path, defaults to "yolo11n-seg.pt".
        modelzyolo11n-seg.pt	show_confTshow_labels
show_boxesN )getsuper__init__CFGr   r   r	   )selfkwargs	__class__r
   _/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/solutions/instance_segmentation.pyr   !   s
   zInstanceSegmentation.__init__c                 C   sz   |  | | jdu r| jd |}nt|d| j| jj| jjd}|j| j	| j
| j| jdd}| | t|t| jdS )a  
        Perform instance segmentation on the input image and annotate the results.

        Args:
            im0 (numpy.ndarray): The input image for segmentation.

        Returns:
            (SolutionResults): Object containing the annotated image and total number of tracked instances.

        Examples:
            >>> segmenter = InstanceSegmentation()
            >>> frame = cv2.imread("image.jpg")
            >>> summary = segmenter.segment(frame)
            >>> print(summary)
        NzRNo masks detected! Ensure you're using a supported Ultralytics segmentation model.)pathnamesboxesmasksinstance)
line_widthr   conflabels
color_mode)plot_imtotal_tracks)extract_tracksr   LOGGERwarningr   r   
track_datadataplotr   r	   r   r   display_outputr   len	track_ids)r   im0r   resultsr
   r
   r   process0   s   


zInstanceSegmentation.process)__name__
__module____qualname____doc__r   r*   __classcell__r
   r
   r   r   r      s    r   N)ultralytics.engine.resultsr   ultralytics.solutions.solutionsr   r   r   r
   r
   r
   r   <module>   s   