§
    ezniAz  ã                   ó|  — d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlm	c m
Z d dlZd dlmZ  G d„ de j        ¦  «        Zd0d„Zd1d„Zd	„ Z	 	 	 	 	 	 	 	 	 	 	 d2d„Zd„ Zd„ Zd3d„Zd„ Zd„ Zd4d„Zd5d„Zd„ Zd„ Zd„ Zd„ Zd„ Z d „ Z!d!„ Z"d6d#„Z#d$„ Z$d%„ Z%d7d&„Z&d'„ Z'd8d(„Z(d9d)„Z)d:d+„Z*d,ej+        d-ej,        fd.„Z-d/„ Z.dS );é    N)ÚLOGGERc                   ó2   — e Zd ZdZd	d„Zd„ Zd„ Zd„ Zd„ ZdS )
ÚProfilea\  
    YOLOv8 Profile class. Use as a decorator with @Profile() or as a context manager with 'with Profile():'.

    Example:
        ```python
        from ultralytics.utils.ops import Profile

        with Profile() as dt:
            pass  # slow operation here

        print(dt)  # prints "Elapsed time is 9.5367431640625e-07 s"
        ```
    ç        c                 óZ   — || _         t          j                             ¦   «         | _        dS )zt
        Initialize the Profile class.

        Args:
            t (float): Initial time. Defaults to 0.0.
        N)ÚtÚtorchÚcudaÚis_available)Úselfr   s     úu/var/www/html/cool-property-finder/image-detection-backend/venv/lib/python3.11/site-packages/ultralytics/utils/ops.pyÚ__init__zProfile.__init__    s$   € ð ˆŒÝ”J×+Ò+Ñ-Ô-ˆŒ	ˆ	ˆ	ó    c                 ó8   — |                       ¦   «         | _        | S )zStart timing.)ÚtimeÚstart©r   s    r   Ú	__enter__zProfile.__enter__*   s   € à—Y’Y‘[”[ˆŒ
Øˆr   c                 ór   — |                       ¦   «         | j        z
  | _        | xj        | j        z  c_        dS )zStop timing.N)r   r   Údtr   )r   ÚtypeÚvalueÚ	tracebacks       r   Ú__exit__zProfile.__exit__/   s.   € à—)’)‘+”+ ¤
Ñ*ˆŒØˆŒ�$”'ÑˆŒˆˆr   c                 ó   — d| j         › d�S )zZReturns a human-readable string representing the accumulated elapsed time in the profiler.zElapsed time is z s)r   r   s    r   Ú__str__zProfile.__str__4   s   € à, $¤&Ð,Ð,Ð,Ð,r   c                 ór   — | j         rt          j                              ¦   «          t          j        ¦   «         S )zGet current time.)r
   r	   Úsynchronizer   r   s    r   r   zProfile.time8   s,   € àŒ9ð 	%ÝŒJ×"Ò"Ñ$Ô$Ð$ÝŒy‰{Œ{Ðr   N)r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   © r   r   r   r      sn   € € € € € ðð ð.ð .ð .ð .ðð ð ð
ð ð ð
-ð -ð -ðð ð ð ð r   r   é€  c                 óŽ  — | j         \  }}|dk    |dk    z  ||k    z  ||k    z  }||         ||         }}t          |¦  «        rgt          j        |                     ¦   «         |                     ¦   «         |                     ¦   «         |                     ¦   «         g| j        ¬¦  «        nt          j        d| j        ¬¦  «        S )a‡  
    Convert 1 segment label to 1 box label, applying inside-image constraint, i.e. (xy1, xy2, ...) to (xyxy).

    Args:
        segment (torch.Tensor): the segment label
        width (int): the width of the image. Defaults to 640
        height (int): The height of the image. Defaults to 640

    Returns:
        (np.ndarray): the minimum and maximum x and y values of the segment.
    r   ©Údtypeé   )ÚTÚanyÚnpÚarrayÚminÚmaxr'   Úzeros)ÚsegmentÚwidthÚheightÚxÚyÚinsides         r   Úsegment2boxr6   ?   s¸   € ð Œ9�D€A€qØ�1Šf˜˜ašÑ  A¨¢JÑ/°1¸²;Ñ?€FØˆfŒI�q˜”y€q€AÝRUÐVWÑRXÔRXð  �2Œ8�Q—U’U‘W”W˜aŸeše™gœg q§u¢u¡w¤w°·²±´Ð8ÀÄÐNÑNÔNÐNÕ^`Ô^fØ	�”ð_ ñ _ ô _ ð  r   Tc                 óà  — |€}t          | d         |d         z  | d         |d         z  ¦  «        }t          | d         |d         |z  z
  dz  dz
  ¦  «        t          | d         |d         |z  z
  dz  dz
  ¦  «        f}n|d         d         }|d         }|r4|dddgfxx         |d         z  cc<   |dddgfxx         |d         z  cc<   |ddd…fxx         |z  cc<   t          ||¦  «        S )	a¿  
    Rescales bounding boxes (in the format of xyxy) from the shape of the image they were originally specified in
    (img1_shape) to the shape of a different image (img0_shape).

    Args:
        img1_shape (tuple): The shape of the image that the bounding boxes are for, in the format of (height, width).
        boxes (torch.Tensor): the bounding boxes of the objects in the image, in the format of (x1, y1, x2, y2)
        img0_shape (tuple): the shape of the target image, in the format of (height, width).
        ratio_pad (tuple): a tuple of (ratio, pad) for scaling the boxes. If not provided, the ratio and pad will be
            calculated based on the size difference between the two images.
        padding (bool): If True, assuming the boxes is based on image augmented by yolo style. If False then do regular
            rescaling.

    Returns:
        boxes (torch.Tensor): The scaled bounding boxes, in the format of (x1, y1, x2, y2)
    Nr   é   é   çš™™™™™¹?.é   r(   )r-   ÚroundÚ
clip_boxes)Ú
img1_shapeÚboxesÚ
img0_shapeÚ	ratio_padÚpaddingÚgainÚpads          r   Úscale_boxesrE   S   s.  € ð" ÐÝ�:˜a”= :¨a¤=Ñ0°*¸Q´-À*ÈQÄ-Ñ2OÑPÔPˆÝ�Z ”] Z°¤]°TÑ%9Ñ9¸QÑ>ÀÑDÑEÔEÅuØ˜Œ]˜Z¨œ]¨TÑ1Ñ1°QÑ6¸Ñ<ñH>ô H>ð >ˆˆð ˜Œ|˜AŒˆØ˜Œlˆàð %Øˆc�A�q�6ˆkÐÐÔ˜c !œfÑ$ÐÐÑØˆc�A�q�6ˆkÐÐÔ˜c !œfÑ$ÐÐÑØ	ˆ#ˆr�ˆrˆ'€N€N„N�dÑ€N€N�NÝ�e˜ZÑ(Ô(Ð(r   c                 ó¬   — t          |t          j        ¦  «        r!t          |                     ¦   «         ¦  «        }t          j        | |z  ¦  «        |z  S )a  
    Returns the nearest number that is divisible by the given divisor.

    Args:
        x (int): The number to make divisible.
        divisor (int | torch.Tensor): The divisor.

    Returns:
        (int): The nearest number divisible by the divisor.
    )Ú
isinstancer	   ÚTensorÚintr.   ÚmathÚceil)r3   Údivisors     r   Úmake_divisiblerM   s   sF   € õ �'�5œ<Ñ(Ô(ð %Ý�g—k’k‘m”mÑ$Ô$ˆÝŒ9�Q˜‘[Ñ!Ô! GÑ+Ð+r   ç      Ð?çÍÌÌÌÌÌÜ?Fr#   é,  çš™™™™™©?é0u  é   c                 ó”  — d|cxk    rdk    sn J d|› d�¦   «         ‚d|cxk    rdk    sn J d|› d�¦   «         ‚t          | t          t          f¦  «        r| d         } | j        d         }|p| j        d         dz
  }| j        d         |z
  dz
  }d|z   }| dd…d|…f                              d¦  «        |k    }d|	|z  z   }||dk    z  }|                      d	d
¦  «        } t          | ddd…f         ¦  «        | ddd…f<   t          j        ¦   «         }t          j	        dd|z   f| j
        ¬¦  «        g|z  }t          | ¦  «        D �]þ\  }}|||                  }|rÈt          ||         ¦  «        r³||         }t          j	        t          |¦  «        ||z   dz   f|j
        ¬¦  «        }t          |dd…dd…f         ¦  «        |dd…dd…f<   d|t          t          |¦  «        ¦  «        |dd…df                              ¦   «         dz   f<   t          j        ||fd¦  «        }|j        d         sŒì|                     d||fd¦  «        \  }}}|rit          j        ||k    ¦  «        \  }}t          j        ||         ||d|z   df         |dd…df                              ¦   «         ||         fd¦  «        }nb|                     dd¬¦  «        \  }}t          j        |||                     ¦   «         |fd¦  «        |                     d	¦  «        |k             }|�D||dd…dd…f         t          j        ||j
        ¬¦  «        k                         d¦  «                 }|j        d         }|s�Œ-||
k    r.||dd…df                              d¬¦  «        d|
…                  }|dd…dd…f         |rdn|z  }|dd…dd…f         |z   |dd…df         } }t0          j                             || |¦  «        }|d|…         }||         ||<   t          j        ¦   «         |z
  |k    rt7          j        d|d›d�¦  «          n�Œ |S )a¢  
    Perform non-maximum suppression (NMS) on a set of boxes, with support for masks and multiple labels per box.

    Args:
        prediction (torch.Tensor): A tensor of shape (batch_size, num_classes + 4 + num_masks, num_boxes)
            containing the predicted boxes, classes, and masks. The tensor should be in the format
            output by a model, such as YOLO.
        conf_thres (float): The confidence threshold below which boxes will be filtered out.
            Valid values are between 0.0 and 1.0.
        iou_thres (float): The IoU threshold below which boxes will be filtered out during NMS.
            Valid values are between 0.0 and 1.0.
        classes (List[int]): A list of class indices to consider. If None, all classes will be considered.
        agnostic (bool): If True, the model is agnostic to the number of classes, and all
            classes will be considered as one.
        multi_label (bool): If True, each box may have multiple labels.
        labels (List[List[Union[int, float, torch.Tensor]]]): A list of lists, where each inner
            list contains the apriori labels for a given image. The list should be in the format
            output by a dataloader, with each label being a tuple of (class_index, x1, y1, x2, y2).
        max_det (int): The maximum number of boxes to keep after NMS.
        nc (int, optional): The number of classes output by the model. Any indices after this will be considered masks.
        max_time_img (float): The maximum time (seconds) for processing one image.
        max_nms (int): The maximum number of boxes into torchvision.ops.nms().
        max_wh (int): The maximum box width and height in pixels

    Returns:
        (List[torch.Tensor]): A list of length batch_size, where each element is a tensor of
            shape (num_boxes, 6 + num_masks) containing the kept boxes, with columns
            (x1, y1, x2, y2, confidence, class, mask1, mask2, ...).
    r   r8   zInvalid Confidence threshold z&, valid values are between 0.0 and 1.0zInvalid IoU r(   Nç      à?éÿÿÿÿéþÿÿÿ.é   )Údeviceé   g      ð?T)Úkeepdim)Ú
descendingu   WARNING âš ï¸� NMS time limit z.3fz
s exceeded)rG   ÚlistÚtupleÚshapeÚamaxÚ	transposeÚ	xywh2xyxyr   r	   r/   rY   Ú	enumerateÚlenÚrangeÚlongÚcatÚsplitÚwhereÚfloatr.   ÚviewÚtensorr*   ÚargsortÚtorchvisionÚopsÚnmsr   Úwarning)!Ú
predictionÚ
conf_thresÚ	iou_thresÚclassesÚagnosticÚmulti_labelÚlabelsÚmax_detÚncÚmax_time_imgÚmax_nmsÚmax_whÚbsÚnmÚmiÚxcÚ
time_limitr   ÚoutputÚxir3   ÚlbÚvÚboxÚclsÚmaskÚiÚjÚconfÚnÚcr?   Úscoress!                                    r   Únon_max_suppressionr�   ƒ   sÉ  € ðZ �
ÐÐÒÐ˜aÒÐÐÐÐÐ!sÀÐ!sÐ!sÐ!sÑÔÐØ�	ÐÐÒÐ˜QÒÐÐÐÐÐ `¨yÐ `Ð `Ð `ÑÔÐÝ�*�t¥U˜mÑ,Ô,ð #Ø ”]ˆ
à	Ô	˜!Ô	€BØ	Ð	(�
Ô  Ô# aÑ'€BØ	Ô	˜!Ô	˜rÑ	! AÑ	%€BØ	
ˆR‰€BØ	�A�A�A�q˜�t�GÔ	×	!Ò	! !Ñ	$Ô	$ zÒ	1€Bð �| bÑ(Ñ(€JØ�2˜’6Ñ€Kà×%Ò% b¨"Ñ-Ô-€JÝ# J¨s°B°Q°B¨wÔ$7Ñ8Ô8€Jˆs�B�Q�BˆwÑåŒ	‰Œ€AÝŒk˜1˜a "™f˜+¨jÔ.?Ð@Ñ@Ô@ÐAÀBÑF€FÝ˜:Ñ&Ô&ð ;ñ ;‰ˆˆAð ˆb�ŒfŒIˆð ð 	%•c˜& œ*‘o”oð 	%Ø˜”ˆBÝ”�S ™WœW b¨2¡g°¡kÐ2¸1¼8ÐDÑDÔDˆAÝ   A A A q¨ s F¤Ñ,Ô,ˆAˆaˆaˆa��!�ˆe‰HØ58ˆA�e•C˜‘G”G‰nŒn˜b    A œhŸmšm™oœo°Ñ1Ð1Ñ2Ý”	˜1˜a˜& !Ñ$Ô$ˆAð Œw�qŒzð 	Øð Ÿš ! R¨ ¨aÑ0Ô0‰ˆˆS�$àð 	WÝ”;˜s ZÒ/Ñ0Ô0‰DˆAˆqÝ”	˜3˜qœ6 1 Q¨¨A©¨t ^Ô#4°a¸¸¸¸4¸´j×6FÒ6FÑ6HÔ6HÈ$ÈqÌ'ÐRÐTUÑVÔVˆAˆAà—g’g˜a¨�gÑ.Ô.‰GˆD�!Ý”	˜3  a§g¢g¡i¤i°Ð6¸Ñ:Ô:¸4¿9º9ÀR¹=¼=È:Ò;UÔVˆAð ÐØ�1�Q�Q�Q˜˜!˜�V”9¥¤¨W¸Q¼XÐ FÑ FÔ FÒF×KÒKÈAÑNÔNÔOˆAð ŒG�AŒJˆØð 	ÙØˆwŠ;ˆ;Ø�!�A�A�A�q�D”'—/’/¨T�/Ñ2Ô2°8°G°8Ô<Ô=ˆAð ˆaˆaˆa��1�ˆfŒI˜hÐ2˜˜¨FÑ3ˆØ˜!˜!˜!˜R˜a˜R˜%œ 1™ a¨¨¨¨1¨¤gˆvˆÝŒO×Ò  v¨yÑ9Ô9ˆØˆhˆwˆhŒKˆð �q”Tˆˆr‰
ÝŒI‰KŒK˜!‰O˜zÒ)Ð)ÝŒNÐV¸JÐVÐVÐVÐVÑWÔWÐWØˆEñ *ð €Mr   c                 ó  — t          | t          j        ¦  «        r•| d                              d|d         ¦  «        | d<   | d                              d|d         ¦  «        | d<   | d                              d|d         ¦  «        | d<   | d                              d|d         ¦  «        | d<   nZ| dddgf                              d|d         ¦  «        | dddgf<   | ddd	gf                              d|d         ¦  «        | ddd	gf<   | S )
a%  
    Takes a list of bounding boxes and a shape (height, width) and clips the bounding boxes to the shape.

    Args:
        boxes (torch.Tensor): the bounding boxes to clip
        shape (tuple): the shape of the image

    Returns:
        (torch.Tensor | numpy.ndarray): Clipped boxes
    ©.r   r   r8   ©.r8   ©.r9   ©.r;   .r9   r;   ©rG   r	   rH   ÚclampÚclip)r?   r_   s     r   r=   r=     s
  € õ �%�œÑ&Ô&ð BØ˜fœ×+Ò+¨A¨u°Q¬xÑ8Ô8ˆˆf‰Ø˜fœ×+Ò+¨A¨u°Q¬xÑ8Ô8ˆˆf‰Ø˜fœ×+Ò+¨A¨u°Q¬xÑ8Ô8ˆˆf‰Ø˜fœ×+Ò+¨A¨u°Q¬xÑ8Ô8ˆˆf‰ˆà" 3¨¨A¨ ;Ô/×4Ò4°Q¸¸a¼ÑAÔAˆˆc�A�q�6ˆkÑØ" 3¨¨A¨ ;Ô/×4Ò4°Q¸¸a¼ÑAÔAˆˆc�A�q�6ˆkÑØ€Lr   c                 ód  — t          | t          j        ¦  «        rK| d                              d|d         ¦  «        | d<   | d                              d|d         ¦  «        | d<   nJ| d                              d|d         ¦  «        | d<   | d                              d|d         ¦  «        | d<   | S )aE  
    Clip line coordinates to the image boundaries.

    Args:
        coords (torch.Tensor | numpy.ndarray): A list of line coordinates.
        shape (tuple): A tuple of integers representing the size of the image in the format (height, width).

    Returns:
        (torch.Tensor | numpy.ndarray): Clipped coordinates
    r’   r   r8   r“   r–   )Úcoordsr_   s     r   Úclip_coordsr›     s£   € õ �&�%œ,Ñ'Ô'ð :Ø œ×-Ò-¨a°°q´Ñ:Ô:ˆˆv‰Ø œ×-Ò-¨a°°q´Ñ:Ô:ˆˆv‰ˆà œ×,Ò,¨Q°°a´Ñ9Ô9ˆˆv‰Ø œ×,Ò,¨Q°°a´Ñ9Ô9ˆˆv‰Ø€Mr   c                 ó¢  — | j         }|dd…         |dd…         k    r| S |€]t          |d         |d         z  |d         |d         z  ¦  «        }|d         |d         |z  z
  dz  |d         |d         |z  z
  dz  f}n|d         d         }|d         }t          t          |d         dz
  ¦  «        ¦  «        t          t          |d         dz
  ¦  «        ¦  «        }}t          t          |d         |d         z
  dz   ¦  «        ¦  «        t          t          |d         |d         z
  dz   ¦  «        ¦  «        }	}t	          | j         ¦  «        dk     r$t          dt	          | j         ¦  «        › �¦  «        ‚| ||…||	…f         } t          j        | |d         |d         f¦  «        } t	          | j         ¦  «        dk    r| dd…dd…df         } | S )ao  
    Takes a mask, and resizes it to the original image size.

    Args:
        masks (np.ndarray): resized and padded masks/images, [h, w, num]/[h, w, 3].
        im0_shape (tuple): the original image shape
        ratio_pad (tuple): the ratio of the padding to the original image.

    Returns:
        masks (torch.Tensor): The masks that are being returned.
    Nr9   r   r8   r:   z/"len of masks shape" should be 2 or 3, but got )r_   r-   rI   r<   rd   Ú
ValueErrorÚcv2Úresize)
ÚmasksÚ	im0_shaperA   Ú	im1_shaperC   rD   ÚtopÚleftÚbottomÚrights
             r   Úscale_imager§   /  sÕ  € ð ”€IØ��!�„}˜	 " 1 "œÒ%Ð%ØˆØÐÝ�9˜Q”< )¨A¤,Ñ.°	¸!´¸yÈ¼|Ñ0KÑLÔLˆØ˜Œ|˜i¨œl¨TÑ1Ñ1°QÑ6¸À1¼È	ÐRSÌÐW[ÑH[Ñ9[Ð_`Ñ8`Ð`ˆˆà˜Œ|˜AŒˆØ˜ŒlˆÝ•U˜3˜qœ6 C™<Ñ(Ô(Ñ)Ô)­3­u°S¸´V¸c±\Ñ/BÔ/BÑ+CÔ+Cˆ€CÝ�˜y¨œ|¨c°!¬fÑ4°sÑ:Ñ;Ô;Ñ<Ô<½cÅ%È	ÐRSÌÐWZÐ[\ÔW]ÑH]Ð`cÑHcÑBdÔBdÑ>eÔ>eˆE€Få
ˆ5Œ;ÑÔ˜!ÒÐÝÐ]Í3ÈuÌ{ÑK[ÔK[Ð]Ð]Ñ^Ô^Ð^Ø�#�f�*˜d 5˜jÐ(Ô)€EÝŒJ�u˜y¨œ|¨Y°q¬\Ð:Ñ;Ô;€EÝ
ˆ5Œ;ÑÔ˜1ÒÐØ�a�a�a˜˜˜˜D�jÔ!ˆà€Lr   c                 ó|  — | j         d         dk    sJ d| j         › �¦   «         ‚t          | t          j        ¦  «        rt          j        | ¦  «        nt          j        | ¦  «        }| d         | d         z   dz  |d<   | d         | d         z   dz  |d<   | d         | d         z
  |d<   | d         | d         z
  |d<   |S )	až  
    Convert bounding box coordinates from (x1, y1, x2, y2) format to (x, y, width, height) format where (x1, y1) is the
    top-left corner and (x2, y2) is the bottom-right corner.

    Args:
        x (np.ndarray | torch.Tensor): The input bounding box coordinates in (x1, y1, x2, y2) format.

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in (x, y, width, height) format.
    rV   r(   ú9input shape last dimension expected 4 but input shape is r’   r”   r9   r“   r•   ©r_   rG   r	   rH   Ú
empty_liker+   ©r3   r4   s     r   Ú	xyxy2xywhr­   R  sÇ   € ð Œ7�2Œ;˜!ÒÐÐÐbÐYZÔY`ÐbÐbÑÔÐÝ)¨!­U¬\Ñ:Ô:ÐP�Ô˜ÑÔÐÅÄÈaÑ@PÔ@P€AØ�6”˜Q˜vœYÑ&¨!Ñ+€A€f�IØ�6”˜Q˜vœYÑ&¨!Ñ+€A€f�IØ�&”	˜A˜fœIÑ%€A€f�IØ�&”	˜A˜fœIÑ%€A€f�IØ€Hr   c                 ól  — | j         d         dk    sJ d| j         › �¦   «         ‚t          | t          j        ¦  «        rt          j        | ¦  «        nt          j        | ¦  «        }| d         dz  }| d         dz  }| d         |z
  |d<   | d         |z
  |d<   | d         |z   |d<   | d         |z   |d<   |S )	až  
    Convert bounding box coordinates from (x, y, width, height) format to (x1, y1, x2, y2) format where (x1, y1) is the
    top-left corner and (x2, y2) is the bottom-right corner.

    Args:
        x (np.ndarray | torch.Tensor): The input bounding box coordinates in (x, y, width, height) format.

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in (x1, y1, x2, y2) format.
    rV   r(   r©   r”   r9   r•   r’   r“   rª   )r3   r4   ÚdwÚdhs       r   rb   rb   f  sÃ   € ð Œ7�2Œ;˜!ÒÐÐÐbÐYZÔY`ÐbÐbÑÔÐÝ)¨!­U¬\Ñ:Ô:ÐP�Ô˜ÑÔÐÅÄÈaÑ@PÔ@P€AØ	
ˆ6Œ�Q‰€BØ	
ˆ6Œ�Q‰€BØ�&”	˜B‘€A€f�IØ�&”	˜B‘€A€f�IØ�&”	˜B‘€A€f�IØ�&”	˜B‘€A€f�IØ€Hr   c                 ó¸  — | j         d         dk    sJ d| j         › �¦   «         ‚t          | t          j        ¦  «        rt          j        | ¦  «        nt          j        | ¦  «        }|| d         | d         dz  z
  z  |z   |d<   || d         | d         dz  z
  z  |z   |d<   || d         | d         dz  z   z  |z   |d<   || d         | d         dz  z   z  |z   |d<   |S )	aF  
    Convert normalized bounding box coordinates to pixel coordinates.

    Args:
        x (np.ndarray | torch.Tensor): The bounding box coordinates.
        w (int): Width of the image. Defaults to 640
        h (int): Height of the image. Defaults to 640
        padw (int): Padding width. Defaults to 0
        padh (int): Padding height. Defaults to 0
    Returns:
        y (np.ndarray | torch.Tensor): The coordinates of the bounding box in the format [x1, y1, x2, y2] where
            x1,y1 is the top-left corner, x2,y2 is the bottom-right corner of the bounding box.
    rV   r(   r©   r’   r”   r9   r“   r•   rª   )r3   ÚwÚhÚpadwÚpadhr4   s         r   Ú
xywhn2xyxyr¶   |  sõ   € ð Œ7�2Œ;˜!ÒÐÐÐbÐYZÔY`ÐbÐbÑÔÐÝ)¨!­U¬\Ñ:Ô:ÐP�Ô˜ÑÔÐÅÄÈaÑ@PÔ@P€AØ�Q�v”Y  6¤¨Q¡Ñ.Ñ/°$Ñ6€A€f�IØ�Q�v”Y  6¤¨Q¡Ñ.Ñ/°$Ñ6€A€f�IØ�Q�v”Y  6¤¨Q¡Ñ.Ñ/°$Ñ6€A€f�IØ�Q�v”Y  6¤¨Q¡Ñ.Ñ/°$Ñ6€A€f�IØ€Hr   r   c                 óÈ  — |rt          | ||z
  ||z
  f¦  «        } | j        d         dk    sJ d| j        › �¦   «         ‚t          | t          j        ¦  «        rt          j        | ¦  «        nt          j        | ¦  «        }| d         | d         z   dz  |z  |d<   | d         | d         z   dz  |z  |d<   | d         | d         z
  |z  |d<   | d         | d         z
  |z  |d<   |S )	aÎ  
    Convert bounding box coordinates from (x1, y1, x2, y2) format to (x, y, width, height, normalized) format. x, y,
    width and height are normalized to image dimensions.

    Args:
        x (np.ndarray | torch.Tensor): The input bounding box coordinates in (x1, y1, x2, y2) format.
        w (int): The width of the image. Defaults to 640
        h (int): The height of the image. Defaults to 640
        clip (bool): If True, the boxes will be clipped to the image boundaries. Defaults to False
        eps (float): The minimum value of the box's width and height. Defaults to 0.0

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in (x, y, width, height, normalized) format
    rV   r(   r©   r’   r”   r9   r“   r•   )r=   r_   rG   r	   rH   r«   r+   )r3   r²   r³   r˜   Úepsr4   s         r   Ú
xyxy2xywhnr¹   “  sÿ   € ð ð .Ý�q˜1˜s™7 A¨¡GÐ,Ñ-Ô-ˆØŒ7�2Œ;˜!ÒÐÐÐbÐYZÔY`ÐbÐbÑÔÐÝ)¨!­U¬\Ñ:Ô:ÐP�Ô˜ÑÔÐÅÄÈaÑ@PÔ@P€AØ�F”)˜a œiÑ'¨1Ñ,°Ñ1€A€f�IØ�F”)˜a œiÑ'¨1Ñ,°Ñ1€A€f�IØ�6”˜Q˜vœYÑ&¨!Ñ+€A€f�IØ�6”˜Q˜vœYÑ&¨!Ñ+€A€f�IØ€Hr   c                 óæ   — t          | t          j        ¦  «        r|                      ¦   «         nt	          j        | ¦  «        }| d         | d         dz  z
  |d<   | d         | d         dz  z
  |d<   |S )aX  
    Convert the bounding box format from [x, y, w, h] to [x1, y1, w, h], where x1, y1 are the top-left coordinates.

    Args:
        x (np.ndarray | torch.Tensor): The input tensor with the bounding box coordinates in the xywh format

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in the xyltwh format
    r’   r”   r9   r“   r•   ©rG   r	   rH   Úcloner+   Úcopyr¬   s     r   Ú	xywh2ltwhr¾   ­  óg   € õ   ¥5¤<Ñ0Ô0Ð@ˆ�Š‰	Œ	ˆ	µb´g¸a±j´j€AØ�&”	˜A˜fœI¨™MÑ)€A€f�IØ�&”	˜A˜fœI¨™MÑ)€A€f�IØ€Hr   c                 óÚ   — t          | t          j        ¦  «        r|                      ¦   «         nt	          j        | ¦  «        }| d         | d         z
  |d<   | d         | d         z
  |d<   |S )aU  
    Convert nx4 bounding boxes from [x1, y1, x2, y2] to [x1, y1, w, h], where xy1=top-left, xy2=bottom-right.

    Args:
        x (np.ndarray | torch.Tensor): The input tensor with the bounding boxes coordinates in the xyxy format

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in the xyltwh format.
    r”   r’   r•   r“   r»   r¬   s     r   Ú	xyxy2ltwhrÁ   ½  ó_   € õ   ¥5¤<Ñ0Ô0Ð@ˆ�Š‰	Œ	ˆ	µb´g¸a±j´j€AØ�&”	˜A˜fœIÑ%€A€f�IØ�&”	˜A˜fœIÑ%€A€f�IØ€Hr   c                 óæ   — t          | t          j        ¦  «        r|                      ¦   «         nt	          j        | ¦  «        }| d         | d         dz  z   |d<   | d         | d         dz  z   |d<   |S )zú
    Convert nx4 boxes from [x1, y1, w, h] to [x, y, w, h] where xy1=top-left, xy=center.

    Args:
        x (torch.Tensor): the input tensor

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in the xywh format.
    r’   r”   r9   r“   r•   r»   r¬   s     r   Ú	ltwh2xywhrÄ   Í  r¿   r   c                 ó  — t          | t          j        ¦  «        }|rt          j        t          j        fnt
          j        t
          j        f\  }}| j        \  }}}}}}	}
}||z   dz  }||	z   dz  }||z
  }||z
  } ||dz  |dz  z   ¦  «        } |||z
  dz  ||	z
  dz  z   ¦  «        } || |¦  «        }|dt          j	        z  z  }|rt          j
        |||||f¦  «        j        nt          j        |||||fd¬¦  «        S )a9  
    Convert batched Oriented Bounding Boxes (OBB) from [xy1, xy2, xy3, xy4] to [xywh, rotation].

    Args:
        corners (numpy.ndarray | torch.Tensor): Input corners of shape (n, 8).

    Returns:
        (numpy.ndarray | torch.Tensor): Converted data in [cx, cy, w, h, rotation] format of shape (n, 5).
    r9   ç     €f@r8   ©Údim)rG   r+   ÚndarrayÚarctan2Úsqrtr	   Úatan2r)   rJ   ÚpiÚvstackÚstack)ÚcornersÚis_numpyrÌ   rË   Úx1Úy1Úx2Úy2Úx3Úy3Úx4Úy4ÚcxÚcyÚdx21Údy21r²   r³   Úrotations                      r   Úxyxyxyxy2xywhrrß   Ý  s(  € õ ˜'¥2¤:Ñ.Ô.€HØ+3ÐR•2”:�rœwÐ'Ð'½%¼+ÅuÄzÐ9R�K€Eˆ4à%,¤YÑ"€BˆˆB��B˜˜B Ø
ˆr‰'�Q‰€BØ
ˆr‰'�Q‰€BØ�‰7€DØ�‰7€DàˆˆT�Q‰Y˜ ™Ñ"Ñ#Ô#€AØˆˆb�2‰g˜!‰^˜r B™w¨1™nÑ,Ñ-Ô-€Aàˆu�d�U˜DÑ!Ô!€HØ��œ‘Ñ€Hà4<Ðn�2Œ9�b˜"˜a  HÐ-Ñ.Ô.Ô0Ð0Å%Ä+ÈrÐSUÐWXÐZ[Ð]eÐNfÐlmÐBnÑBnÔBnÐnr   c           
      óH  — t          | t          j        ¦  «        }|rt          j        t          j        fnt
          j        t
          j        f\  }}| j        \  }}}}}|t          j        dz  z  }|dz  }	|dz  }
 ||¦  «        } ||¦  «        }|	|z  }|	|z  }|
|z  }|
|z  }||z
  |z
  }||z   |z
  }||z   |z
  }||z
  |z
  }||z   |z   }||z
  |z   }||z
  |z   }||z   |z   }|r!t          j	        ||||||||f¦  «        j        nt          j
        ||||||||fd¬¦  «        S )a>  
    Convert batched Oriented Bounding Boxes (OBB) from [xywh, rotation] to [xy1, xy2, xy3, xy4].

    Args:
        center (numpy.ndarray | torch.Tensor): Input data in [cx, cy, w, h, rotation] format of shape (n, 5).

    Returns:
        (numpy.ndarray | torch.Tensor): Converted corner points of shape (n, 8).
    rÆ   r9   r8   rÇ   )rG   r+   rÉ   ÚcosÚsinr	   r)   rJ   rÍ   rÎ   rÏ   )ÚcenterrÑ   rá   râ   rÚ   rÛ   r²   r³   rÞ   ÚdxÚdyÚcos_rotÚsin_rotÚ
dx_cos_rotÚ
dx_sin_rotÚ
dy_cos_rotÚ
dy_sin_rotrÒ   rÓ   rÔ   rÕ   rÖ   r×   rØ   rÙ   s                            r   Úxywhr2xyxyxyxyrì   ù  sx  € õ ˜&¥"¤*Ñ-Ô-€HØ#+ÐG•”�œÐÐµ%´)½U¼YÐ1G�H€Cˆà#œXÑ€BˆˆAˆq�(Ø•”˜%‘Ñ€Hà	
ˆQ‰€BØ	
ˆQ‰€Bàˆc�(‰mŒm€GØˆc�(‰mŒm€GØ�g‘€JØ�g‘€JØ�g‘€JØ�g‘€Jà	ˆj‰˜:Ñ	%€BØ	ˆj‰˜:Ñ	%€BØ	ˆj‰˜:Ñ	%€BØ	ˆj‰˜:Ñ	%€BØ	ˆj‰˜:Ñ	%€BØ	ˆj‰˜:Ñ	%€BØ	ˆj‰˜:Ñ	%€BØ	ˆj‰˜:Ñ	%€Bà<Dð 1�2Œ9�b˜"˜b " b¨"¨b°"Ð5Ñ6Ô6Ô8Ð8Í%Ì+Ø	ˆR��R˜˜R  RÐ(¨aðK1ñ K1ô K1ð 1r   c                 óÚ   — t          | t          j        ¦  «        r|                      ¦   «         nt	          j        | ¦  «        }| d         | d         z   |d<   | d         | d         z   |d<   |S )a  
    It converts the bounding box from [x1, y1, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right.

    Args:
        x (np.ndarray | torch.Tensor): the input image

    Returns:
        y (np.ndarray | torch.Tensor): the xyxy coordinates of the bounding boxes.
    r”   r’   r•   r“   r»   r¬   s     r   Ú	ltwh2xyxyrî      rÂ   r   c                 ó(  — g }| D ]m}|j         \  }}|                     |                     ¦   «         |                     ¦   «         |                     ¦   «         |                     ¦   «         g¦  «         Œnt	          t          j        |¦  «        ¦  «        S )a-  
    It converts segment labels to box labels, i.e. (cls, xy1, xy2, ...) to (cls, xywh)

    Args:
        segments (list): list of segments, each segment is a list of points, each point is a list of x, y coordinates

    Returns:
        (np.ndarray): the xywh coordinates of the bounding boxes.
    )r)   Úappendr-   r.   r­   r+   r,   )Úsegmentsr?   Úsr3   r4   s        r   Úsegments2boxesró   0  sw   € ð €EØð ;ð ;ˆØŒs‰ˆˆ1Ø�Š�a—e’e‘g”g˜qŸušu™wœw¨¯ª©¬°·²±´Ð9Ñ:Ô:Ð:Ð:Ý•R”X˜e‘_”_Ñ%Ô%Ð%r   éè  c                 óº  ‡‡‡— t          | ¦  «        D ]Ç\  }Št          j        ‰‰dd…dd…f         fd¬¦  «        Št          j        dt	          ‰¦  «        dz
  |¦  «        Št          j        t	          ‰¦  «        ¦  «        Št          j        ˆˆˆfd„t          d¦  «        D ¦   «         t          j        ¬¦  «                             dd¦  «        j	        | |<   ŒÈ| S )	ae  
    Inputs a list of segments (n,2) and returns a list of segments (n,2) up-sampled to n points each.

    Args:
        segments (list): a list of (n,2) arrays, where n is the number of points in the segment.
        n (int): number of points to resample the segment to. Defaults to 1000

    Returns:
        segments (list): the resampled segments.
    r   r8   N)Úaxisc           	      óP   •— g | ]"}t          j        ‰‰‰d d …|f         ¦  «        ‘Œ#S ©N)r+   Úinterp)Ú.0rŠ   rò   r3   Úxps     €€€r   ú
<listcomp>z%resample_segments.<locals>.<listcomp>P  s3   ø€ Ð%RÐ%RÐ%RÀA¥b¤i°°2°q¸¸¸¸A¸´wÑ&?Ô&?Ð%RÐ%RÐ%Rr   r9   r&   rV   )
rc   r+   ÚconcatenateÚlinspacerd   Úarangere   Úfloat32Úreshaper)   )rñ   r�   rŠ   rò   r3   rû   s      @@@r   Úresample_segmentsr  A  sÙ   øøø€ õ ˜(Ñ#Ô#ð Hð H‰ˆˆ1ÝŒN˜A˜q  1  a a a œy˜>°Ð2Ñ2Ô2ˆÝŒK˜�3˜q™6œ6 A™: qÑ)Ô)ˆÝŒY•s˜1‘v”vÑÔˆÝ”nÐ%RÐ%RÐ%RÐ%RÐ%RÐ%RÍÈqÉÌÐ%RÑ%RÔ%RÝ+-¬:ð7ñ 7ô 7ß7>²w¸qÀ"±~´~Äað 	�‰ˆà€Or   c                 óT  — | j         \  }}}t          j        |dd…dd…df         dd¦  «        \  }}}}t          j        || j        |j        ¬¦  «        dddd…f         }	t          j        || j        |j        ¬¦  «        ddd…df         }
| |	|k    |	|k     z  |
|k    z  |
|k     z  z  S )aV  
    It takes a mask and a bounding box, and returns a mask that is cropped to the bounding box.

    Args:
        masks (torch.Tensor): [n, h, w] tensor of masks
        boxes (torch.Tensor): [n, 4] tensor of bbox coordinates in relative point form

    Returns:
        (torch.Tensor): The masks are being cropped to the bounding box.
    Nr(   r8   )rY   r'   )r_   r	   Úchunkrÿ   rY   r'   )r    r?   r�   r³   r²   rÒ   rÓ   rÔ   rÕ   ÚrrŽ   s              r   Ú	crop_maskr  U  s»   € ð Œk�G€A€qˆ!Ý”[  q q q¨!¨!¨!¨T zÔ!2°A°qÑ9Ô9�N€BˆˆB�ÝŒ�Q˜uœ|°2´8Ð<Ñ<Ô<¸TÀ4ÈÈÈ¸]ÔK€AÝŒ�Q˜uœ|°2´8Ð<Ñ<Ô<¸TÀ1À1À1Àd¸]ÔK€Aà�Q˜"’W  R¢Ñ(¨A°ªGÑ4¸¸BºÑ?Ñ@Ð@r   c                 óN  — | j         \  }}}||                      ¦   «                              |d¦  «        z                       ¦   «                              d||¦  «        }t	          j        |d         |dd¬¦  «        d         }t          ||¦  «        }|                     d¦  «        S )aß  
    Takes the output of the mask head, and applies the mask to the bounding boxes. This produces masks of higher quality
    but is slower.

    Args:
        protos (torch.Tensor): [mask_dim, mask_h, mask_w]
        masks_in (torch.Tensor): [n, mask_dim], n is number of masks after nms
        bboxes (torch.Tensor): [n, 4], n is number of masks after nms
        shape (tuple): the size of the input image (h,w)

    Returns:
        (torch.Tensor): The upsampled masks.
    rV   NÚbilinearF©ÚmodeÚalign_cornersr   rU   )r_   rj   rk   ÚsigmoidÚFÚinterpolater  Úgt_©ÚprotosÚmasks_inÚbboxesr_   rŽ   ÚmhÚmwr    s           r   Úprocess_mask_upsampler  h  s’   € ð ”�I€A€rˆ2Ø˜Ÿš™œ×+Ò+¨A¨rÑ2Ô2Ñ2×;Ò;Ñ=Ô=×BÒBÀ2ÀrÈ2ÑNÔN€EÝŒM˜% œ+ u°:ÈUÐSÑSÔSÐTUÔV€EÝ�e˜VÑ$Ô$€EØ�9Š9�S‰>Œ>Ðr   c                 ó<  — | j         \  }}}|\  }}	||                      ¦   «                              |d¦  «        z                       ¦   «                              d||¦  «        }
|                     ¦   «         }|dd…dfxx         ||	z  z  cc<   |dd…dfxx         ||	z  z  cc<   |dd…dfxx         ||z  z  cc<   |dd…dfxx         ||z  z  cc<   t          |
|¦  «        }
|r$t          j        |
d         |dd¬	¦  «        d         }
|
                     d
¦  «        S )a;  
    Apply masks to bounding boxes using the output of the mask head.

    Args:
        protos (torch.Tensor): A tensor of shape [mask_dim, mask_h, mask_w].
        masks_in (torch.Tensor): A tensor of shape [n, mask_dim], where n is the number of masks after NMS.
        bboxes (torch.Tensor): A tensor of shape [n, 4], where n is the number of masks after NMS.
        shape (tuple): A tuple of integers representing the size of the input image in the format (h, w).
        upsample (bool): A flag to indicate whether to upsample the mask to the original image size. Default is False.

    Returns:
        (torch.Tensor): A binary mask tensor of shape [n, h, w], where n is the number of masks after NMS, and h and w
            are the height and width of the input image. The mask is applied to the bounding boxes.
    rV   Nr   r9   r;   r8   r  Fr	  rU   )	r_   rj   rk   r  r¼   r  r  r  r  )r  r  r  r_   ÚupsamplerŽ   r  r  ÚihÚiwr    Údownsampled_bboxess               r   Úprocess_maskr  }  sT  € ð  ”�I€A€rˆ2Ø�F€BˆØ˜Ÿš™œ×+Ò+¨A¨rÑ2Ô2Ñ2×;Ò;Ñ=Ô=×BÒBÀ2ÀrÈ2ÑNÔN€EàŸš™œÐØ�q�q�q˜!�tÐÐÔ  R¡Ñ'ÐÐÑØ�q�q�q˜!�tÐÐÔ  R¡Ñ'ÐÐÑØ�q�q�q˜!�tÐÐÔ  R¡Ñ'ÐÐÑØ�q�q�q˜!�tÐÐÔ  R¡Ñ'ÐÐÑå�eÐ/Ñ0Ô0€EØð [Ý”˜e Dœk¨5°zÐQVÐWÑWÔWÐXYÔZˆØ�9Š9�S‰>Œ>Ðr   c                 ó>  — | j         \  }}}||                      ¦   «                              |d¦  «        z                       ¦   «                              d||¦  «        }t	          |d         |¦  «        d         }t          ||¦  «        }|                     d¦  «        S )aÐ  
    It takes the output of the mask head, and crops it after upsampling to the bounding boxes.

    Args:
        protos (torch.Tensor): [mask_dim, mask_h, mask_w]
        masks_in (torch.Tensor): [n, mask_dim], n is number of masks after nms
        bboxes (torch.Tensor): [n, 4], n is number of masks after nms
        shape (tuple): the size of the input image (h,w)

    Returns:
        masks (torch.Tensor): The returned masks with dimensions [h, w, n]
    rV   Nr   rU   )r_   rj   rk   r  Úscale_masksr  r  r  s           r   Úprocess_mask_nativer  �  sˆ   € ð ”�I€A€rˆ2Ø˜Ÿš™œ×+Ò+¨A¨rÑ2Ô2Ñ2×;Ò;Ñ=Ô=×BÒBÀ2ÀrÈ2ÑNÔN€EÝ˜˜dœ UÑ+Ô+¨AÔ.€EÝ�e˜VÑ$Ô$€EØ�9Š9�S‰>Œ>Ðr   c                 óz  — | j         dd…         \  }}t          ||d         z  ||d         z  ¦  «        }||d         |z  z
  ||d         |z  z
  g}|r |dxx         dz  cc<   |dxx         dz  cc<   |rJt          t          |d         dz
  ¦  «        ¦  «        t          t          |d         dz
  ¦  «        ¦  «        fnd\  }}t          t          ||d         z
  dz   ¦  «        ¦  «        t          t          ||d         z
  dz   ¦  «        ¦  «        }
}	| d||	…||
…f         } t	          j        | |dd	¬
¦  «        } | S )a  
    Rescale segment masks to shape.

    Args:
        masks (torch.Tensor): (N, C, H, W).
        shape (tuple): Height and width.
        padding (bool): If True, assuming the boxes is based on image augmented by yolo style. If False then do regular
            rescaling.
    r9   Nr   r8   r:   )r   r   .r  Fr	  )r_   r-   rI   r<   r  r  )r    r_   rB   r  r  rC   rD   r£   r¤   r¥   r¦   s              r   r  r  ±  sN  € ð Œ[˜˜˜Œ_�F€BˆÝˆr�E˜!”H‰}˜b 5¨¤8™mÑ,Ô,€DØ��a”˜4‘Ñ  e¨A¤h°¡oÑ!5Ð
6€CØð ØˆAˆˆŒ�!‰ˆˆ‰ØˆAˆˆŒ�!‰ˆˆ‰ØHOÐ[••U˜3˜qœ6 C™<Ñ(Ô(Ñ)Ô)­3­u°S¸´V¸c±\Ñ/BÔ/BÑ+CÔ+CÐDÐDÐU[�I€CˆÝ�˜r C¨¤F™{¨SÑ0Ñ1Ô1Ñ2Ô2µC½¸bÀ3ÀqÄ6¹kÈCÑ>OÑ8PÔ8PÑ4QÔ4QˆE€FØ�#�s˜6�z 4¨ :Ð-Ô.€EåŒM˜% ¨ZÀuÐMÑMÔM€EØ€Lr   c                 ó  — |€]t          | d         |d         z  | d         |d         z  ¦  «        }| d         |d         |z  z
  dz  | d         |d         |z  z
  dz  f}n|d         d         }|d         }|r,|dxx         |d         z  cc<   |dxx         |d         z  cc<   |dxx         |z  cc<   |dxx         |z  cc<   t          ||¦  «        }|r,|dxx         |d         z  cc<   |dxx         |d         z  cc<   |S )a×  
    Rescale segment coordinates (xy) from img1_shape to img0_shape.

    Args:
        img1_shape (tuple): The shape of the image that the coords are from.
        coords (torch.Tensor): the coords to be scaled of shape n,2.
        img0_shape (tuple): the shape of the image that the segmentation is being applied to.
        ratio_pad (tuple): the ratio of the image size to the padded image size.
        normalize (bool): If True, the coordinates will be normalized to the range [0, 1]. Defaults to False.
        padding (bool): If True, assuming the boxes is based on image augmented by yolo style. If False then do regular
            rescaling.

    Returns:
        coords (torch.Tensor): The scaled coordinates.
    Nr   r8   r9   r’   r“   )r-   r›   )r>   rš   r@   rA   Ú	normalizerB   rC   rD   s           r   Úscale_coordsr#  É  s>  € ð  ÐÝ�:˜a”= :¨a¤=Ñ0°*¸Q´-À*ÈQÄ-Ñ2OÑPÔPˆØ˜!Œ}˜z¨!œ}¨tÑ3Ñ3°qÑ8¸:Àa¼=È:ÐVWÌ=Ð[_ÑK_Ñ;_ÐcdÑ:dÐdˆˆà˜Œ|˜AŒˆØ˜Œlˆàð !ØˆvˆˆŒ˜#˜aœ&Ñ ˆˆ‰ØˆvˆˆŒ˜#˜aœ&Ñ ˆˆ‰Ø
ˆ6€N€N„N�dÑ€N€N�NØ
ˆ6€N€N„N�dÑ€N€N�NÝ˜ Ñ,Ô,€FØð (ØˆvˆˆŒ˜* Qœ-Ñ'ˆˆ‰ØˆvˆˆŒ˜* Qœ-Ñ'ˆˆ‰Ø€Mr   Úlargestc                 ó’  — g }|                       ¦   «                              ¦   «                              ¦   «                              d¦  «        D ]ø}t	          j        |t          j        t          j        ¦  «        d         }|rˆ|dk    rt          j	        d„ |D ¦   «         ¦  «        }nw|dk    r\t          j
        |t          j
        d„ |D ¦   «         ¦  «                             ¦   «                  ¦  «                             dd¦  «        }nt          j        d	¦  «        }|                     |                     d
¦  «        ¦  «         Œù|S )aB  
    It takes a list of masks(n,h,w) and returns a list of segments(n,xy)

    Args:
        masks (torch.Tensor): the output of the model, which is a tensor of shape (batch_size, 160, 160)
        strategy (str): 'concat' or 'largest'. Defaults to largest

    Returns:
        segments (List): list of segment masks
    Úuint8r   Úconcatc                 ó:   — g | ]}|                      d d¦  «        ‘ŒS )rV   r9   )r  ©rú   r3   s     r   rü   z"masks2segments.<locals>.<listcomp>ü  s&   € Ð#@Ð#@Ð#@¸ A§I¢I¨b°!Ñ$4Ô$4Ð#@Ð#@Ð#@r   r$  c                 ó,   — g | ]}t          |¦  «        ‘ŒS r#   )rd   r)  s     r   rü   z"masks2segments.<locals>.<listcomp>þ  s   € Ð(;Ð(;Ð(;°A­¨Q©¬Ð(;Ð(;Ð(;r   rV   r9   )r   r9   r   )rI   ÚcpuÚnumpyÚastyperž   ÚfindContoursÚRETR_EXTERNALÚCHAIN_APPROX_SIMPLEr+   rý   r,   Úargmaxr  r/   rð   )r    Ústrategyrñ   r3   rŽ   s        r   Úmasks2segmentsr3  ì  s"  € ð €HØ�YŠY‰[Œ[�_Š_ÑÔ×$Ò$Ñ&Ô&×-Ò-¨gÑ6Ô6ð 	-ð 	-ˆÝÔ˜Q¥Ô 1µ3Ô3JÑKÔKÈAÔNˆØð 	!Ø˜8Ò#Ð#Ý”NÐ#@Ð#@¸aÐ#@Ñ#@Ô#@ÑAÔA��Ø˜YÒ&Ð&Ý”H˜Q�rœxÐ(;Ð(;¸Ð(;Ñ(;Ô(;Ñ<Ô<×CÒCÑEÔEÔFÑGÔG×OÒOÐPRÐTUÑVÔV�øå”˜Ñ Ô ˆAØ�Š˜Ÿš Ñ+Ô+Ñ,Ô,Ð,Ð,Ø€Or   ÚbatchÚreturnc                 ó  — |                       dddd¦  «                             ¦   «         dz                       dd¦  «                             t          j        ¦  «                             ¦   «                              ¦   «         S )as  
    Convert a batch of FP32 torch tensors (0.0-1.0) to a NumPy uint8 array (0-255), changing from BCHW to BHWC layout.

    Args:
        batch (torch.Tensor): Input tensor batch of shape (Batch, Channels, Height, Width) and dtype torch.float32.

    Returns:
        (np.ndarray): Output NumPy array batch of shape (Batch, Height, Width, Channels) and dtype uint8.
    r   r9   r;   r8   éÿ   )ÚpermuteÚ
contiguousr—   Útor	   r&  r+  r,  )r4  s    r   Úconvert_torch2numpy_batchr;    se   € ð �MŠM˜!˜Q  1Ñ%Ô%×0Ò0Ñ2Ô2°SÑ8×?Ò?ÀÀ3ÑGÔG×JÒJÍ5Ì;ÑWÔW×[Ò[Ñ]Ô]×cÒcÑeÔeÐer   c                 ó0   — t          j        dd| ¬¦  «        S )zî
    Cleans a string by replacing special characters with underscore _

    Args:
        s (str): a string needing special characters replaced

    Returns:
        (str): a string with special characters replaced by an underscore _
    u"   [|@#!Â¡Â·$â‚¬%&()=?Â¿^*;:,Â¨Â´><+]Ú_)ÚpatternÚreplÚstring)ÚreÚsub)rò   s    r   Ú	clean_strrC    s   € õ Œ6Ð>ÀSÐQRÐSÑSÔSÐSr   )r$   r$   )NT)rN   rO   NFFr#   rP   r   rQ   rR   rS   rø   )r$   r$   r   r   )r$   r$   Fr   )rô   )F)T)NFT)r$  )/Ú
contextlibrJ   rA  r   rž   r,  r+   r	   Útorch.nn.functionalÚnnÚ
functionalr  rn   Úultralytics.utilsr   ÚContextDecoratorr   r6   rE   rM   r�   r=   r›   r§   r­   rb   r¶   r¹   r¾   rÁ   rÄ   rß   rì   rî   ró   r  r  r  r  r  r  r#  r3  rH   rÉ   r;  rC  r#   r   r   ú<module>rJ     sí  ðð Ð Ð Ð Ø €€€Ø 	€	€	€	Ø €€€à 
€
€
€
Ø Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð Ð Ð Ð Ø Ð Ð Ð à $Ð $Ð $Ð $Ð $Ð $ð+ð +ð +ð +ð +ˆjÔ)ñ +ô +ð +ð\ ð  ð  ð  ð()ð )ð )ð )ð@,ð ,ð ,ð$ ØØØØØØØØØØðð ð ð ðDð ð ð,ð ð ð( ð  ð  ð  ðFð ð ð(ð ð ð,ð ð ð ð.ð ð ð ð4ð ð ð ð ð ð ð ð ð oð oð oð8$1ð $1ð $1ðNð ð ð &ð &ð &ð"ð ð ð ð(Að Að Að&ð ð ð*ð ð ð ð@ð ð ð(ð ð ð ð0 ð  ð  ð  ðFð ð ð ð2
f U¤\ð 
f°b´jð 
fð 
fð 
fð 
fð
Tð 
Tð 
Tð 
Tð 
Tr   