o
    oi<                      @  s   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 d dl
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 d dlmZ g dZG dd deZeddG dd dZeddG dd dZdddZG dd deZdS )     )annotations)	dataclass)Enum)AnyOptionalUnionN)Tensor)KORNIA_CHECK_SHAPE)PILImage)onnx)	ModelBase)draw_rectangle)BoundingBoxBoundingBoxDataFormatObjectDetectorObjectDetectorResultresults_from_detectionsc                   @  s    e Zd ZdZdZdZdZdZdS )r   z.Enum class that maps bounding box data format.r         N)__name__
__module____qualname____doc__XYWHXYXYCXCYWHCENTER_XYWH r   r   P/home/ubuntu/.local/lib/python3.10/site-packages/kornia/models/detection/base.pyr   *   s    r   T)frozenc                   @  s"   e Zd ZU dZded< ded< dS )r   a  Bounding box data class.

    Useful for representing bounding boxes in different formats for object detection.

    Args:
        data: tuple of bounding box data. The length of the tuple depends on the data format.
        data_format: bounding box data format.

    z!tuple[float, float, float, float]datar   data_formatNr   r   r   r   __annotations__r   r   r   r   r   9   s   
 
r   c                   @  s*   e Zd ZU dZded< ded< ded< dS )	r   zObject detection result.

    Args:
        class_id: class id of the detected object.
        confidence: confidence score of the detected object.
        bbox: bounding box of the detected object in xywh format.

    intclass_idfloat
confidencer   bboxNr"   r   r   r   r   r   I   s
   
 	r   
detectionsr   formatstr | BoundingBoxDataFormatreturnlist[ObjectDetectorResult]c                 C  s   t | ddg t|trt|  }g }| D ]/}|  }t|dkr%q|t	t
|d |d t|d |d |d |d	 f|d
d q|S )a}  Convert a detection tensor to a list of :py:class:`ObjectDetectorResult`.

    Args:
        detections: tensor with shape :math:`(D, 6)`, where :math:`D` is the number of detections in the given image,
            :math:`6` represents class id, score, and `xywh` bounding box.
        format: detection format.

    Returns:
        list of :py:class:`ObjectDetectorResult`.

    D6   r   r   r            )r    r!   )r%   r'   r(   )r	   
isinstancestrr   uppersqueezetolistlenappendr   r$   r   )r)   r*   resultsdetr   r   r   r   Y   s    

"r   c                      s   e Zd ZU dZdZded< e d2dd	Z	d3d4ddZ		
d5d6ddZ
	
				
d7d8 fd$d%Zd&d&d'd
d
d&d(d9d0d1Z  ZS ):r   zNWrap an object detection model and perform pre-processing and post-processing.	detectionr5   nameimagesUnion[Tensor, list[Tensor]]r,   c                 C  s.   |  |\}}| |\}}| |||}|S )a  Detect objects in a given list of images.

        Args:
            images: If list of RGB images. Each image is a Tensor with shape :math:`(3, H, W)`.
                If Tensor, a Tensor with shape :math:`(B, 3, H, W)`.

        Returns:
            list of detections found in each image. For item in a batch, shape is :math:`(D, 6)`, where :math:`D` is the
            number of detections in the given image, :math:`6` represents class id, score, and `xywh` bounding box.

        )pre_processormodelpost_processor)selfr?   images_sizeslogitsboxesr)   r   r   r   forward~   s   zObjectDetector.forwardNtorchr)   Optional[Tensor]output_type.Union[Tensor, list[Tensor], list[Image.Image]]c           
      C  s   |p|  |}g }t||D ]4\}}|d  }|D ] }	t|t|	d |	d |	d |	d  |	d |	d  ggg}q||d  q| j||t|tdS )zDVery simple drawing.

        Needs to be more fancy later.
        Nr   )is_batch)	rH   zipcloner   rI   r   r:   _tensor_to_typer4   )
rD   r?   r)   rK   detsoutputimager=   out_imgoutr   r   r   	visualize   s   4zObjectDetector.visualize	directoryOptional[str]Nonec                 C  s   |  ||}| || dS )zSave the output image(s) to a directory.

        Args:
            images: input tensor.
            detections: detection tensor.
            directory: directory to save the images.

        N)rZ   _save_outputs)rD   r?   r)   r[   outputsr   r   r   save   s   zObjectDetector.save  T	onnx_name
image_sizeOptional[int]include_pre_and_post_processorboolr`   additional_metadataOptional[list[tuple[str, str]]]kwargsr   onnx.ModelProtoc              	     sl   |du rd| j  d| d}t j|fdd|pd|pdgg ddd|p$d	|p'd	g|r,| n| j||d
|S )a  Export an RT-DETR object detection model to ONNX format.

        Either `model_name` or `config` must be provided. If neither is provided,
        a default pretrained model (`rtdetr_r18vd`) will be built.

        Args:
            onnx_name:
                The name of the output ONNX file. If not provided, a default name in the
                format "Kornia-<ClassName>.onnx" will be used.
            image_size:
                The size to which input images will be resized during preprocessing.
                If None, image_size will be dynamic.
                For RTDETR, recommended scales include [480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800].
            include_pre_and_post_processor:
                Whether to include the pre-processor and post-processor in the exported model.
            save:
                If to save the model or load it.
            additional_metadata:
                Additional metadata to add to the ONNX model.
            kwargs: Additional arguments to convert to onnx.

        Nkornia__z.onnxrP   r1   )rP   rP   r0   r   i`  )input_shapeoutput_shapepseudo_shaperB   r`   rg   )r>   superto_onnxrB   )rD   rb   rc   re   r`   rg   ri   	__class__r   r   rq      s   zObjectDetector.to_onnxFinductor	fullgraphdynamicbackendmodeoptionsdisablerv   rw   rx   ry   rz   %Optional[dict[str, str | int | bool]]r{   c             	   C  s    t j| j||||||d| _dS )zLCompile the internal object detection model with :py:func:`torch.compile()`.ru   N)rI   compilerB   )rD   rv   rw   rx   ry   rz   r{   r   r   r   r}      s   zObjectDetector.compile)r?   r@   r,   r@   )NrI   )r?   r@   r)   rJ   rK   r5   r,   rL   )NN)r?   r@   r)   rJ   r[   r\   r,   r]   )Nra   TTN)rb   r\   rc   rd   re   rf   r`   rf   rg   rh   ri   r   r,   rj   )rv   rf   rw   rf   rx   r5   ry   r\   rz   r|   r{   rf   r,   r]   )r   r   r   r   r>   r#   rI   inference_moderH   rZ   r`   rq   r}   __classcell__r   r   rr   r   r   y   s,   
 0r   )r)   r   r*   r+   r,   r-   )
__future__r   dataclassesr   enumr   typingr   r   r   rI   kornia.corer   kornia.core.checkr	   kornia.core.externalr
   Imager   kornia.models.baser   kornia.utils.drawr   __all__r   r   r   r   r   r   r   r   r   <module>   s&   	
 