
    Ng'                     l   d dl mZ d dlZd dlmZ 	 dej        dedej        fdZdej        d	e	d
e	fdZ
dej        dej        dee         fdZdej        dej        dee         fdZd Zd Z ed          dd            Z ed          	 ddej        dee         dej        fd            Zd ZdS )    )ListN)requires_dependenciesboxesaxisreturnc                     |dv sJ t          j        | dd|ddf                   }t          j        |t                    }| dd|ddf         D ]\  }}|||xx         dz  cc<   |S )u  
    Obtain the projection histogram through a set of bboxes and finally output it in per-pixel form

    Args:
        boxes: [N, 4]
        axis: 0 - x coordinates are projected in the horizontal direction, 1 - y coordinates
        are projected in the vertical direction

    Returns:
        1D projection histogram, the length is the maximum value of the projection direction
        coordinate (we don’t need the actual side length of the picture because we just
        want to find the interval of the text box)
    )r      N   )dtyper	   )npmaxzerosint)r   r   lengthresstartends         ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/unstructured/partition/utils/xycut.pyprojection_by_bboxesr      s     6>>>>VE!!!TW1W*%&&F
(6
%
%
%CAAAtwQwJ'  
sE#I!J    
arr_values	min_valuemin_gapc                 l   t          j        | |k              d         }t          |          sdS |dd         |dd         z
  }t          j        ||k              d         }||         }||dz            }t          j        |d|d                   }t          j        ||d                   }	|	dz  }	||	fS )uU  Split projection profile:

    ```
                              ┌──┐
         arr_values           │  │       ┌─┐───
             ┌──┐             │  │       │ │ |
             │  │             │  │ ┌───┐ │ │min_value
             │  │<- min_gap ->│  │ │   │ │ │ |
         ────┴──┴─────────────┴──┴─┴───┴─┴─┴─┴───
         0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
    ```

    Args:
        arr_values (np.array): 1-d array representing the projection profile.
        min_value (float): Ignore the profile if `arr_value` is less than `min_value`.
        min_gap (float): Ignore the gap if less than this value.

    Returns:
        tuple: Start indexes and end indexes of split groups.
    r   Nr	   )r   whereleninsertappend)
r   r   r   	arr_indexarr_diffarr_diff_indexarr_zero_intvl_startarr_zero_intvl_end	arr_startarr_ends
             r   split_projection_profiler'   +   s    , i/003Iy>> 
 }y2.HXh011!4N$^4">A#56 	,a1>>Ii,im<<GqLGgr   indicesr   c                 8   t          |           t          |          k    sJ | dddf                                         }| |         }||         }t          |d          }t          |dd          }|sdS |\  }}	t	          ||	          D ]\  }
}|
|dddf         k    |dddf         |k     z  }||         }||         }|dddf                                         }||         }||         }t          |d          }t          |dd          }|s|\  }}t          |          dk    r|                    |           t	          ||          D ]C\  }}||dddf         k    |dddf         |k     z  }t          ||         ||         |           DdS )z

    Args:
        boxes: (N, 4)
        indices: during the recursion process, the index of box in the original data
         is always represented.
        res: save output

    Nr	   r   r   r   )r   argsortr   r'   zipextendrecursive_xy_cut)r   r(   r   _indicesy_sorted_boxesy_sorted_indicesy_projectionpos_yarr_y0arr_y1r0r1y_sorted_boxes_chunky_sorted_indices_chunkx_sorted_boxes_chunkx_sorted_indices_chunkx_projectionpos_xarr_x0arr_x1c0c1s                         r   r.   r.   V   s    u::W%%%%QQQT{""$$H8_Nx( (n1EEEL$\1a88E NFFff%%  B .A..>!!!Q$3G"3LM-h7!1(!;'1-55773H=!7!A ,2FQOOO(q!<< 	v;;!JJ-... &&)) 	 	FB2111a488=QRSRSRSUVRV=WZ\=\]H$X.&x0   	3 r   c                 8   t          |           t          |          k    sJ | dddf                                         }| |         }||         }t          |d          }t          |dd          }|sdS |\  }}	t	          ||	          D ]\  }
}|
|dddf         k    |dddf         |k     z  }||         }||         }|dddf                                         }||         }||         }t          |d          }t          |dd          }|s|\  }}t          |          dk    r|                    |           t	          ||          D ]C\  }}||dddf         k    |dddf         |k     z  }t          ||         ||         |           DdS )a"  
    Args:
        boxes: (N, 4) - Numpy array representing bounding boxes with shape (N, 4)
        where each row is (left, top, right, bottom)
        indices: An array representing indices that correspond to boxes in the original data
        res: A list to save the output results
    Nr   r*   r	   )r   r+   r   r'   r,   r-   recursive_xy_cut_swapped)r   r(   r   r/   x_sorted_boxesx_sorted_indicesr<   r=   r>   r?   r@   rA   r:   r;   r8   r9   r2   r3   r4   r5   r6   r7   s                         r   rC   rC      s    u::W%%%%QQQT{""$$H8_Nx( (n1EEEL$\1a88E NFF ff%%    B.A..>!!!Q$3G"3LM-h7!1(!; (1-55773H=!7!A ,2FQOOO(q!<< 	v;;!JJ-... &&)) 	 	FB2111a488=QRSRSRSUVRV=WZ\=\]H$$X.&x0   	5   r   c                 x   t          |           dk    sJ t          | d d d                   }t          | d d d                   }t          | dd d                   }t          | dd d                   }t          |d          }t          |d          }t          |d          }t          |d          }||||gS )N   r
   r	   r   )r   minr   )pointsleftrighttopbottoms        r   points_to_bboxrN      s    v;;! vccc{DsssE
fQTTl

C1FtQ<<D
c1++CqMME^^F#uf%%r   c                 $    | \  }}}}||||||||gS )N )bboxrJ   rL   rK   rM   s        r   bbox2pointsrR      s'    #D#uf#uc5&$??r   cv2r
   c                    dd l }|}|}|}|}|                    | |d         d         |d         d         f|d         d         |d         d         f||           |                    | |d         d         |d         d         f|d         d         |d         d         f||           |                    | |d         d         |d         d         f|d         d         |d         d         f||           |                    | |d         d         |d         d         f|d         d         |d         d         f||           | S )Nr   r	   )color	thicknessr
      )rS   line)	imgrI   rV   rU   rS   br2bl_colortl2tr_colortr2br_colorbl2tl_colors	            r   vis_polygonr^      s|   JJJKKKKHH	1vay|$	1vay|$     HH	1vay|$	1vay|$     HH	1vay|$	1vay|$     HH	1vay|$	1vay|$     Jr   r      r   rY   textsc                    ddl }t          j        |          }t          |          |j        d         k    sJ t          |          D ]\  }}t          | |                    dd          d|           t          |          }|\  }}	}
}||
z   dz  }|	|z   dz  }||         }|j	        }|
                    ||dd          d         }|                    | |dt          |          z  z
  ||d         z
  dz
  f|dt          |          z  z
  |d         z   |dz
  f|d          } |                    | ||dt          |          z  z
  |dz
  f|dd	d|j        
          } | S )z}

    Args:
        img:
        points: [N, 8]  8: x1,y1,x2,y2,x3,y3,x4,y4
        texts:
        color:

    Returns:

    r   Nr   r
   )rV   rU   g      ?   r	   )   rd   rd   )rV   lineType)rS   r   arrayr   shape	enumerater^   reshaperN   FONT_HERSHEY_SIMPLEXgetTextSize	rectangleputTextLINE_AA)rY   rI   ra   rU   rS   i_pointsrQ   rJ   rL   rK   rM   cxcytxtfontcat_sizes                    r   
vis_pointsrv     s   $ JJJXfFu::a(((('' 
 

7CQ//1EJJJJg&&#' c5&Ulq Flq Ah'??3c155a8mm!c#hh,Xa[ 01 45!c#hh,!,b1f5
 
 kk!c#hh,Q'[  	
 	
 Jr   c                     d t          t          |                    D             }t          |                                 ||          }|S )Nc                 ,    g | ]}t          |          S rP   )str).0ro   s     r   
<listcomp>z+vis_polygons_with_index.<locals>.<listcomp>E  s    000SVV000r   )ranger   rv   copy)imagerI   ra   res_imgs       r   vis_polygons_with_indexr   D  sA    00U3v;;//000Evu55GNr   )r
   N)r_   )typingr   numpyr   unstructured.utilsr   ndarrayr   r   floatr'   r.   rC   rN   rR   r^   ry   rv   r   rP   r   r   <module>r      s             4 4 4 4 4 4
 # "*    8( ( (PU ( ( ( (V8BJ 8 8$s) 8 8 8 8v9BJ 9 9$s) 9 9 9 9x& & & @ @ @
 u& & & &R u
 	4 4	4 94
 Z4 4 4 4n    r   