o
    {qi۞                     @   sh   d dl Z ddlmZmZmZmZ ddlmZmZ ddl	m
Z
 g dZG dd	 d	eZG d
d deZdS )    N   )add_simple_inference_argsget_subcommand_argsperform_simple_inferencestr2bool   )PaddleXPipelineWrapperPipelineCLISubcommandExecutor)create_config_from_structure)PP-OCRv3PP-OCRv4PP-OCRv5c                       s`  e Zd Z																																																																	d fdd	Zedd Zddddddddddddddddddddd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dddddddddddddddddddddZdd Ze	dd Z
dd Zdd Z  ZS )PPStructureV3NcB           F         s   |Ad ur|At vrtd|A dt  dttdd ||||frL|@d us(|Ad urK| |@|A\}C}D|Cd u s8|Dd u rGtdt|@ dt|A d|C}|D}n|@d usT|Ad ur[tjdd	d
 t 	 }E||Ed< ||Ed< |E
d |E
d |E| _t jdi |B d S )NzInvalid OCR version: z. Supported values are .c                 S   s   | d u S N )pr   r   j/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddleocr/_pipelines/pp_structurev3.py<lambda>i   s    z(PPStructureV3.__init__.<locals>.<lambda>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text_detection_model_nametext_recognition_model_nameselfkwargsr   )_SUPPORTED_OCR_VERSIONS
ValueErrorallmap_get_ocr_model_namesreprwarningswarnlocalscopypop_paramssuper__init__)Fr   layout_detection_model_namelayout_detection_model_dirlayout_threshold
layout_nmslayout_unclip_ratiolayout_merge_bboxes_modechart_recognition_model_namechart_recognition_model_dirchart_recognition_batch_sizeregion_detection_model_nameregion_detection_model_dir#doc_orientation_classify_model_name"doc_orientation_classify_model_dirdoc_unwarping_model_namedoc_unwarping_model_dirr   text_detection_model_dirtext_det_limit_side_lentext_det_limit_typetext_det_threshtext_det_box_threshtext_det_unclip_ratiotextline_orientation_model_nametextline_orientation_model_dirtextline_orientation_batch_sizer   text_recognition_model_dirtext_recognition_batch_sizetext_rec_score_threshtable_classification_model_nametable_classification_model_dir,wired_table_structure_recognition_model_name+wired_table_structure_recognition_model_dir/wireless_table_structure_recognition_model_name.wireless_table_structure_recognition_model_dir&wired_table_cells_detection_model_name%wired_table_cells_detection_model_dir)wireless_table_cells_detection_model_name(wireless_table_cells_detection_model_dir%table_orientation_classify_model_name$table_orientation_classify_model_dirseal_text_detection_model_nameseal_text_detection_model_dirseal_det_limit_side_lenseal_det_limit_typeseal_det_threshseal_det_box_threshseal_det_unclip_ratio seal_text_recognition_model_nameseal_text_recognition_model_dir seal_text_recognition_batch_sizeseal_rec_score_threshformula_recognition_model_nameformula_recognition_model_dirformula_recognition_batch_sizeuse_doc_orientation_classifyuse_doc_unwarpinguse_textline_orientationuse_seal_recognitionuse_table_recognitionuse_formula_recognitionuse_chart_recognitionuse_region_detectionlangZocr_versionr   Zdet_model_namerec_model_nameparams	__class__r   r   r'      sJ   E


zPPStructureV3.__init__c                 C      dS )NzPP-StructureV3r   r   r   r   r   _paddlex_pipeline_name      z$PPStructureV3._paddlex_pipeline_nameFT)r]   r^   r_   r`   ra   rb   rc   rd   r*   r+   r,   r-   r8   r9   r:   r;   r<   rB   rQ   rR   rS   rT   rU   rY   #use_wired_table_cells_trans_to_html&use_wireless_table_cells_trans_to_htmluse_table_orientation_classify use_ocr_results_with_table_cellsuse_e2e_wired_table_rec_model use_e2e_wireless_table_rec_modelc          !      K   s   | j j|fi d|d|d|d|d|d|d|d|	d	|
d
|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|| S Nr]   r^   r_   r`   ra   rb   rc   rd   r*   r+   r,   r-   r8   r9   r:   r;   r<   rB   rQ   rR   rS   rT   rU   rY   rn   ro   rp   rq   rr   rs   )paddlex_pipelinepredict!r   inputr]   r^   r_   r`   ra   rb   rc   rd   r*   r+   r,   r-   r8   r9   r:   r;   r<   rB   rQ   rR   rS   rT   rU   rY   rn   ro   rp   rq   rr   rs   r   r   r   r   predict_iter   s   $	
zPPStructureV3.predict_iterc          !      K   s   t | j|fi d|d|d|d|d|d|d|d|	d	|
d
|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|| S rt   )listry   rw   r   r   r   rv      s   $	
zPPStructureV3.predictc                 C   s   | j |S r   )ru   concatenate_markdown_pages)r   Zmarkdown_listr   r   r   r{     s   z(PPStructureV3.concatenate_markdown_pagesc                 C   s   t  S r   )"PPStructureV3CLISubcommandExecutor)clsr   r   r   get_cli_subcommand_executor"  s   z)PPStructureV3.get_cli_subcommand_executorc                 C   s  i d| j d d| j d d| j d p| 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 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 d0 d1| j d2 d3| j d4 d5| j d6 d7| j d8 d9| j d: d;| j d< d=| j d> i d?| j d@ dA| j dB dC| j dD dE| j dF dG| j dH dI| j dJ dK| j dL dM| j dN dO| j dP dQ| j dR dS| j dT dU| j dV dW| j dX dY| j dZ d[| j d\ d]| j d^ d_| j d, i d`| j d. da| j d0 db| j d2 dc| j d4 dd| j d6 de| j d8 df| j d: dg| j d< dh| j d> di| j d@ dj| j dB dk| j dD dl| j dF dm| j dn do| j dp dq| j d0 dr| j ds | j dt | j du | j dv | j dw | j dx | j dy | j dz | j d{ | j d| d}	}t|S )~Nz9SubPipelines.DocPreprocessor.use_doc_orientation_classifyr]   z.SubPipelines.DocPreprocessor.use_doc_unwarpingr^   Zuse_doc_preprocessorz0SubPipelines.GeneralOCR.use_textline_orientationr_   r`   ra   rb   rc   rd   z%SubModules.LayoutDetection.model_namer(   z$SubModules.LayoutDetection.model_dirr)   z$SubModules.LayoutDetection.thresholdr*   z%SubModules.LayoutDetection.layout_nmsr+   z.SubModules.LayoutDetection.layout_unclip_ratior,   z3SubModules.LayoutDetection.layout_merge_bboxes_moder-   z&SubModules.ChartRecognition.model_namer.   z%SubModules.ChartRecognition.model_dirr/   z&SubModules.ChartRecognition.batch_sizer0   z%SubModules.RegionDetection.model_namer1   z$SubModules.RegionDetection.model_dirr2   zISubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_namer3   zHSubPipelines.DocPreprocessor.SubModules.DocOrientationClassify.model_dirr4   z?SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_namer5   z>SubPipelines.DocPreprocessor.SubModules.DocUnwarping.model_dirr6   z;SubPipelines.GeneralOCR.SubModules.TextDetection.model_namer   z:SubPipelines.GeneralOCR.SubModules.TextDetection.model_dirr7   z?SubPipelines.GeneralOCR.SubModules.TextDetection.limit_side_lenr8   z;SubPipelines.GeneralOCR.SubModules.TextDetection.limit_typer9   z7SubPipelines.GeneralOCR.SubModules.TextDetection.threshr:   z;SubPipelines.GeneralOCR.SubModules.TextDetection.box_threshr;   z=SubPipelines.GeneralOCR.SubModules.TextDetection.unclip_ratior<   zASubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_namer=   z@SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_dirr>   zASubPipelines.GeneralOCR.SubModules.TextLineOrientation.batch_sizer?   z=SubPipelines.GeneralOCR.SubModules.TextRecognition.model_namer   z<SubPipelines.GeneralOCR.SubModules.TextRecognition.model_dirr@   z=SubPipelines.GeneralOCR.SubModules.TextRecognition.batch_sizerA   z?SubPipelines.GeneralOCR.SubModules.TextRecognition.score_threshrB   zGSubPipelines.TableRecognition.SubModules.TableClassification.model_namerC   zFSubPipelines.TableRecognition.SubModules.TableClassification.model_dirrD   zRSubPipelines.TableRecognition.SubModules.WiredTableStructureRecognition.model_namerE   zQSubPipelines.TableRecognition.SubModules.WiredTableStructureRecognition.model_dirrF   zUSubPipelines.TableRecognition.SubModules.WirelessTableStructureRecognition.model_namerG   zTSubPipelines.TableRecognition.SubModules.WirelessTableStructureRecognition.model_dirrH   zLSubPipelines.TableRecognition.SubModules.WiredTableCellsDetection.model_namerI   zKSubPipelines.TableRecognition.SubModules.WiredTableCellsDetection.model_dirrJ   zOSubPipelines.TableRecognition.SubModules.WirelessTableCellsDetection.model_namerK   zNSubPipelines.TableRecognition.SubModules.WirelessTableCellsDetection.model_dirrL   zLSubPipelines.TableRecognition.SubModules.TableOrientationClassify.model_namerM   zKSubPipelines.TableRecognition.SubModules.TableOrientationClassify.model_dirrN   zYSubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.model_namezXSubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.model_dirz]SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.limit_side_lenzYSubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.limit_typezUSubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.threshzYSubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.box_threshz[SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextDetection.unclip_ratioz_SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_namez^SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.model_dirz_SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextLineOrientation.batch_sizez[SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.model_namezZSubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.model_dirz[SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.batch_sizez]SubPipelines.TableRecognition.SubPipelines.GeneralOCR.SubModules.TextRecognition.score_threshzUSubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.model_namerO   zTSubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.model_dirrP   zYSubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.limit_side_lenzUSubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.limit_typerR   rS   rT   rU   rV   rW   rX   rZ   r[   r\   )	zQSubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.threshzUSubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.box_threshzWSubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextDetection.unclip_ratiozWSubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.model_namezVSubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.model_dirzWSubPipelines.SealRecognition.SubPipelines.SealOCR.SubModules.TextRecognition.batch_sizezHSubPipelines.FormulaRecognition.SubModules.FormulaRecognition.model_namezGSubPipelines.FormulaRecognition.SubModules.FormulaRecognition.model_dirzHSubPipelines.FormulaRecognition.SubModules.FormulaRecognition.batch_size)r%   r
   )r   Z	STRUCTUREr   r   r   _get_paddlex_config_overrides&  s  	






"%(+.147:=@CFILORUX[^adgjmpsvy|     	                !  $  '  *  -  0  3  6  9  <  ?  [z+PPStructureV3._get_paddlex_config_overridesc                 C   s  g d}g d}g d}g d}g d}g d}|d u rd}|d u r@|g d| | v r/d	}n||| | | | v r>d
}ndS |d	kr~d\}	}
|dv rOd}
n"||v rVd}	n||v r]d}	n|dkrdd}	n|dkrkd}	n|dkrqd}	|	d urz|	 d}
d|
fS |dkr|dkrdS |dkrdS dS d }	||v rd}	n||v rd}	n||v rd}	n||v rd}	n||v r|}	d }
|	dkrd}
d|
fS |	d ur|	 d}
d|
fS )N)*afazbscscydadeesetfrgahrhuidisitZkuZlaltlvmimsmtnlnoocpiplptroZrs_latinskslsqsvswtltruzvifrenchgerman)arfaZugur)rubeuk)r   Zrs_cyrillicr   bgr   ZmnZabqZadyZkbdZavaZdarZinhZcheZlbeZleztab)himrneZbhmaiangZbhoZmahZscknewZgomsaZbgc)chenkoreanjapanchinese_chttekatar   )r   r   r   r   r   thelr   r   )NN)r   r   r   r   zPP-OCRv5_server_reclatinZeslavr   r   r   z_PP-OCRv5_mobile_reczPP-OCRv5_server_detr   )PP-OCRv4_mobile_detzPP-OCRv4_mobile_recr   )r   zen_PP-OCRv4_mobile_recarabiccyrillicZ
devanagarizPP-OCRv3_mobile_recz_PP-OCRv3_mobile_reczPP-OCRv3_mobile_detr   )r   re   Zppocr_versionZLATIN_LANGSZARABIC_LANGSZESLAV_LANGSZCYRILLIC_LANGSZDEVANAGARI_LANGSZSPECIFIC_LANGSZrec_langrf   r   r   r   r     s   ,

z"PPStructureV3._get_ocr_model_names)ANNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN)__name__
__module____qualname__r'   propertyrl   ry   rv   r{   classmethodr~   r   r   __classcell__r   r   rh   r   r      s   n
KI
 ^r   c                   @   s(   e Zd Zedd Zdd Zdd ZdS )r|   c                 C   rj   )NZpp_structurev3r   rk   r   r   r   subparser_name  rm   z1PPStructureV3CLISubcommandExecutor.subparser_namec                 C   s  t | |jdtdd |jdtdd |jdtdd |jdtd	d |jd
tdd |jdtdd |jdtdd |jdtdd |jdtdd |jdtdd |jdtdd |jdtdd |jdtdd |jdtdd |jdtdd |jd td!d |jd"td#d |jd$td%d |jd&td'd |jd(td)d |jd*td+d |jd,td-d |jd.td/d |jd0td1d |jd2td3d |jd4td5d |jd6td7d |jd8td9d |jd:td;d |jd<td=d |jd>td?d |jd@tdAd |jdBtdCd |jdDtdEd |jdFtdCd |jdGtdHd |jdItdJd |jdKtdLd |jdMtdNd |jdOtdPd |jdQtdRd |jdStdTd |jdUtdVd |jdWtdXd |jdYtdZd |jd[td\d |jd]td^d |jd_td`d |jdatdbd |jdctddd |jdetdfd |jdgtdhd |jditdjd |jdktdld |jdmtdnd |jdotdpd |jdqtdrd |jdstdtd |jdutdvd |jdwtdxd |jdytdzd d S ){Nz--layout_detection_model_namez#Name of the layout detection model.)typehelpz--layout_detection_model_dirz-Path to the layout detection model directory.z--layout_thresholdz/Score threshold for the layout detection model.z--layout_nmsz'Whether to use NMS in layout detection.z--layout_unclip_ratioz+Expansion coefficient for layout detection.z--layout_merge_bboxes_modez!Overlapping box filtering method.z--chart_recognition_model_namez$Name of the chart recognition model.z--chart_recognition_model_dirz.Path to the chart recognition model directory.z--chart_recognition_batch_sizez+Batch size for the chart recognition model.z--region_detection_model_namez#Name of the region detection model.z--region_detection_model_dirz-Path to the region detection model directory.z%--doc_orientation_classify_model_namez<Name of the document image orientation classification model.z$--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--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!--textline_orientation_model_namez7Name of the text line orientation classification model.z --textline_orientation_model_dirz;Path to the text line orientation classification 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--text_rec_score_threshzrText recognition threshold used in general OCR. Text results with scores greater than this threshold are retained.z!--table_classification_model_namez'Name of the table classification model.z --table_classification_model_dirz1Path to the table classification model directory.z.--wired_table_structure_recognition_model_namez4Name of the wired table structure recognition model.z---wired_table_structure_recognition_model_dirz>Path to the wired table structure recognition model directory.z1--wireless_table_structure_recognition_model_namez7Name of the wireless table structure recognition model.z0--wireless_table_structure_recognition_model_dirz(--wired_table_cells_detection_model_namez.Name of the wired table cells detection model.z'--wired_table_cells_detection_model_dirz8Path to the wired table cells detection model directory.z+--wireless_table_cells_detection_model_namez1Name of the wireless table cells detection model.z*--wireless_table_cells_detection_model_dirz;Path to the wireless table cells detection model directory.z --seal_text_detection_model_namez&Name of the seal text detection model.z--seal_text_detection_model_dirz0Path to the seal text detection model directory.z--seal_det_limit_side_lenzZThis sets a limit on the side length of the input image for the seal text detection model.z--seal_det_limit_typez}This determines how the side length limit is applied to the input image before feeding it into the seal text deteciton model.z--seal_det_threshzDetection pixel threshold for the seal text detection model. Pixels with scores greater than this threshold in the output probability map are considered text pixels.z--seal_det_box_threshzDetection box threshold for the seal 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--seal_det_unclip_ratiozSeal text detection expansion coefficient, which expands the text region using this method. The larger the value, the larger the expansion area.z"--seal_text_recognition_model_namez(Name of the seal text recognition model.z!--seal_text_recognition_model_dirz2Path to the seal text recognition model directory.z"--seal_text_recognition_batch_sizez/Batch size for the seal text recognition model.z--seal_rec_score_threshzcSeal text recognition threshold. Text results with scores greater than this threshold are retained.z --formula_recognition_model_namez&Name of the formula recognition model.z--formula_recognition_model_dirz0Path to the formula recognition model directory.z --formula_recognition_batch_sizez-Batch size for the formula 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--use_seal_recognitionz Whether to use seal recognition.z--use_table_recognitionz!Whether to use table recognition.z--use_formula_recognitionz#Whether to use formula recognition.z--use_chart_recognitionz!Whether to use chart recognition.z--use_region_detectionz Whether to use region detection.)r   add_argumentstrfloatr   int)r   Z	subparserr   r   r   _update_subparser  sd  
z4PPStructureV3CLISubcommandExecutor._update_subparserc                 C   s   t |}tt| d S r   )r   r   r   )r   argsrg   r   r   r   execute_with_args  s
   z4PPStructureV3CLISubcommandExecutor.execute_with_argsN)r   r   r   r   r   r   r   r   r   r   r   r|     s    
  ?r|   )r    Z
_utils.clir   r   r   r   baser   r	   utilsr
   r   r   r|   r   r   r   r   <module>   s        