o
    0jpU                     @   s  d dl Z 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% ddl&m'Z' ddl
m(Z( ej)G dd deZ*edG dd deZ+dS )    N)AnyDictListOptionalTupleUnion   )logging)pipeline_requires_extra   )ImageBatchSampler)	ReadImage)	HPIConfigPaddlePredictorOption)	DetResult)	benchmark   )(AutoParallelImageSimpleInferencePipeline)BasePipeline)CropByBoxes)DocPreprocessorResult)	OCRResult   )SingleTableRecognitionResultTableRecognitionResult)get_table_recognition_res)get_neighbor_boxes_idxc                !       s  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dZdee	 dee	 dee	 dee	 def
ddZdededede	fddZdejdedeeejf fddZdd Zd d Z		!	d1dejded"ed#e	d$e	d%e	defd&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 d*ee d+ee d,ee d-ee d.ee d#e	d%ee	 def d/d0Z  Z S )3_TableRecognitionPipelinezTable Recognition PipelineNFdeviceengineengine_config	pp_optionuse_hpip
hpi_configconfigr   r    r!   r"   r#   r$   returnc             	      s(  t  jd||||||d| |dd| _| jr,|di dddi}	| |	| _|dd| _| jrH|d	i d
ddi}
| |
| _|d	i dddi}| || _	|dd| _
| j
rw|di dddi}| || _n|di dd| _t | _tdd| _tdd| _dS )a  Initializes the table 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_preprocessorTZSubPipelinesZDocPreprocessorZpipeline_config_errorz+config error for doc_preprocessor_pipeline!use_layout_detectionZ
SubModulesZLayoutDetectionZmodel_config_errorz"config error for layout_det_model!ZTableStructureRecognitionz'config error for table_structure_model!use_ocr_modelZ
GeneralOCRz&config error for general_ocr_pipeline!Nr   )Z
batch_sizeZBGR)format )super__init__getr'   create_pipelinedoc_preprocessor_pipeliner(   Zcreate_modellayout_det_modeltable_structure_modelr)   general_ocr_pipelinegeneral_ocr_config_bakr   _crop_by_boxesr   batch_samplerr   
img_reader)selfr%   r   r    r!   r"   r#   r$   kwargsZdoc_preprocessor_configZlayout_det_configZtable_structure_configZgeneral_ocr_config	__class__r+   w/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/table_recognition/pipeline.pyr-   )   sZ   
z"_TableRecognitionPipeline.__init__use_doc_orientation_classifyuse_doc_unwarpingr(   r)   c                 C   s\   |du r|du r| j }n|du s|du rd}nd}|du r | j}|du r'| j}t|||d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.
            use_ocr_model (Optional[bool]): Whether to use OCR model.

        Returns:
            dict: A dictionary containing the model settings.
        NTF)r'   r(   r)   )r'   r(   r)   dict)r8   r=   r>   r(   r)   r'   r+   r+   r<   get_model_settingsw   s   z,_TableRecognitionPipeline.get_model_settingsmodel_settingsoverall_ocr_reslayout_det_resc                 C   s   |d r| j std dS |d r'|durtd dS | js'td dS |d rB|dur6td	 dS | js@td
 dS dS |du rMtd dS dS )a0  
        Check if the input parameters are valid based on the initialized models.

        Args:
            model_settings (Dict): A dictionary containing input parameters.
            overall_ocr_res (OCRResult): Overall OCR result obtained after running the OCR pipeline.
                The overall OCR result with convert_points_to_boxes information.
            layout_det_res (DetResult): The 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.r)   zLThe OCR models have already been initialized, please set use_ocr_model=Falsez>Set use_ocr_model, but the models for OCR are not initialized.z7Set use_ocr_model=False, but no OCR results were found.T)r'   r	   errorr(   r)   )r8   rA   rB   rC   r+   r+   r<   check_model_settings_valid   s@   
z4_TableRecognitionPipeline.check_model_settings_validimage_arrayinput_paramsc                 C   sP   |d r |d }|d }t | j|||dd }|d }||fS i }|}||fS )a  
        Preprocess the document image based on input parameters.

        Args:
            image_array (np.ndarray): The input image array.
            input_params (dict): Dictionary containing preprocessing parameters.

        Returns:
            tuple[DocPreprocessorResult, np.ndarray]: A tuple containing the preprocessing
                                              result dictionary and the processed image array.
        r'   r=   r>   r=   r>   r   
output_img)listr0   )r8   rF   rG   r=   r>   doc_preprocessor_resdoc_preprocessor_imager+   r+   r<   predict_doc_preprocessor_res   s"   z6_TableRecognitionPipeline.predict_doc_preprocessor_resc           
   	   C      t |ts	| }g }tt|D ]-}dd || D \}}}}t| |||||ddf d }	|d|	d  q|S )t  
        Splits OCR bounding boxes by table cells and retrieves text.

        Args:
            ori_img (ndarray): The original image from which text regions will be extracted.
            cells_bboxes (list or ndarray): Detected cell bounding boxes to extract text from.

        Returns:
            list: A list containing the recognized texts from each cell.
        c                 S      g | ]}t |qS r+   mathceil.0kr+   r+   r<   
<listcomp>      M_TableRecognitionPipeline.split_ocr_bboxes_by_table_cells.<locals>.<listcomp>Nr    	rec_texts
isinstancerJ   tolistrangelenr3   appendjoin
r8   Zori_imgZcells_bboxesZ
texts_listix1y1Zx2y2Zrec_ter+   r+   r<   split_ocr_bboxes_by_table_cells      
(z9_TableRecognitionPipeline.split_ocr_bboxes_by_table_cellsc           
   	   C   rN   )rO   c                 S   rP   r+   rQ   rT   r+   r+   r<   rW   !  rX   rY   Nr   rZ   r[   r\   rc   r+   r+   r<   rh     ri   T	table_box use_ocr_results_with_table_cellsflag_find_nei_textcell_sort_by_y_projectionc                 C   s   t | |d }|dkr|d }dd |D }| ||}	ng }	t||||	||d}
d}|rKt|d |}t|dkrK|D ]}||d	 | d
 7 }q>||
d< |
S )a+  
        Predict table recognition results from an image array, layout detection results, and OCR results.

        Args:
            image_array (np.ndarray): The input image represented as a numpy array.
            overall_ocr_res (OCRResult): Overall OCR result obtained after running the OCR pipeline.
                The overall OCR results containing text recognition information.
            table_box (list): The table box coordinates.
            use_ocr_results_with_table_cells (bool): whether to use OCR results with cells.
            flag_find_nei_text (bool): Whether to find neighboring text.
            cell_sort_by_y_projection (bool): Whether to sort the matched OCR boxes by y-projection.
        Returns:
            SingleTableRecognitionResult: single table recognition result.
        r   TZbboxc                 S   s(   g | ]}|d  |d |d |d gqS )r   r   r      r+   )rU   rectr+   r+   r<   rW   C  s    zR_TableRecognitionPipeline.predict_single_table_recognition_res.<locals>.<listcomp>rm   rZ   Z	rec_boxesr[   z; Zneighbor_texts)rJ   r2   rh   r   r   r`   )r8   rF   rB   rj   rk   rl   rm   Ztable_structure_predZtable_cells_resultZcells_texts_listZsingle_table_recognition_resZneighbor_textZmatch_idx_listidxr+   r+   r<   $predict_single_table_recognition_res)  s8   z>_TableRecognitionPipeline.predict_single_table_recognition_resinputtext_det_limit_side_lentext_det_limit_typetext_det_threshtext_det_box_threshtext_det_unclip_ratiotext_rec_score_threshc                  k   s   |  ||||}|du rd}| |||sddiV  t| |D ]\}}| |jd }|d r?t| j|||dd }nd|i}|d }|d	 r\t| j|||	|
|||d
d }n|dkrn| j	dksgJ | 
| j	| _g }d}|d s|du ri }|jdd \}}dd|d |d g}| j||||d|d}||d< || |d7 }nD|d rt| |d }|d D ]2}|d  dv r| ||g}|d }|d }| j|d ||||d}||d< || |d7 }q|jd |jd |||||d}t|V  q"dS )az  
        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) of pdf(s) to be processed.
            use_layout_detection (bool): Whether to use layout detection.
            use_doc_orientation_classify (bool): Whether to use document orientation classification.
            use_doc_unwarping (bool): Whether to use document unwarping.
            overall_ocr_res (OCRResult): The overall OCR result with convert_points_to_boxes information.
                It will be used if it is not None and use_ocr_model is False.
            layout_det_res (DetResult): The layout detection result.
                It will be used if it is not None and use_layout_detection is False.
            use_ocr_results_with_table_cells (bool): whether to use OCR results with cells.
            cell_sort_by_y_projection (bool): Whether to sort the matched OCR boxes by y-projection.
            **kwargs: Additional keyword arguments.

        Returns:
            TableRecognitionResult: The predicted table recognition result.
        NFrD   z0the input params for model settings are invalid!r   r'   rH   rI   r)   )rt   ru   rv   rw   rx   ry   Tr   r(   r   )rl   rm   table_region_idZboxeslabel)tableboxZimgrp   )Z
input_pathZ
page_indexrK   rC   rB   table_res_listrA   )r@   rE   	enumerater6   r7   Z	instancesrJ   r0   r3   r4   r/   shaperr   ra   r1   lowerr5   Zinput_pathsZpage_indexesr   ) r8   rs   r=   r>   r(   r)   rB   rC   rt   ru   rv   rw   rx   ry   rk   rm   r9   rA   Zimg_idZ
batch_datarF   rK   rL   r~   rz   Z
img_heightZ	img_widthrj   Zsingle_table_rec_resZbox_infoZcrop_img_infoZsingle_img_resr+   r+   r<   predict^  s   '



	
	z!_TableRecognitionPipeline.predict)FTF)NNNNNNNNNNNNFN)!__name__
__module____qualname____doc__r   r   strr   r   boolr   r   r-   r?   r@   r   r   rE   npZndarrayr   r   rM   rh   rJ   r   rr   r   intfloatr   r   __classcell__r+   r+   r:   r<   r   %   s    	N
'
7
 
8	
r   Zocrc                   @   s&   e Zd ZdgZedd Zdd ZdS )TableRecognitionPipelineZtable_recognitionc                 C   s   t S )N)r   )r8   r+   r+   r<   _pipeline_cls  s   z&TableRecognitionPipeline._pipeline_clsc                 C   s   dS )Nr   r+   )r8   r%   r+   r+   r<   _get_batch_size  s   z(TableRecognitionPipeline._get_batch_sizeN)r   r   r   entitiespropertyr   r   r+   r+   r+   r<   r     s
    
r   ),rR   typingr   r   r   r   r   r   numpyr   utilsr	   Z
utils.depsr
   Zcommon.batch_samplerr   Zcommon.readerr   modelsr   r   Zmodels.object_detection.resultr   Zutils.benchmarkr   Z	_parallelr   baser   
componentsr   Zdoc_preprocessor.resultr   Z
ocr.resultr   resultr   r   Z!table_recognition_post_processingr   r   Ztime_methodsr   r   r+   r+   r+   r<   <module>   s2       G