
    {-j!                     j    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e	e
e          ZdS )    N)Dict)Image	ImageDraw   )BaseCVResult	HtmlMixin	JsonMixin	XlsxMixinc                        e Zd ZdZd
 fdZdeeej        f         fdZ	deeef         fdZ
deeef         fdZdeeef         fdZdeeef         fd	Z xZS )LayoutParsingResultzLayout Parsing ResultreturnNc                     t                                          |           t          j        |            t          j        |            dS )z@Initializes a new instance of the class with the specified data.N)super__init__r   r
   )selfdata	__class__s     q/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/inference/pipelines/layout_parsing/result.pyr   zLayoutParsingResult.__init__   sC    4   4         c           
      T   i }| d         }|d         r |j         di | d         j         | d         j        d         |d<   | d         j        d         |d<   |d         rt          | d	                   d
k    rt          j        t          j        | d         d         d d d d d d df                             }t          j        |          }d}t          t          | d	                             D ]J}| d	         |         }|d         }|D ]/}	d |	D             \  }
}}}|
                    |
|||g|d           0K||d<   |d         rkt          | d                   d
k    rRt          t          | d                             D ]/}| d         |         }|d         }|j        }d| }|d         ||<   0|d         rkt          | d                   d
k    rRt          t          | d                             D ]/}| d         |         }|d         }|j        }d| }|d         ||<   0|S )Nmodel_settingsuse_doc_preprocessordoc_preprocessor_reslayout_det_resresoverall_ocr_resocr_res_imguse_table_recognitiontable_res_listr   
output_img)   r   r   cell_box_listc                 ,    g | ]}t          |          S  )int).0poss     r   
<listcomp>z/LayoutParsingResult._to_img.<locals>.<listcomp>4   s    %>%>%>3c#hh%>%>%>r      )outlinewidthtable_cell_imguse_seal_recognitionseal_res_listseal_region_idseal_res_regionuse_formula_recognitionformula_res_listformula_region_idformula_res_regionr%   )updateimglenr   	fromarraycopydeepcopyr   Drawrange	rectangle)r   res_img_dictr   r-   
table_drawrectangle_colorsno	table_resr#   boxx1y1x2y2seal_resr0   sub_seal_res_dictkeyformula_resr4   sub_formula_res_dicts                        r   _to_imgzLayoutParsingResult._to_img!   s   ./01 	DLCC$'=">"BCCC)-.>)?)CE)J%&*./@*A*Em*T&'12 	<s4@P;Q7R7RUV7V7V"_d#9:<HAAAttQStTUU N #77J)OS&6!78899   !1237	 )/ :(  C%>%>#%>%>%>NBB((RR(/ )    
 .<L)*01 	Ec$:O6P6PST6T6TSo!67788 E E05!)*:!;$,L!888$5m$DS!! 45		@D+,--11S&8!9::;; @ @"#56s;$/0C$D!'2$>+<>>$8$?S!!r   c                 &   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
         rwt          | d                   dk    r^g |d<   t          t          | d                             D ]6}| d         |         }|d                             |j         d                    7|d         rwt          | d                   dk    r^g |d<   t          t          | d                             D ]6}| d         |         }|d                             |j         d                    7|d         rwt          | d                   dk    r^g |d<   t          t          | d                             D ]6}| d         |         }|d                             |j         d                    7t	          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/   r2   r3   )strr8   r=   appendr	   _to_str	r   argskwargsr   r   rB   rC   rI   rL   s	            r   rU   zLayoutParsingResult._to_strO   st    !,/\!,/\./!/#'(:#;  !"89 	S+/0F+G+KE+RD'(!%&6!7!;E!B"&'8"9"=e"D12 	Ds4@P;Q7R7RUV7V7V%'D!"S&6!78899 D D !1237	%&--imE.BCCCC01 	Bc$:O6P6PST6T6T$&D!So!67788 B B05_%,,X\%-@AAAA45	HD+,--11')D#$S&8!9::;; H H"#56s;'(//0FGGGG 7777777r   c                 &   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
         rwt          | d                   dk    r^g |d<   t          t          | d                             D ]6}| d         |         }|d                             |j         d                    7|d         rwt          | d                   dk    r^g |d<   t          t          | d                             D ]6}| d         |         }|d                             |j         d                    7|d         rwt          | d                   dk    r^g |d<   t          t          | d                             D ]6}| d         |         }|d                             |j         d                    7t	          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.
        rP   rQ   r   rR   r   r   r   r   r   r   r   r   r.   r/   r2   r3   )jsonr8   r=   rT   r	   _to_jsonrV   s	            r   r[   zLayoutParsingResult._to_jsonx   su    !,/\!,/\./!/#'(:#;  !"89 	T+/0F+G+LU+SD'(!%&6!7!<U!C"&'8"9">u"E12 	Es4@P;Q7R7RUV7V7V%'D!"S&6!78899 E E !1237	%&--inU.CDDDD01 	Cc$:O6P6PST6T6T$&D!So!67788 C C05_%,,X]5-ABBBB45	ID+,--11')D#$S&8!9::;; I I"#56s;'(//0@0GHHHH!$8888888r   c                     | d         }i }|d         rit          | d                   dk    rPt          t          | d                             D ]-}| d         |         }|d         }d| }|j        d         ||<   .|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)r8   r=   html)r   r   res_html_dictrB   rC   r]   rK   s          r   _to_htmlzLayoutParsingResult._to_html        ./12 	<s4@P;Q7R7RUV7V7VS&6!78899 < < !1237	"+,=">000%.^F%;c""r   c                     | d         }i }|d         rit          | d                   dk    rPt          t          | d                             D ]-}| d         |         }|d         }d| }|j        d         ||<   .|S )zConverts the prediction HTML to an XLSX file path.

        Returns:
            Dict[str, str]: The str type XLSX representation result.
        r   r   r   r   r]   r^   r_   )r8   r=   xlsx)r   r   res_xlsx_dictrB   rC   r]   rK   s          r   _to_xlsxzLayoutParsingResult._to_xlsx   rc   r   )r   N)__name__
__module____qualname____doc__r   r   rS   npndarrayrN   rU   r[   rb   rg   __classcell__)r   s   @r   r   r      s        ! ! ! ! ! !,c2:o. , , , ,\'8$sCx. '8 '8 '8 '8R'94S> '9 '9 '9 '9R$sCx.     $sCx.        r   r   )r:   typingr   numpyrl   PILr   r   common.resultr   r   r	   r
   r   r%   r   r   <module>rs      s                               J J J J J J J J J J J Jg g g g g,	9 g g g g gr   