
    Ng(+                    L   d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
mZ d dlZd dlmZ d dlmZ e G d	 d
                      Zd%dZd&dZd'dZe G d d                      Ze G d d                      Z G d de          Z G d de          Z	 d(d)d!Zd*d$ZdS )+    )annotations)deepcopy)	dataclassfield)cached_property)OptionalUnionN)Source)safe_divisionc                  "   e Zd ZU ded<   ded<   ded<   ded<   d"dZd"dZd"d	Zed#d            Zed#d            Z	ed#d            Z
ed#d            Zd$dZd$dZd%d&dZd'dZed             Zd(dZed)d            Zd*dZd*dZd+d,d!ZdS )-	RectangleUnion[int, float]x1y1x2y2paddingc                V    |                      |                              |          }|S )zIncreases (or decreases, if padding is negative) the size of the rectangle by extending
        the boundary outward (resp. inward).)hpadvpadselfr   
out_objects      e/var/www/html/ai-engine/env/lib/python3.11/site-packages/unstructured_inference/inference/elements.pypadzRectangle.pad   s)     YYw'',,W55
    c                d    t          |           }|xj        |z  c_        |xj        |z  c_        |S )zIncreases (or decreases, if padding is negative) the size of the rectangle by extending
        the left and right sides of the boundary outward (resp. inward).)r   r   r   r   s      r   r   zRectangle.hpad   5     d^^
  r   c                d    t          |           }|xj        |z  c_        |xj        |z  c_        |S )zIncreases (or decreases, if padding is negative) the size of the rectangle by extending
        the top and bottom of the boundary outward (resp. inward).)r   r   r   r   s      r   r   zRectangle.vpad#   r   r   returnc                     | j         | j        z
  S )zWidth of rectangler   r   r   s    r   widthzRectangle.width+        w  r   c                     | j         | j        z
  S )zHeight of rectangler   r   r#   s    r   heightzRectangle.height0   r%   r   c                &    | j         | j        z   dz  S )z,Finds the horizontal midpoint of the object.   r"   r#   s    r   
x_midpointzRectangle.x_midpoint5        $'!Q&&r   c                &    | j         | j        z   dz  S )z*Finds the vertical midpoint of the object.r*   r'   r#   s    r   
y_midpointzRectangle.y_midpoint:   r,   r   otherboolc                .    |                      |           S )zAChecks whether this rectangle is disjoint from another rectangle.)
intersectsr   r/   s     r   is_disjointzRectangle.is_disjoint?   s    ??5))))r   c                    |                                  s|                                 rdS t          | |          d         S )z;Checks whether this rectangle intersects another rectangle.F)r      )	_has_noneintersectionsr3   s     r   r2   zRectangle.intersectsC   s?    >> 	u00 	5T5))$//r   Nerror_marginOptional[Union[int, float]]c                    ||                     |          n|}t          | j        |j        k    | j        |j        k    | j        |j        k    | j        |j        k    g          S )zDChecks whether this rectangle is contained within another rectangle.)r   allr   r   r   r   )r   r/   r9   padded_others       r   is_inzRectangle.is_inI   se    2>2Juyy...PULO+LO+LO+LO+	
 
 	
r   c                `    t          | j        du | j        du | j        du | j        du f          S )z)return false when one of the coord is nanN)anyr   r   r   r   r#   s    r   r7   zRectangle._has_noneU   s2    DGtOTW_dgotwRVWXXXr   c                n    | j         | j        f| j         | j        f| j        | j        f| j        | j        ffS )z!Gets coordinates of the rectangle)r   r   r   r   r#   s    r   coordinateszRectangle.coordinatesY   s9     $'"TWdg$6$'8JTWVZV]L^__r   Optional[Rectangle]c                f   |                                  s|                                 rdS t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }||k    s||k    rdS t          ||||          S )zoGives the rectangle that is the intersection of two rectangles, or None if the
        rectangles are disjoint.N)r7   maxr   minr   r   r   r   )r   r/   r   r   r   r   s         r   intersectionzRectangle.intersection^   s     >> 	u00 	4%(##%(##%(##%(##77b2gg4RR(((r   floatc                     | j         | j        z  S )z Gives the area of the rectangle.)r$   r(   r#   s    r   areazRectangle.areak   s     zDK''r   c                    |                      |          }|dn|j        }| j        |j        z   |z
  }t          ||          S )zGives the intersection-over-union of two rectangles. This tends to be a good metric of
        how similar the regions are. Returns 0 for disjoint rectangles, 1 for two identical
        rectangles -- area of intersection / area of union.N        rG   rJ   r   )r   r/   rG   intersection_area
union_areas        r   intersection_over_unionz!Rectangle.intersection_over_unionp   sN     ((//#/#7CC\=NY+.??
.
;;;r   c                    |                      |          }|dn|j        }t          | j        |j                  }t          ||          S )zGives the area-of-intersection over the minimum of the areas of the rectangles. Useful
        for identifying when one rectangle is almost-a-subset of the other. Returns 0 for disjoint
        rectangles, 1 when either is a subset of the other.NrL   )rG   rJ   rF   r   )r   r/   rG   rN   min_areas        r   intersection_over_minimumz#Rectangle.intersection_over_minimumy   sM     ((//#/#7CC\=Nty%*--.999r         ?subregion_thresholdc                    |                      |          }|dn|j        }|t          || j                  k     o| j        |j        k    S )zReturns whether this region is almost a subregion of other. This is determined by
        comparing the intersection area over self area to some threshold, and checking whether self
        is the smaller rectangle.NrL   rM   )r   r/   rU   rG   rN   s        r   is_almost_subregion_ofz Rectangle.is_almost_subregion_of   sT     ((//#/#7CC\=N#m4Ety&Q&QQ 
I#	
r   )r   r   )r    r   )r/   r   r    r0   N)r/   r   r9   r:   r    r0   )r    r0   )r/   r   r    rC   )r    rH   )r/   r   r    rH   rT   )r/   r   rU   rH   r    r0   )__name__
__module____qualname____annotations__r   r   r   propertyr$   r(   r+   r.   r4   r2   r>   r7   rB   rG   rJ   rP   rS   rW    r   r   r   r      s                  ! ! ! X! ! ! ! X! ' ' ' X' ' ' ' X'* * * *0 0 0 0

 

 

 

 

Y Y Y Y ` ` X`) ) ) ) ( ( ( X(< < < <: : : :
 
 
 
 
 
 
r   r   regionsr    c                     t          d | D                       }t          d | D                       }t          d | D                       }t          d | D                       }t          ||||          S )zHReturns the smallest rectangular region that contains all regions passedc              3  $   K   | ]}|j         V  d S rX   )r   .0regions     r   	<genexpr>z,minimal_containing_region.<locals>.<genexpr>   $      --6VY------r   c              3  $   K   | ]}|j         V  d S rX   )r   rc   s     r   rf   z,minimal_containing_region.<locals>.<genexpr>   rg   r   c              3  $   K   | ]}|j         V  d S rX   )r   rc   s     r   rf   z,minimal_containing_region.<locals>.<genexpr>   rg   r   c              3  $   K   | ]}|j         V  d S rX   )r   rc   s     r   rf   z,minimal_containing_region.<locals>.<genexpr>   rg   r   )rF   rE   r   )r`   r   r   r   r   s        r   minimal_containing_regionrk      s    	--W---	-	-B	--W---	-	-B	--W---	-	-B	--W---	-	-BRR$$$r   rectsc                 \    t          j        d | D                       }t          |          S )zReturns a square boolean matrix of intersections of an arbitrary number of rectangles, i.e.
    the ijth entry of the matrix is True if and only if the ith Rectangle and jth Rectangle
    intersect.c                B    g | ]}|j         |j        |j        |j        gS r_   )r   r   r   r   )rd   rs     r   
<listcomp>z!intersections.<locals>.<listcomp>   s)    ???AadAD!$/???r   )nparraycoords_intersections)rl   coordss     r   r8   r8      s0    
 X?????@@F'''r   rt   
np.ndarrayc                   | dddf         | dddf         | dddf         | dddf         f\  }}}}|d         |d         k    |d         |d         k    z  |d         |d         k    j         z  |d         |d         k    j         z   }|S )zReturns a square boolean matrix of intersections of given stack of coords, i.e.
    the ijth entry of the matrix is True if and only if the ith coords and jth coords
    intersect.Nr   r6   r*      ).N)T)rt   x1sy1sx2sy2sr8   s         r   rs   rs      s      1vaaad|VAAAqD\6!!!Q$<OCc3 
TS^	#t9s9~%	't9s9~%
(	) t9s9~%
(	)M r   c                  \    e Zd ZU ded<   dZded<   dZded<   dd
Ze	 	 ddd            ZdS )
TextRegionr   bboxNOptional[str]textOptional[Source]sourcer    strc                *    t          | j                  S rX   )r   r   r#   s    r   __str__zTextRegion.__str__   s    49~~r   r   r   r   r   r   c                >    t          ||||          } | d|||d|S )z%Constructs a region from coordinates.)r   r   r   r_   )r   )	clsr   r   r   r   r   r   kwargsr   s	            r   from_coordszTextRegion.from_coords   s6     RR((sAV$AA&AAAr   )r    r   )NN)r   r   r   r   r   r   r   r   r   r   r   r   r    r~   )	rZ   r[   r\   r]   r   r   r   classmethodr   r_   r   r   r~   r~      s         OOOD#F####     ##'B B B B [B B Br   r~   c                      e Zd ZU ded<    ed           Zded<   dZded<   d	 ZddZd Z	e
dd            Zd Zed             Zed             Zed             Zed             Zedd            ZdS )TextRegionsru   element_coordsc                 *    t          j        g           S rX   )rq   rr   r_   r   r   <lambda>zTextRegions.<lambda>   s    bhrll r   )default_factorytextsNzSource | Noner   c                    | j         j        dk    r?| j        j        dk    r1t          j        d g| j        j        d         z            | _         d S d S d S Nr   )r   sizer   rq   rr   shaper#   s    r   __post_init__zTextRegions.__post_init__   sV    :?aD$7$<q$@$@4&4+>+DQ+G"GHHDJJJ  $@$@r   r    c                \    t          | j        |         | j        |         | j                  S )z#slice text regions based on indicesr   r   r   )r   r   r   r   )r   indicess     r   slicezTextRegions.slice   s3    .w7*W%;
 
 
 	
r   c                      j          fd j        D             S  fdt           j         j                   D             S )z9return a list of TextRegion objects representing the dataNc           
     b    g | ]+\  }}}}t                               ||||d j                  ,S rX   r~   r   r   )rd   r   r   r   r   r   s        r   rp   z'TextRegions.as_list.<locals>.<listcomp>   sK       $RR &&r2r2tT[II  r   c           
     h    g | ].\  \  }}}}}t                               |||||j                  /S r_   r   )rd   r   r   r   r   r   r   s         r   rp   z'TextRegions.as_list.<locals>.<listcomp>   sP     
 
 
& RR$ ""2r2r4EE
 
 
r   )r   r   zipr#   s   `r   as_listzTextRegions.as_list   ss    :   (,(;   
 
 
 
*-d.A4:*N*N
 
 
 	
r   r`   listc                D   g g }}|D ]]}|                     |j        j        |j        j        |j        j        |j        j        f           |                     |j                   ^ | t          j        |          t          j        |          |d         j	                  S )zccreate TextRegions from a list of TextRegion objects; the objects must have the same
        sourcer   r   )
appendr   r   r   r   r   r   rq   rr   r   )r   r`   rt   r   re   s        r   	from_listzTextRegions.from_list   s     B 	& 	&FMM6;>6;>6;>6;>Z[[[LL%%%%s"(6"2"2"(5//RYZ[R\Rcddddr   c                &    | j         j        d         S r   )r   r   r#   s    r   __len__zTextRegions.__len__   s    "(++r   c                $    | j         dddf         S )zleft coordinateNr   r   r#   s    r   r   zTextRegions.x1        "111a4((r   c                $    | j         dddf         S )ztop coordinateNr6   r   r#   s    r   r   zTextRegions.y1  r   r   c                $    | j         dddf         S )zright coordinateNr*   r   r#   s    r   r   zTextRegions.x2  r   r   c                $    | j         dddf         S )zbottom coordinateNrw   r   r#   s    r   r   zTextRegions.y2  r   r   c                @    | j         | j        z
  | j        | j        z
  z  S )z7areas of each region; only compute it when it is needed)r   r   r   r   r#   s    r   areaszTextRegions.areas  s!     $'!dg&788r   )r    r   )r`   r   )r    ru   )rZ   r[   r\   r]   r   r   r   r   r   r   r   r   r   r^   r   r   r   r   r   r   r_   r   r   r   r      sI        .B.BCCCECCCC F    I I I
 
 
 


 

 

 e e e [e, , , ) ) X) ) ) X) ) ) X) ) ) X) 9 9 9 _9 9 9r   r   c                      e Zd ZdS )EmbeddedTextRegionNrZ   r[   r\   r_   r   r   r   r             Dr   r   c                      e Zd ZdS )ImageTextRegionNr   r_   r   r   r   r     r   r   r   rT   region1region2same_region_thresholdrH   r0   c                4    |                      |          |k    S )zReturns whether bounding boxes are almost the same. This is determined by checking if the
    intersection over union is above some threshold.)rP   )r   r   r   s      r   )region_bounding_boxes_are_almost_the_samer     s     **7336KKKr   region_to_growregion_to_matchc                    t          | |          j        \  \  }}}\  }}}||||f\  | _        | _        | _        | _        dS )zEGrows a region to the minimum size necessary to contain both regions.N)rk   rB   r   r   r   r   )r   r   new_x1new_y1_new_x2new_y2s          r   grow_region_to_match_regionr   (  sb    /H0 0  -VVa)&&1
 		RNN~(.*;^=N=N=Nr   )r`   r   r    r   )rl   r   )rt   ru   r    ru   rY   )r   r   r   r   r   rH   r    r0   )r   r   r   r   )
__future__r   copyr   dataclassesr   r   	functoolsr   typingr   r	   numpyrq    unstructured_inference.constantsr
   unstructured_inference.mathr   r   rk   r8   rs   r~   r   r   r   r   r   r_   r   r   <module>r      s"   " " " " " "       ( ( ( ( ( ( ( ( % % % % % % " " " " " " " "     3 3 3 3 3 3 5 5 5 5 5 5 {
 {
 {
 {
 {
 {
 {
 {
|% % % %( ( ( (   . B B B B B B B B2 A9 A9 A9 A9 A9 A9 A9 A9H	 	 	 	 	 	 	 		 	 	 	 	j 	 	 	 $(L L L L L     r   