o
    jĎi                     @   sl   d dl Z d dlmZmZ d dlmZmZmZ d dlZ	d dl
mZ g dZG dd dZG dd	 d	eeZdS )
    N)ABCabstractmethod)ListTupleUnion)find_stack_level)albumentationsfiftyoneyoloc                   @   s   e Zd ZdedededefddZdd defd	d
Zdd defddZdd defddZdd defddZ	e
defddZe
defddZe
defddZdd defddZdd defddZdd defddZdS )Boxx_tly_tlx_bry_brc                 C   s   || _ || _|| _|| _d S N)r   r   r   r   )selfr   r   r   r    r   O/home/jeff/fluffinator/venv/lib/python3.10/site-packages/pybboxes/boxes/base.py__init__   s   
zBox.__init__otherreturnc                 C   
   |  |S r   )unionr   r   r   r   r   __add__      
zBox.__add__c                 C   s   t | j| | S r   intareaintersectionr   r   r   r   __sub__      zBox.__sub__c                 C   r   r   )r   r   r   r   r   __mul__   r   zBox.__mul__c                 C   s   | j |j  S r   )r   r   r   r   r   __truediv__   s   zBox.__truediv__c                 C   s   | j | j S r   )widthheightr   r   r   r   r      s   zBox.areac                 C      t | j| j S r   )r   r   r   r&   r   r   r   r%   #      z
Box.heightc                 C   r'   r   )r   r   r   r&   r   r   r   r$   '   r(   z	Box.widthc                 C   sp   t | j| jf|j|jf\}}t | j| jf|j|jf\}}||ks(||kr*dS || }|| }t|| S )Nr   )npmaximumr   r   minimumr   r   r   )r   r   r   r   r   r   intersection_widthintersection_heightr   r   r   r   +   s     zBox.intersectionc                 C   s   t | j|j | | S r   r   r   r   r   r   r   4   s   z	Box.unionc                 C   s   |  || | S r   )r   r   r   r   r   r   iou7   r!   zBox.iouN)__name__
__module____qualname__r   r   r   r    r"   floatr#   propertyr   r%   r$   r   r   r.   r   r   r   r   r      s    	r   c                       sf  e Zd Z		dAdeeef deeef deeef deeef deeef def fd	d
Zdd Z	e
deedf fddZe
dd Zejdeeef fddZdd Ze
defddZdefddZedd Zdd ZdBddZdCdedeeeeeef d f fd d!ZdCdedeeeeeef d f fd"d#ZdCdedeeeeeef d f fd$d%ZedCdedeeeeeef d f fd&d'ZdCdedeeeeeef d f fd(d)Ze
d*d+ Zd,eddfd-d.ZdDd/d0Zd1edd fd2d3Zd4edd fd5d6Ze e		7dEd8ed9ed:ed;edeeef dedd fd<d=Z!e d>eee"e#j$f fd?d@Z%  Z&S )FBaseBoundingBoxNFv1v2v3v4
image_sizestrictc                    sB   || _ || _d | _| |||| | jdd}tt| j|  d S )NT)return_values)_image_sizer:   _is_oob_validate_and_set_valuesto_vocsuperr4   r   )r   r5   r6   r7   r8   r9   r:   
voc_values	__class__r   r   r   <   s   	zBaseBoundingBox.__init__c                 C   sP   | j \}}ddd | jD }d| d| j d| j d|pd d|p$d d	S )
N c                 S   s&   g | ]}t |tr|d nt|qS )z.4f)
isinstancer2   str).0vr   r   r   
<listcomp>N   s   & z,BaseBoundingBox.__repr__.<locals>.<listcomp>z<[z] (xz) | Image: (?z)>)r9   joinvaluesr$   r%   )r   image_widthimage_heightstr_valsr   r   r   __repr__L   s   
0zBaseBoundingBox.__repr__r   c                 C      | j S )z
        Whether the box is OOB (Out-of-bounds).

        Returns:
            None -> unknown. False -> Not OOB. True -> OOB.
        )r=   r&   r   r   r   is_oobQ   s   zBaseBoundingBox.is_oobc                 C   s   | j d ur| j S dS )NNNr<   r&   r   r   r   r9   [   s   
zBaseBoundingBox.image_sizec                 C   s
   || _ d S r   rU   )r   r9   r   r   r   r9   b   s   
c                 C   s   | j dkrdS dS )NrT   TF)r9   r&   r   r   r   is_image_size_nullf   s   
z"BaseBoundingBox.is_image_size_nullc                 C   rR   r   _valuesr&   r   r   r   rM   k   s   zBaseBoundingBox.valuesc                 G   s   |S r   r   r   rM   r   r   r   _correct_value_typeso   s   z$BaseBoundingBox._correct_value_typesc                 G      d S r   r   rY   r   r   r   _validate_valuesr      z BaseBoundingBox._validate_valuesc                 G   s
   || _ dS )g
        This method is intended to be "final", and should not be overridden in child classes.
        NrW   rY   r   r   r   _set_valuesv   s   
zBaseBoundingBox._set_valuesc                 G   s(   || _ | j| }| j|  | j|  dS )zM
        Validate and sets given values if validation is successful.
        N)
raw_valuesrZ   r\   r_   rY   r   r   r   r>   |   s   

z(BaseBoundingBox._validate_and_set_valuesr;   c                 C      |   |S r   )r?   to_albumentationsr   r;   r   r   r   rb         z!BaseBoundingBox.to_albumentationsc                 C   ra   r   )r?   to_cocorc   r   r   r   re      rd   zBaseBoundingBox.to_cococ                 C   ra   r   )r?   to_fiftyonerc   r   r   r   rf      rd   zBaseBoundingBox.to_fiftyonec                 C   r[   r   r   rc   r   r   r   r?      r]   zBaseBoundingBox.to_vocc                 C   ra   r   )r?   to_yolorc   r   r   r   rg      rd   zBaseBoundingBox.to_yoloc                 C   s   | j j ddS )Nboundingbox )rC   r/   lowerreplacer&   r   r   r   name   s   zBaseBoundingBox.nameopc                 O   sT   |   }t||}||i |}t|d| j }| }| j|j| j| jd d S )Nto_)r9   r:   )r?   getattrrl   r   rM   r9   r:   )r   rm   argskwargsrefined_boxbox_opbox_conversionr   r   r   _generic_operation   s   
z"BaseBoundingBox._generic_operationc                 C   s   |  d | S )zh
        Clamps the box with respect to the image borders. If the box is not OOB, does nothing.
        clampru   r&   r   r   r   rv      s   
zBaseBoundingBox.clampfactorc                 C   s   |  d| | S )Nscalerw   )r   rx   r   r   r   ry      s   zBaseBoundingBox.scaleamountc                 C   s<   | j tv r| j\}}|d | |d | f}| d| | S )a(  
        Perform a shift operation on the bounding box inplace.

        Args:
            amount: The amount to shift the bounding box. The first value is the
                amount to shift the x-coordinate, and the second value is the
                amount to shift the y-coordinate.
        r      shift)rl   NORMALIZED_BOXESr9   ru   )r   rz   r$   r%   r   r   r   r|      s
   
	
zBaseBoundingBox.shiftTr   r   r   r   c                 C   r[   r   r   )clsr   r   r   r   r9   r:   r   r   r   from_voc   s   zBaseBoundingBox.from_vocarc                 K   s@   t jdtt d t|dkrtdt| d| |i |S )r^   zThe functionality of the `from_array()` method is changed from only supporting a single box values to support (arbitrary) n-dimensional array of box values starting from 0.2 onward requiring Python3.8 or higher.)
stacklevel   z,Given array must be length of 4, got length .)warningswarnFutureWarningr   len
ValueError)r~   r   rq   r   r   r   
from_array   s   zBaseBoundingBox.from_array)NF)r   N)F)r   r4   )NT)'r/   r0   r1   r   r   r2   r   boolr   rQ   r3   rS   r9   setterrV   rM   rZ   r   r\   r_   r>   rb   re   rf   r?   rg   rl   rF   ru   rv   ry   r|   classmethodr   r   r)   ndarrayr   __classcell__r   r   rB   r   r4   ;   s    




	


(	((*(

	
$r4   )r   abcr   r   typingr   r   r   numpyr)   pybboxes.utilsr   r}   r   r4   r   r   r   r   <module>   s    /