
    h                     B    S SK Jr  S SKJr  S SKJrJr   " S S\5      rg)    )BasePredictor)Results)nmsopsc                   0    \ rS rSrSrS rS rS rS rSr	g)	DetectionPredictor   a  
A class extending the BasePredictor class for prediction based on a detection model.

This predictor specializes in object detection tasks, processing model outputs into meaningful detection results
with bounding boxes and class predictions.

Attributes:
    args (namespace): Configuration arguments for the predictor.
    model (nn.Module): The detection model used for inference.
    batch (list): Batch of images and metadata for processing.

Methods:
    postprocess: Process raw model predictions into detection results.
    construct_results: Build Results objects from processed predictions.
    construct_result: Create a single Result object from a prediction.
    get_obj_feats: Extract object features from the feature maps.

Examples:
    >>> from ultralytics.utils import ASSETS
    >>> from ultralytics.models.yolo.detect import DetectionPredictor
    >>> args = dict(model="yolo11n.pt", source=ASSETS)
    >>> predictor = DetectionPredictor(overrides=args)
    >>> predictor.predict_cli()
c                    [        U SS5      SLn[        R                  " UU R                  R                  U R                  R
                  U R                  R                  U R                  R                  U R                  R                  U R                  R                  S:X  a  SO[        U R                  R                  5      [        U R                  SS5      U R                  R                  S:H  US9
n[        U[        5      (       d  [        R                   " U5      nU(       a$  U R#                  U R$                  US	   5      nUS   nU R&                  " XU40 UD6nU(       a  [)        UW5       H  u  pXl        M     U$ )
a  
Post-process predictions and return a list of Results objects.

This method applies non-maximum suppression to raw model predictions and prepares them for visualization and
further analysis.

Args:
    preds (torch.Tensor): Raw predictions from the model.
    img (torch.Tensor): Processed input image tensor in model input format.
    orig_imgs (torch.Tensor | list): Original input images before preprocessing.
    **kwargs (Any): Additional keyword arguments.

Returns:
    (list): List of Results objects containing the post-processed predictions.

Examples:
    >>> predictor = DetectionPredictor(overrides=dict(model="yolo11n.pt"))
    >>> results = predictor.predict("path/to/image.jpg")
    >>> processed_results = predictor.postprocess(preds, img, orig_imgs)
_featsNdetectr   end2endFobb)max_detncr   rotatedreturn_idxs   )getattrr   non_max_suppressionargsconfiouclassesagnostic_nmsr   tasklenmodelnames
isinstancelistr   convert_torch2numpy_batchget_obj_featsr   construct_resultszipfeats)
selfpredsimg	orig_imgskwargs
save_feats	obj_featsresultsrfs
             `/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/models/yolo/detect/predict.pypostprocessDetectionPredictor.postprocess"   s+   * T8T2$>
''IINNIIMMIIII""II%%IINNh.qC

8H8H4IDJJ	59IINNe+"
 )T**55i@I**4;;aAI!HE((YI&IGY/ 0     c                    SSK n[        S U 5       5      nUR                  U Vs/ s HQ  oUR                  SSSS5      R	                  UR
                  S   SXER
                  S   U-  5      R                  SS9PMS     snSS9n[        Xb5       VVs/ s H  u  pxUR
                  S   (       a  Xx   O/ PM!     snn$ s  snf s  snnf )	z.Extract object features from the feature maps.r   Nc              3   >   #    U  H  oR                   S    v   M     g7f)r   N)shape).0xs     r0   	<genexpr>3DetectionPredictor.get_obj_feats.<locals>.<genexpr>X   s     .Iq
Is         r   )dim)torchmincatpermutereshaper6   meanr$   )	r&   	feat_mapsidxsr?   sr8   r,   r%   idxs	            r0   r"    DetectionPredictor.get_obj_featsT   s    .I..IIenoen`aYYq!Q"**1771:r1ggajAoNSSXZS[enouv  
	 FIEYZEYzuciil
2EYZZ pZs   AB<&Cc                     [        XU R                  S   5       VVVs/ s H  u  pEnU R                  XBXV5      PM     snnn$ s  snnnf )a  
Construct a list of Results objects from model predictions.

Args:
    preds (list[torch.Tensor]): List of predicted bounding boxes and scores for each image.
    img (torch.Tensor): Batch of preprocessed images used for inference.
    orig_imgs (list[np.ndarray]): List of original images before preprocessing.

Returns:
    (list[Results]): List of Results objects containing detection information for each image.
r   )r$   batchconstruct_result)r&   r'   r(   r)   predorig_imgimg_paths          r0   r#   $DetectionPredictor.construct_results^   sJ     -0$**Q-,P
,P( !!$X@,P
 	
 
s   Ac           	          [         R                  " UR                  SS USS2SS24   UR                  5      USS2SS24'   [        X4U R                  R
                  USS2SS24   S9$ )a  
Construct a single Results object from one image prediction.

Args:
    pred (torch.Tensor): Predicted boxes and scores with shape (N, 6) where N is the number of detections.
    img (torch.Tensor): Preprocessed image tensor used for inference.
    orig_img (np.ndarray): Original image before preprocessing.
    img_path (str): Path to the original image file.

Returns:
    (Results): Results object containing the original image, image path, class names, and scaled bounding boxes.
r;   N      )pathr   boxes)r   scale_boxesr6   r   r   r   )r&   rM   r(   rN   rO   s        r0   rL   #DetectionPredictor.construct_resulto   sf     oociimT!RaR%[(..QQUxdjj6F6FdSTVXWXVXSXkZZr3    N)
__name__
__module____qualname____firstlineno____doc__r1   r"   r#   rL   __static_attributes__rX   r3   r0   r   r      s    20d[
"[r3   r   N)ultralytics.engine.predictorr   ultralytics.engine.resultsr   ultralytics.utilsr   r   r   rX   r3   r0   <module>rb      s     7 . &u[ u[r3   