o
    0jD                  	   @  s   d dl mZ d dlZd dlmZ d dlmZ d dlZd dlm	Z	m
Z
mZ ddlmZ dd	lmZmZmZmZmZmZmZ dd
lmZ ddlmZmZmZmZmZmZmZ ddl m!Z! ddl"m#Z# G dd deeeeeeZ$dS )    )annotationsN)deepcopy)List)Image	ImageDraw	ImageFont   )PINGFANG_FONT   )BaseCVResult	HtmlMixin	JsonMixin
LatexMixinMarkdownMixin	WordMixin	XlsxMixin)MarkdownConverter)build_handle_funcs_dictformat_centered_by_htmlformat_chart2markdown_tableformat_image_plainformat_image_scaled_by_htmlformat_text_plainsimplify_table   )LayoutBlock)get_seg_flagc                      s~   e Zd ZdZd fddZdddZdd
dZd ddZdddZdddZ	dddZ
d!d"ddZd"ddZd"ddZ  ZS )#LayoutParsingResultV2zLayout Parsing Result V2returnNonec                   sL   t  | t|  t|  t|  t|  t|  t|  dS )z@Initializes a new instance of the class with the specified data.N)super__init__r   r   r   r   r   r   )selfdata	__class__ u/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/layout_parsing/result_v2.pyr!   5   s   




zLayoutParsingResultV2.__init__dict[str, np.ndarray]c              
   C  s  ddl m} i }| d }|d r | d j D ]\}}|||< q| d jd |d< |d r6| d	 jd |d	< | d
 jd |d
< |d rt| d dkrtt| d d d d d d d d df }t	
|}d}tt| d D ](}	| d |	 }
|
d }|D ]}dd |D \}}}}|j||||g|dd qqr||d< |d rt| d dkrtt| d D ]}	| d |	 }|d }|j}d| }|d ||< qt| d d d d d d d d df }t	
|d}tdt|j d }tjtj|dd}| d  }|D ]T}|j}|j}|j}||d!}|j||d" |d urV|d d |d |d  f}t|j|d  |k rKt|d |d#  |d |d  f}|j|t||d$d% q||d&< |S )'Nr   )get_show_colormodel_settingsuse_doc_preprocessordoc_preprocessor_reslayout_det_resresZuse_region_detectionZregion_det_resoverall_ocr_resZocr_res_imguse_table_recognitiontable_res_listr   
output_img)   r   r   cell_box_listc                 S  s   g | ]}t |qS r&   )int).0posr&   r&   r'   
<listcomp>X   s    z1LayoutParsingResultV2._to_img.<locals>.<listcomp>   )outlinewidthtable_cell_imguse_seal_recognitionseal_res_listseal_region_idZseal_res_regionZRGBAg;On?zutf-8)encodingparsing_res_listF)fillg?red)fontrC   Zlayout_order_res)utilsr)   imgitemslenr   Z	fromarraycopyr   r   ZDrawrangeZ	rectangler6   r<   r   Ztruetyper	   pathbboxorder_indexlabeltextstr)r"   r)   Zres_img_dictr*   keyvaluer=   Z
table_drawZrectangle_colorsno	table_resr5   boxx1y1Zx2y2seal_resr@   Zsub_seal_res_dictimageZdrawZ	font_sizerE   Zparsing_resultblockrM   indexrO   Z
fill_colorZtext_positionr&   r&   r'   _to_img?   sl   
(

*

zLayoutParsingResultV2._to_imgdict[str, str]c           
      O  s  i }| d |d< | d |d< | d |d< | d |d< | d |d< | d }||d< | d }dd	 |D }||d< | d d
 rF| d j d |d< | d j d |d< | d j d |d< |d rt| d dkrg |d< tt| d D ]}| d | }|d |j d  qp|d rt| d dkrg |d< tt| d D ]}| d | }|d |j d  q|d rt| d dkrg |d< tt| d D ]}| d | }	|d |	j d  qtj|g|R i |S )ax  Converts the instance's attributes to a dictionary and then to a string.

        Args:
            *args: Additional positional arguments passed to the base class method.
            **kwargs: Additional keyword arguments passed to the base class method.

        Returns:
            Dict[str, str]: A dictionary with the instance's attributes converted to strings.
        
input_path
page_index
page_countr<   heightr*   rB   c                 S  s&   g | ]}|j |j|j|j|jd qS )Zblock_labelblock_contentZ
block_bboxZblock_idZblock_order)rO   contentrM   r]   rN   )r7   parsing_resr&   r&   r'   r9      s    z1LayoutParsingResultV2._to_str.<locals>.<listcomp>r+   r,   r.   r-   r/   r0   r1   r   r>   r?   use_formula_recognitionformula_res_list)rQ   rI   rK   appendr   _to_str)
r"   argskwargsr#   r*   rB   rT   rU   rZ   formula_resr&   r&   r'   rk      sF   

zLayoutParsingResultV2._to_strTc              	     s   | d d j d |rdd fdd ndd t | d d	d
r't}n }| d dd
r9 fdd}n }| d dd
rQ|rLfdd}ndd }n }| d dd
r`dd }n }t ||||dd}| d dg D ]}||d qu|S )z6Build label-to-handler mapping for content formatting.r,   r2   r   c                 S  s   t t| S N)r   r   r\   r&   r&   r'   <lambda>   s    z@LayoutParsingResultV2._build_handle_funcs_dict.<locals>.<lambda>c                   s   t t|  dS )Noriginal_image_width)r   r   rp   rr   r&   r'   rq      s    c                 S  s   | j S ro   rf   rp   r&   r&   r'   rq      s    r*   Zuse_chart_recognitionFr>   c                   s   d  | | gS N
)joinrp   )format_image_funcformat_text_funcr&   r'   rq      s    r0   c                   s   d |  dd S )Nrv   z<table>z<table border="1">)replacerp   )ry   r&   r'   rq      s
    c                 S  s   t d| j S ru   )r   rf   rp   r&   r&   r'   rq          rh   c                 S  s   d| j  dS )Nz$$rt   rp   r&   r&   r'   rq      r{   T)Z	text_funcZ
image_funcZ
chart_funcZ
table_funcZformula_funcZ	seal_funcZuse_plain_header_footer_imageZmarkdown_ignore_labelsN)shaper   getr   r   pop)r"   prettyZformat_chart_funcZformat_seal_funcZformat_table_funcZformat_formula_funchandle_funcs_dictrO   r&   )rx   ry   rs   r'   _build_handle_funcs_dict   s@   

	z.LayoutParsingResultV2._build_handle_funcs_dictc                 O  sJ  | d  ddr| jdd}i }| d |d< | d |d< | d |d< | d	 |d	< | d
 |d
< | d }||d< | d }g }|D ]1}|j|j|j|j|jd}	| d  ddrj| |jre||j ||	d< n|j|	d< ||	 q>||d< | d d r| d jd |d< | d jd |d< | d jd |d< |d rt	| d dkrg |d< t
t	| d D ]}
| d |
 }|d |jd  q|d rt	| d dkrg |d< t
t	| d D ]}
| d |
 }|d |jd  q|d rt	| d dkrg |d< t
t	| d D ]}
| d |
 }|d |jd  qtj|g|R i |S )a[  
        Converts the object's data to a JSON dictionary.

        Args:
            *args: Positional arguments passed to the JsonMixin._to_json method.
            **kwargs: Keyword arguments passed to the JsonMixin._to_json method.

        Returns:
            Dict[str, str]: A dictionary containing the object's data in JSON format.
        r*   Zformat_block_contentFTr   r`   ra   rb   r<   rc   rB   rd   re   r+   r,   r.   r-   r/   r0   r1   r   r>   r?   rh   ri   )r}   r   rO   rf   rM   r]   rN   rj   jsonrI   rK   r   _to_json)r"   rl   rm   r   r#   r*   rB   Zparsing_res_list_jsonrg   Zres_dictrT   rU   rZ   rn   r&   r&   r'   r      sb   

zLayoutParsingResultV2._to_jsonc                 C  j   | d }i }|d r3t | d dkr3tt | d D ]}| d | }|d }d| }|jd ||< q|S )zConverts the prediction to its corresponding HTML representation.

        Returns:
            Dict[str, str]: The str type HTML representation result.
        r*   r0   r1   r   table_region_idtable_pred)rI   rK   html)r"   r*   Zres_html_dictrT   rU   r   rR   r&   r&   r'   _to_html1     
zLayoutParsingResultV2._to_htmlc                 C  r   )zConverts the prediction HTML to an XLSX file path.

        Returns:
            Dict[str, str]: The str type XLSX representation result.
        r*   r0   r1   r   r   r   r   )rI   rK   Zxlsx)r"   r*   Zres_xlsx_dictrT   rU   r   rR   r&   r&   r'   _to_xlsxA  r   zLayoutParsingResultV2._to_xlsxFdictc                 C  sF   | j |d}tj| d ||dt| d d}| d |d< | d |d< |S )z
        Save the parsing result to a Markdown file.

        Args:
            pretty (Optional[bool]): whether to pretty markdown by HTML, default by True.

        Returns:
            Dict
        r   rB   Timgs_in_doc)r   show_formula_numberZuse_seg_flagZget_seg_flag_funcr   ra   r`   )r   r   convertr   )r"   r   r   r   resultr&   r&   r'   _to_markdownQ  s   
z"LayoutParsingResultV2._to_markdownc                 C  sH   ddl m} || d | dg d\}}|| d d jd | d	 |d
S )a  Convert the object's parsing result into a Word-compatible dict.

        Returns:
            dict: {
                "word_blocks": List[Dict],       # Simplified list of content blocks
                "original_image_width": int,   # Pixel width of the source page
                "input_path": str,             # Original input file path
                "images": List[Dict]           # List of {"path": str, "img": PIL.Image}
            }
        r
   )build_word_blocksrB   r   )r   r,   r2   r   r`   )word_blocksrs   r`   images)common.result.converterr   r}   r|   )r"   r   r   r   r&   r&   r'   _to_wordi  s   

zLayoutParsingResultV2._to_wordc                 C  s   g }g }| d D ]U}|j }t|dd}|dv r#|jdu rq|jd }n|dkr>|jdur2|jd }n|r=|dd	}d
}nq|t|d}|| |jdur]||jd |jd d q||| d dS )a  
        Convert the object's parsing result into a latex-compatible dict.

        Returns:
            dict: {
                "latex_blocks": List[Dict],       # Simplified list of content blocks
                "input_path": str,             # Original input file path
                "images": List[Dict]           # List of {"path": str, "img": PIL.Image}
            }
        rB   rf    )r[   ZsealNrL   chart|	table)typerf   rG   )rL   rG   r`   )latex_blocksr   r`   )rO   getattrr[   rz   r   rj   )r"   r   r[   r\   rO   rf   Z
block_dictr&   r&   r'   	_to_latex  s6   



zLayoutParsingResultV2._to_latex)r   r   )r   r(   )r   r_   )T)TF)r   r   )__name__
__module____qualname____doc__r!   r^   rk   r   r   r   r   r   r   r   __classcell__r&   r&   r$   r'   r   0   s    


@
7
9
B

r   )%
__future__r   rJ   r   typingr   numpynpZPILr   r   r   Zutils.fontsr	   Zcommon.resultr   r   r   r   r   r   r   r   r   Z-common.result.converter.markdown_format_funcsr   r   r   r   r   r   r   Zlayout_objectsr   rF   r   r   r&   r&   r&   r'   <module>   s   $	$	
