o
    Vhf                     @   sL   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 deZ	dS )    N)	LetterBox)BasePredictor)Results)opsc                   @   s    e Zd ZdZdd Zdd ZdS )RTDETRPredictorar  
    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.

    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              	   C   sP  t |ttfs|dg}|d jd }|d jd|d fdd\}}t |ts+t|}g }t|||| jd D ]n\}}	}
}t	|}|	j
ddd\}}|d| jjk}| jjdurk|tj| jj|jdkd	|@ }tj|||gdd| }|
jdd
 \}}|ddd
gf  |9  < |dd	dgf  |9  < |t|
|| jj|d q7|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:
            (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ow r8   U/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/models/rtdetr/predict.pypostprocess    s&   

 
"zRTDETRPredictor.postprocessc                    s"   t | jddd  fdd|D S )a  
        Pre-transforms the 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_fillc                    s   g | ]} |d qS ))imager8   ).0x	letterboxr8   r9   
<listcomp>T   s    z1RTDETRPredictor.pre_transform.<locals>.<listcomp>)r   imgsz)r&   imr8   r@   r9   pre_transformH   s   zRTDETRPredictor.pre_transformN)__name__
__module____qualname____doc__r:   rE   r8   r8   r8   r9   r      s    (r   )
r    ultralytics.data.augmentr   ultralytics.engine.predictorr   ultralytics.engine.resultsr   ultralytics.utilsr   r   r8   r8   r8   r9   <module>   s   