o
    0j!                     @   s\   d dl Z d dlmZ d dlZd dlmZmZ ddlm	Z	m
Z
mZmZ G dd de	e
eZdS )    N)Dict)Image	ImageDraw   )BaseCVResult	HtmlMixin	JsonMixin	XlsxMixinc                       s   e Zd ZdZd fddZdeeejf fddZ	deeef fdd	Z
deeef fd
dZdeeef fddZdeeef fddZ  ZS )LayoutParsingResultzLayout Parsing ResultreturnNc                    s$   t  | t|  t|  dS )z@Initializes a new instance of the class with the specified data.N)super__init__r   r	   )selfdata	__class__ r/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/layout_parsing/result.pyr      s   
zLayoutParsingResult.__init__c              
   C   s  i }| d }|d r|j di | d j | 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 qhqZ||d< |d rt| d d
krt	t| d D ]}| d | }|d }|j}d| }|d ||< q|d rt| d d
krt	t| d D ]}| d | }|d }|j}d| }|d ||< q|S )Nmodel_settingsuse_doc_preprocessordoc_preprocessor_reslayout_det_resresoverall_ocr_resZocr_res_imguse_table_recognitiontable_res_listr   Z
output_img)   r   r   cell_box_listc                 S   s   g | ]}t |qS r   )int).0posr   r   r   
<listcomp>4   s    z/LayoutParsingResult._to_img.<locals>.<listcomp>   )outlinewidthtable_cell_imguse_seal_recognitionseal_res_listseal_region_idZseal_res_regionuse_formula_recognitionformula_res_listformula_region_idZformula_res_regionr   )updateZimglenr   Z	fromarraycopydeepcopyr   ZDrawrangeZ	rectangle)r   Zres_img_dictr   r&   Z
table_drawZrectangle_colorsno	table_resr   boxx1y1Zx2y2seal_resr)   Zsub_seal_res_dictkeyformula_resr,   Zsub_formula_res_dictr   r   r   _to_img!   sN   (


zLayoutParsingResult._to_imgc           	      O     i }| d |d< | d |d< | d }||d< | d |d< | d d r+| d j d |d< | d j d |d< | d	 j d |d	< |d
 rht| d dkrhg |d< tt| d D ]}| d | }|d |j d  qU|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_indexr   parsing_res_listr   r   r   r   r   r   r   r   r'   r(   r*   r+   )strr.   r1   appendr   _to_str	r   argskwargsr   r   r2   r3   r8   r:   r   r   r   rB   O   s8   
zLayoutParsingResult._to_strc           	      O   r<   )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=   r>   r   r?   r   r   r   r   r   r   r   r   r'   r(   r*   r+   )jsonr.   r1   rA   r   _to_jsonrC   r   r   r   rG   x   s8   zLayoutParsingResult._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   r   r   r   table_region_idtable_pred)r.   r1   html)r   r   Zres_html_dictr2   r3   rI   r9   r   r   r   _to_html      
zLayoutParsingResult._to_htmlc                 C   rH   )zConverts the prediction HTML to an XLSX file path.

        Returns:
            Dict[str, str]: The str type XLSX representation result.
        r   r   r   r   rI   rJ   rK   )r.   r1   Zxlsx)r   r   Zres_xlsx_dictr2   r3   rI   r9   r   r   r   _to_xlsx   rN   zLayoutParsingResult._to_xlsx)r   N)__name__
__module____qualname____doc__r   r   r@   npZndarrayr;   rB   rG   rM   rO   __classcell__r   r   r   r   r
      s    .))r
   )r/   typingr   numpyrT   ZPILr   r   Zcommon.resultr   r   r   r	   r
   r   r   r   r   <module>   s   