o
    oi                     @   s`   d dl mZmZmZmZmZmZ d dlmZm	Z	m
Z
mZ d dlmZ dgZG dd deeZdS )    )AnyClassVarListOptionalTupleUnion)ModuleTensorrandtensor)ONNXExportMixinBoxFilteringc                       s  e Zd ZU dZg dZeee  ed< g dZ	eee  ed< g dZ
eee  ed< 			dd	eeeef  d
eeeee f  deddf fddZ	dded	ee d
ee deeee f fddZ	ddee deee  deeedf ef fddZ  ZS )r   a   Filter boxes according to the desired threshold.

    Args:
        confidence_threshold: an 0-d scalar that represents the desired threshold.
        classes_to_keep: a 1-d list of classes to keep. If None, keep all classes.
        filter_as_zero: whether to filter boxes as zero.

    )r      ONNX_DEFAULT_INPUTSHAPEONNX_DEFAULT_OUTPUTSHAPE)      r   ONNX_EXPORT_PSEUDO_SHAPENFconfidence_thresholdclasses_to_keepfilter_as_zeroreturnc                    sh   t    || _d | _d | _|d urt|tr|nt|| _|d ur2t|tr+|p*|nt|| _d S d S N)super__init__r   r   r   
isinstancer	   r   )selfr   r   r   	__class__ Q/home/ubuntu/.local/lib/python3.10/site-packages/kornia/models/detection/utils.pyr   (   s   
zBoxFiltering.__init__boxesc                 C   s   t d|j|jd}|p| jp|}|dddddf |k}|p!| j}|durC|ddddddf }|ddd}||kjdd}n|d d  }||@ }| jra||dddddf  }	|	S g }
t	|j
d D ]}|| }|| }|| }|
| qj|
S )ax  Filter boxes according to the desired threshold.

        To be ONNX-friendly, the inputs for direct forwarding need to be all tensors.

        Args:
            boxes: [B, D, 6], where B is the batchsize,  D is the number of detections in the image,
                6 represent (class_id, confidence_score, x, y, w, h).
            confidence_threshold: an 0-d scalar that represents the desired threshold.
            classes_to_keep: a 1-d tensor of classes to keep. If None, keep all classes.

        Returns:
            Union[Tensor, List[Tensor]]
                If `filter_as_zero` is True, return a tensor of shape [D, 6], where D is the total number of
                detections as input.
                If `filter_as_zero` is False, return a list of tensors of shape [D, 6], where D is the number of
                valid detections for each element in the batch.

        g        )devicedtypeN   r   r   )dim)r   r#   r$   r   r   viewanyboolr   rangeshapeappend)r   r"   r   r   zero_tensorconfidence_mask	class_ids
class_maskcombined_maskfiltered_boxesfiltered_boxes_listiboxmaskvalid_boxesr    r    r!   forward;   s*   
zBoxFiltering.forwardinput_shapepseudo_shape.c                    s2   t  fddt|D  }jd u r|dfS |S )Nc                    s6   g | ]\}}|d kr du rj | n | n|qS )r   N)r   ).0r4   r&   r:   r   r    r!   
<listcomp>u   s    $z4BoxFiltering._create_dummy_input.<locals>.<listcomp>g?)r
   	enumerater   )r   r9   r:   pseudo_inputr    r<   r!   _create_dummy_inputq   s   
z BoxFiltering._create_dummy_input)NNF)NNr   )__name__
__module____qualname____doc__r   r   r   int__annotations__r   r   r   r   r	   floatr)   r   r8   r   r   r@   __classcell__r    r    r   r!   r      sF   
 	
7
N)typingr   r   r   r   r   r   kornia.corer   r	   r
   r   kornia.core.mixin.onnxr   __all__r   r    r    r    r!   <module>   s
    