o
    oi                     @  sp   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ eG dd dZeG d	d
 d
ZdS )    )annotations)	dataclass)Optional)Tensor)KORNIA_CHECK)resizec                   @  sR   e Zd ZU dZded< ded< dZded< edd	d
ZdddZddddZ	dS )SegmentationResultsa`  Encapsulate the results obtained by a Segmentation model.

    Args:
        logits: Results logits with shape :math:`(B, C, H, W)`, where :math:`C` refers to the number of predicted masks
        scores: The scores from the logits. Shape :math:`(B, C)`
        mask_threshold: The threshold value to generate the `binary_masks` from the `logits`

    r   logitsscoresg        floatmask_thresholdreturnc                 C  s"   | j dur	| j }n| j}|| jkS )a  Binary mask generated from logits considering the mask_threshold.

        Shape will be the same of logits :math:`(B, C, H, W)` where :math:`C` is the number masks predicted.

        .. note:: If you run `original_res_logits`, this will generate the masks
           based on the original resolution logits.
           Otherwise, this will use the low resolution logits (self.logits).
        N)_original_res_logitsr	   r   )selfx r   T/home/ubuntu/.local/lib/python3.10/site-packages/kornia/contrib/models/structures.pybinary_masks+   s   


z SegmentationResults.binary_masks
input_sizetuple[int, int]original_sizeimage_size_encoderOptional[tuple[int, int]]c                 C  s^   | j }t|trt||dddd}|dd|d d|d f }t||dddd}|| _| jS )aa  Remove padding and upscale the logits to the original image size.

        Resize to image encoder input -> remove padding (bottom and right) -> Resize to original size

        .. note:: This method set a internal `original_res_logits` which will be used if available for the binary masks.

        Args:
            input_size: The size of the image input to the model, in (H, W) format. Used to remove padding.
            original_size: The original size of the image before resizing for input to the model, in (H, W) format.
            image_size_encoder: The size of the input image for image encoder, in (H, W) format. Used to resize the
                                logits back to encoder resolution before remove the padding.

        Returns:
            Batched logits in :math:`(K, C, H, W)` format, where (H, W) is given by original_size.

        bilinearF)sizeinterpolationalign_corners	antialias.Nr      )r	   
isinstancetupler   r   )r   r   r   r   r   r   r   r   original_res_logits<   s   
z'SegmentationResults.original_res_logitsr   dimintc                 C  s:   | j || _ | j|| _t| jtr| j|| _| S )z7Realize a squeeze for the dim given for all properties.)r	   squeezer
   r   r   r   )r   r"   r   r   r   r$   Z   s
   zSegmentationResults.squeezeN)r   r   )r   r   r   r   r   r   r   r   )r   )r"   r#   r   r   )
__name__
__module____qualname____doc____annotations__r   propertyr   r!   r$   r   r   r   r   r      s   
 	
r   c                   @  s\   e Zd ZU dZdZded< dZded< dZded< dd
dZe	dddZ
e	dddZdS )Promptsa  Encapsulate the prompts inputs for a Model.

    Args:
        points: A tuple with the keypoints (coordinates x, y) and their respective labels. Shape :math:`(K, N, 2)` for
                the keypoints, and :math:`(K, N)`
        boxes: Batched box inputs, with shape :math:`(K, 4)`. Expected to be into xyxy format.
        masks: Batched mask prompts to the model with shape :math:`(K, 1, H, W)`

    NzOptional[tuple[Tensor, Tensor]]pointsOptional[Tensor]boxesmasksr   Nonec                 C  sB   t | jtrt | jtrt| jjd | jjd kd d S d S d S )Nr   z,The prompts should have the same batch size!)r   	keypointsr   r.   r   shaper   r   r   r   __post_init__t   s   "zPrompts.__post_init__c                 C     t | jtr| jd S dS )z The keypoints from the `points`.r   Nr   r,   r    r3   r   r   r   r1   x      zPrompts.keypointsc                 C  r5   )z'The keypoints labels from the `points`.r   Nr6   r3   r   r   r   keypoints_labels}   r7   zPrompts.keypoints_labels)r   r0   )r   r-   )r%   r&   r'   r(   r,   r)   r.   r/   r4   r*   r1   r8   r   r   r   r   r+   d   s   
 

r+   N)
__future__r   dataclassesr   typingr   kornia.corer   kornia.core.checkr   kornia.geometry.transformr   r   r+   r   r   r   r   <module>   s   G