
    h^                     B    S SK Jr  S SKJr  S SKJrJr   " S S\5      rg)    )Results)DetectionPredictor)DEFAULT_CFGopsc                   N   ^  \ rS rSrSr\SS4U 4S jjrU 4S jrS rS r	Sr
U =r$ )	SegmentationPredictor   a  
A class extending the DetectionPredictor class for prediction based on a segmentation model.

This class specializes in processing segmentation model outputs, handling both bounding boxes and masks in the
prediction results.

Attributes:
    args (dict): Configuration arguments for the predictor.
    model (torch.nn.Module): The loaded YOLO segmentation model.
    batch (list): Current batch of images being processed.

Methods:
    postprocess: Apply non-max suppression and process segmentation detections.
    construct_results: Construct a list of result objects from predictions.
    construct_result: Construct a single result object from a prediction.

Examples:
    >>> from ultralytics.utils import ASSETS
    >>> from ultralytics.models.yolo.segment import SegmentationPredictor
    >>> args = dict(model="yolo11n-seg.pt", source=ASSETS)
    >>> predictor = SegmentationPredictor(overrides=args)
    >>> predictor.predict_cli()
Nc                 H   > [         TU ]  XU5        SU R                  l        g)a  
Initialize the SegmentationPredictor with configuration, overrides, and callbacks.

This class specializes in processing segmentation model outputs, handling both bounding boxes and masks in the
prediction results.

Args:
    cfg (dict): Configuration for the predictor.
    overrides (dict, optional): Configuration overrides that take precedence over cfg.
    _callbacks (list, optional): List of callback functions to be invoked during prediction.
segmentN)super__init__argstask)selfcfg	overrides
_callbacks	__class__s       a/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/models/yolo/segment/predict.pyr   SegmentationPredictor.__init__!   s     	4"		    c                 r   > [        US   [        5      (       a  US   S   OUS   n[        TU ]  US   X#US9$ )a  
Apply non-max suppression and process segmentation detections for each image in the input batch.

Args:
    preds (tuple): Model predictions, containing bounding boxes, scores, classes, and mask coefficients.
    img (torch.Tensor): Input image tensor in model format, with shape (B, C, H, W).
    orig_imgs (list | torch.Tensor | np.ndarray): Original image or batch of images.

Returns:
    (list): List of Results objects containing the segmentation predictions for each image in the batch.
        Each Results object includes both bounding boxes and segmentation masks.

Examples:
    >>> predictor = SegmentationPredictor(overrides=dict(model="yolo11n-seg.pt"))
    >>> results = predictor.postprocess(preds, img, orig_img)
   r   )protos)
isinstancetupler   postprocess)r   predsimg	orig_imgsr   r   s        r   r   !SegmentationPredictor.postprocess0   sC    $ ",E!He!<!<q"%(w"58SF"KKr   c                     [        XU R                  S   U5       VVVVs/ s H  u  pVpxU R                  XRXgU5      PM     snnnn$ s  snnnnf )a  
Construct a list of result objects from the predictions.

Args:
    preds (list[torch.Tensor]): List of predicted bounding boxes, scores, and masks.
    img (torch.Tensor): The image after preprocessing.
    orig_imgs (list[np.ndarray]): List of original images before preprocessing.
    protos (list[torch.Tensor]): List of prototype masks.

Returns:
    (list[Results]): List of result objects containing the original images, image paths, class names,
        bounding boxes, and masks.
r   )zipbatchconstruct_result)	r   r   r    r!   r   predorig_imgimg_pathprotos	            r   construct_results'SegmentationPredictor.construct_resultsE   sP      47uTUX^3_
3_/ !!$XG3_
 	
 
s    A
c           	         UR                   S   S:X  a  SnGOU R                  R                  (       ay  [        R                  " UR                   SS USS2SS24   UR                   5      USS2SS24'   [        R
                  " XQSS2SS24   USS2SS24   UR                   SS 5      nOw[        R                  " XQSS2SS24   USS2SS24   UR                   SS SS9n[        R                  " UR                   SS USS2SS24   UR                   5      USS2SS24'   Ub+  UR                  S5      S:  n[        U5      (       d  X   Xg   pa[        X4U R                  R                  USS2SS24   US	9$ )
a  
Construct a single result object from the prediction.

Args:
    pred (torch.Tensor): The predicted bounding boxes, scores, and masks.
    img (torch.Tensor): The image after preprocessing.
    orig_img (np.ndarray): The original image before preprocessing.
    img_path (str): The path to the original image.
    proto (torch.Tensor): The prototype masks.

Returns:
    (Results): Result object containing the original image, image path, class names, bounding boxes, and masks.
r   N         T)upsample)r   )pathnamesboxesmasks)shaper   retina_masksr   scale_boxesprocess_mask_nativeprocess_maskamaxallr   modelr4   )r   r'   r    r(   r)   r*   r6   keeps           r   r&   &SegmentationPredictor.construct_resultX   s`    ::a=AEYY##//#))AB-a!ehnnUDBQBK++E12;QUX^^\^]^M_`E$$UABKa!eciiPQPRm^bcE//#))AB-a!ehnnUDBQBK::h'!+Dt99"j%+exdjj6F6FdSTVXWXVXSXkafggr    )__name__
__module____qualname____firstlineno____doc__r   r   r   r+   r&   __static_attributes____classcell__)r   s   @r   r   r      s-    0 '$4 #L*
&h hr   r   N)ultralytics.engine.resultsr   &ultralytics.models.yolo.detect.predictr   ultralytics.utilsr   r   r   rA   r   r   <module>rL      s!    / E .jh. jhr   