a
    ¡˜ëjÚ'  ã                   @   sÐ   d dl mZ d dlZd dlmZ ejeejdœdd„Zeje	e	dœdd	„Z
ejejee d
œdd„Zejejee d
œdd„Zdd„ Zdd„ Zedƒddd„ƒZedƒdejee ejdœdd„ƒZdd„ ZdS )é    )ÚListN)Úrequires_dependencies)ÚboxesÚaxisÚreturnc                 C   sn   |dv sJ ‚t  | dd…|dd…f ¡}t j|td�}| dd…|dd…f D ]\}}|||…  d7  < qL|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é   )Zdtyper   )ÚnpÚmaxÚzerosÚint)r   r   ÚlengthÚresÚstartÚend© r   úa/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/utils/xycut.pyÚprojection_by_bboxes   s    r   )Ú
arr_valuesÚ	min_valueÚmin_gapc           
      C   sŽ   t  | |k¡d }t|ƒsdS |dd… |dd…  }t  ||k¡d }|| }||d  }t  |d|d ¡}t  ||d ¡}	|	d7 }	||	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   Z	arr_indexZarr_diffZarr_diff_indexZarr_zero_intvl_startZarr_zero_intvl_endZ	arr_startZarr_endr   r   r   Úsplit_projection_profile+   s    r   )r   Úindicesr   c                 C   sf  t | ƒt |ƒksJ ‚| dd…df  ¡ }| | }|| }t|dd�}t|ddƒ}|sXdS |\}}	t||	ƒD ]ö\}
}|
|dd…df k|dd…df |k @ }|| }|| }|dd…df  ¡ }|| }|| }t|dd�}t|ddƒ}|sìqj|\}}t |ƒdk�r| |¡ qjt||ƒD ]F\}}||dd…df k|dd…df |k @ }t|| || |ƒ �qqjd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   Ú_indicesZy_sorted_boxesZ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Úc1r   r   r   r"   V   s>    (
(ýr"   c                 C   sf  t | ƒt |ƒksJ ‚| dd…df  ¡ }| | }|| }t|dd�}t|ddƒ}|sXdS |\}}	t||	ƒD ]ö\}
}|
|dd…df k|dd…df |k @ }|| }|| }|dd…df  ¡ }|| }|| }t|dd�}t|ddƒ}|sìqj|\}}t |ƒdk�r| |¡ qjt||ƒD ]F\}}||dd…df k|dd…df |k @ }t|| || |ƒ �qqjd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#   Zx_sorted_boxesZx_sorted_indicesr.   r/   r0   r1   r2   r3   r,   r-   r*   r+   r$   r%   r&   r'   r(   r)   r   r   r   r4   ‘   s>    
(
(ýr4   c                 C   sŒ   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Úbottomr   r   r   Úpoints_to_bboxÍ   s    



r<   c                 C   s    | \}}}}||||||||gS )Nr   )Úbboxr8   r:   r9   r;   r   r   r   Úbbox2pointsÝ   s    r>   Úcv2r   c           	      C   s  dd l }|}|}|}|}|j| |d d |d d f|d d |d d f||d� |j| |d d |d d f|d d |d d f||d� |j| |d d |d d f|d d |d d f||d� |j| |d d |d d f|d d |d d f||d� | S )Nr   r   )ÚcolorÚ	thicknessr   é   )r?   Úline)	Úimgr7   rA   r@   r?   Zbr2bl_colorZtl2tr_colorZtr2br_colorZbl2tl_colorr   r   r   Úvis_polygonâ   sD    ûûûûrE   ©r   éÈ   r   )rD   Útextsr   c                 C   s  ddl }t |¡}t|ƒ|jd ks(J ‚t|ƒD ]è\}}t| | dd¡d|d� t|ƒ}|\}}	}
}||
 d }|	| d }|| }|j	}| 
||dd¡d }| | |dt|ƒ  ||d  d f|dt|ƒ  |d  |d f|d¡} |j| ||dt|ƒ  |d f|dd	d|jd
�} q0| S )z}

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

    Returns:

    r   Nr   r   )rA   r@   g      à?é   r   )éÿ   rJ   rJ   )rA   ZlineType)r?   r	   Úarrayr   ÚshapeÚ	enumeraterE   Zreshaper<   ZFONT_HERSHEY_SIMPLEXZgetTextSizeZ	rectangleZputTextZLINE_AA)rD   r7   rH   r@   r?   ÚiZ_pointsr=   r8   r:   r9   r;   ZcxÚcyÚtxtÚfontZcat_sizer   r   r   Ú
vis_points  s<    
ûørR   c                 C   s*   dd„ t t|ƒƒD ƒ}t|  ¡ ||ƒ}|S )Nc                 S   s   g | ]}t |ƒ‘qS r   )Ústr)Ú.0rN   r   r   r   Ú
<listcomp>E  ó    z+vis_polygons_with_index.<locals>.<listcomp>)Úranger   rR   Úcopy)Úimager7   rH   Zres_imgr   r   r   Úvis_polygons_with_indexD  s    rZ   )r   N)rF   )Útypingr   Únumpyr	   Zunstructured.utilsr   Zndarrayr   r   Úfloatr   r"   r4   r<   r>   rE   rS   rR   rZ   r   r   r   r   Ú<module>   s$   
+;<) üû7