
    {-j8                     *   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 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   )SealRecognitionResultc            !           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ede	fdZdee	         dee	         de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
eee         f                  dee
eef                  dee	         dee
eeeef         f                  dee         dee         dee         dee         dee         dee         dee         def d Z xZS )"_SealRecognitionPipelinezSeal 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        r|                    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          }|                     |          | _	        t                      | _        t          |                    dd                    | _        t          d          | _        dS )a  Initializes the seal 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!	thresholdN
layout_nmslayout_unclip_ratiolayout_merge_bboxes_modeSealOCRz#config error for seal_ocr_pipeline!
batch_sizer   )r2   BGR)format )super__init__getr%   create_pipelinedoc_preprocessor_pipeliner)   create_modellayout_det_modelseal_ocr_pipeliner   _crop_by_boxesr   batch_samplerr   
img_reader)selfr"   r   r   r   r   r    r!   kwargsdoc_preprocessor_configlayout_det_configlayout_kwargsr-   r.   r/   r0   seal_ocr_config	__class__s                    u/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/inference/pipelines/seal_recognition/pipeline.pyr7   z!_SealRecognitionPipeline.__init__$   s   2 	 	
'!	
 	
 	
 	
 	
 %+JJ/Et$L$L!$ 		&,jj&D&D&H&H!+-Z' '# .2-A-A'. .D* %+JJ/Et$L$L!$ 	 &

< < < @ @!%'KL! ! M.22;EEE	R-6k*/33L$GGG
T.8l+'8'<'<)4( ( # 	
 8K34,=,A,A.- - ( 	
 =U89$5D$5!% %%2% %D! !**^R88<</1VW
 
 "&!5!5o!F!F)mm.&**\ST:U:UVVV#5111    model_settingslayout_det_resc                     |d         r| j         st          j        d           dS |d         r5|t          j        d           dS | j        st          j        d           dS dS )	aw  
        Check if the input parameters are valid based on the initialized models.

        Args:
            model_settings (Dict): A dictionary containing input parameters.
            layout_det_res (DetResult): 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.T)r%   r
   errorr)   )rA   rJ   rK   s      rH   check_model_settings_validz3_SealRecognitionPipeline.check_model_settings_validx   s     01 	$:S 	Md   501 	)t   u, h   utrI   use_doc_orientation_classifyuse_doc_unwarpingr)   c                 h    |
|| j         }n|du s|du rd}nd}|| 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.

        Returns:
            dict: A dictionary containing the model settings.
        NTF)r%   r)   )r%   r)   dict)rA   rO   rP   r)   r%   s        rH   get_model_settingsz+_SealRecognitionPipeline.get_model_settings   sm    " (/4E4M#'#<  +t337HD7P7P'+$$',$'#'#< !5!5
 
 
 	
rI   inputlayout_thresholdr.   r/   r0   seal_det_limit_side_lenseal_det_limit_typeseal_det_threshseal_det_box_threshseal_det_unclip_ratioseal_rec_score_threshc              +     &K   |                      |||          }|                     ||          sddiV  |}|'t          |t                    s|g}t	          |          }t          |                     |                    D ]\  }}|                     |j                  }|d         r&t          | 	                    |||                    }nd |D             }d |D             }|d         sO|Md |D             }t          | 
                    ||
|||||	                    &&D ]}d
|d<   d &D             }n|d         r(t          |                     |||||	                    }nJg }|D ]E}	 t          |          }n# t          $ r t          d          w xY w|                    |           Fg }dg}t!          ||          D ]\  }}|d         D ]X}|d                                         dv r:|                     ||g          }|d         }|                    |d                    Y|                    t'          |                     t          | 
                    ||
|||||	                    &&fdt!          |d d         |d
d                    D             }|D ]} d
}!| D ]}|!|d<   |!d
z  }!t!          |j        |j        |||          D ]"\  }"}#}$}} |"|#|$|| |d}%t-          |%          V  #d S )NrM   z0the input params for model settings are invalid!r%   )rO   rP   c                     g | ]}d |iS 
output_imgr5   ).0arrs     rH   
<listcomp>z4_SealRecognitionPipeline.predict.<locals>.<listcomp>   s    +X+X+XC\3,?+X+X+XrI   c                     g | ]
}|d          S r^   r5   r`   items     rH   rb   z4_SealRecognitionPipeline.predict.<locals>.<listcomp>   s*     ' ' ''+\"' ' 'rI   r)   c                     g | ]}i S r5   r5   )r`   _s     rH   rb   z4_SealRecognitionPipeline.predict.<locals>.<listcomp>   s    %J%J%JQb%J%J%JrI   )text_det_limit_side_lentext_det_limit_typetext_det_threshtext_det_box_threshtext_det_unclip_ratiotext_rec_score_threshr   seal_region_idc                     g | ]}|gS r5   r5   rd   s     rH   rb   z4_SealRecognitionPipeline.predict.<locals>.<listcomp>   s    EEE4EEErI   )r-   r.   r/   r0   zNo more layout det resultsr   boxeslabel)sealimgc                 *    g | ]\  }}||         S r5   r5   )r`   ijflat_seal_resultss      rH   rb   z4_SealRecognitionPipeline.predict.<locals>.<listcomp>.  s6          1 &ac*     rI   )
input_path
page_indexdoc_preprocessor_resrK   seal_res_listrJ   )rS   rN   
isinstancelistiter	enumerater?   r@   	instancesr:   r=   r<   nextStopIteration
ValueErrorappendziplowerr>   leninput_pathspage_indexesr   )'rA   rT   rO   rP   r)   rK   rU   r.   r/   r0   rV   rW   rX   rY   rZ   r[   rB   rJ   external_layout_det_resultsrg   
batch_dataimage_arraysdoc_preprocessor_resultsdoc_preprocessor_imageslayout_det_resultsseal_resseal_resultscropped_imgschunk_indicesdoc_preprocessor_imagebox_infocrop_img_infoseal_results_for_imgrn   ry   rz   r{   single_img_resrw   s'                                         @rH   predictz _SealRecognitionPipeline.predict   s     ( 00(*;=Q
 
 ..~~NN 	PNOOOO&4#&294@@ L/J.K+*./J*K*K'&t'9'9%'@'@AA s	< s	<MAz??:+?@@L45 	Y+/22$5Q*; 3  , ,(( ,Y+X<+X+X+X(' '/G' ' '#
 ##9:J,/7%J%J2I%J%J%J"$(**/0G,?(7,?.C.C +  
% 
%! !2 3 3H12H-..EE3DEEE!"89 B)---3&6'10C5M .  * *&& *,&4 B BK-12M-N-NNN, K K K",-I"J"JJK*11.AAAA!!">A+-?? ? 
< 
<:*N %37$; F F#G,2244@@,0,?,? 6
- -M -:!,<M(//e0DEEE!((\):):;;;;$(**$0G,?(7,?.C.C +  
% 
%!        #M#2#$6abb8I J J     
 -9 , ,(%&N$8 , ,5C!12&!+, &'(" < < $$ #-",,@&4%9&4" " ,N;;;;;;+<}s	< s	<s   8FF")NNNNNNNNNNNNNN)__name__
__module____qualname____doc__r   r   strr   r   boolr   r   r7   r   rN   rR   rS   r   npndarrayfloatr   intr   r   __classcell__)rG   s   @rH   r   r       s       ## !% $2659AER2 R2 R2R2 	R2
 R2  S#X/R2 12R2 R2 U4S>9#<=>R2 
R2 R2 R2 R2 R2 R2h " 4= 	       D
&.tn
 $D>
 'tn	

 

 
 
 
F 8<,0/3FJ9=%)KO2615-1+//31515!T< T<S$s)RZbj1AABT< '/tnT< $D>	T<
 'tnT< !y$y/'A!BCT< #5#56T< TNT< &eE53F,F&GHT< #+3-T< "*#T< &c]T< "%T< &e_T<  (T<   (!T<$ 
%T< T< T< T< T< T< T< T<rI   r   ocrc                   0    e Zd ZdgZed             Zd ZdS )SealRecognitionPipelineseal_recognitionc                     t           S )N)r   )rA   s    rH   _pipeline_clsz%SealRecognitionPipeline._pipeline_clsU  s    ''rI   c                 .    |                     dd          S )Nr2   r   )r8   )rA   r"   s     rH   _get_batch_sizez'SealRecognitionPipeline._get_batch_sizeY  s    zz,***rI   N)r   r   r   entitiespropertyr   r   r5   rI   rH   r   r   Q  sD        "#H( ( X(+ + + + +rI   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   resultr   time_methodsr   r   r5   rI   rH   <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 ( ( ( ( ( ( @ @ @ @ @ @       $ $ $ $ $ $ ) ) ) ) ) ) m< m< m< m< m<| m< m< m<`	 + + + + +F + +  + + +rI   