
    j_                         d dl Z d dlZddlmZmZmZmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZ d	d
ddddddddddZg dZ G d de          Z G d de          ZdS )    N   )add_simple_inference_argsget_subcommand_argsperform_simple_inferencestr2bool)DeprecatedOptionAction
deprecatedwarn_deprecated_param)logger   )PaddleXPipelineWrapperPipelineCLISubcommandExecutor)create_config_from_structuretext_detection_model_dirtext_det_limit_side_lentext_det_limit_typetext_det_threshtext_det_box_threshtext_det_unclip_ratiotext_recognition_model_dirtext_recognition_batch_sizeuse_textline_orientationtextline_orientation_model_dirtextline_orientation_batch_sizedet_model_dirdet_limit_side_lendet_limit_typedet_db_threshdet_db_box_threshdet_db_unclip_ratiorec_model_dirrec_batch_numuse_angle_clscls_model_dircls_batch_num)PP-OCRv3PP-OCRv4PP-OCRv5c                        e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zed             Zddddddddddd
dZddddddddddd
dZ ed          d             Z	e
d	             Zd
 Zd Z xZS )	PaddleOCRNc           	      H   |$|t           vrt          d| dt            d          t          t          d |||
|f                    rV||Q|                     ||          \  }}||0t          dt          |           dt          |           d          |}|}
n||t          j        dd	           i d
|d|d|d|d|d|d|d|d|	d|
d|d|d|d|d|d|d||||||||d}i }|                                D ]r\  } }!| t          v r_t          |          }"t          | |"           |"|v sJ t          |"           d            ||"         t          d|  d|" d          |!||"<   m|!|| <   s|| _         t                      j        d i | d S )!NzInvalid OCR version: z. Supported values are .c                 
    | d u S N )ps    h/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddleocr/_pipelines/ocr.py<lambda>z$PaddleOCR.__init__.<locals>.<lambda>]   s
    !t)     z)No models are available for the language z and OCR version z^`lang` and `ocr_version` will be ignored when model names or model directories are not `None`.r   )
stacklevel#doc_orientation_classify_model_name"doc_orientation_classify_model_dirdoc_unwarping_model_namedoc_unwarping_model_dirtext_detection_model_namer   textline_orientation_model_namer   r   text_recognition_model_namer   r   use_doc_orientation_classifyuse_doc_unwarpingr   r   r   )r   r   r   text_det_input_shapetext_rec_score_threshreturn_word_boxtext_rec_input_shapez is not a valid parameter name.`z` and `z` are mutually exclusive.r0   )_SUPPORTED_OCR_VERSIONS
ValueErrorallmap_get_ocr_model_namesreprwarningswarnitems_DEPRECATED_PARAM_NAME_MAPPINGr
   _paramssuper__init__)$selfr6   r7   r8   r9   r:   r   r;   r   r   r<   r   r   r=   r>   r   r   r   r   r   r   r?   r@   rA   rB   langocr_versionkwargsdet_model_namerec_model_nameparamsbase_paramsnamevalnew_name	__class__s$                                      r2   rP   zPaddleOCR.__init__8   s   < "{:Q'Q'QfffLcfff   ##-,/.	 

 

 	 ;#:151J1J+2 2. ")^-C$uDJJuuaefqararuuu   -;).<+;#:t    

13V
02T
 '(@
 &'>	

 ()B
 '(@
 ./N
 -.L
 ./N
 *+F
 )*D
 *+F
 +,H
  !2
 '(@
  &'>!
" "#6#
$  /#6%:$8%:.$81
 
 
4  	( 	(ID#5559$?%dH555&&&8nnEEE '&&(#/$LDLLLLL   $'x  $'D!!'';'''''r4   c                     dS )NOCRr0   rQ   s    r2   _paddlex_pipeline_namez PaddleOCR._paddlex_pipeline_name       ur4   
r=   r>   r   r   r   r   r   r   r@   rA   c       
         L    | j                             |||||||||	|
|          S Nrb   )paddlex_pipelinepredictrQ   inputr=   r>   r   r   r   r   r   r   r@   rA   s               r2   predict_iterzPaddleOCR.predict_iter   sE     $,,)E/%=$; 3+ 3"7"7+ - 
 
 	
r4   c       
         \    t          |                     |||||||||	|
|                    S rd   )listri   rg   s               r2   rf   zPaddleOCR.predict   sO     -I"3)A(?$7 /$7&;&; /   
 
 	
r4   zPlease use `predict` instead.c                      | j         |fi |S r/   )rf   )rQ   imgrT   s      r2   ocrzPaddleOCR.ocr   s    t|C**6***r4   c                     t                      S r/   )PaddleOCRCLISubcommandExecutor)clss    r2   get_cli_subcommand_executorz%PaddleOCR.get_cli_subcommand_executor   s    -///r4   c           
         i d| j         d         d| j         d         d| j         d         d| j         d         d	| j         d
         d| j         d         d| j         d         d| j         d         d| j         d         d| j         d         d| j         d         d| j         d         d| j         d         d| j         d         d| j         d         p| j         d         d| j         d         d| j         d          | j         d!         | j         d"         | j         d#         | j         d$         | j         d%         | j         d&         | j         d'         | j         d(         d)}t          |          S )*NzISubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_namer6   zHSubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_dirr7   z?SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_namer8   z>SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_dirr9   z#SubModules.TextDetection.model_namer:   z"SubModules.TextDetection.model_dirr   z)SubModules.TextLineOrientation.model_namer;   z(SubModules.TextLineOrientation.model_dirr   z)SubModules.TextLineOrientation.batch_sizer   z%SubModules.TextRecognition.model_namer<   z$SubModules.TextRecognition.model_dirr   z%SubModules.TextRecognition.batch_sizer   z9SubPipelines.DocPreprocessor.use_doc_orientation_classifyr=   z.SubPipelines.DocPreprocessor.use_doc_unwarpingr>   use_doc_preprocessorr   z'SubModules.TextDetection.limit_side_lenr   r   r   r   r   r?   r@   rA   rB   )z#SubModules.TextDetection.limit_typezSubModules.TextDetection.threshz#SubModules.TextDetection.box_threshz%SubModules.TextDetection.unclip_ratioz$SubModules.TextDetection.input_shapez'SubModules.TextRecognition.score_threshz*SubModules.TextRecognition.return_word_boxz&SubModules.TextRecognition.input_shape)rN   r   )rQ   	STRUCTUREs     r2   _get_paddlex_config_overridesz'PaddleOCR._get_paddlex_config_overrides   sE   C
WY]Ye5ZC
 WX\Xd4Y	C
 Nt|*PC
 Mdl)OC
 24<+4C
  1$,*3!C
& 81:'C
, 709-C
2 81:3C
8 4T\-69C
> 3DL,5?C
D 4T\-6EC
J H.JKC
P =dl#?QC
V #DL1O$P %1|/0YC
Z '5O(P[C
\ 6t|)8]C
b 48<@U3V/3|<M/N37<@U3V59\'6 59L&5 8<|'8 ;?,!; 7;l&7AC
 C
 C
	H ,I666r4   c                    g d}g d}g d}g d}g d}g d}|d}|!|g d|z   |z   |z   |z   |z   v rd	}n	||v rd
}ndS |d	k    rod\  }	}
|dv rd}
nX|dk    rd}
nO||v rd}	nH||v rd}	nA||v rd}	n:||v rd}	n3||v rd}	n,|dk    rd}	n#|dk    rd}	n|dk    rd}	n|dk    rd}	n|dk    rd}	|	|	 d}
d|
fS |dk    r|dk    rdS |dk    rdS dS d }	||v rd}	n||v rd}	n||v rd}	n||v rd}	n||v r|}	d }
|	dk    rd}
n|	|	 d }
d!|
fS )"N)1afazbscscydadeesetfrgahrhuidisitkulaltlvmimsmtnlnoocpiplptrors_latinskslsqsvswtltruzvifrenchgermanfieugllbrmcaqu)arfaugurpsr   sdbal)rubeuk)!r   rs_cyrillicr   bgr   mnabqadykbdavadarinhchelbeleztabkkkytgmkttcvbamhrmoudmkvosbuaxaltyvsahkaa)himrnebhmaiangbhomahscknewgomsabgc)chenkoreanjapanchinese_chttekatar   )	r   r   r   r   r   thelr   r   r)   r'   )NN)r   r   r   zPP-OCRv5_server_recr   zen_PP-OCRv5_mobile_reclatineslavarabiccyrillic
devanagarir   r   r   r   r   z_PP-OCRv5_mobile_reczPP-OCRv5_server_detr(   )PP-OCRv4_mobile_detzPP-OCRv4_mobile_rec)r   zen_PP-OCRv4_mobile_reczPP-OCRv3_mobile_recz_PP-OCRv3_mobile_reczPP-OCRv3_mobile_detr0   )rQ   rR   ppocr_versionLATIN_LANGSARABIC_LANGSESLAV_LANGSCYRILLIC_LANGSDEVANAGARI_LANGSSPECIFIC_LANGSrec_langrV   s              r2   rH   zPaddleOCR._get_ocr_model_names4  s   2
 2
 2
f IHH((("
 "
 "
F
 
 
	
 	
 	
 <D 
 
 
    !! ### #$ !+.)) * "zJ&&'1$Hn555!6!9$$"$$"%%#''%)))'!!##$,!B!B!B(.88j((t||CCFF!z H{"""%%#''%)))'>))#H!N4!6%$,!B!B!B(.88r4   )NNNNNNNNNNNNNNNNNNNNNNNNNN)__name__
__module____qualname__rP   propertyr`   ri   rf   r	   rn   classmethodrr   rv   rH   __classcell__)r\   s   @r2   r+   r+   7   s        -1+/!% $"&!%(,'+(,$(#'$(%)!% $  "!"!7k( k( k( k( k( k(Z   X &*!% $  ""
 
 
 
 
B &*!% $  ""
 
 
 
 
> Z/00+ + 10+ 0 0 [0E7 E7 E7NK9 K9 K9 K9 K9 K9 K9r4   r+   c                   0    e Zd Zed             Zd Zd ZdS )rp   c                     dS )Nrn   r0   r_   s    r2   subparser_namez-PaddleOCRCLISubcommandExecutor.subparser_name  ra   r4   c                 V   t          |           |                    dt          d           |                    dt          d           |                    dt          d           |                    dt          d	           |                    d
t          d           |                    dt          d           |                    dt          d           |                    dt          d           |                    dt          d           |                    dt          d           |                    dt          d           |                    dt          d           |                    dt          d           |                    dt          d           |                    dt          d           |                    d t          d!           |                    d"t          d#           |                    d$t
          d%           |                    d&t
          d'           |                    d(t
          d)           |                    d*d+t          d,d-.           |                    d/t
          d0           |                    d1t          d2           |                    d3d+t          d,d4.           |                    d5t          d6           |                    d7t          t          d89           t          t          t          t
          t
          t
          t          t          t          t          t          d:}t                                          D ]>\  }}||v s
J |            |                    d;|z   t          ||         d<| d=>           ?d S )?Nz%--doc_orientation_classify_model_namez<Name of the document image orientation classification model.)typehelpz$--doc_orientation_classify_model_dirzFPath to the document image orientation classification model directory.z--doc_unwarping_model_namez'Name of the text image unwarping model.z--doc_unwarping_model_dirz,Path to the image unwarping model directory.z--text_detection_model_namez!Name of the text detection model.z--text_detection_model_dirz+Path to the text detection model directory.z!--textline_orientation_model_namez7Name of the text line orientation classification model.z --textline_orientation_model_dirzAPath to the text line orientation classification model directory.z!--textline_orientation_batch_sizez>Batch size for the text line orientation classification model.z--text_recognition_model_namez#Name of the text recognition model.z--text_recognition_model_dirz-Path to the text recognition model directory.z--text_recognition_batch_sizez*Batch size for the text recognition model.z--use_doc_orientation_classifyz9Whether to use document image orientation classification.z--use_doc_unwarpingz$Whether to use text image unwarping.z--use_textline_orientationz4Whether to use text line orientation classification.z--text_det_limit_side_lenzUThis sets a limit on the side length of the input image for the text detection model.z--text_det_limit_typezxThis determines how the side length limit is applied to the input image before feeding it into the text deteciton model.z--text_det_threshzDetection pixel threshold for the text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.z--text_det_box_threshzDetection box threshold for the text detection model. A detection result is considered a text region if the average score of all pixels within the border of the result is greater than this threshold.z--text_det_unclip_ratiozText detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.z--text_det_input_shape   )CHWz(Input shape of the text detection model.)nargsr   metavarr  z--text_rec_score_threshz^Text recognition threshold. Text results with scores greater than this threshold are retained.z--return_word_boxz<Whether to return the coordinates of the recognition result.z--text_rec_input_shapez*Input shape of the text recognition model.z--langz/Language in the input image for OCR processing.z--ocr_versionzPP-OCR version to use.)r   choicesr  r   z--z[Deprecated] Please use `--z
` instead.)actionr   r  )
r   add_argumentstrintr   floatrD   rM   rL   r   )rQ   	subparserdeprecated_arg_typesrY   r[   s        r2   _update_subparserz0PaddleOCRCLISubcommandExecutor._update_subparser  s   !),,,3O 	 	
 	
 	

 	2Y 	 	
 	
 	

 	(: 	 	
 	
 	

 	'? 	 	
 	
 	

 	)4 	 	
 	
 	

 	(> 	 	
 	
 	

 	/J 	 	
 	
 	

 	.T 	 	
 	
 	

 	/Q 	 	
 	
 	

 	+6 	 	
 	
 	

 	*@ 	 	
 	
 	

 	+= 	 	
 	
 	

 	,L 	 	
 	
 	

 	!7 	 	
 	
 	

 	(G 	 	
 	
 	

 	'h 	 	
 	
 	

 	# L 	 	
 	
 	

 	 t 	 	
 	
 	

 	# [ 	 	
 	
 	

 	% _ 	 	
 	
 	

 	$#; 	 	
 	
 	
 	%q 	 	
 	
 	

 	O 	 	
 	
 	

 	$#= 	 	
 	
 	
 	3%V 	 	
 	
 	
 	+)	 	 	
 	
 	
 !"%!"!&#(  %   
  
 =BBDD 	 	ND(///////""t-)$/G8GGG	 #    	 	r4   c                 B   t          |          }t                                          D ]`\  }}||v sJ ||         }||         }|,|*t          j        d||           t          j        d           ||                    |           at          t          |           d S )Nz)`--%s` and `--%s` are mutually exclusive.r   )
r   rM   rL   r   errorsysexitpopr   r+   )rQ   argsrW   rY   r[   rZ   new_vals          r2   execute_with_argsz0PaddleOCRCLISubcommandExecutor.execute_with_args  s    $T**<BBDD 
	! 
	!ND(6>>>>,CX&G7#6?x   {

4    F33333r4   N)r   r   r   r   r   r  r  r0   r4   r2   rp   rp     sO          X^ ^ ^@4 4 4 4 4r4   rp   )r  rJ   
_utils.clir   r   r   r   _utils.deprecationr   r	   r
   _utils.loggingr   baser   r   utilsr   rM   rD   r+   rp   r0   r4   r2   <module>r     sv  & 


                     
 $ # # # # # G G G G G G G G / / / / / / 03+&.212/56" "  ?>> H9 H9 H9 H9 H9& H9 H9 H9Vs4 s4 s4 s4 s4%B s4 s4 s4 s4 s4r4   