
    FPhc                     Z    d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
  G d d	e      Zy
)a>  
SAM model interface.

This module provides an interface to the Segment Anything Model (SAM) from Ultralytics, designed for real-time image
segmentation tasks. The SAM model allows for promptable segmentation with unparalleled versatility in image analysis,
and has been trained on the SA-1B dataset. It features zero-shot performance capabilities, enabling it to adapt to new
image distributions and tasks without prior knowledge.

Key Features:
    - Promptable segmentation
    - Real-time performance
    - Zero-shot transfer capabilities
    - Trained on SA-1B dataset
    )Path)Model)
model_info   )	build_sam	Predictorc                   `     e Zd ZdZd	d
 fdZddefdZd fd	ZddZddZ	e
d        Z xZS )SAMa/  
    SAM (Segment Anything Model) interface class.

    SAM is designed for promptable real-time image segmentation. It can be used with a variety of prompts such as
    bounding boxes, points, or labels. The model has capabilities for zero-shot performance and is trained on the SA-1B
    dataset.
    c                 p    |r"t        |      j                  dvrt        d      t        |   |d       y)a&  
        Initializes the SAM model with a pre-trained model file.

        Args:
            model (str): Path to the pre-trained SAM model file. File should have a .pt or .pth extension.

        Raises:
            NotImplementedError: If the model file extension is not .pt or .pth.
        )z.ptz.pthz8SAM prediction requires pre-trained *.pt or *.pth model.segment)modeltaskN)r   suffixNotImplementedErrorsuper__init__)selfr   	__class__s     gC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/models/sam/model.pyr   zSAM.__init__#   s7     T%[''>%&`aau95    weightsc                 $    t        |      | _        y)z
        Loads the specified weights into the SAM model.

        Args:
            weights (str): Path to the weights file.
            task (str, optional): Task name. Defaults to None.
        N)r   r   )r   r   r   s      r   _loadz	SAM._load1   s     w'
r   c                     t        dddd      }|j                  |       t        |||      }t        	|   ||fd|i|S )a  
        Performs segmentation prediction on the given image or video source.

        Args:
            source (str): Path to the image or video file, or a PIL.Image object, or a numpy.ndarray object.
            stream (bool, optional): If True, enables real-time streaming. Defaults to False.
            bboxes (list, optional): List of bounding box coordinates for prompted segmentation. Defaults to None.
            points (list, optional): List of points for prompted segmentation. Defaults to None.
            labels (list, optional): List of labels for prompted segmentation. Defaults to None.

        Returns:
            (list): The model predictions.
        g      ?r   predicti   )confr   modeimgsz)bboxespointslabelsprompts)dictupdater   r   )
r   sourcestreamr    r!   r"   kwargs	overridesr#   r   s
            r   r   zSAM.predict;   sJ     d$O	i fVFCwvvIwI&IIr   c                 0     | j                   |||||fi |S )a}  
        Alias for the 'predict' method.

        Args:
            source (str): Path to the image or video file, or a PIL.Image object, or a numpy.ndarray object.
            stream (bool, optional): If True, enables real-time streaming. Defaults to False.
            bboxes (list, optional): List of bounding box coordinates for prompted segmentation. Defaults to None.
            points (list, optional): List of points for prompted segmentation. Defaults to None.
            labels (list, optional): List of labels for prompted segmentation. Defaults to None.

        Returns:
            (list): The model predictions.
        )r   )r   r&   r'   r    r!   r"   r(   s          r   __call__zSAM.__call__N   s"     t||FFFFFMfMMr   c                 2    t        | j                  ||      S )aq  
        Logs information about the SAM model.

        Args:
            detailed (bool, optional): If True, displays detailed information about the model. Defaults to False.
            verbose (bool, optional): If True, displays information on the console. Defaults to True.

        Returns:
            (tuple): A tuple containing the model's information.
        )detailedverbose)r   r   )r   r-   r.   s      r   infozSAM.info^   s     $**xIIr   c                     ddt         iiS )z
        Provides a mapping from the 'segment' task to its corresponding 'Predictor'.

        Returns:
            (dict): A dictionary mapping the 'segment' task to its corresponding 'Predictor'.
        r   	predictorr   )r   s    r   task_mapzSAM.task_mapk   s     K344r   )zsam_b.pt)returnN)N)FNNN)NFNNN)FT)__name__
__module____qualname____doc__r   strr   r   r+   r/   propertyr2   __classcell__)r   s   @r   r   r      s?    6(S (J&N J 5 5r   r   N)r7   pathlibr   ultralytics.engine.modelr   ultralytics.utils.torch_utilsr   buildr   r   r	   r    r   r   <module>r@      s)     * 4  Y5% Y5r   