
    {-j!                         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eee          Z G d	 d
eee          ZdS )    N)Path)Dict)Image	ImageDraw   )BaseCVResult	HtmlMixin	JsonMixin	XlsxMixinc                        e Zd ZdZdeddf fdZ 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 )SingleTableRecognitionResultzsingle table recognition resultdatareturnNc                     t                                          |           t          j        |            t          j        |            d S )Nsuper__init__r	   r   selfr   	__class__s     t/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/inference/pipelines/table_recognition/result.pyr   z%SingleTableRecognitionResult.__init__   sC    4   4         c                     t                                                      }| d         x}&t          |          }|j        |j        }}| d| | S |S N
page_index_r   _get_input_fnr   stemsuffixr   fnpage_idxfpr   r    r   s         r   r   z*SingleTableRecognitionResult._get_input_fn!   ^    WW""$$\**H7bB7BI&D//X/v///Ir   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_html r   s    r   _to_htmlz%SingleTableRecognitionResult._to_html*        [)**r   c                     d| d         iS )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+   s    r   _to_xlsxz%SingleTableRecognitionResult._to_xlsx2   r-   r   c                 r    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   s       r   r4   z$SingleTableRecognitionResult._to_str:   sY      $_ 5_ -[!%&6!7 7777777r   c                 r    i }| d         |d<   | d         |d<   | d         |d<   t          j        |g|R i |S )[  
        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.
        r2   r)   r3   )r
   _to_jsonr5   s       r   r:   z%SingleTableRecognitionResult._to_jsonJ   sY      $_ 5_ -[!%&6!7!$8888888r   )__name__
__module____qualname____doc__r   r   r   strr,   r/   r4   r:   __classcell__r   s   @r   r   r      s        ))!T !d ! ! ! ! ! !
    +$sCx. + + + ++$sCx. + + + +8$sCx. 8 8 8 8 94S> 9 9 9 9 9 9 9 9r   r   c                        e Zd ZdZdeddf fdZ 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 )TableRecognitionResultzTable Recognition Resultr   r   Nc                     t                                          |           t          j        |            t          j        |            | d         i | d<   d S d S )Nlayout_det_resr   r   s     r   r   zTableRecognitionResult.__init___   sb    4   4    !)%'D!""" *)r   c                     t                                                      }| d         x}&t          |          }|j        |j        }}| d| | S |S r   r   r!   s         r   r   z$TableRecognitionResult._get_input_fnf   r%   r   c           
         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          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<   |S )NrE   r   resmodel_settingsuse_doc_preprocessordoc_preprocessor_resoverall_ocr_restable_res_list
output_img)   r   r   r2   c                 ,    g | ]}t          |          S r*   )int).0poss     r   
<listcomp>z2TableRecognitionResult._to_img.<locals>.<listcomp>   s    %>%>%>3c#hh%>%>%>r      )outlinewidthtable_cell_imgr*   )lenimgupdater   	fromarraycopydeepcopyr   Drawrange	rectangle)r   res_img_dictrE   rI   rY   
table_drawrectangle_colorsno	table_resr2   boxx1y1x2y2s                  r   _to_imgzTableRecognitionResult._to_imgo   s   ./~""-;-?-FL)*./01 	DLCC$'=">"BCCC::d#459:::t$%&&**"_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)*r   c                     i }| d         |d<   | d         |d<   | d         |d<   | d         d         r| d         j         d         |d<   t          | d                   dk    r| d         j         d         |d<   | d	         j         d         |d	<   g |d
<   t          t          | d
                             D ]6}| d
         |         }|d
                             |j         d                    7t	          j        |g|R i |S )r1   
input_pathr   rI   rJ   rK   rH   rE   r   rL   rM   )r?   rZ   ra   appendr
   r4   r   r6   r7   r   rf   rg   s         r   r4   zTableRecognitionResult._to_str   s8    !,/\!,/\!%&6!7 !"89 	S+/0F+G+KE+RD'(t$%&&**%)*:%;%?%FD!""&'8"9"=e"D!#T"234455 	@ 	@C-.s3I!")))-*>???? 7777777r   c                     i }| d         |d<   | d         |d<   | d         |d<   | d         d         r| d         j         d         |d<   t          | d                   dk    r| d         j         d         |d<   | d	         j         d         |d	<   g |d
<   t          t          | d
                             D ]6}| d
         |         }|d
                             |j         d                    7t	          j        |g|R i |S )r9   ro   r   rI   rJ   rK   rH   rE   r   rL   rM   )jsonrZ   ra   rp   r
   r:   rq   s         r   r:   zTableRecognitionResult._to_json   s8    !,/\!,/\!%&6!7 !"89 	T+/0F+G+LU+SD'(t$%&&**%)*:%;%@%GD!""&'8"9">u"E!#T"234455 	A 	AC-.s3I!"))).*?@@@@!$8888888r   c                     i }t          t          | d                             D ]L}| d         |         }|d         }d| }|j        d         ||<   ||                             dd          ||<   M|S )r'   rM   table_region_idtable_r(   z<table>z<table border="1">)ra   rZ   htmlreplace)r   res_html_dictrf   rg   ru   keys         r   r,   zTableRecognitionResult._to_html   s     T"234455 	 	C-.s3I'(9:O,?,,C!*!7M#!.s!3!;!;/" "M# r   c                     i }t          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.
        rM   ru   rv   r(   )ra   rZ   xlsx)r   res_xlsx_dictrf   rg   ru   rz   s         r   r/   zTableRecognitionResult._to_xlsx   sp     T"234455 	8 	8C-.s3I'(9:O,?,,C!*!7M#r   )r;   r<   r=   r>   r   r   r   r?   npndarrayrm   r4   r:   r,   r/   r@   rA   s   @r   rC   rC   \   s       ""(T (d ( ( ( ( ( (    c2:o.    :8$sCx. 8 8 8 8294S> 9 9 9 94$sCx.    "$sCx.        r   rC   )r^   pathlibr   typingr   numpyr~   PILr   r   common.resultr   r	   r
   r   r   rC   r*   r   r   <module>r      s                                     J J J J J J J J J J J J@9 @9 @9 @9 @9<I @9 @9 @9F@ @ @ @ @\9i @ @ @ @ @r   