
    3j*F                       S SK Jr  S SKJr  S SKJr  S SKJrJrJ	r	J
r
JrJ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
KJr  S SKJr  S SKJr  S SKJr  / SQr " S S\5      r\" SS9 " S S5      5       r\" SS9 " S S5      5       r       SS jr! " S S\\5      r" " S S\RF                  \5      r$ " S S5      r%g)    )annotations)	dataclass)Enum)AnyClassVarListOptionalTupleUnionN)nn)KORNIA_CHECK_SHAPE)PILImage)onnx)ONNXExportMixin)draw_rectangle)	ModelBase)ResizePreProcessor)BoundingBoxBoundingBoxDataFormatBoxFilteringObjectDetectorObjectDetectorResultRTDETRDetectorBuilderresults_from_detectionsc                  (    \ rS rSrSrSrSrSrSrSr	g)r   .   z.Enum class that maps bounding box data format.r          N)
__name__
__module____qualname____firstlineno____doc__XYWHXYXYCXCYWHCENTER_XYWH__static_attributes__r       Y/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/contrib/object_detection.pyr   r   .   s    8DDFKr*   r   T)frozenc                  .    \ rS rSr% SrS\S'   S\S'   Srg)	r   =   zBounding 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_formatr   Nr    r!   r"   r#   r$   __annotations__r)   r   r*   r+   r   r   =   s     ,+&&r*   r   c                  8    \ rS rSr% SrS\S'   S\S'   S\S'   S	rg
)r   M   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   bboxr   Nr1   r   r*   r+   r   r   M   s     M
r*   r   c                p   [        U SS/5        [        U[        5      (       a  [        UR	                  5          n/ nU  Hs  nUR                  5       R                  5       n[        U5      S:w  a  M2  UR                  [        [        US   5      US   [        US   US   US   US	   4US
9S95        Mu     U$ )au  Convert a detection torch.Tensor to a list of :py:class:`ObjectDetectorResult`.

Args:
    detections: torch.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/   r0   )r6   r8   r9   )r   
isinstancestrr   uppersqueezetolistlenappendr   r5   r   )
detectionsformatresultsdets       r+   r   r   ]   s     zC:.&#&v||~6*,Gkkm""$s8q= SVq6 s1vs1vs1vs1v&FTZ[	
	  Nr*   c                  .  ^  \ rS rSr% SrSrS\S'   SU 4S jjr\SS j5       r	\
R                  " 5       SS j5       r  S       SS
 jjr  S       SS jjr     S             SS jjrSSSS	S	SS.             SS jjrSrU =r$ )r      zNWrap an object detection model and perform pre-processing and post-processing.	detectionrB   namec                d   > [         TU ]  5         UR                  5       U l        X l        X0l        g)zInitialize ObjectDetector.

Args:
    model: The object detection model.
    pre_processor: Pre-processing module (e.g., ResizePreProcessor).
    post_processor: Post-processing module (e.g., DETRPostProcessor).
N)super__init__evalmodelpre_processorpost_processor)selfrT   rU   rV   	__class__s       r+   rR   ObjectDetector.__init__   s)     	ZZ\
*,r*   c                    [        S5      e)aM  Build ObjectDetector from config.

This is a placeholder to satisfy the abstract method requirement.
Use kornia.contrib.object_detection.RTDETRDetectorBuilder.build() or instantiate ObjectDetector directly.

Args:
    config: Configuration object (not used, kept for interface compatibility).

Returns:
    ObjectDetector instance.

zObjectDetector.from_config() is not implemented. Use kornia.contrib.object_detection.RtdetrBuilder.build() or instantiate ObjectDetector directly.)NotImplementedError)configs    r+   from_configObjectDetector.from_config   s     "p
 	
r*   c                v    U R                  U5      u  pU R                  U5      u  p4U R                  X4U5      nU$ )a  Detect objects in a given list of images.

Args:
    images: If list of RGB images. Each image is a torch.Tensor with shape :math:`(3, H, W)`.
        If torch.Tensor, a torch.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.

)rU   rT   rV   )rW   imagesimages_sizeslogitsboxesrH   s         r+   forwardObjectDetector.forward   s?      $11&9

6*((E
r*   Nc                   U=(       d    U R                  U5      n/ n[        X5       Hs  u  pgUS   R                  5       nU HA  n	[        U[        R
                  " U	S   U	S   U	S   U	S   -   U	S   U	S   -   ///5      5      nMC     UR                  US   5        Mu     U R                  XS[        U[        R
                  5      S9$ )z4Very simple drawing.

Needs to be more fancy later.
Nr   )is_batch)	rd   zipcloner   torchTensorrG   _tensor_to_typerA   )
rW   r`   rH   output_typedetsoutputimagerN   out_imgouts
             r+   	visualizeObjectDetector.visualize   s     1T\\&1 #F 1EDk'')G (LLCGSWc"gB6GRSVWYSZIZ#["\!]^ !
 MM'!*% !2 ##F*VUZUaUaBb#ccr*   c                H    U R                  X5      nU R                  XC5        g)zSave the output image(s) to a directory.

Args:
    images: input torch.Tensor.
    detections: detection torch.Tensor.
    directory: directory to save the images.

N)rw   _save_outputs)rW   r`   rH   	directoryoutputss        r+   saveObjectDetector.save   s      ..47.r*   c           
         Uc  SU R                    SU S3n[        R                  " U U4SSU=(       d    SU=(       d    S// SQSSU=(       d    SU=(       d    S/U(       a  U OU R                  UUS	.UD6$ )
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.

kornia__z.onnxrj   r>   rj   rj   r=   r   i`  )input_shapeoutput_shapepseudo_shaperT   r}   additional_metadata)rO   r   to_onnxrT   )rW   	onnx_name
image_sizeinclude_pre_and_post_processorr}   r   kwargss          r+   r   ObjectDetector.to_onnx   s    > !$))Aj\?I&&

 Q
 0b*2BC$Q
 1c:3DE8$djj 3

 

 
	
r*   Finductor	fullgraphdynamicbackendmodeoptionsdisablec          
     V    [         R                  " U R                  UUUUUUS9U l        g)zLCompile the internal object detection model with :py:func:`torch.compile()`.r   N)rn   compilerT   )rW   r   r   r   r   r   r   s          r+   r   ObjectDetector.compile  s-     ]]JJ

r*   )rT   rV   rU   )rT   	nn.ModulerU   r   rV   r   returnNone)r\   r   r   r   )r`   'Union[torch.Tensor, list[torch.Tensor]]r   r   )Nrn   )r`   r   rH   Optional[torch.Tensor]rq   rB   r   z:Union[torch.Tensor, list[torch.Tensor], list[Image.Image]]NN)r`   r   rH   r   r{   Optional[str]r   r   )Ni  TTN)r   r   r   Optional[int]r   boolr}   r   r   zOptional[list[tuple[str, str]]]r   r   r   zonnx.ModelProto)r   r   r   r   r   rB   r   r   r   z%Optional[dict[str, str | int | bool]]r   r   r   r   )r    r!   r"   r#   r$   rO   r2   rR   staticmethodr]   rn   inference_moderd   rw   r}   r   r   r)   __classcell__rX   s   @r+   r   r      sq   XD#- 
 
$  * .2"	d7d +d 	d
 
Dd4 .2#'	/7/ +/ !	/
 
/& $($'/3?C,
 ,
 ",
 )-	,

 ,
 =,
 ,
 
,
b  !"9=
 
 	

 
 
 7
 
 

 
r*   r   c                     ^  \ rS rSr% Sr/ SQrS\S'   / SQrS\S'   / SQrS\S'      S       SU 4S	 jjjr	  S       SS
 jjr
 S     SS jjrSrU =r$ )r   i$  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   zClassVar[List[int]]ONNX_DEFAULT_INPUTSHAPEONNX_DEFAULT_OUTPUTSHAPE)r@      r=   ONNX_EXPORT_PSEUDO_SHAPEc                Z  > [         TU ]  5         X0l        S U l        S U l        Ub<  [        U[        R                  5      (       a  UO[        R                  " U5      U l        UbF  [        U[        R                  5      (       a  U=(       d    UO[        R                  " U5      U l        g g N)	rQ   rR   filter_as_zeroclasses_to_keepconfidence_thresholdrA   rn   ro   tensor)rW   r   r   r   rX   s       r+   rR   BoxFiltering.__init__2  s     	,#$(!&#-ou||#L#LRWR^R^_nRo    + 2ELLAA %<(<\\"67 % ,r*   c                <   [         R                  " SUR                  UR                  S9nU=(       d    U R                  =(       d    UnUSS2SS2S4   U:  nU=(       d    U R
                  nUb3  USS2SS2SS24   nUR                  SSS5      nXc:H  R                  SS9nOUS-  S-   R                  5       nXW-  nU R                  (       a  XSS2SS2S4   -  n	U	$ / n
[        UR                  S   5       H   nX   nX   nX   nU
R                  U5        M"     U
$ )a(  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 torch.Tensor of classes to keep. If None, keep all classes.

Returns:
    Union[torch.Tensor, List[torch.Tensor]]
        If `filter_as_zero` is True, return a torch.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dtypeNr   r   rj   )dim)rn   r   r   r   r   r   viewanyr   r   rangeshaperG   )rW   rc   r   r   zero_tensorconfidence_mask	class_ids
class_maskcombined_maskfiltered_boxesfiltered_boxes_listiboxmaskvalid_boxess                  r+   rd   BoxFiltering.forwardG  s2   2 ll3u||5;;O LD$=$=L 	  1a.+?? *AT-A-A&aAaCi(I-221a<O#6;;;CJ *A-1779J (4"1a:%>>N!! u{{1~&A(C #D)K&&{3	 ' #"r*   c                    [         R                  " [        U5       VVs/ s H#  u  p4US:X  a  Uc  U R                  U   OX#   OUPM%     snn6 nU R                  c  US4$ U$ s  snnf )Nrj   g?)rn   rand	enumerater   r   )rW   r   r   r   r   pseudo_inputs         r+   _create_dummy_input BoxFiltering._create_dummy_input  s     zz (44FA ehkmdml6J$//2P\P_svv4
 $$,$$s   *A!
)r   r   r   )NNF)r   z$Optional[Union[torch.Tensor, float]]r   z(Optional[Union[torch.Tensor, List[int]]]r   r   r   r   r   )rc   torch.Tensorr   r   r   r   r   z'Union[torch.Tensor, List[torch.Tensor]]r   )r   z	List[int]r   zOptional[List[int]]r   z$Union[Tuple[Any, ...], torch.Tensor])r    r!   r"   r#   r$   r   r2   r   r   rR   rd   r   r)   r   r   s   @r+   r   r   $  s     4?0>4?1?4>1> FJDH$	B B 	
 
 0 8<26	7#7# 57# 0	7#
 
17#t KO$4G	- r*   r   c                  V    \ rS rSrSr\      S             SS jj5       rSrg)r   i  a;  A builder class for constructing RT-DETR object detection models.

This class provides static methods to:
    - Build an object detection model from a model name or configuration.
    - Export the model to ONNX format for inference.

Note:
    To use this model, load image tensors and call ``model.save(images)``.
Nc                   SSK nSSKJn  SSKJnJn	  U b  Ub  [        S5      eUb'  UR                  U5      n
U=(       d    UR                  nOU ba  U(       a-  UR                  U 5      n
U	R                  U 5      R                  nOiUR                  U 5      n
U	R                  U 5      R                  nO<UR                  SSS9  UR                  S	5      n
U	R                  S	5      R                  nUc  Ub  UR                  OS
n[        U
[        X35      U" UU=(       d#    [        R                   R#                  5       (       + U
R$                  R&                  U
R$                  R(                  S95      $ )a{  Build and returns an RT-DETR object detector model.

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

Args:
    model_name:
        Name of the RT-DETR model to load. Can be one of the available pretrained models.
        Including 'rtdetr_r18vd', 'rtdetr_r34vd', 'rtdetr_r50vd_m', 'rtdetr_r50vd', 'rtdetr_r101vd'.
    config:
        A custom configuration object for building the RT-DETR model.
    pretrained:
        Whether to load a pretrained version of the model (applies when `model_name` is provided).
    image_size:
        The size to which input images will be resized during preprocessing.
        If None, no resizing will be inferred from config file. Recommended scales include
        [480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800].
    confidence_threshold: Threshold to filter results based on confidence scores.
    confidence_filtering: Whether to filter results based on confidence scores.


Returns:
    ObjectDetector
        An object detector instance initialized with the specified model, preprocessor, and post-processor.

r   N)DETRPostProcessor)RTDETRRTDETRConfigz1Either `model_name` or `config` should be `None`.zHNo `model_name` or `config` found. Will build pretrained `rtdetr_r18vd`.r   )
stacklevelrtdetr_r18vdg333333?)r   confidence_filteringnum_classesnum_top_queries)warningskornia.models.rt_detrr   kornia.models.rt_detr.modelr   r   
ValueErrorr]   
input_sizefrom_pretrained	from_namewarnr   r   r   rn   r   is_in_onnx_exportdecoderr   num_queries)
model_namer\   
pretrainedr   r   r   r   r   r   r   rT   s              r+   buildRTDETRDetectorBuilder.build  sG   F 	;D!f&8PQQ&&v.E#8v'8'8J#..z:)33J?JJ
((4)33J?JJ
MMdqrMs**>:E%//?JJJ'BHBT6#>#>Z] z6%9%9%_A]A]A_=_!MM55 % 9 9		
 		
r*   r   )NNTNNN)r   r   r\   zOptional[Any]r   r   r   r   r   zOptional[float]r   zOptional[bool]r   r   )r    r!   r"   r#   r$   r   r   r)   r   r*   r+   r   r     sx     $( $$(04/3E
!E
E
 E
 "	E

 .E
 -E
 
E
 E
r*   r   )rH   r   rI   zstr | BoundingBoxDataFormatr   zlist[ObjectDetectorResult])&
__future__r   dataclassesr   enumr   typingr   r   r   r	   r
   r   rn   r   kornia.core.checkr   kornia.core.externalr   Imager   kornia.core.mixin.onnxr   kornia.image.drawr   kornia.models.baser   kornia.models.processorsr   __all__r   r   r   r   r   Moduler   r   r   r*   r+   <module>r      s   $ # !  > >   0 2 % 2 , ( 7D  $' ' ' $    &A  Fa
Y a
Hg299o gTQ
 Q
r*   