
    hG                        S SK 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J	r	J
r
JrJrJrJrJr  SSKJr  S	\S
\S\S\4S jrS	\S
\S\S\S\4
S jr\ R*                  R,                  S	\S
\S\S\S\4
S j5       r\ R*                  R,                  S	\S
\S\S\S\4
S j5       rS	\S\S\4S jrS	\S\\\4   S\4S jrS	\S\S\S\4S jrS	\S\4S jrS\S\S\\\4   4S jr S\S\S\4S jr!S\S\S\4S jr"S%S\S\S\S\4S  jjr#S%S\S\S\S\4S! jjr$S%S\S\S\S\\\4   4S" jjr%S#\ R                  S\ R                  4S$ jr&g)&    N)Tensor)_assert_has_ops   )_log_api_usage_once   )_box_cxcywh_to_xyxy_box_cxcywhr_to_xywhr_box_xywh_to_xyxy_box_xywhr_to_cxcywhr_box_xywhr_to_xyxyxyxy_box_xyxy_to_cxcywh_box_xyxy_to_xywh_box_xyxyxyxy_to_xywhr)_upcastboxesscoresiou_thresholdreturnc                    [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        [        5         [         R                  R                  R                  XU5      $ )a  
Performs non-maximum suppression (NMS) on the boxes according
to their intersection-over-union (IoU).

NMS iteratively removes lower scoring boxes which have an
IoU greater than ``iou_threshold`` with another (higher scoring)
box.

If multiple boxes have the exact same score and satisfy the IoU
criterion with respect to a reference box, the selected box is
not guaranteed to be the same between CPU and GPU. This is similar
to the behavior of argsort in PyTorch when repeated values are present.

Args:
    boxes (Tensor[N, 4])): boxes to perform NMS on. They
        are expected to be in ``(x1, y1, x2, y2)`` format with ``0 <= x1 < x2`` and
        ``0 <= y1 < y2``.
    scores (Tensor[N]): scores for each one of the boxes
    iou_threshold (float): discards all overlapping boxes with IoU > iou_threshold

Returns:
    Tensor: int64 tensor with the indices of the elements that have been kept
    by NMS, sorted in decreasing order of scores
)	torchjitis_scripting
is_tracingr   nmsr   opstorchvision)r   r   r   s      M/var/www/auris/envauris/lib/python3.13/site-packages/torchvision/ops/boxes.pyr   r      sT    2 99!!##EII,@,@,B,BC 99  $$UMBB    idxsc                 p   [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        U R                  5       U R                  R                  S:X  a  SOS:  a&  [        R                  " 5       (       d  [        XX#5      $ [        XX#5      $ )a  
Performs non-maximum suppression in a batched fashion.

Each index value correspond to a category, and NMS
will not be applied between elements of different categories.

Args:
    boxes (Tensor[N, 4]): boxes where NMS will be performed. They
        are expected to be in ``(x1, y1, x2, y2)`` format with ``0 <= x1 < x2`` and
        ``0 <= y1 < y2``.
    scores (Tensor[N]): scores for each one of the boxes
    idxs (Tensor[N]): indices of the categories for each one of the boxes.
    iou_threshold (float): discards all overlapping boxes with IoU > iou_threshold

Returns:
    Tensor: int64 tensor with the indices of the elements that have been kept by NMS, sorted
    in decreasing order of scores
cpui  i )r   r   r   r   r   batched_nmsnumeldevicetyper   _is_tracing_batched_nms_vanilla_batched_nms_coordinate_trick)r   r   r   r   s       r   r"   r"   3   s}    0 99!!##EII,@,@,B,BK( {{} 1 1U :HQ\QhQhQjQj#E4GG,UDPPr   c                 L   U R                  5       S:X  a.  [        R                  " S[        R                  U R                  S9$ U R                  5       nUR                  U 5      U[        R                  " S5      R                  U 5      -   -  nXS S 2S 4   -   n[        XaU5      nU$ )Nr   )r   dtyper$   r   )	r#   r   emptyint64r$   maxtotensorr   )r   r   r   r   max_coordinateoffsetsboxes_for_nmskeeps           r   r(   r(   V   s     {{}{{4u{{5<<HHYY[Nggena1C1CE1J JKGAtG,,M}m4DKr   c                 H   [         R                  " U[         R                  S9n[         R                  " U5       H6  n[         R                  " X%:H  5      S   n[        X   X   U5      nSXFU   '   M8     [         R                  " U5      S   nXU   R                  SS9S      $ )N)r+   r   T)
descendingr   )r   
zeros_likebooluniquewherer   sort)	r   r   r   r   	keep_maskclass_idcurr_indicescurr_keep_indiceskeep_indicess	            r   r'   r'   j   s       uzz:ILL&{{4#34Q7 3V5I=Y59	012 ' ;;y)!,L|,11T1B1EFFr   min_sizec                 >   [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        U SS2S4   U SS2S4   -
  U SS2S4   U SS2S4   -
  p2X!:  X1:  -  n[         R                  " U5      S   nU$ )a#  
Remove every box from ``boxes`` which contains at least one side length
that is smaller than ``min_size``.

.. note::
    For sanitizing a :class:`~torchvision.tv_tensors.BoundingBoxes` object, consider using
    the transform :func:`~torchvision.transforms.v2.SanitizeBoundingBoxes` instead.

Args:
    boxes (Tensor[N, 4]): boxes in ``(x1, y1, x2, y2)`` format
        with ``0 <= x1 < x2`` and ``0 <= y1 < y2``.
    min_size (float): minimum size

Returns:
    Tensor[K]: indices of the boxes that have both sides
    larger than ``min_size``
Nr   r      r   )r   r   r   r   r   remove_small_boxesr:   )r   rA   wshsr4   s        r   rD   rD   {   s    $ 99!!##EII,@,@,B,B./1a4[5A;&adeAqDk(ANr~.D;;tQDKr   sizec           	         [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        U R                  5       nU SSSS24   nU SSSS24   nUu  pV[        R                  " 5       (       a  [         R                  " U[         R                  " SU R                  U R                  S95      n[         R                  " U[         R                  " X`R                  U R                  S95      n[         R                  " U[         R                  " SU R                  U R                  S95      n[         R                  " U[         R                  " XPR                  U R                  S95      nO UR                  SUS9nUR                  SUS9n[         R                  " X44US9nUR!                  U R"                  5      $ )	a  
Clip boxes so that they lie inside an image of size ``size``.

.. note::
    For clipping a :class:`~torchvision.tv_tensors.BoundingBoxes` object, consider using
    the transform :func:`~torchvision.transforms.v2.ClampBoundingBoxes` instead.

Args:
    boxes (Tensor[N, 4]): boxes in ``(x1, y1, x2, y2)`` format
        with ``0 <= x1 < x2`` and ``0 <= y1 < y2``.
    size (Tuple[height, width]): size of the image

Returns:
    Tensor[N, 4]: clipped boxes
.r   Nr   r   r*   )minr.   )dim)r   r   r   r   r   clip_boxes_to_imagerJ   r   r&   r.   r0   r+   r$   rI   clampstackreshapeshape)r   rG   rJ   boxes_xboxes_yheightwidthclipped_boxess           r   rK   rK      s]     99!!##EII,@,@,B,B/0
))+CCAIGCAIGMF  ))GU\\!5;;u||%\]))GU\\%{{SXS_S_%`a))GU\\!5;;u||%\]))GU\\&TYT`T`%ab--A5-1--A6-2KK 2<M  --r   in_fmtout_fmtc                    [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        SnX;  d  X#;  a  [        SU SU 35      eX:X  a  U R                  5       $ X4nUS:X  a  [        U 5      n U $ US:X  a  [        U 5      n U $ US:X  a  [        U 5      n U $ US:X  a  [        U 5      n U $ US:X  a  [        U 5      n [        U 5      n U $ US	:X  a  [        U 5      n [        U 5      n U $ US
:X  a  [        U 5      n U $ US:X  a  [        U 5      n U $ US:X  a1  [        U 5      R                  U R                  5      n [!        U 5      n U $ US:X  a1  [#        U 5      R                  U R                  5      n [        U 5      n U $ US:X  a  [!        U 5      n U $ US:X  a  [#        U 5      n U $ [%        SUS    SUS    35      e)a  
Converts :class:`torch.Tensor` boxes from a given ``in_fmt`` to ``out_fmt``.

.. note::
    For converting a :class:`torch.Tensor` or a :class:`~torchvision.tv_tensors.BoundingBoxes` object
    between different formats,
    consider using :func:`~torchvision.transforms.v2.functional.convert_bounding_box_format` instead.
    Or see the corresponding transform :func:`~torchvision.transforms.v2.ConvertBoundingBoxFormat`.

Supported ``in_fmt`` and ``out_fmt`` strings are:

``'xyxy'``: boxes are represented via corners, x1, y1 being top left and x2, y2 being bottom right.
This is the format that torchvision utilities expect.

``'xywh'``: boxes are represented via corner, width and height, x1, y2 being top left, w, h being width and height.

``'cxcywh'``: boxes are represented via centre, width and height, cx, cy being center of box, w, h
being width and height.

``'xywhr'``: boxes are represented via corner, width and height, x1, y2 being top left, w, h being width and height.
r is rotation angle w.r.t to the box center by :math:`|r|` degrees counter clock wise in the image plan

``'cxcywhr'``: boxes are represented via centre, width and height, cx, cy being center of box, w, h
being width and height.
r is rotation angle w.r.t to the box center by :math:`|r|` degrees counter clock wise in the image plan

``'xyxyxyxy'``: boxes are represented via corners, x1, y1 being top left, x2, y2 top right,
x3, y3 bottom right, and x4, y4 bottom left.

Args:
    boxes (Tensor[N, K]): boxes which will be converted. K is the number of coordinates (4 for unrotated bounding boxes, 5 or 8 for rotated bounding boxes)
    in_fmt (str): Input format of given boxes. Supported formats are ['xyxy', 'xywh', 'cxcywh', 'xywhr', 'cxcywhr', 'xyxyxyxy'].
    out_fmt (str): Output format of given boxes. Supported formats are ['xyxy', 'xywh', 'cxcywh', 'xywhr', 'cxcywhr', 'xyxyxyxy']

Returns:
    Tensor[N, K]: Boxes into converted format.
)xyxyxywhcxcywhxywhrcxcywhrxyxyxyxyz6Unsupported Bounding Box Conversions for given in_fmt z and out_fmt )rY   rX   )rZ   rX   )rX   rY   )rX   rZ   )rY   rZ   )rZ   rY   )r\   r[   )r[   r\   )r\   r]   )r]   r\   )r[   r]   )r]   r[   r   r   )r   r   r   r   r   box_convert
ValueErrorcloner
   r   r   r   r	   r   r/   r+   r   r   NotImplementedError)r   rU   rV   allowed_fmtses        r   r^   r^      s>   L 99!!##EII,@,@,B,BK(L !W%@QRXQYYfgnfopqq{{}	A!%(< L; 
 	 #E*8 L7 
	!%(4 L3 
 	 #E*0 L/ 
 	 !%(#E** L) 
 	 #E*!%($ L# 
"	"%e,  L 
"	"%e, L 
%	%%e,//<&u- L 
%	%&u-00=%e, L 
#	#&u- L 
#	#&u- L "$Z[\]^[_Z``mnopqnrms"tuur   c                    [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        [        U 5      n U SS2S4   U SS2S4   -
  U SS2S4   U SS2S4   -
  -  $ )aT  
Computes the area of a set of bounding boxes, which are specified by their
(x1, y1, x2, y2) coordinates.

Args:
    boxes (Tensor[N, 4]): boxes for which the area will be computed. They
        are expected to be in (x1, y1, x2, y2) format with
        ``0 <= x1 < x2`` and ``0 <= y1 < y2``.

Returns:
    Tensor[N]: the area for each box
Nr   r   rC   r   )r   r   r   r   r   box_arear   )r   s    r   re   re     so     99!!##EII,@,@,B,BH%ENE!Q$K%1+%%1+ad*CDDr   boxes1boxes2c                 `   [        U 5      n[        U5      n[        R                  " U S S 2S S S24   US S 2S S24   5      n[        R                  " U S S 2S SS 24   US S 2SS 24   5      n[	        XT-
  5      R                  SS9nUS S 2S S 2S4   US S 2S S 2S4   -  nUS S 2S 4   U-   U-
  nXx4$ )Nr   r   rI   r   )re   r   r.   rI   r   rL   )	rf   rg   area1area2ltrbwhinterunions	            r   _box_inter_unionrq   &  s    VEVE	6!T2A2+&q"1"u	6B	6!T12+&q!"u	6B				A		&Bq!QwK"Q1W+%E!T'NU"U*E<r   c                     [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        [        X5      u  p#X#-  nU$ )a  
Return intersection-over-union (Jaccard index) between two sets of boxes.

Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with
``0 <= x1 < x2`` and ``0 <= y1 < y2``.

Args:
    boxes1 (Tensor[N, 4]): first set of boxes
    boxes2 (Tensor[M, 4]): second set of boxes

Returns:
    Tensor[N, M]: the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2
)r   r   r   r   r   box_iourq   )rf   rg   ro   rp   ious        r   rs   rs   5  sI     99!!##EII,@,@,B,BG$#F3LE
-CJr   c                    [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        [        X5      u  p#X#-  n[         R                  " U SS2SSS24   USS2SS24   5      n[         R                  " U SS2SSS24   USS2SS24   5      n[        Xe-
  5      R                  SS9nUSS2SS2S4   USS2SS2S4   -  nXHU-
  U-  -
  $ )a  
Return generalized intersection-over-union (Jaccard index) between two sets of boxes.

Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with
``0 <= x1 < x2`` and ``0 <= y1 < y2``.

Args:
    boxes1 (Tensor[N, 4]): first set of boxes
    boxes2 (Tensor[M, 4]): second set of boxes

Returns:
    Tensor[N, M]: the NxM matrix containing the pairwise generalized IoU values
    for every element in boxes1 and boxes2
Nr   r   ri   r   )r   r   r   r   r   generalized_box_iourq   rI   r.   r   rL   )	rf   rg   ro   rp   rt   ltirbiwhiareais	            r   rv   rv   K  s     99!!##EII,@,@,B,B/0#F3LE
-C
))F1dBQB;'2A2
7C
))F1dAB;'12
7C
#)

"
"q
"
)C1aL3q!Qw<'E%-5(((r   epsc                    [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        [        U 5      n [        U5      n[        XU5      u  p4U SS2SS4   U SS2SS4   -
  nU SS2SS4   U SS2SS4   -
  nUSS2S4   USS2S4   -
  nUSS2S4   USS2S4   -
  nS[         R                  S-  -  [         R                  " [         R                  " XV-  5      [         R                  " Xx-  5      -
  S5      -  n	[         R                  " 5          U	SU-
  U	-   U-   -  n
SSS5        UW
U	-  -
  $ ! , (       d  f       N= f)a  
Return complete intersection-over-union (Jaccard index) between two sets of boxes.
Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with
``0 <= x1 < x2`` and ``0 <= y1 < y2``.
Args:
    boxes1 (Tensor[N, 4]): first set of boxes
    boxes2 (Tensor[M, 4]): second set of boxes
    eps (float, optional): small number to prevent division by zero. Default: 1e-7
Returns:
    Tensor[N, M]: the NxM matrix containing the pairwise complete IoU values
    for every element in boxes1 and boxes2
Nr   r   rC   r      )r   r   r   r   r   complete_box_iour   _box_diou_ioupipowatanno_grad)rf   rg   r{   diourt   w_predh_predw_gth_gtvalphas              r   r~   r~   i  sB    99!!##EII,@,@,B,B,-V_FV_Ffc2IDAtQJ&D!"44FAtQJ&D!"44F!Q$<&A,&D!Q$<&A,&D	
ehhk	eii

6?(CejjQUQ\F](]_`aaA	QWq[3&' 
%!) 
s   <E
E)c                     [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        [        U 5      n [        U5      n[        XUS9u  p4U$ )a  
Return distance intersection-over-union (Jaccard index) between two sets of boxes.

Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with
``0 <= x1 < x2`` and ``0 <= y1 < y2``.

Args:
    boxes1 (Tensor[N, 4]): first set of boxes
    boxes2 (Tensor[M, 4]): second set of boxes
    eps (float, optional): small number to prevent division by zero. Default: 1e-7

Returns:
    Tensor[N, M]: the NxM matrix containing the pairwise distance IoU values
    for every element in boxes1 and boxes2
)r{   )r   r   r   r   r   distance_box_iour   r   )rf   rg   r{   r   _s        r   r   r     sW      99!!##EII,@,@,B,B,-V_FV_FF4GDKr   c                 x   [        X5      n[        R                  " U S S 2S S S24   US S 2S S24   5      n[        R                  " U S S 2S SS 24   US S 2SS 24   5      n[	        XT-
  5      R                  SS9nUS S 2S S 2S4   S-  US S 2S S 2S4   S-  -   U-   nU S S 2S4   U S S 2S4   -   S-  nU S S 2S4   U S S 2S4   -   S-  n	US S 2S4   US S 2S4   -   S-  n
US S 2S4   US S 2S4   -   S-  n[	        US S 2S 4   U
S S S 24   -
  5      S-  [	        U	S S 2S 4   US S S 24   -
  5      S-  -   nX<U-  -
  U4$ )Nr   r   ri   r   rC   )rs   r   rI   r.   r   rL   )rf   rg   r{   rt   rw   rx   ry   diagonal_distance_squaredx_py_px_gy_gcenters_distance_squareds                r   r   r     s   
&
!C
))F1dBQB;'2A2
7C
))F1dAB;'12
7C
#)

"
"q
"
)C!$Q1W!2s1a7|q7H IC O!Q$<&A,&!
+C!Q$<&A,&!
+C!Q$<&A,&!
+C!Q$<&A,&!
+C 'AtGs47|(C D IgVYZ[]aZaVbehimopipeqVqNrvwNwx -FFGLLr   masksc                    [         R                  R                  5       (       d2  [         R                  R                  5       (       d  [	        [
        5        U R                  5       S:X  a.  [         R                  " SU R                  [         R                  S9$ U R                  S   n[         R                  " US4U R                  [         R                  S9n[        U 5       H  u  p4[         R                  " US:g  5      u  pV[         R                  " U5      X#S4'   [         R                  " U5      X#S4'   [         R                  " U5      X#S4'   [         R                  " U5      X#S4'   M     U$ )aV  
Compute the bounding boxes around the provided masks.

Returns a [N, 4] tensor containing bounding boxes. The boxes are in ``(x1, y1, x2, y2)`` format with
``0 <= x1 <= x2`` and ``0 <= y1 <= y2``.

.. warning::

    In most cases the output will guarantee ``x1 < x2`` and ``y1 < y2``. But
    if the input is degenerate, e.g. if a mask is a single row or a single
    column, then the output may have x1 = x2 or y1 = y2.

Args:
    masks (Tensor[N, H, W]): masks to transform where N is the number of masks
        and (H, W) are the spatial dimensions.

Returns:
    Tensor[N, 4]: bounding boxes
r   )r   r}   )r$   r+   r}   r   r   rC   )r   r   r   r   r   masks_to_boxesr#   zerosr$   floatrO   	enumerater:   rI   r.   )r   nbounding_boxesindexmaskyxs          r   r   r     s   ( 99!!##EII,@,@,B,BN+{{}{{6%,,ekkJJAA[[!QEKKPN '{{419%#(99Q<ax #(99Q<ax #(99Q<ax #(99Q<ax  ( r   )gHz>)'r   r   r   torchvision.extensionr   utilsr   _box_convertr   r	   r
   r   r   r   r   r   _utilsr   r   r   r"   r   _script_if_tracingr(   r'   rD   tupleintrK   strr^   re   rq   rs   rv   r~   r   r   r    r   r   <module>r      s      1 '	 	 	 Cv Cv Ce C C> Q Q Q  Q 	 Q
  QF   	
  & GGG G 	G
 G G f  & 4!.v !.U38_ !. !.HUv Us US UV UpEF Ev E*V V ffn8M F F v ,) ) )6 )<V V % 6 BV V % 6 2M& M& Mu MfV\nH] M&%%,, %5<< %r   