
    {-jf                     F   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# ej$         G d de                      Z% ed           G d de                      Z&dS )    )AnyDictListOptionalTupleUnionN   )logging)pipeline_requires_extra   )ImageBatchSampler)	ReadImage)	HPIConfigPaddlePredictorOption)	DetResult)	benchmark   )(AutoParallelImageSimpleInferencePipeline)BasePipeline)CropByBoxes)	OCRResult   )LayoutParsingResult)get_sub_regions_ocr_ressorted_layout_boxesc            1           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ddfdZ	 	 	 	 	 	 d1dededededededee         dee         dee         dee         dee         dee         defdZdede	fdZdee	         dee	         d ee	         d!ee	         d"ee	         defd#Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d2d$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
eef                  d'ee	         d(ee
eeeef         e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e         def0d0Z xZS )3_LayoutParsingPipelinezLayout Parsing PipelineNFdeviceengineengine_config	pp_optionuse_hpip
hpi_configconfigr   r    r!   r"   r#   r$   returnc          
           t                      j        d||||||d| |                     |           t          d          | _        t          d          | _        t                      | _        dS )a  Initializes the layout parsing 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   r   )
batch_sizeBGR)formatN )	super__init__inintial_predictorr   batch_samplerr   
img_readerr   _crop_by_boxes)
selfr%   r   r    r!   r"   r#   r$   kwargs	__class__s
            s/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/inference/pipelines/layout_parsing/pipeline.pyr-   z_LayoutParsingPipeline.__init__&   s    2 	 	
'!	
 	
 	
 	
 	
 	'''.!<<<#5111)mm    c                 @   |                     dd          | _        |                     dd          | _        |                     dd          | _        |                     dd          | _        | j        rF|                     di                                ddd	i          }|                     |          | _        |                     d
i                                dddi          }i }|                     dd          x}||d<   |                     dd          x}||d<   |                     dd          x}	 ||d<   |                     dd          x}	 ||d<    | j        |fi || _        |                     di                                dddi          }	|                     |	          | _	        | j        rF|                     di                                dddi          }
|                     |
          | _
        | j        rF|                     di                                dddi          }|                     |          | _        | j        rF|                     di                                dddi          }|                     |          | _        dS )zInitializes the predictor based on the provided configuration.

        Args:
            config (Dict): A dictionary containing the configuration for the predictor.

        Returns:
            None
        use_doc_preprocessorTuse_table_recognitionuse_seal_recognitionuse_formula_recognitionSubPipelinesDocPreprocessorpipeline_config_errorz+config error for doc_preprocessor_pipeline!
SubModulesLayoutDetectionmodel_config_errorz"config error for layout_det_model!	thresholdN
layout_nmslayout_unclip_ratiolayout_merge_bboxes_mode
GeneralOCRz&config error for general_ocr_pipeline!SealRecognitionz+config error for seal_recognition_pipeline!TableRecognitionz,config error for table_recognition_pipeline!FormulaRecognitionz.config error for formula_recognition_pipeline!)getr8   r9   r:   r;   create_pipelinedoc_preprocessor_pipelinecreate_modellayout_det_modelgeneral_ocr_pipelineseal_recognition_pipelinetable_recognition_pipelineformula_recognition_pipeline)r2   r%   doc_preprocessor_configlayout_det_configlayout_kwargsrB   rC   rD   rE   general_ocr_configseal_recognition_configtable_recognition_configformula_recognition_configs                r5   r.   z)_LayoutParsingPipeline.inintial_predictorP   s&    %+JJ/Et$L$L!%+ZZ0G%N%N"$*JJ/Et$L$L!'-zz2KT'R'R$$ 		&,jj&D&D&H&H!+-Z' '# .2-A-A'. .D* #JJ|R88<<!#GH
 
 *..{DAAAIN)2M+&+//dCCCJP*4M,'#4#8#89NPT#U#UU 4GM/0(9(=(=*D) ) $ 	
 9QM45 1 12C U U} U U#ZZ;;??$&NO
 
 %)$8$89K$L$L!$ 		&,jj&D&D&H&H!+-Z' '# .2-A-A'. .D* % 		'-zz."'E'E'I'I"+-[( ($ /3.B.B(/ /D+ ' 		)/NB)G)G)K)K$+-]* *& 150D0D*1 1D- 	r6   imagelayout_det_resoverall_ocr_restable_res_listseal_res_listformula_res_listtext_det_limit_side_lentext_det_limit_typetext_det_threshtext_det_box_threshtext_det_unclip_ratiotext_rec_score_threshc                    g }i }d}d}d}t          j        |          }g }t          |d                   D ]\  }}i }|d         }|d                                         }||d<   ||d<   d|d<   |                    |           |d	k    rHt          |          dk    r3t          |          |k    s
J d
            ||         d         |d<   |dz  }n|dk    rGt          |          dk    r3t          |          |k    s
J d            ||         d         |d<   |dz  }n|dk    rZt          |          dk    rFt          |          |k    s
J d            d                    ||         d                   |d<   |dz  }nqt          ||gd          \  }}|D ]:}|                    |d          |g||<   ||                             |           ;d                    |d                   |d<   |                    |           |	                                D ]}t          |          dk    r|D ]}t          j
        |j        |j                  dz  }||         d         }d |D             \  }}} }!|||!|| ddf         |||!|| ddf<   t          |                     ||||	|
||                    d         }"d                    |"d                   ||         d<   t          ||d          }#t          |#d         |#d                   D ]+\  }$}%i }|$|d<   d|d<   |%|d<   |                    |           ,t!          ||j        d                    }|S )!a  
        Retrieves the layout parsing result based on the layout detection result, OCR result, and other recognition results.
        Args:
            image (list): The input image.
            layout_det_res (DetResult): The detection result containing the layout information of the document.
            overall_ocr_res (OCRResult): The overall OCR result containing text information.
            table_res_list (list): A list of table recognition results.
            seal_res_list (list): A list of seal recognition results.
            formula_res_list (list): A list of formula recognition results.
            text_det_limit_side_len (Optional[int], optional): The maximum side length of the text detection region. Defaults to `None`.
            text_det_limit_type (Optional[str], optional): The type of limit for the text detection region. Defaults to `None`.
            text_det_thresh (Optional[float], optional): The confidence threshold for text detection. Defaults to `None`.
            text_det_box_thresh (Optional[float], optional): The confidence threshold for text detection bounding boxes. Defaults to `None`
            text_det_unclip_ratio (Optional[float], optional): The unclip ratio for text detection. Defaults to `None`.
            text_rec_score_thresh (Optional[float], optional): The score threshold for text recognition. Defaults to `None`.
        Returns:
            list: A list of dictionaries representing the layout parsing result.
        r   boxes
coordinatelabel
block_bboxblock_label block_contentformulazThe number of                         formula regions of layout parsing pipeline                         and formula recognition pipeline are different!rec_formular   tablezThe number of                         table regions of layout parsing pipeline                         and table recognition pipeline are different!	pred_htmlsealzThe number of                         seal regions of layout parsing pipeline                         and seal recognition pipeline are different!z, 	rec_textsT)return_match_idxN
)dtype   c                 ,    g | ]}t          |          S r+   )int).0is     r5   
<listcomp>zA_LayoutParsingPipeline.get_layout_parsing_res.<locals>.<listcomp>
  s    %:%:%:c!ff%:%:%:r6   )r`   ra   rb   rc   rd   re   F)flag_within	rec_boxes
other_text)w)nparray	enumeratelowerappendlenjoinr   rJ   valuesonesshaperv   listrO   zipr   )&r2   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   layout_parsing_resmatched_ocr_dictformula_indextable_index
seal_indexobject_boxesobject_box_idxbox_infosingle_box_resboxri   ocr_res_in_boxmatched_idxesmatched_idxlayout_box_idsidxwht_imx1y1x2y2sub_ocr_resocr_without_layout_boxesocr_rec_boxocr_rec_texts&                                         r5   get_layout_parsing_resz-_LayoutParsingPipeline.get_layout_parsing_res   s|   B  
(1.2I(J(J 3	6 3	6$NHN<(CW%++--E+.N<(,1N=).0N?+$$$	!!'((1,,,--===I >== 7G}6U%7N?3 "Q&M'!!~&&**N++k999G :99 7E[6Q#7N?3  1$K&}%%))M**Z777F 877 7;ii%j1+>7 7N?3 !OJ0G#cUT1 1 1- $1 M MK'++K>>F9G8H(55(5<<^LLLL26))N;<W2X2X/%%n5555.5577 	 	N>""Q&&)  CWU[DDDsJF,S1,?C%:%:c%:%:%:NBB.3BrE2b5!!!O.DF2b5"R%?+"&11"4K0C,;0C2G2G 2  
# 
# 
#K @Dyy#K0@ @&s+O<< $;\u$
 $
 $
  *-$[13KK3X*
 *
 	6 	6%K  N+6N<(,8N=).:N?+%%n555501Cu{ST~VVV!!r6   input_paramsc                     |d         r| j         st          j        d           dS |d         r| j        st          j        d           dS |d         r| j        st          j        d           dS dS )	a4  
        Check if the input parameters are valid based on the initialized models.

        Args:
            input_params (Dict): A dictionary containing input parameters.

        Returns:
            bool: True if all required models are initialized according to input parameters, False otherwise.
        r8   zRSet use_doc_preprocessor, but the models for doc preprocessor are not initialized.Fr:   zRSet use_seal_recognition, but the models for seal recognition are not initialized.r9   zTSet use_table_recognition, but the models for table recognition are not initialized.T)r8   r
   errorr:   r9   )r2   r   s     r5   check_model_settings_validz1_LayoutParsingPipeline.check_model_settings_valid,  s     ./ 	8Q 	Md   5./ 	8Q 	Md   5/0 	9S 	Mf   5tr6   use_doc_orientation_classifyuse_doc_unwarpingr:   r9   r;   c                     |
|| j         }n|du s|du rd}nd}|| j        }|| 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_seal_recognition (Optional[bool]): Whether to use seal recognition.
            use_table_recognition (Optional[bool]): Whether to use table recognition.

        Returns:
            dict: A dictionary containing the model settings.
        NTF)r8   r:   r9   r;   )r8   r:   r9   r;   dict)r2   r   r   r:   r9   r;   r8   s          r5   get_model_settingsz)_LayoutParsingPipeline.get_model_settingsK  s    ( (/4E4M#'#<  +t337HD7P7P'+$$',$'#'#<  ($($>!"*&*&B#!5!5"7$;	
 
 
 	
r6   inputuse_textline_orientationlayout_thresholdrC   rD   rE   seal_det_limit_side_lenseal_det_limit_typeseal_det_threshseal_det_box_threshseal_det_unclip_ratioseal_rec_score_threshc              +   z  K   |                      |||||          }|                     |          sddiV  t          |                     |                    D ]\  }}|                     |j                  d         }|d         r,t          |                     |||                    d         }nd|i}|d         }t          |                     |||	|
|                    d         }t          | 	                    ||||||||                    d         } |d	         r8t          | 
                    |d
d
d
d
| |                    d         }!|!d         }"ng }"|d         r<t          |                     |d
d
d
|||||||                    d         }#|#d         }$ng }$|d         r6t          |                     |d
d
d
|                    d         }%|%d         }&ng }&|                     ||| |"|$|&||||||          }'|j        d         |j        d         ||| |"|$|&|'|d
}(t!          |(          V  dS )a
  
        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) or pdf(s) to be processed.
            use_doc_orientation_classify (Optional[bool]): Whether to use document orientation classification.
            use_doc_unwarping (Optional[bool]): Whether to use document unwarping.
            use_textline_orientation (Optional[bool]): Whether to use textline orientation prediction.
            use_seal_recognition (Optional[bool]): Whether to use seal recognition.
            use_table_recognition (Optional[bool]): Whether to use table recognition.
            use_formula_recognition (Optional[bool]): Whether to use formula recognition.
            layout_threshold (Optional[float]): The threshold value to filter out low-confidence predictions. Default is None.
            layout_nms (Optional[bool], optional): Whether to use layout-aware NMS. Defaults to `False`.
            layout_unclip_ratio (Optional[Union[float, Tuple[float, float]]], optional): The ratio of unclipping the bounding box.
                Defaults to `None`.
                If it's a single number, then both width and height are used.
                If it's a tuple of two numbers, then they are used separately for width and height respectively.
                If it's None, then no unclipping will be performed.
            layout_merge_bboxes_mode (Optional[str], optional): The mode for merging bounding boxes. Defaults to `None`.
            text_det_limit_side_len (Optional[int]): Maximum side length for text detection.
            text_det_limit_type (Optional[str]): Type of limit to apply for text detection.
            text_det_thresh (Optional[float]): Threshold for text detection.
            text_det_box_thresh (Optional[float]): Threshold for text detection boxes.
            text_det_unclip_ratio (Optional[float]): Ratio for unclipping text detection boxes.
            text_rec_score_thresh (Optional[float]): Score threshold for text recognition.
            seal_det_limit_side_len (Optional[int]): Maximum side length for seal detection.
            seal_det_limit_type (Optional[str]): Type of limit to apply for seal detection.
            seal_det_thresh (Optional[float]): Threshold for seal detection.
            seal_det_box_thresh (Optional[float]): Threshold for seal detection boxes.
            seal_det_unclip_ratio (Optional[float]): Ratio for unclipping seal detection boxes.
            seal_rec_score_thresh (Optional[float]): Score threshold for seal recognition.

            **kwargs: Additional keyword arguments.

        Returns:
            LayoutParsingResult: The predicted layout parsing result.
        r   z0the input params for model settings are invalid!r   r8   )r   r   
output_img)rB   rC   rD   rE   )r   r`   ra   rb   rc   rd   re   r9   F)r   r   use_layout_detectionuse_ocr_modelr\   r[   r]   r:   )
r   r   r   r[   r   r   r   r   r   r   r^   r;   )r   r   r   r[   r_   )r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   )

input_path
page_indexdoc_preprocessor_resr[   r\   r]   r^   r_   parsing_res_listmodel_settingsN)r   r   r   r/   r0   	instancesr   rL   rN   rO   rQ   rP   rR   r   input_pathspage_indexesr   ))r2   r   r   r   r   r:   r9   r;   r   rC   rD   rE   r`   ra   rb   rc   rd   re   r   r   r   r   r   r   r3   r   img_id
batch_dataimage_arrayr   doc_preprocessor_imager[   r\   table_res_allr]   seal_res_allr^   formula_res_allr_   r   single_img_ress)                                            r5   predictz_LayoutParsingPipeline.predictw  sp     B 00( !#
 
 ..~>> 	PNOOOO"+D,>,>u,E,E"F"F t	6 t	6FJ//**>??BK45 	C'+22#5Q*; 3  ( ( ($$ )5k'B$%9,%G"!%%*.)(;-E &    N #))*-E,C(;$3(;*?*? * 	 	  O 56 $ $33.5:*/-2&+(7'5 4  
! 
! 
! "//?!@!#45 ##22.5:*/-2'50G,?(7,?.C.C 3        !-_ = "78 &"&55.-25:*/'5 6  # # # $33E#F  #% #::&- /-+!1(?$7 /$7&;&;  ;      )4Q7(5a8(<"0#2"0!.$4$4"0 N &n555555it	6 t	6r6   )NNNNNN)NNNNNNNNNNNNNNNNNNNNNN)__name__
__module____qualname____doc__r   r   strr   r   boolr   r   r-   r.   r   r   r   ry   floatr   r   r   r   r   r   ndarrayr   r   r   __classcell__)r4   s   @r5   r   r   "   sg       !! !% $2659AE(, (, (,(, 	(,
 (,  S#X/(, 12(, (, U4S>9#<=>(, 
(, (, (, (, (, (,TV V$ V V V V@ 26-1+//31515B" B"B" "B" #	B"
 B" B" B" "*#B" &c]B" "%B" &e_B"  (B"  (B" 
B" B" B" B"Ht     >*
&.tn*
 $D>*
 'tn	*

  (~*
 "*$*
 
*
 *
 *
 *
^ 8<,037/304269=%)QU2615-1+//3151515-1+//315151@6 @6S$s)RZbj1AAB@6 '/tn@6 $D>	@6
 #+4.@6 'tn@6  (~@6 "*$@6 #5#56@6 TN@6 &eE53F,L&MN@6 #+3-@6 "*#@6 &c]@6 "%@6  &e_!@6"  (#@6$  (%@6& "*#'@6( &c])@6* "%+@6, &e_-@6.  (/@60  (1@64 
5@6 @6 @6 @6 @6 @6 @6 @6r6   r   ocrc                   0    e Zd ZdgZed             Zd ZdS )LayoutParsingPipelinelayout_parsingc                     t           S )N)r   )r2   s    r5   _pipeline_clsz#LayoutParsingPipeline._pipeline_cls>  s    %%r6   c                     dS )Nr   r+   )r2   r%   s     r5   _get_batch_sizez%LayoutParsingPipeline._get_batch_sizeB  s    qr6   N)r   r   r   entitiespropertyr   r   r+   r6   r5   r   r   :  sD         !H& & X&    r6   r   )'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   
ocr.resultr   resultr   r   r   time_methodsr   r   r+   r6   r5   <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 ( ( ( ( ( ( @ @ @ @ @ @       $ $ $ $ $ $ " " " " " " ' ' ' ' ' ' ? ? ? ? ? ? ? ? T6 T6 T6 T6 T6\ T6 T6 T6n     D      r6   