
    |he                     R    d dl 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)    N)	LetterBox)BasePredictor)Results)opsc                       e Zd ZdZd Zd Zy)RTDETRPredictoraM  
    RT-DETR (Real-Time Detection Transformer) Predictor extending the BasePredictor class for making predictions.

    This class leverages Vision Transformers to provide real-time object detection while maintaining high accuracy.
    It supports key features like efficient hybrid encoding and IoU-aware query selection.

    Attributes:
        imgsz (int): Image size for inference (must be square and scale-filled).
        args (dict): Argument overrides for the predictor.
        model (torch.nn.Module): The loaded RT-DETR model.
        batch (list): Current batch of processed inputs.

    Methods:
        postprocess: Postprocess raw model predictions to generate bounding boxes and confidence scores.
        pre_transform: Pre-transform input images before feeding them into the model for inference.

    Examples:
        >>> from ultralytics.utils import ASSETS
        >>> from ultralytics.models.rtdetr import RTDETRPredictor
        >>> args = dict(model="rtdetr-l.pt", source=ASSETS)
        >>> predictor = RTDETRPredictor(overrides=args)
        >>> predictor.predict_cli()
    c           	         t        |t        t        f      s|dg}|d   j                  d   }|d   j	                  d|dz
  fd      \  }}t        |t              st        j                  |      }g }t        |||| j                  d         D ]>  \  }}	}
}t        j                  |      }|	j                  dd      \  }}|j                  d      | j                  j                  kD  }| j                  j                  J|t        j                   | j                  j                  |j"                        k(  j%                  d	      |z  }t        j&                  |||gd      |   }|
j                  dd
 \  }}|ddd
gfxx   |z  cc<   |dd	dgfxx   |z  cc<   |j)                  t+        |
|| j,                  j.                  |             A |S )a*  
        Postprocess the raw predictions from the model to generate bounding boxes and confidence scores.

        The method filters detections based on confidence and class if specified in `self.args`. It converts
        model predictions to Results objects containing properly scaled bounding boxes.

        Args:
            preds (list | tuple): List of [predictions, extra] from the model, where predictions contain
                bounding boxes and scores.
            img (torch.Tensor): Processed input images with shape (N, 3, H, W).
            orig_imgs (list | torch.Tensor): Original, unprocessed images.

        Returns:
            results (List[Results]): A list of Results objects containing the post-processed bounding boxes,
                confidence scores, and class labels.
        Nr      )dimT)keepdim)device      .   )pathnamesboxes)
isinstancelisttupleshapesplitr   convert_torch2numpy_batchzipbatch	xywh2xyxymaxsqueezeargsconfclassestorchtensorr   anycatappendr   modelr   )selfpredsimg	orig_imgsndbboxesscoresresultsbboxscoreorig_imgimg_path	max_scoreclsidxpredohows                     `/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/models/rtdetr/predict.pypostprocesszRTDETRPredictor.postprocess$   s   " %$/DME1X^^BqBF<)T*55i@I/2669djjYZm/\ 
	a+D%8==&D"YYr4Y8NIs##B'$))..8Cyy  ,ell499+<+<SZZPPUUVWX[^^99dIs3<SAD^^BQ'FBq!f#q!f#NN78($**BRBRZ^_`
	a     c                 j    t        | j                  dd      }|D cg c]  } ||       c}S c c}w )a  
        Pre-transform input images before feeding them into the model for inference.

        The input images are letterboxed to ensure a square aspect ratio and scale-filled. The size must be square
        (640) and scale_filled.

        Args:
            im (List[np.ndarray]  | torch.Tensor): Input images of shape (N, 3, H, W) for tensor,
                [(H, W, 3) x N] for list.

        Returns:
            (list): List of pre-transformed images ready for model inference.
        FT)auto
scale_fill)image)r   imgsz)r)   im	letterboxxs       r;   pre_transformzRTDETRPredictor.pre_transformL   s/     djjuF	,./q	"///s   0N)__name__
__module____qualname____doc__r<   rF    r=   r;   r   r      s    0&P0r=   r   )
r#   ultralytics.data.augmentr   ultralytics.engine.predictorr   ultralytics.engine.resultsr   ultralytics.utilsr   r   rK   r=   r;   <module>rP      s$     . 6 . !P0m P0r=   