a
    ¡˜ëjH`  ã                	   @   sì  d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZmZ d dlZd dlmZmZ d d	lmZmZmZ d d
lmZ d dl m!Z! d dl"m#Z# edejej$ƒZ%dd„ Z&dZ'dZ(G dd„ de	ƒZ)eG dd„ dƒƒZ*eG dd„ dƒƒZ+e,e-e.e.e.f dœdd„Z/e-e.e.e.e.f e-e.e.f e0dœdd„Z1dGe-e.e.e.e.f e-e.e.f e.e.e0e0e.d œd!d"„Z2dHe-e.e.e.e.f e-e.e.f e.e.e.d%œd&d'„Z3dIe-e.e.e.e.f e-e.e.f e.e.e0d*œd+d,„Z4dJee,e-e.e.e.f f e0e-e,e-e.e.e.f f d-œd.d/„Z5e)e-e.e.e.e.f e.e.d0œd1d2„Z6eje,e-e.e.e.e.f e)e.e,d3œd4d5„Z7eje+e,d6œd7d8„Z8G d9d:„ d:eƒZ9G d;d<„ d<e9eƒZ:G d=d>„ d>e:ƒZ;G d?d@„ d@e:ƒZ<G dAdB„ dBe9ƒZ=G dCdD„ dDe9ƒZ>G dEdF„ dFe!ƒZ?dS )Ké    N)ÚABCÚabstractmethod)Ú	dataclass)ÚEnum)ÚBytesIO)ÚPath)ÚAnyÚ	GeneratorÚListÚOptionalÚTypeVarÚUnion)ÚcolorsÚfont_manager)ÚImageÚ	ImageDrawÚ	ImageFont)ÚElementType)ÚAnalysisProcessor)Úconvert_pdf_to_imageÚ	PageImagec                  C   sJ   dg} t  ¡ }|stdƒ‚| D ]"}|D ]}||v r&|    S q&q|d S )Nz	Arial.ttfzNo fonts availabler   )r   ZfindSystemFontsÚ
ValueError)Zpreferred_fontsZavailable_fontsÚfontZavailable_font© r   ú{/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/pdf_image/analysis/bbox_visualisation.pyÚget_font   s    r   )Zwhite©éÿ   r   r   )Zblack©r   r   r   c                   @   s    e Zd ZdZdZdZdZdZdS )ÚTextAlignmentÚtop_leftÚ	top_rightÚbottom_leftÚbottom_rightÚcenterN)Ú__name__Ú
__module__Ú__qualname__ÚTOP_LEFTÚ	TOP_RIGHTÚBOTTOM_LEFTÚBOTTOM_RIGHTÚCENTERr   r   r   r   r   &   s
   r   c                   @   s^   e Zd ZU dZee ed< dZee ed< dZee ed< dZ	ee ed< dZ
ee ed< dS )Ú
BboxLabelsNr    r!   r"   r#   r$   )r%   r&   r'   r    r   ÚstrÚ__annotations__r!   r"   r#   r$   r   r   r   r   r-   .   s
   
r-   c                   @   s2   e Zd ZU eeeeef ed< dZee ed< dS )ÚBBoxÚpointsNÚlabels)	r%   r&   r'   ÚtupleÚintr/   r2   r   r-   r   r   r   r   r0   7   s   
r0   )ÚcolorÚreturnc                 C   sZ   zt  | ¡}W n ty*   tdƒ ‚ Y n0 t|d d ƒt|d d ƒt|d d ƒfS )zÂConvert a color name to RGB values.

    Args:
        color: A color name supported by matplotlib.

    Returns:
        A tuple of three integers representing the RGB values of the color.
    ÚErrorr   r   é   é   )r   Zto_rgbr   Úprintr4   )r5   Z
rgb_colorsr   r   r   Úget_rgb_color=   s    	r;   )ÚbboxÚ	page_sizer6   c                 C   sX   | \}}}}|\}}t  |d |d  ¡}|| }	|| }
t  |
d |	d  ¡}|| S )a  Compute the ratio of the bounding box to the page size.

    Args:
        bbox: Tuple containing coordinates of the bbox: (x1, y1, x2, y2).
        page_size: Tuple containing page size: (width, height).

    Returns:
        The ratio of the bounding box to the page size.
    r9   )ÚmathÚsqrt)r<   r=   Úx1Úy1Úx2Úy2Z
page_widthZpage_heightZpage_diagonalZ
bbox_widthZbbox_heightZbbox_diagonalr   r   r   Ú_get_bbox_to_page_ratioN   s    
rD   ç{®Gáz„?ç      à?)r<   r=   Ú	min_valueÚ	max_valueÚratio_for_min_valueÚratio_for_max_valuer6   c           	      C   sH   t | |ƒ}t ||f||fd¡}t||d  |d  ƒ}t|t||ƒƒS )a—  Compute the optimal value for a given bounding box using a linear function
    generated for given min and max values and ratios

    Args:
        bbox: Tuple containing coordinates of the bbox: (x1, y1, x2, y2).
        page_size: Tuple containing page size: (width, height).
        min_value: The minimum value returned by the function.
        max_value: The maximum value returned by the function.
        ratio_for_min_value: The ratio of the bbox to page size for the min value.
        ratio_for_max_value: The ratio of the bbox to page size for the max value.

    Returns:
        The optimal value for the given bounding box and parameters given.
    r8   r   )rD   ÚnpZpolyfitr4   ÚmaxÚmin)	r<   r=   rG   rH   rI   rJ   Zbbox_to_page_ratioZcoefficientsÚvaluer   r   r   Ú_get_optimal_value_for_bboxa   s    
rO   é   é    )r<   r=   Úmin_font_sizeÚmax_font_sizer6   c                 C   s   t | |||d�S )a§  Compute the optimal font size for a given bounding box.

    Args:
        bbox: Tuple containing coordinates of the bbox: (x1, y1, x2, y2).
        page_size: Tuple containing page size: (width, height).
        min_font_size: The minimum font size returned by the function.
        max_font_size: The maximum font size returned by the function.

    Returns:
        The optimal font size for the given bounding box.
    ©r<   r=   rG   rH   ©rO   )r<   r=   rR   rS   r   r   r   Úget_bbox_text_size}   s    ürV   r8   é   )r<   r=   Úmin_thicknessÚmax_thicknessr6   c                 C   s   t | |||d�S )an  Compute the optimal thickness for a given bounding box.

    Args:
        bbox: Tuple containing coordinates of the bbox: (x1, y1, x2, y2).
        page_size: Tuple containing page size: (width, height).
        min_thickness: The minimum font size returned by the function.
        max_thickness: The maximum font size returned by the function.

    Returns:

    rT   rU   )r<   r=   rX   rY   r   r   r   Úget_bbox_thickness–   s    ürZ   )Úbackground_colorÚbrightness_thresholdr6   c                 C   sN   t | tƒrt| ƒ} d| d  d| d   d| d   d }||krFtS tS dS )zõReturns the contrastive text color (black or white) for a given background color.

    Args:
        background_color: Tuple containing RGB values of the background color.

    Returns:
        Tuple containing RGB values of the text color.
    g‰A`åÐ"Ó?r   gbX9´Èâ?r8   gÉv¾Ÿ/½?r   N)Ú
isinstancer.   r;   ÚCOLOR_BLACKÚCOLOR_WHITE)r[   r\   Zbackground_brightnessr   r   r   Úget_text_color¯   s    
"þr`   )Ú	alignmentÚbbox_pointsÚ
text_widthÚtext_heightc                 C   sÚ  t t|d ƒdƒ}t t|d ƒdƒ}||d  }|| }|\}}	}
}| tju rèt|d d ƒ}t|d d ƒ}||
| d  |	||	 d  f}|d | |d | f|d | |d | ff}|d | t|d ƒ |d |d  f}nê| tju �r||	| f|| |	ff}|| |	| f}n´| tju �rT|
| |	f|
|	| ff}|
| | |	f}n~| tju �rŠ||| f|| |ff}|| || f}nH| tju �rÄ|
| || f|
|ff}|
| | || f}ntd| › �ƒ‚||fS )	Ngš™™™™™É?é
   g333333Ó?r9   gÍÌÌÌÌÌð?r   r8   zUnknown alignment )	rL   r4   r   r,   r(   r)   r*   r+   r   )ra   rb   rc   rd   ÚindentZvertical_correctionZ
rect_widthZrect_heightr@   rA   rB   rC   Zhorizontal_halfZvertical_halfZcenter_pointÚlabel_rectangleÚlabel_coordsr   r   r   Úget_label_rect_and_coordsÅ   sR    
 

þ

þûþ

þ

þ

þþri   )Ú
image_drawÚtextrb   ra   Ú	font_sizer[   c              
   C   sÚ   t  tƒ |¡}| jd||dd�\}}}	}
|	| }|
| }t||||ƒ\}}t|ƒ}z| j|||d� W n" tyˆ   | j|||d� Y n0 t|ƒ\}}z| j	||||dd� W n& tyÔ   | j	||||dd� Y n0 dS )aº  Draw a label stick to a bounding box.
    The alignment parameter specifies where the label should be placed.

    Args:
        image_draw:         ImageDraw object to draw on the image.
        text:               Text to draw.
        bbox_points:        Bounding box points.
        alignment:          Text alignment.
        font_size:          Font size of the text.
        background_color:   RGB values of the background color.
    ©r   r   r$   ©r   Úalign)ÚfillÚoutline)rp   r   ro   N)
r   Útruetyper   Útextbboxri   r;   Ú	rectangleÚ	TypeErrorr`   rk   )rj   rk   rb   ra   rl   r[   r   Útext_x1Útext_y1Útext_x2Útext_y2rc   rd   rg   rh   Zrgb_background_colorZ
text_colorZtext_color_rgbr   r   r   Údraw_bbox_label  s6    ÿÿý
ýrz   )rj   r<   r5   c                 C   s~  |j \}}}}||ks||kr2td|j › �ƒ dS ||f}||f}t|j | jjd�}	t|j | jjd�}
z| j||f||	d� W n. ty¬   t|ƒ}| j||f||	d� Y n0 |j	du�rz|j	j
 }rÞt| ||j tj|
|d� |j	j }�rt| ||j tj|
|d� |j	j }�r*t| ||j tj|
|d� |j	j }�rPt| ||j tj|
|d� |j	j }�rzt| ||j tj|
d |d� dS )a  Draw bbox with additional labels on the image..

    Args:
        image_draw:     ImageDraw object to draw on the image.
        bbox:           Bounding box to draw.
        color:          RGB values of the color of the bounding box (edges + label backgrounds).
    zInvalid bbox coordinates: N)r<   r=   )rq   Úwidth)rb   ra   rl   r[   r9   )r1   r:   rZ   ZimÚsizerV   rt   ru   r;   r2   r    rz   r   r(   r!   r)   r"   r*   r#   r+   r$   r,   )rj   r<   r5   r@   rA   rB   rC   r    r#   Zbox_thicknessrl   Z	rgb_colorZtop_left_labelZtop_right_labelZbottom_left_labelZbottom_right_labelZcenter_labelr   r   r   Údraw_bbox_on_image7  sv    úúúúúr}   c                   @   sd   e Zd ZU dZeed< eed< edœdd„Zeje	ejdœdd	„Z
ee	eeeef d
œdd„ƒZdS )ÚLayoutDrawerÚunknownÚlayout_sourceZlaytout_dump©Úlayout_dumpc                 C   s
   || _ d S ©Nr�   ©Úselfr‚   r   r   r   Ú__init__†  s    zLayoutDrawer.__init__)Ú
page_imageÚpage_numr6   c           	      C   s¾   | j  d¡}|s(td| jj› d�ƒ |S t|ƒ|k rRtd|› dt|ƒ› d�ƒ |S t |¡}||d  }|| d¡kr”| d¡}td	|› d
|› �ƒ t|d dƒD ]\}}|  |||¡ q¢|S )z=Draw the layout bboxes with additional metadata on the image.ZpageszWarning: layout in drawer z is empty - skippingzError! Page z not found in layout (pages: ú)r8   ÚnumberzWarning: Requested page num z differs from dump Úelements)	r‚   Úgetr:   Ú	__class__r%   Úlenr   Ú	enumerateÚrender_element_on_page)	r…   r‡   rˆ   Zlayout_pagesrj   Zpage_layout_dumpZdump_page_numÚidxr‹   r   r   r   Údraw_layout_on_page‰  s    

z LayoutDrawer.draw_layout_on_page©r‘   rj   r‹   c                 C   s   dS )z#Draw a single element on the image.Nr   )r…   r‘   rj   r‹   r   r   r   r�   ›  s    z#LayoutDrawer.render_element_on_pageN)r%   r&   r'   r€   r.   r/   Údictr†   r   r4   r’   r   r   r   r�   r   r   r   r   r~   ‚  s   
r~   c                   @   sH   e Zd ZU eed< dZeed< dZeed< ee	e
eef dœdd„ZdS )	ÚSimpleLayoutDrawerr5   FÚ
show_orderÚshow_text_lengthr“   c                 C   s†   |  d¡rt|d ƒnd}|  d¡}| jr0|› nd }| jrDd|› �nd }t|d t|rbd|d›�nd ||d�d	�}t||| jd
� d S )Nrk   r   Úprobúlen: r<   úprob: ú.2f)r!   r"   r$   ©r1   r2   ©r5   )rŒ   rŽ   r–   r—   r0   r-   r}   r5   )r…   r‘   rj   r‹   Útext_lenÚelement_probZelement_orderr<   r   r   r   r�   ¥  s    
ýþz)SimpleLayoutDrawer.render_element_on_pageN)r%   r&   r'   r.   r/   r–   Úboolr—   r4   r   r”   r   r�   r   r   r   r   r•      s   
r•   c                       s*   e Zd ZdZdeedœ‡ fdd„Z‡  ZS )ÚPdfminerLayoutDrawerZpdfminerÚred©r‚   r5   c                    s"   || _ || _d| _tƒ  |¡ d S )NT)r‚   r5   r–   Úsuperr†   ©r…   r‚   r5   ©r�   r   r   r†   ¸  s    zPdfminerLayoutDrawer.__init__)r¢   ©r%   r&   r'   r€   r”   r.   r†   Ú__classcell__r   r   r¦   r   r¡   µ  s   r¡   c                       s*   e Zd ZdZdeedœ‡ fdd„Z‡  ZS )ÚOCRLayoutDrawerZocrr¢   r£   c                    s"   || _ d| _d| _tƒ  |¡ d S )NF)r5   r–   r—   r¤   r†   r¥   r¦   r   r   r†   Â  s    zOCRLayoutDrawer.__init__)r¢   r§   r   r   r¦   r   r©   ¿  s   r©   c                   @   s€   e Zd ZdZejdejdejdejdej	dej
dejdejd	ejd
ejdejdiZeeeeef dœdd„Zeedœdd„ZdS )ÚODModelLayoutDrawerZod_modelZsalmonÚorangeÚmediumpurpleZnavyZdeeppinkÚgreenÚsiennaZ
darkorangeÚblueÚ	turquoiseÚgreenyellowr“   c           	      C   s\   |d }|  d¡}|d }|  |¡}t|t|› |r>d|d›�nd d�d�}t|||d� d S )	NÚtyper˜   r<   rš   r›   )r    r!   rœ   r�   )rŒ   Úget_element_type_colorr0   r-   r}   )	r…   r‘   rj   r‹   Úelement_typerŸ   rb   r5   r<   r   r   r   r�   Ú  s    

þþz*ODModelLayoutDrawer.render_element_on_page©r´   r6   c                 C   s   | j  |d¡S ©NZcyan©Ú	color_maprŒ   ©r…   r´   r   r   r   r³   è  s    z*ODModelLayoutDrawer.get_element_type_colorN)r%   r&   r'   r€   r   ZCAPTIONZFOOTNOTEZFORMULAZ	LIST_ITEMZPAGE_FOOTERZPAGE_HEADERZPICTUREZSECTION_HEADERZTABLEZTEXTZTITLEr¸   r4   r   r”   r.   r   r�   r³   r   r   r   r   rª   É  s   õrª   c                   @   sh   e Zd ZdZdddddddd	d
dddddœZedœdd„Zeeee	e
f dœdd„Ze	e	dœdd„ZdS )ÚFinalLayoutDrawerÚfinalZbrownr¢   r±   r°   r­   r«   r®   r¯   ZgoldZlightskybluer¬   ZmagentaZcrimson)ZCheckBoxZListItemZTitleZNarrativeTextÚHeaderZFooterZFigureCaptionImageZTableZAddressZEmailAddressZFormulaZCodeSnippetZ
PageNumberr�   c                 C   s
   || _ d S rƒ   r�   r„   r   r   r   r†   ÿ  s    zFinalLayoutDrawer.__init__r“   c              	   C   sœ   |d }|  d¡}|  d¡r(t|d ƒnd}|d }|  |¡}|  d¡}	t|t|› |rbd|d›�nd d	|› �|	rzd
|	› �nd |› d�d�}
t||
|d� d S )Nr²   r˜   rk   r   r<   Úclusterrš   r›   r™   zcl: )r    r!   r#   r"   r$   rœ   r�   )rŒ   rŽ   r³   r0   r-   r}   )r…   r‘   rj   r‹   r´   rŸ   rž   rb   r5   r½   r<   r   r   r   r�     s"    


ûþ
z(FinalLayoutDrawer.render_element_on_pagerµ   c                 C   s   | j  |d¡S r¶   r·   r¹   r   r   r   r³     s    z(FinalLayoutDrawer.get_element_type_colorN)r%   r&   r'   r€   r¸   r”   r†   r4   r   r.   r   r�   r³   r   r   r   r   rº   ì  s$   órº   c                
       s¨   e Zd Zdeeeef  eeeef ee eeee	 edœ‡ fdd„Z
edœd	d
„Zdd„ Zejedœdd„Zeej ejdœdd„Zeejddf dœdd„Z‡  ZS )ÚAnalysisDrawerNTFÚpng)ÚfilenameÚis_imageÚsave_dirÚfileÚdraw_captionÚ	draw_gridÚresizeÚformatc	           	         s<   || _ || _|| _|| _|| _g | _|| _tƒ  ||¡ d S rƒ   )	rÄ   rÅ   rÆ   rÁ   rÇ   ÚdrawersrÃ   r¤   r†   )	r…   rÀ   rÁ   rÂ   rÃ   rÄ   rÅ   rÆ   rÇ   r¦   r   r   r†     s    zAnalysisDrawer.__init__)Údrawerc                 C   s   | j  |¡ d S rƒ   )rÈ   Úappend)r…   rÉ   r   r   r   Ú
add_drawer0  s    zAnalysisDrawer.add_drawerc           
      C   sª  t | jƒj}t | jƒd | d }|jddd� t|  ¡ ƒD �]f\}}g }|d }| jD ]à}z|j| 	¡ |d�}W n2   t
 d|› d| j› d	|jj› �¡ Y qXY n0 | jrÂ| j|d
|j› �d�}| j�s.| jd urú| t|j| j ƒt|j| j ƒf¡}|j|d|› d|j› d| j› � ddd� | ¡  qX| |¡ qX|r<|  |¡}	| jd u�rz|	 t|	j| j ƒt|	j| j ƒf¡}	|	j|d|› d| j› � ddd� |	 ¡  q<d S )NZanalysisZbboxesT)ÚparentsÚexist_okr8   )rˆ   z$Error while drawing layout for page z
 for file z with drawer zLayout source: )ÚcaptionÚpageZ_layout_Ú.éU   )ÚoptimizeZqualityz_layout_all.)r   rÀ   ÚstemrÂ   Úmkdirr�   Úload_source_imagerÈ   r’   ÚcopyÚloggingÚ	exceptionr�   r%   rÄ   Úadd_captionr€   rÅ   rÆ   r4   r{   ÚheightÚsaverÇ   ÚcloserÊ   Úpaste_images_on_grid)
r…   Zfilename_stemZanalysis_save_dirZpage_idxZorig_image_pageZimages_for_gridrˆ   rÉ   ÚimageZ
grid_imager   r   r   Úprocess3  sd    
ÿþÿ
ÿ
ÿ
ÿÿü

ÿýzAnalysisDrawer.process)rÞ   rÎ   c                 C   sÎ   t  tƒ d¡}t |¡}|jd||dd�\}}}}|| }	t|| d ƒ}
|j|	 d df}tjd|j|
fd	d
�}t |¡}|j	||d|d� t d|j|j
|
 f¡}| |d¡ | |d|
f¡ | ¡  |S )Né4   rm   r$   rn   g      ø?r9   re   ÚRGBr   r�   r   )r   r   )r   rr   r   r   rs   r4   r{   r   Únewrk   rÚ   ÚpasterÜ   )r…   rÞ   rÎ   r   Zdrawrv   rw   rx   ry   rc   rd   Ztext_xyZcaption_imageZcaption_drawZexpanded_imager   r   r   rÙ   e  s     
ÿ
zAnalysisDrawer.add_caption)Úimagesr6   c                 C   sì   g }t dt|ƒdƒD ]:}|| }|t|ƒd k r<||d  nd}| ||f¡ qtdd„ |D ƒƒ}tdd„ |D ƒƒ}t d||f¡}d}	|D ]H\}
}| |
d|	f¡ |rº| ||
j|	f¡ |	t|
j	|rÌ|j	ndƒ7 }	qŒ|D ]}| 
¡  qÚ|S )	zECreates a single image that presents all the images on a grid 2 x n/2r   r9   r8   Nc                 S   s,   g | ]$}|d  j |d r"|d j nd  ‘qS ©r   r8   )r{   ©Ú.0Úpairr   r   r   Ú
<listcomp>�  ó    z7AnalysisDrawer.paste_images_on_grid.<locals>.<listcomp>c                 S   s.   g | ]&}t |d  j|d r$|d jnd ƒ‘qS rå   )rL   rÚ   ræ   r   r   r   ré   ‚  rê   rá   )ÚrangerŽ   rÊ   rL   Úsumr   râ   rã   r{   rÚ   rÜ   )r…   rä   ÚpairsÚiZ
left_imageZright_imageZmax_pair_widthZ
sum_heightZnew_imZheight_shiftZ
image_leftZimage_rightrÞ   r   r   r   rÝ   x  s"     
z#AnalysisDrawer.paste_images_on_grid)r6   c                 c   sR  t  ¡ ��2}g }| jr–| jrŒz2t | j¡}t|ƒ| j }|j|dd� |g}W q” t	yˆ } z"t
d| j› d|› �ƒ W Y d }~q”d }~0 0 qì| jg}nVzt| j| j|dd�}W n< t	yê } z$t
d| j› �d|› �ƒ W Y d }~n
d }~0 0 |D ]<}t |¡�}| d	¡V  W d   ƒ qð1 �s"0    Y  qðW d   ƒ n1 �sD0    Y  d S )
NZPNG)rÇ   z-Error while converting image to PNG for file z, exception: T)rÀ   rÃ   Zoutput_folderZ	path_onlyz-Error while converting pdf to image for file zexception: rá   )ÚtempfileÚTemporaryDirectoryrÁ   rÃ   r   Úopenr   rÀ   rÛ   Ú	Exceptionr:   r   Úconvert)r…   Útemp_dirZimage_pathsrÞ   Zoutput_fileÚexZ
image_pathr   r   r   rÕ   ‘  s>    

ÿÿ
ü

þz AnalysisDrawer.load_source_image)NTFNr¿   )r%   r&   r'   r   r   r.   r   r    r   Úfloatr†   r~   rË   rß   r   rÙ   r
   rÝ   r	   rÕ   r¨   r   r   r¦   r   r¾     s(        ÷
÷2r¾   )rE   rF   )rP   rQ   )r8   rW   )rF   )@r×   r>   rï   Úabcr   r   Zdataclassesr   Úenumr   Úior   Úpathlibr   Útypingr   r	   r
   r   r   r   ÚnumpyrK   Z
matplotlibr   r   ZPILr   r   r   Z unstructured_inference.constantsr   Z3unstructured.partition.pdf_image.analysis.processorr   Z0unstructured.partition.pdf_image.pdf_image_utilsr   Zndarrayr   r   r_   r^   r   r-   r0   r.   r3   r4   r;   rö   rD   rO   rV   rZ   r`   ri   rz   r}   r~   r•   r¡   r©   rª   rº   r¾   r   r   r   r   Ú<module>   s¤    &  ú
ù  ü
û  ü
û ÿþüAú3ýK

#-