o
    0j!                     @   s|   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	 ddl
mZmZmZmZ G dd deeeZG d	d
 d
eeeZdS )    N)Path)Dict)Image	ImageDraw   )BaseCVResult	HtmlMixin	JsonMixin	XlsxMixinc                       s   e Zd ZdZdeddf fddZ 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 )SingleTableRecognitionResultzsingle table recognition resultdatareturnNc                    s$   t  | t|  t|  d S )Nsuper__init__r   r
   selfr   	__class__ u/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/table_recognition/result.pyr      s   
z%SingleTableRecognitionResult.__init__c                    F   t   }| d  }d ur!t|}|j|j}}| d| | S |S N
page_index_r   _get_input_fnr   stemsuffixr   fnZpage_idxfpr   r   r   r   r   r   !      
z*SingleTableRecognitionResult._get_input_fnc                 C      d| d iS )Converts the prediction to its corresponding HTML representation.

        Returns:
            Dict[str, str]: The str type HTML representation result.
        pred	pred_htmlr   r   r   r   r   _to_html*      z%SingleTableRecognitionResult._to_htmlc                 C   r#   )zConverts the prediction HTML to an XLSX file path.

        Returns:
            str: The path to the XLSX file containing the prediction data.
        r%   r&   r   r'   r   r   r   _to_xlsx2   r)   z%SingleTableRecognitionResult._to_xlsxc                 O   @   i }| d |d< | d |d< | d |d< t j|g|R i |S )x  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.
        cell_box_listr&   table_ocr_pred)r	   _to_strr   argskwargsr   r   r   r   r/   :   s
   
z$SingleTableRecognitionResult._to_strc                 O   r+   )[  
        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	   _to_jsonr0   r   r   r   r4   J   s
   z%SingleTableRecognitionResult._to_json)__name__
__module____qualname____doc__r   r   r   strr(   r*   r/   r4   __classcell__r   r   r   r   r      s    	r   c                       s   e Zd ZdZdeddf fddZ 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 )TableRecognitionResultzTable Recognition Resultr   r   Nc                    s<   t  | t|  t|  | d d u ri | d< d S d S )Nlayout_det_resr   r   r   r   r   r   _   s   

zTableRecognitionResult.__init__c                    r   r   r   r   r   r   r   r   f   r"   z$TableRecognitionResult._get_input_fnc              
   C   s  i }| d }t |dkr|jd |d< | d }|d r&|jdi | d j |jdi | d j t | 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 qnq`||d< |S )Nr<   r   resmodel_settingsuse_doc_preprocessordoc_preprocessor_resoverall_ocr_restable_res_listZ
output_img)   r   r   r-   c                 S   s   g | ]}t |qS r   )int).0posr   r   r   
<listcomp>   s    z2TableRecognitionResult._to_img.<locals>.<listcomp>   )outlinewidthtable_cell_imgr   )lenZimgupdater   Z	fromarraycopydeepcopyr   ZDrawrangeZ	rectangle)r   Zres_img_dictr<   r>   rL   Z
table_drawZrectangle_colorsno	table_resr-   boxx1y1Zx2y2r   r   r   _to_imgo   s2   (
zTableRecognitionResult._to_imgc                 O      i }| d |d< | d |d< | d |d< | d d r#| d j d |d< t| d dkr4| d j d |d< | d	 j d |d	< g |d
< tt| d
 D ]}| d
 | }|d
 |j d  qItj|g|R i |S )r,   
input_pathr   r>   r?   r@   r=   r<   r   rA   rB   )r9   rM   rQ   appendr	   r/   r   r1   r2   r   rR   rS   r   r   r   r/      s   
zTableRecognitionResult._to_strc                 O   rY   )r3   rZ   r   r>   r?   r@   r=   r<   r   rA   rB   )jsonrM   rQ   r[   r	   r4   r\   r   r   r   r4      s   zTableRecognitionResult._to_jsonc                 C   s^   i }t t| d D ]"}| d | }|d }d| }|jd ||< || dd||< q
|S )r$   rB   table_region_idtable_r%   z<table>z<table border="1">)rQ   rM   htmlreplace)r   Zres_html_dictrR   rS   r^   keyr   r   r   r(      s   

zTableRecognitionResult._to_htmlc                 C   sJ   i }t t| d D ]}| d | }|d }d| }|jd ||< q
|S )zConverts the prediction HTML to an XLSX file path.

        Returns:
            Dict[str, str]: The str type XLSX representation result.
        rB   r^   r_   r%   )rQ   rM   Zxlsx)r   Zres_xlsx_dictrR   rS   r^   rb   r   r   r   r*      s   
zTableRecognitionResult._to_xlsx)r5   r6   r7   r8   r   r   r   r9   npZndarrayrX   r/   r4   r(   r*   r:   r   r   r   r   r;   \   s    	r;   )rO   pathlibr   typingr   numpyrc   ZPILr   r   Zcommon.resultr   r   r	   r
   r   r;   r   r   r   r   <module>   s   C