o
    jĎiT                     @   s   d dl mZmZ d dlmZmZ d dlmZ d dlm	Z	 				ddede
d	e
d
eeef dedeeef fddZddedede
fddZddede
fddZddedede
fddZddedede
fddZdS )    )TupleUnion)BboxTypeGenericBboxType)BaseBoundingBox	load_bboxNTbbox	from_typeto_type
image_sizereturn_valuesreturnc                 K   sP   t | ts|stdtd|| |d|} t| d| }| }|r&|jS |S )a  
    Converts given bbox with given `from_type` to given `to_type`. It uses VOC format
    as an intermediate format.

    Args:
        bbox: (generic) Bounding box.
        from_type: (str) Type/Format of the given bounding box.
        to_type: (str) Type/Format of the resulting bounding box.
        image_size: (tuple(int,int)) Image size as (w, h) tuple, it is required if the one side of the
            boxes requires scaling.
        return_values: (bool) Whether to return values as a Tuple, or BoundingBox object.
            True by default for compatibility purposes.

    Return:
        Bounding box in type `to_type`.
    z?if `bbox` is not a BoundingBox object, `from_type` is required.)namevaluesr   to_N )
isinstancer   
ValueErrorr   getattrr   )r	   r
   r   r   r   kwargssource_to_targettarget_bboxr   r   O/home/jeff/fluffinator/venv/lib/python3.10/site-packages/pybboxes/functional.pyconvert_bbox   s   
r   cocobbox1bbox2	bbox_typec                 K   s0   t d|| d|} t d||d|}| | S )a9  
    Computes intersection area between given bounding boxes.

    Args:
        bbox1: Bounding box 1.
        bbox2: Bounding box 2.
        bbox_type: Format of the bounding boxes. It's 'coco' [x-tl, y-tl, w, h] by default.

    Returns:
        Intersection area if bounding boxes intersect, 0 otherwise.
    )r   r   Nr   r   r   r   r   r   r   r   r   compute_intersection+   s   r    c                 K   s   t | | |fi |S )z2
    Computes the area of given bounding box.
    )r    )r	   r   r   r   r   r   compute_area<   s   r!   c                 K   sJ   t | |fd|i|}t| fd|i|}t|fd|i|}|| | S )z
    Computes union area of given boxes.

    Args:
        bbox1: Bounding box 1.
        bbox2: Bounding box 2.
        bbox_type: Format of the bounding boxes. It's 'coco' [x-tl, y-tl, w, h] by default.

    Returns:
        Union area.
    r   )r    r!   )r   r   r   r   intersectionarea1area2r   r   r   compute_unionC   s   r%   c                 K   s(   t | ||fi |t| ||fi | S )a2  
    Computes Intersection over Union (IoU) (special form of Jaccard Index) metric.

    Args:
        bbox1: Bounding box 1.
        bbox2: Bounding box 2.
        bbox_type: Format of the bounding boxes. It's 'coco' [x-tl, y-tl, w, h] by default.

    Returns:
        Intersection over Union ratio.
    )r    r%   r   r   r   r   compute_iouU   s   (r&   )NNNT)r   )typingr   r   pybboxes._typingr   r   pybboxes.boxes.baser   pybboxes.boxes.bboxr   strintboolr   r    r!   r%   r&   r   r   r   r   <module>   s2    


#