
    h                     R    S SK r S SKJr  S SKJr  S SKJr  S SKJr   " S S\5      r	g)    N)	LetterBox)BasePredictor)Results)opsc                   $    \ rS rSrSrS rS rSrg)RTDETRPredictor   a  
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           
         [        U[        [        45      (       d  US/nUS   R                  S   nUS   R	                  SUS-
  4SS9u  pV[        U[        5      (       d  [
        R                  " U5      n/ n[        XVX0R                  S   5       GHa  u  pp[
        R                  " U5      nU	R                  SSS9u  pUR                  S5      U R                  R                  :  nU R                  R                  bH  U[        R                   " U R                  R                  UR"                  S9:H  R%                  S	5      U-  n[        R&                  " XU/SS9U   nXSS2S4   R)                  SS
9   SU R                  R*                   nU
R                  SS u  nnUSSS/4==   U-  ss'   USS	S/4==   U-  ss'   UR-                  [/        XU R0                  R2                  US95        GMd     U$ )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   )
descending   .   )pathnamesboxes)
isinstancelisttupleshapesplitr   convert_torch2numpy_batchzipbatch	xywh2xyxymaxsqueezeargsconfclassestorchtensorr   anycatargsortmax_detappendr   modelr   )selfpredsimg	orig_imgsndbboxesscoresresultsbboxscoreorig_imgimg_path	max_scoreclsidxpredohows                     [/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/models/rtdetr/predict.pypostprocessRTDETRPredictor.postprocess$   s   " %$//DME1X^^BqBF<)T**55i@I/269jjYZm/\+D==&D"YYr4Y8NI##B'$))..8Cyy  ,ell499+<+<SZZPPUUVWX[^^99ds3<SADQT
**d*;<=Ptyy?P?PQD^^BQ'FBq!f#q!f#NN78$**BRBRZ^_` 0]     c                 b    [        U R                  SSS9nU Vs/ s H  o2" US9PM
     sn$ s  snf )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_transformRTDETRPredictor.pre_transformM   s3     djjuF	,./Bq	"B///s   , N)__name__
__module____qualname____firstlineno____doc__r@   rK   __static_attributes__rM   rB   r?   r   r      s    0'R0rB   r   )
r%   ultralytics.data.augmentr   ultralytics.engine.predictorr   ultralytics.engine.resultsr   ultralytics.utilsr   r   rM   rB   r?   <module>rX      s$     . 6 . !Q0m Q0rB   