
    {-jsY                     2   d dl 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mZmZmZmZmZ ddl m!Z! ej"         G d de                      Z# edd           G d de                      Z$dS )    )AnyDictListOptionalUnionN   )logging)pipeline_requires_extra   )ImageBatchSampler)	ReadImage)	HPIConfigPaddlePredictorOption)	benchmark   )(AutoParallelImageSimpleInferencePipeline)BasePipeline)CropByPolysSortPolyBoxesSortQuadBoxescal_ocr_word_boxconvert_points_to_boxesrotate_image   )	OCRResultc                       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j                 dee         deej                 fdZ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         dee         dee         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         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 )$_OCRPipelinezOCR PipelineNFdeviceengineengine_config	pp_optionuse_hpip
hpi_configconfigr   r    r!   r"   r#   r$   returnc          
          t                      j        d5||||||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         | _	        | j	        dk    r|                    dd          | _
        |                    dd          | _        |                    dd          | _        |                    dd          | _        |                    dd          | _        |                    dd          | _        |                    dd          | _        t#                      | _        t'          d           | _        n| j	        d!k    r|                    dd"          | _
        |                    dd#          | _        |                    dd          | _        |                    dd$          | _        |                    dd          | _        |                    dd%          | _        |                    dd          | _        t+                      | _        t'          d&           | _        n't-          d'                    | j	                            |                     || j
        | j        | j        | j        | j        | j        | j        (          | _        |                    d	i                               d)dd*i          }|                    d+d,          | _        |                    d-d.          | _        |                    dd          | _        |                     || j        /          | _        t9          |                    d0d1          2          | _        t=          d34          | _        dS )6a  Initializes the OCR 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_textline_orientation
SubModulesTextLineOrientationmodel_config_errorz,config error for textline_orientation_model!TextDetectionz config error for text_det_model!	text_typegenerallimit_side_leni  
limit_typemaxmax_side_limiti  threshg333333?
box_threshg333333?input_shapeNunclip_ratiog       @quad)det_box_typeseali  ming?g      ?polyzUnsupported text type {})r3   r4   r6   r7   r8   r:   r9   TextRecognitionz config error for text_rec_model!score_threshr   return_word_boxF)r9   
batch_sizer   )rC   BGR)format ) super__init__getr(   create_pipelinedoc_preprocessor_pipeliner,   create_modeltextline_orientation_modelr1   text_det_limit_side_lentext_det_limit_typetext_det_max_side_limittext_det_threshtext_det_box_threshr9   text_det_unclip_ratior   _sort_boxesr   _crop_by_polysr   
ValueErrorrE   text_det_modeltext_rec_score_threshrB   text_rec_modelr   batch_samplerr   
img_reader)selfr%   r   r    r!   r"   r#   r$   kwargsdoc_preprocessor_configtextline_orientation_configtext_det_configtext_rec_config	__class__s                h/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/inference/pipelines/ocr/pipeline.pyrH   z_OCRPipeline.__init__*   sc   2 	 	
'!	
 	
 	
 	
 	
 %+JJ/Et$L$L!$ 		&,jj&D&D&H&H!+-Z' '# .2-A-A'. .D* )/

3Mt(T(T%( 	*0**\2*F*F*J*J%%'UV+ +' /3.?.?+/ /D+ !**\266::24VW
 
  ,>Y&&+:+>+>?OQT+U+UD('6':':<'O'OD$+:+>+>?OQU+V+VD(#2#6#6x#E#ED '6':':<'M'MD$.22=$GGD)8)<)<^S)Q)QD&,D"-6"B"B"BD^v%%+:+>+>?OQT+U+UD('6':':<'O'OD$+:+>+>?OQU+V+VD(#2#6#6x#E#ED '6':':<'M'MD$)8)<)<^S)Q)QD&.22=$GGD,D"-6"B"B"BD7>>t~NNOOO"//7/7'/3( 0 	
 	
 !**\266::!#EF
 
 &5%8%8%K%K".223DeLL*..}dCC"//)9 0 
 
 /&**\ST:U:UVVV#5111    image_array_listrotate_angle_listc                 L   t          |          t          |          k    s+J dt          |           dt          |           d            |D ]}|dv sJ d|             g }t          ||          D ]/\  }}|dz  }t          ||          }|                    |           0|S )a  
        Rotate the given image arrays by their corresponding angles.
        0 corresponds to 0 degrees, 1 corresponds to 180 degrees.

        Args:
            image_array_list (List[np.ndarray]): A list of input image arrays to be rotated.
            rotate_angle_list (List[int]): A list of rotation indicators (0 or 1).
                                        0 means rotate by 0 degrees
                                        1 means rotate by 180 degrees

        Returns:
            List[np.ndarray]: A list of rotated image arrays.

        Raises:
            AssertionError: If any rotate_angle is not 0 or 1.
            AssertionError: If the lengths of input lists don't match.
        zLength of image_array_list (z*) must match length of rotate_angle_list ())r   r   z&rotate_angle must be 0 or 1, now it's    )lenzipr   append)	r\   re   rf   anglerotated_imagesimage_arrayrotate_indicatorrotate_anglerotated_images	            rc   r   z_OCRPipeline.rotate_image   s   ( #$$)
 )
 
 
 
 E#.>*?*?  E  Ekn  pA  lB  lB  E  E  E
 
 
 ' 	U 	UEF???$TU$T$T????-01ACT-U-U 	1 	1)K)+c1L(lCCM!!-0000rd   model_settingsc                     |d         r| j         st          j        d           dS |d         r| j        st          j        d           dS dS )a8  
        Check if the input parameters are valid based on the initialized models.

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

        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,   z^Set use_textline_orientation, but the models for use_textline_orientation are not initialized.T)r(   r	   errorr,   )r\   rs   s     rc   check_model_settings_validz'_OCRPipeline.check_model_settings_valid   sz     01 	$:S 	Md   5 56	1	 Mp   5trd   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_textline_orientation (Optional[bool]): Whether to use textline orientation.

        Returns:
            dict: A dictionary containing the model settings.
        NTF)r(   r,   )r(   r,   dict)r\   rw   rx   r,   r(   s        rc   get_model_settingsz_OCRPipeline.get_model_settings   sm    " (/4E4M#'#<  +t337HD7P7P'+$$',$#+'+'D$!5%=
 
 
 	
rd   rN   rO   rP   rQ   rR   rS   c                     || j         }|| j        }|| j        }|| j        }|| j        }|| j        }t          ||||||          S )a
  
        Get text detection parameters.

        If a parameter is None, its default value from the instance will be used.

        Args:
            text_det_limit_side_len (Optional[int]): The maximum side length of the text box.
            text_det_limit_type (Optional[str]): The type of limit to apply to the text box.
            text_det_max_side_limit (Optional[int]): The maximum side length of the text box.
            text_det_thresh (Optional[float]): The threshold for text detection.
            text_det_box_thresh (Optional[float]): The threshold for the bounding box.
            text_det_unclip_ratio (Optional[float]): The ratio for unclipping the text box.

        Returns:
            dict: A dictionary containing the text detection parameters.
        N)r3   r4   r7   r6   r8   r:   )rN   rO   rP   rQ   rR   rS   rz   )r\   rN   rO   rP   rQ   rR   rS   s          rc   get_text_det_paramsz _OCRPipeline.get_text_det_params   s    2 #*&*&B#&"&":"*&*&B#""2O&"&": ($($>!2*"2*.
 
 
 	
rd   inputrX   rB   c           	   #   D   )*+,K                         |||          +                     +          sddiV                       |||||	|
          , j         j        t                               |                    D ]\  }}                     |j                  }+d         r&t           
                    |||                    }nd |D             }d |D             }t            j        |fi ,          }d |D             * fd	*D             *+ ,fd
t          |j        |j        |*          D             }r|D ]}g |d<   g |d<   t          t          t!          |                              }*fd|D             }|rg }dg}|D ]}t                               ||         *|                             )g }g }t          )*|                   D ]\\  }}|j        dk    rL|j        d         dk    r;|j        d         dk    r*|                    |           |                    |           ]|*|<   |||         d<   |                    |           |                    |d         t!          |          z              +d         r6d                      |          D             }                     ||          }ndgt!          |          z  }t          |          D ])\  }}||         }|||         ||dz                     |d<   *t          |          D ]\  }}|||         ||dz                     )||         }*|         }d t          )          D             } t1          | d           }!)fd|!D             }"t                               |"                    D ]\  }}#|!|         d         }$|#| |$         d<   t          t!          |                     D ]4}%| |%         d         }#|#d         k    rrt5          |#d         d         ||%         |#d         d                   \  }&}'|d                             |&           |d                             |'           |d                             |#d         d                    n!|d                             |#d                    |d                             |#d                    |d                              |#d!                    |d"                             ||%                    6|D ]g} j        d#k    r2t9          |d"                   }(|(|d$<   rd% |d         D             |d&<   nt;          j        g           |d$<   t?          |          V  hdS )'aQ  
        Predict OCR results based on input images or arrays with optional preprocessing steps.

        Args:
            input (Union[str, list[str], np.ndarray, list[np.ndarray]]): Input image of pdf path(s) or numpy array(s).
            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.
            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_max_side_limit (Optional[int]): Maximum side length 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.
            return_word_box (Optional[bool]): Whether to return word boxes along with recognized texts.
        Returns:
            OCRResult: Generator yielding OCR results for each input image.
        ru   z0the input params for model settings are invalid!Nr(   )rw   rx   c                     g | ]}d |iS 
output_imgrF   ).0arrs     rc   
<listcomp>z(_OCRPipeline.predict.<locals>.<listcomp>h  s    +X+X+XC\3,?+X+X+Xrd   c                     g | ]
}|d          S r   rF   r   items     rc   r   z(_OCRPipeline.predict.<locals>.<listcomp>j  s*     ' ' ''+\"' ' 'rd   c                     g | ]
}|d          S )dt_polysrF   r   s     rc   r   z(_OCRPipeline.predict.<locals>.<listcomp>r  s    FFF$T*-FFFrd   c                 :    g | ]}                     |          S rF   )rT   )r   r   r\   s     rc   r   z(_OCRPipeline.predict.<locals>.<listcomp>t  s'    NNNT--d33NNNrd   c                 D    g | ]\  }}}}||||j         	g g g g d S ))
input_path
page_indexdoc_preprocessor_resr   rs   text_det_paramsr1   rX   rB   	rec_texts
rec_scores	rec_polys	vis_fonts)r1   )
r   r   r   r   r   rs   rB   r\   r   rX   s
        rc   r   z(_OCRPipeline.predict.<locals>.<listcomp>v  se         KJ
,@( #-",,@ (&4'6!%-B'6!#"$!#!#   rd   	text_wordtext_word_regionc                 F    g | ]}t          |                   d k    |S )r   )rj   )r   idxdt_polys_lists     rc   r   z(_OCRPipeline.predict.<locals>.<listcomp>  s0    MMMs]35G1H1H11L1Ls1L1L1Lrd   r   r   r   r,   c                     g | ]M}t          t          j        |d          t          j                                                  d                   NS )	class_ids)dtyper   )intnpasarrayint64ravel)r   textline_angle_infos     rc   r   z(_OCRPipeline.predict.<locals>.<listcomp>  sd     	 	 	 0 J 3K @  #egga) 	 	 	rd   textline_orientation_anglesc                 j    g | ]0\  }}||j         d          t          |j         d                   z  d1S )r   r   )
sub_img_idsub_img_ratio)shapefloat)r   img_idsub_imgs      rc   r   z(_OCRPipeline.predict.<locals>.<listcomp>  sW     ) ) )
 ,FG +1-4]1-=gmTUFV@W@W-W ) ) )rd   c                     | d         S )Nr   rF   )xs    rc   <lambda>z&_OCRPipeline.predict.<locals>.<lambda>  s    ?9K rd   )keyc                 ,    g | ]}|d                   S )r   rF   )r   r   all_subs_of_imgs     rc   r   z(_OCRPipeline.predict.<locals>.<listcomp>  s/     * * *=>,8* * *rd   )rB   r   rec_res	rec_scorerec_textr   r   r   vis_fontr   r2   	rec_boxesc                 ,    g | ]}t          |          S rF   )r   )r   lines     rc   r   z(_OCRPipeline.predict.<locals>.<listcomp>  s0     2 2 2 $ 4D992 2 2rd   text_word_boxes) r{   rv   r}   rX   rB   	enumeraterZ   r[   	instanceslistrK   rW   rk   input_pathspage_indexesrangerj   rU   sizer   rl   extendrM   r   sortedrY   r   r1   r   r   arrayr   )-r\   r~   rw   rx   r,   rN   rO   rP   rQ   rR   rS   rX   rB   _
batch_dataimage_arraysdoc_preprocessor_resultsdoc_preprocessor_imagesdet_resultsresultsresindicesall_subs_of_imgschunk_indicesr   filtered_subsfiltered_polysr   r?   anglesir   sub_img_info_listsorted_subs_infosorted_subs_of_imgr   r   snoword_box_content_listword_box_listr   r   r   rs   r   s-   `          ``                            @@@@rc   predictz_OCRPipeline.predict$  s     F 00(*;=U
 
 ..~>> 	PNOOOO22##!
 
 !($($>!""2O&t'9'9%'@'@AA _	% _	%MAz??:+?@@L45 	Y+/22$5Q*; 3  , ,(( ,Y+X<+X+X+X(' '/G' ' '# ##$;OOOO K GF+FFFMNNNNNNNM         OR*+,!	O O!  G0  1" 1 1C')C$.0C*++5%<!=!=>>??GMMMMgMMMG XC#% !"" Q QC&*++3C8-:L ' 'O
 %'M%'N),_mC>P)Q)Q 8 8#L1,, 'a 01 4 4 'a 01 4 4)00999*11$777)7M#&/=GCL,$++M:::!((r):S=O=O)OPPPP ""<= :	 	 483R3R,4 4	 	 	F (,'8'89I6'R'R$$ TC(8$9$99F'00  FAs!#,C9?%a(=Q+??:C566
 (00 *C *CFAs&6%a(=Q+??'O "#,C,S1H) )
 09/I/I) ) )% (.)/K/K( ( ($* * * *BR* * *& '0++. ,  ' ' K K
7
 &6a%8%F
CJ)*5i@@$S):%;%;<< C C"3C"8"C";/3HHH. 
MGW$+J$7$:$,SM$+J$7$:H" H" D 5}
 !$K 0 7 78M N N N #$6 7 > >} M M M #K 0 7 7
8KA8N O O O O #K 0 7 7
8K L L L-44W[5IJJJ,33GJ4GHHH,33HSMBBB!C"  % %>Y.. 7K8H I II'0C$& 2 2(+,>(?2 2 2-.
 (*x||C$nn$$$$%g_	% _	%rd   )NNNNNN)NNNNNNNNNNN)__name__
__module____qualname____doc__r   r   strr   r   boolr   r   rH   r   r   ndarrayr   r   rv   rz   r{   r   r}   r   r   __classcell__)rb   s   @rc   r   r   &   s=        !% $2659AEj2 j2 j2j2 	j2
 j2  S#X/j2 12j2 j2 U4S>9#<=>j2 
j2 j2 j2 j2 j2 j2X" $RZ 0"EI#Y"	bj	" " " "H $    8
&.tn
 $D>
 #+4.	

 

 
 
 
D 26-115+//315,
 ,
!)#,
 &c],
 "*#	,

 "%,
 &e_,
  (,
 
,
 ,
 ,
 ,
b 8<,03715-115+//31515*.W% W%S$s)RZbj1AABW% '/tnW% $D>	W%
 #+4.W% "*#W% &c]W% "*#W% "%W% &e_W%  (W%  (W% "$W% 
W% W% W% W% W% W% W% W%rd   r   ocrzocr-core)altc                   .    e Zd ZdZed             Zd ZdS )OCRPipelineOCRc                     t           S )N)r   )r\   s    rc   _pipeline_clszOCRPipeline._pipeline_cls  s    rd   c                 .    |                     dd          S )NrC   r   )rI   )r\   r%   s     rc   _get_batch_sizezOCRPipeline._get_batch_size  s    zz,***rd   N)r   r   r   entitiespropertyr   r   rF   rd   rc   r   r     sA        H  X+ + + + +rd   r   )%typingr   r   r   r   r   numpyr   utilsr	   
utils.depsr
   common.batch_samplerr   common.readerr   modelsr   r   utils.benchmarkr   	_parallelr   baser   
componentsr   r   r   r   r   r   resultr   time_methodsr   r   rF   rd   rc   <module>r      s   4 3 3 3 3 3 3 3 3 3 3 3 3 3           2 2 2 2 2 2 5 5 5 5 5 5 & & & & & & 6 6 6 6 6 6 6 6 ( ( ( ( ( ( @ @ @ @ @ @                            T% T% T% T% T%< T% T% T%n J///+ + + + +: + + 0/+ + +rd   