
    Ng%                     r    d dl mZmZmZmZ d dlmZmZ d dlmZ  G d d          Z G d dee          Z	dS )	    )ListDictr   Any)ABCabstractmethod)copyc                   "    e Zd ZddZd Zd ZdS )BaseLayoutElementFc                     |r| nt          |           }t          |          }|                                D ]0\  }}||v r|||<   d| |v r	||d| <   t          d|           |S )N_zUnknown attribute name: )r   varsitems
ValueError)selfinplacekwargsobjvar_dictkeyvals          V/var/www/html/ai-engine/env/lib/python3.11/site-packages/layoutparser/elements/base.pysetzBaseLayoutElement.set   s    -dd4::99 	C 	CHCh #Sh&&&)S## !AC!A!ABBB
    c                     d                     d t          |                                           D                       }| j        j         d| dS )Nz, c                 "    g | ]\  }}| d | S )= ).0r   r   s      r   
<listcomp>z.BaseLayoutElement.__repr__.<locals>.<listcomp>$   s&    PPPcnnsnnPPPr   ())joinr   r   	__class____name__)r   info_strs     r   __repr__zBaseLayoutElement.__repr__"   sP    99PPT$ZZ=M=M=O=OPPPQQ.)77H7777r   c                 b    |j         | j         urdS t          |           t          |          k    S )NF)r#   r   r   others     r   __eq__zBaseLayoutElement.__eq__'   s-    ?$.005DzzT%[[((r   N)F)r$   
__module____qualname__r   r&   r*   r   r   r   r
   r
      sF           8 8 8
) ) ) ) )r   r
   c                   f   e Zd Zeedefd                        Zeedee         fd                        Zeed                         Z	eed                         Z
eed                         Zeed                         Zeed                         Zed	             Zed
             Zei dfd            Zeddd defd            Zeddd defd            Zedd            Zedd            Zedd            Zed             Zdeeef         fdZedeeef         dd fd            ZdS ) BaseCoordElementreturnc                     dS )zThe name of the classNr   r   s    r   _namezBaseCoordElement._name0   	     	r   c                     dS )z?A list of features names used for initializing the class objectNr   r1   s    r   	_featureszBaseCoordElement._features6   r3   r   c                     d S Nr   r1   s    r   widthzBaseCoordElement.width@   	     	r   c                     d S r7   r   r1   s    r   heightzBaseCoordElement.heightE   r9   r   c                     d S r7   r   r1   s    r   coordinateszBaseCoordElement.coordinatesJ   r9   r   c                     d S r7   r   r1   s    r   pointszBaseCoordElement.pointsO   r9   r   c                     d S r7   r   r1   s    r   areazBaseCoordElement.areaT   r9   r   c                     dS )aY  
        Given the current element in relative coordinates to another element which is in absolute coordinates,
        generate a new element of the current element in absolute coordinates.

        Args:
            other (:obj:`BaseCoordElement`):
                The other layout element involved in the geometric operations.

        Raises:
            Exception: Raise error when the input type of the other element is invalid.

        Returns:
            :obj:`BaseCoordElement`:
                The BaseCoordElement object of the original element in the absolute coordinate system.
        Nr   r(   s     r   condition_onzBaseCoordElement.condition_on]   	    $ 	r   c                     dS )aC  
        Given the current element and another element both in absolute coordinates,
        generate a new element of the current element in relative coordinates to the other element.

        Args:
            other (:obj:`BaseCoordElement`): The other layout element involved in the geometric operations.

        Raises:
            Exception: Raise error when the input type of the other element is invalid.

        Returns:
            :obj:`BaseCoordElement`:
                The BaseCoordElement object of the original element in the relative coordinate system.
        Nr   r(   s     r   relative_tozBaseCoordElement.relative_toq   	    " 	r   Fc                     dS )a  
        Identify whether the current element is within another element.

        Args:
            other (:obj:`BaseCoordElement`):
                The other layout element involved in the geometric operations.
            soft_margin (:obj:`dict`, `optional`, defaults to `{}`):
                Enlarge the other element with wider margins to relax the restrictions.
            center (:obj:`bool`, `optional`, defaults to `False`):
                The toggle to determine whether the center (instead of the four corners)
                of the current element is in the other element.

        Returns:
            :obj:`bool`: Returns `True` if the current element is in the other element and `False` if not.
        Nr   )r   r)   soft_margincenters       r   is_inzBaseCoordElement.is_in   rD   r   Tr)   strictc                     dS )zIntersect the current shape with the other object, with operations defined in
        :doc:`../notes/shape_operations`.
        Nr   r   r)   rL   s      r   	intersectzBaseCoordElement.intersect         r   c                     dS )z|Union the current shape with the other object, with operations defined in
        :doc:`../notes/shape_operations`.
        Nr   rN   s      r   unionzBaseCoordElement.union   rP   r   r   c                     dS )a  Pad the layout element on the four sides of the polygon with the user-defined pixels. If
        safe_mode is set to True, the function will cut off the excess padding that falls on the negative
        side of the coordinates.

        Args:
            left (:obj:`int`, `optional`, defaults to 0): The number of pixels to pad on the upper side of the polygon.
            right (:obj:`int`, `optional`, defaults to 0): The number of pixels to pad on the lower side of the polygon.
            top (:obj:`int`, `optional`, defaults to 0): The number of pixels to pad on the left side of the polygon.
            bottom (:obj:`int`, `optional`, defaults to 0): The number of pixels to pad on the right side of the polygon.
            safe_mode (:obj:`bool`, `optional`, defaults to True): A bool value to toggle the safe_mode.

        Returns:
            :obj:`BaseCoordElement`: The padded BaseCoordElement object.
        Nr   )r   leftrighttopbottom	safe_modes         r   padzBaseCoordElement.pad   rG   r   c                     dS )a  
        Shift the layout element by user specified amounts on x and y axis respectively. If shift_distance is one
        numeric value, the element will by shifted by the same specified amount on both x and y axis.

        Args:
            shift_distance (:obj:`numeric` or :obj:`Tuple(numeric)` or :obj:`List[numeric]`):
                The number of pixels used to shift the element.

        Returns:
            :obj:`BaseCoordElement`: The shifted BaseCoordElement of the same shape-specific class.
        Nr   )r   shift_distances     r   shiftzBaseCoordElement.shift   s	     	r      c                     dS )a  
        Scale the layout element by a user specified amount on x and y axis respectively. If scale_factor is one
        numeric value, the element will by scaled by the same specified amount on both x and y axis.

        Args:
            scale_factor (:obj:`numeric` or :obj:`Tuple(numeric)` or :obj:`List[numeric]`): The amount for downscaling or upscaling the element.

        Returns:
            :obj:`BaseCoordElement`: The scaled BaseCoordElement of the same shape-specific class.
        Nr   )r   scale_factors     r   scalezBaseCoordElement.scale   s	     	r   c                     dS )z
        Crop the input image according to the coordinates of the element.

        Args:
            image (:obj:`Numpy array`): The array of the input image.

        Returns:
            :obj:`Numpy array`: The array of the cropped image.
        Nr   )r   images     r   
crop_imagezBaseCoordElement.crop_image   s	     	r   c                 B      fd j         D             } j        |d<   |S )z
        Generate a dictionary representation of the current object:
            {
                "block_type": <"interval", "rectangle", "quadrilateral"> ,
                "non_empty_block_attr1": value1,
                ...
            }
        c                 R    i | ]#}t          |          |t          |          $S r7   )getattr)r   r   r   s     r   
<dictcomp>z,BaseCoordElement.to_dict.<locals>.<dictcomp>   s?     
 
 
tS!!- s##---r   
block_type)r5   r2   )r   datas   ` r   to_dictzBaseCoordElement.to_dict   s@    
 
 
 
~
 
 

 "Z\r   ri   c                 ~    | j         d         k    sJ dd                       | di fd| j        D             S )zInitialize an instance based on the dictionary representation

        Args:
            data (:obj:`dict`): The dictionary representation of the object
        rh   zIncompatible block types c                 "    i | ]}||         S r   r   )r   fri   s     r   rg   z.BaseCoordElement.from_dict.<locals>.<dictcomp>  s    888Qaa888r   r   )r2   r5   )clsri   s    `r   	from_dictzBaseCoordElement.from_dict  sd     Il++++;tL'9;; ,++ s998888#-888999r   N)T)r   r   r   r   T)r   )r]   )r$   r+   r,   propertyr   strr2   r   r5   r8   r;   r=   r?   rA   rC   rF   rK   boolrO   rR   rY   r\   r`   rc   r   r   rj   classmethodro   r   r   r   r.   r.   /   s       s    ^ X 49    ^ X   ^ X   ^ X   ^ X   ^ X   ^ X   ^&   ^$ ')%    ^.  1 4    ^
  - t    ^    ^$    ^    ^$   ^"c3h    $ :T#s(^ :0B : : : [: : :r   r.   N)
typingr   r   r   abcr   r   r   r
   r.   r   r   r   <module>rv      s    ) ( ( ( ( ( ( ( ( ( ( ( # # # # # # # #      ) ) ) ) ) ) ) )8c: c: c: c: c:s- c: c: c: c: c:r   