
    FPhm                         d dl Z ddZddZy)    Nc                     |\  }}d| | dddf   |k  df<   d| | dddf   |k  df<   || | dddf   ||z
  kD  df<   || | dddf   ||z
  kD  df<   | S )a3  
    Adjust bounding boxes to stick to image border if they are within a certain threshold.

    Args:
        boxes (torch.Tensor): (n, 4)
        image_shape (tuple): (height, width)
        threshold (int): pixel threshold

    Returns:
        adjusted_boxes (torch.Tensor): adjusted bounding boxes
    r   N          )boxesimage_shape	thresholdhws        kC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/models/fastsam/utils.pyadjust_bboxes_to_image_borderr      s     DAq )*E%1+	
!1
$%()E%1+	
!1
$%,-E%1+I
%q
(),-E%1+I
%q
()L    c                    t        ||      }t        j                  | d   |dddf         }t        j                  | d   |dddf         }t        j                  | d   |dddf         }t        j                  | d   |dddf         }||z
  j	                  d      ||z
  j	                  d      z  }	| d   | d   z
  | d   | d   z
  z  }
|dddf   |dddf   z
  |dddf   |dddf   z
  z  }|
|z   |	z
  }|	|z  }|r|j                         dk(  rdS |S t        j                  ||kD        j                         S )a  
    Compute the Intersection-Over-Union of a bounding box with respect to an array of other bounding boxes.

    Args:
        box1 (torch.Tensor): (4, )
        boxes (torch.Tensor): (n, 4)
        iou_thres (float): IoU threshold
        image_shape (tuple): (height, width)
        raw_output (bool): If True, return the raw IoU values instead of the indices

    Returns:
        high_iou_indices (torch.Tensor): Indices of boxes with IoU > thres
    r   Nr   r   r   )r   torchmaxminclampnumelnonzeroflatten)box1r   	iou_thresr	   
raw_outputx1y1x2y2intersection	box1_area	box2_areaunionious                 r   bbox_iour$      sV    *%=E	47E!Q$K	(B	47E!Q$K	(B	47E!Q$K	(B	47E!Q$K	(B G??1%b(::L a47"tAwa'89Iq!tuQT{*uQT{U1a4[/HII 	!L0E 
CIIK1$q-#- ==y)1133r   )   )g?)  r&   F)r   r   r$   r   r   r   <module>r'      s    0%4r   