
    {-jpU                     f   d dl Z d dlmZmZmZmZmZmZ d dlZ	ddl
mZ ddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z% ddl&m'Z' ddl
m(Z( ej)         G d de                      Z* ed           G d de                      Z+dS )    N)AnyDictListOptionalTupleUnion   )logging)pipeline_requires_extra   )ImageBatchSampler)	ReadImage)	HPIConfigPaddlePredictorOption)	DetResult)	benchmark   )(AutoParallelImageSimpleInferencePipeline)BasePipeline)CropByBoxes)DocPreprocessorResult)	OCRResult   )SingleTableRecognitionResultTableRecognitionResult)get_table_recognition_res)get_neighbor_boxes_idxc            !           e Zd ZdZddddddddedee         dee         deeeef                  d	ee         d
e	dee
eeef         ef                  ddf fdZdee	         dee	         dee	         dee	         def
dZdededede	fdZdej        dedeeej        f         fdZd Zd Z	 	 	 d*dej        dedede	de	d e	defd!Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+d"e
eee         ej        eej                 f         dee	         dee	         dee	         dee	         dee         dee         d#ee         d$ee         d%ee         d&ee         d'ee         d(ee         de	d ee	         def d)Z xZ S ),_TableRecognitionPipelinezTable Recognition PipelineNFdeviceengineengine_config	pp_optionuse_hpip
hpi_configconfigr!   r"   r#   r$   r%   r&   returnc          
          t                      j        d||||||d| |                    dd          | _        | j        rF|                    di                               dddi          }	|                     |	          | _        |                    dd          | _        | j        rF|                    d	i                               d
ddi          }
|                     |
          | _        |                    d	i                               dddi          }|                     |          | _	        |                    dd          | _
        | j
        rG|                    di                               dddi          }|                     |          | _        n/|                    di                               dd          | _        t                      | _        t          d          | _        t#          d          | _        dS )a  Initializes the table recognition pipeline.

        Args:
            config (Dict): Configuration dictionary containing various settings.
            device (Optional[str], optional): The device to use for prediction. Defaults to `None`.
            engine (Optional[str], optional): Inference engine. Defaults to `None`.
            engine_config (Optional[Dict[str, Any]], optional): Engine-specific config. Defaults to `None`.
            pp_option (Optional[PaddlePredictorOption], optional): Paddle predictor options.
                Defaults to `None`.
            use_hpip (bool, optional): Whether to use HPIP. Defaults to `False`.
            hpi_config (Optional[Union[Dict[str, Any], HPIConfig]], optional):
                HPIP configuration. Defaults to `None`.
        r    use_doc_preprocessorTSubPipelinesDocPreprocessorpipeline_config_errorz+config error for doc_preprocessor_pipeline!use_layout_detection
SubModulesLayoutDetectionmodel_config_errorz"config error for layout_det_model!TableStructureRecognitionz'config error for table_structure_model!use_ocr_model
GeneralOCRz&config error for general_ocr_pipeline!Nr   )
batch_sizeBGR)format )super__init__getr*   create_pipelinedoc_preprocessor_pipeliner.   create_modellayout_det_modeltable_structure_modelr3   general_ocr_pipelinegeneral_ocr_config_bakr   _crop_by_boxesr   batch_samplerr   
img_reader)selfr'   r!   r"   r#   r$   r%   r&   kwargsdoc_preprocessor_configlayout_det_configtable_structure_configgeneral_ocr_config	__class__s                v/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/inference/pipelines/table_recognition/pipeline.pyr:   z"_TableRecognitionPipeline.__init__)   s?   2 	 	
'!	
 	
 	
 	
 	
 %+JJ/Et$L$L!$ 		&,jj&D&D&H&H!+-Z' '# .2-A-A'. .D* %+JJ/Et$L$L!$ 	I &

< < < @ @!%'KL! ! %)$5$56G$H$HD!!'L"!=!=!A!A'!#LM"
 "
 &*%6%67M%N%N"#ZZ>> 		!'NB!?!?!C!C(*RS" " )-(<(<=O(P(PD%%*0**^R*H*H*L*Ld+ +D' *mm.!<<<#5111    use_doc_orientation_classifyuse_doc_unwarpingr.   r3   c                 |    |
|| j         }n|du s|du rd}nd}|| j        }|| j        }t          |||          S )a  
        Get the model settings based on the provided parameters or default values.

        Args:
            use_doc_orientation_classify (Optional[bool]): Whether to use document orientation classification.
            use_doc_unwarping (Optional[bool]): Whether to use document unwarping.
            use_layout_detection (Optional[bool]): Whether to use layout detection.
            use_ocr_model (Optional[bool]): Whether to use OCR model.

        Returns:
            dict: A dictionary containing the model settings.
        NTF)r*   r.   r3   )r*   r.   r3   dict)rF   rO   rP   r.   r3   r*   s         rM   get_model_settingsz,_TableRecognitionPipeline.get_model_settingsw   s~    & (/4E4M#'#<  +t337HD7P7P'+$$',$'#'#<   .M!5!5'
 
 
 	
rN   model_settingsoverall_ocr_reslayout_det_resc                 v   |d         r| j         st          j        d           dS |d         r5|t          j        d           dS | j        st          j        d           dS |d         r6|t          j        d	           dS | j        st          j        d
           dS n|t          j        d           dS dS )a0  
        Check if the input parameters are valid based on the initialized models.

        Args:
            model_settings (Dict): A dictionary containing input parameters.
            overall_ocr_res (OCRResult): Overall OCR result obtained after running the OCR pipeline.
                The overall OCR result with convert_points_to_boxes information.
            layout_det_res (DetResult): The layout detection result.
        Returns:
            bool: True if all required models are initialized according to input parameters, False otherwise.
        r*   zRSet use_doc_preprocessor, but the models for doc preprocessor are not initialized.Fr.   Nz^The layout detection model has already been initialized, please set use_layout_detection=FalsezRSet use_layout_detection, but the models for layout detection are not initialized.r3   zLThe OCR models have already been initialized, please set use_ocr_model=Falsez>Set use_ocr_model, but the models for OCR are not initialized.z7Set use_ocr_model=False, but no OCR results were found.T)r*   r
   errorr.   r3   )rF   rT   rU   rV   s       rM   check_model_settings_validz4_TableRecognitionPipeline.check_model_settings_valid   s   $ 01 	$:S 	Md   501 	)t   u, h   u/* 	*b   u% T   u	 &WXXXutrN   image_arrayinput_paramsc                     |d         rD|d         }|d         }t          |                     |||                    d         }|d         }ni }|}||fS )a  
        Preprocess the document image based on input parameters.

        Args:
            image_array (np.ndarray): The input image array.
            input_params (dict): Dictionary containing preprocessing parameters.

        Returns:
            tuple[DocPreprocessorResult, np.ndarray]: A tuple containing the preprocessing
                                              result dictionary and the processed image array.
        r*   rO   rP   rO   rP   r   
output_img)listr=   )rF   rZ   r[   rO   rP   doc_preprocessor_resdoc_preprocessor_images          rM   predict_doc_preprocessor_resz6_TableRecognitionPipeline.predict_doc_preprocessor_res   s     ./ 	1+78V+W( ,-@ A#'..1M&7 /  $ $ $  &:,%G""#% %0"#%;;;rN   c           
         t          |t                    s|                                }g }t          t	          |                    D ]~}d ||         D             \  }}}}t          |                     |||||ddf                             d         }	|                    d                    |	d                              |S )t  
        Splits OCR bounding boxes by table cells and retrieves text.

        Args:
            ori_img (ndarray): The original image from which text regions will be extracted.
            cells_bboxes (list or ndarray): Detected cell bounding boxes to extract text from.

        Returns:
            list: A list containing the recognized texts from each cell.
        c                 6    g | ]}t          j        |          S r8   mathceil.0ks     rM   
<listcomp>zM_TableRecognitionPipeline.split_ocr_bboxes_by_table_cells.<locals>.<listcomp>       DDDqdillDDDrN   Nr    	rec_texts
isinstancer_   tolistrangelenrA   appendjoin
rF   ori_imgcells_bboxes
texts_listix1y1x2y2rec_tes
             rM   split_ocr_bboxes_by_table_cellsz9_TableRecognitionPipeline.split_ocr_bboxes_by_table_cells        ,-- 	1'..00L
s<(()) 	< 	<ADDLODDDNBB$33GBrE2b5!!!O4LMMNNqQFbggf[&9::;;;;rN   c           
         t          |t                    s|                                }g }t          t	          |                    D ]~}d ||         D             \  }}}}t          |                     |||||ddf                             d         }	|                    d                    |	d                              |S )rd   c                 6    g | ]}t          j        |          S r8   rf   ri   s     rM   rl   zM_TableRecognitionPipeline.split_ocr_bboxes_by_table_cells.<locals>.<listcomp>!  rm   rN   Nr   rn   ro   rp   rw   s
             rM   r   z9_TableRecognitionPipeline.split_ocr_bboxes_by_table_cells  r   rN   T	table_box use_ocr_results_with_table_cellsflag_find_nei_textcell_sort_by_y_projectionc                 |   t          |                     |                    d         }|dk    r+|d         }d |D             }|                     ||          }	ng }	t          ||||	||          }
d}|rBt	          |d         |          }t          |          dk    r|D ]}||d         |         d	z   z  }||
d
<   |
S )a+  
        Predict table recognition results from an image array, layout detection results, and OCR results.

        Args:
            image_array (np.ndarray): The input image represented as a numpy array.
            overall_ocr_res (OCRResult): Overall OCR result obtained after running the OCR pipeline.
                The overall OCR results containing text recognition information.
            table_box (list): The table box coordinates.
            use_ocr_results_with_table_cells (bool): whether to use OCR results with cells.
            flag_find_nei_text (bool): Whether to find neighboring text.
            cell_sort_by_y_projection (bool): Whether to sort the matched OCR boxes by y-projection.
        Returns:
            SingleTableRecognitionResult: single table recognition result.
        r   Tbboxc                 J    g | ] }|d          |d         |d         |d         g!S )r   r   r	      r8   )rj   rects     rM   rl   zR_TableRecognitionPipeline.predict_single_table_recognition_res.<locals>.<listcomp>C  s>     " " "9=a$q'47DG4" " "rN   r   rn   	rec_boxesro   z; neighbor_texts)r_   r@   r   r   r   rt   )rF   rZ   rU   r   r   r   r   table_structure_predtable_cells_resultcells_texts_listsingle_table_recognition_resneighbor_textmatch_idx_listidxs                 rM   $predict_single_table_recognition_resz>_TableRecognitionPipeline.predict_single_table_recognition_res)  s&   .  $D$>$>{$K$KLLQO+t33!5f!=" "AS" " "  $CC/     "'@ ,&?(
 (
 (
$  	N3,i N >""Q&&) N NC!_[%A#%F%MMMM9F$%56++rN   inputtext_det_limit_side_lentext_det_limit_typetext_det_threshtext_det_box_threshtext_det_unclip_ratiotext_rec_score_threshc              +     K   |                      ||||          }|d}|                     |||          sddiV  t          |                     |                    D ]'\  }}|                     |j                  d         }|d         r,t          |                     |||                    d         }nd|i}|d         }|d	         r0t          |                     |||	|
|||
                    d         }n2|dk    r,| j	        dk    sJ | 
                    | j	                  | _        g }d}|d         s]|[i }|j        dd         \  }}dd|dz
  |dz
  g}|                     ||||d|          }||d<   |                    |           |dz  }n|d         r(t          |                     |                    d         }|d         D ]}|d                                         dv rf|                     ||g          }|d         }|d         }|                     |d         ||||          }||d<   |                    |           |dz  }|j        d         |j        d         |||||d}t'          |          V  )dS )az  
        This function predicts the layout parsing result for the given input.

        Args:
            input (Union[str, list[str], np.ndarray, list[np.ndarray]]): The input image(s) of pdf(s) to be processed.
            use_layout_detection (bool): Whether to use layout detection.
            use_doc_orientation_classify (bool): Whether to use document orientation classification.
            use_doc_unwarping (bool): Whether to use document unwarping.
            overall_ocr_res (OCRResult): The overall OCR result with convert_points_to_boxes information.
                It will be used if it is not None and use_ocr_model is False.
            layout_det_res (DetResult): The layout detection result.
                It will be used if it is not None and use_layout_detection is False.
            use_ocr_results_with_table_cells (bool): whether to use OCR results with cells.
            cell_sort_by_y_projection (bool): Whether to sort the matched OCR boxes by y-projection.
            **kwargs: Additional keyword arguments.

        Returns:
            TableRecognitionResult: The predicted table recognition result.
        NFrX   z0the input params for model settings are invalid!r   r*   r]   r^   r3   )r   r   r   r   r   r   Tr   r.   r   )r   r   table_region_idboxeslabel)tableboximgr   )
input_path
page_indexr`   rV   rU   table_res_listrT   )rS   rY   	enumeraterD   rE   	instancesr_   r=   rA   rB   r<   shaper   ru   r?   lowerrC   input_pathspage_indexesr   ) rF   r   rO   rP   r.   r3   rU   rV   r   r   r   r   r   r   r   r   rG   rT   img_id
batch_datarZ   r`   ra   r   r   
img_height	img_widthr   single_table_rec_resbox_infocrop_img_infosingle_img_ress                                    rM   predictz!_TableRecognitionPipeline.predict^  s     N 00( 	
 
 %,(-%..O^
 
 	P NOOOO"+D,>,>u,E,E"F"F T	9 T	9FJ//**>??BK45 	C'+22#5Q*; 3  ( ( ($$ )5k'B$%9,%G"o. "&--.0G,?(7,?.C.C .  
# 
# 
# 2T992d::::,0,@,@/- -)  NO!"89 %-n>T!#(>(DRaR(H%
I9q=*q.A	'+'P'P*#4',.G (Q ( ($ ;J$%67%%&:;;;1$!"89 %)--.DEE& &&N !/w 7 - -H(..00I==(,(;(;K((T(T(5a(8$1%$8	 EE -e 4 / ) @:S F   - CR,->?&--.BCCC'1, )4Q7(5a8(<"0#2"0"0 N )888888iT	9 T	9rN   )FTF)NNNNNNNNNNNNFN)!__name__
__module____qualname____doc__r   r   strr   r   boolr   r   r:   rR   rS   r   r   rY   npndarrayr   r   rb   r   r_   r   r   r   intfloatr   r   __classcell__)rL   s   @rM   r   r   %   s       $$ !% $2659AEL2 L2 L2L2 	L2
 L2  S#X/L2 12L2 L2 U4S>9#<=>L2 
L2 L2 L2 L2 L2 L2\%
&.tn%
 $D>%
 'tn	%

  ~%
 
%
 %
 %
 %
N55 #5 "	5
 
5 5 5 5n<:<59<	$bj0	1< < < <<  6  @ 27#'*/3, 3,Z3, #3, 	3,
 +/3, !3, $(3, 
&3, 3, 3, 3,p 8<,0/3(,/3.215-1+//315151648!I9 I9S$s)RZbj1AABI9 '/tnI9 $D>	I9
 'tnI9  ~I9 "),I9 !+I9 "*#I9 &c]I9 "%I9 &e_I9  (I9  (I9 +/I9  $,D>!I9$ 
 %I9 I9 I9 I9 I9 I9 I9 I9rN   r   ocrc                   0    e Zd ZdgZed             Zd ZdS )TableRecognitionPipelinetable_recognitionc                     t           S )N)r   )rF   s    rM   _pipeline_clsz&TableRecognitionPipeline._pipeline_cls  s    ((rN   c                     dS )Nr   r8   )rF   r'   s     rM   _get_batch_sizez(TableRecognitionPipeline._get_batch_size  s    qrN   N)r   r   r   entitiespropertyr   r   r8   rN   rM   r   r     sD        #$H) ) X)    rN   r   ),rg   typingr   r   r   r   r   r   numpyr   utilsr
   
utils.depsr   common.batch_samplerr   common.readerr   modelsr   r   models.object_detection.resultr   utils.benchmarkr   	_parallelr   baser   
componentsr   doc_preprocessor.resultr   
ocr.resultr   resultr   r   !table_recognition_post_processingr   r   time_methodsr   r   r8   rN   rM   <module>r      s    : : : : : : : : : : : : : : : :           2 2 2 2 2 2 5 5 5 5 5 5 & & & & & & 6 6 6 6 6 6 6 6 7 7 7 7 7 7 ( ( ( ( ( ( @ @ @ @ @ @       $ $ $ $ $ $ ; ; ; ; ; ; " " " " " " H H H H H H H H H H H H H H ) ) ) ) ) ) A9 A9 A9 A9 A9 A9 A9 A9H     G      rN   