o
    0j                     @   sZ  d dl Z 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mZ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' ddl(m)Z)m*Z* ddl+m,Z- ddl.m,Z, ddlm/Z/ edrd dl0m1Z1 ej2G dd deZ3edG dd deZ4dS )    N)AnyDictListOptionalTupleUnion   )logging)function_requires_depsis_dep_availablepipeline_requires_extra   )ImageBatchSampler)	ReadImage)	HPIConfigPaddlePredictorOption)	DetResult)	benchmark   )(AutoParallelImageSimpleInferencePipeline)BasePipeline)CropByBoxes)DocPreprocessorResult)get_sub_regions_ocr_res)	OCRResult   )SingleTableRecognitionResultTableRecognitionResult)get_table_recognition_res)get_neighbor_boxes_idxscikit-learn)KMeansc                )       sJ  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	 dHd!d"Zd#d$ Zd%d& Z	'dId(d)Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z 					4	4dJdejded5e!d6e	d7e	d8e	d9e	d:e	d;e	de"fd<d=Z#																	4	4dKd>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 dAee& dBee& dCee& dDee& d6e	d7e	d8e	d9e	dEe	d:e	de'f(dFdGZ(  Z)S )L_TableRecognitionPipelineV2z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	i dddi}| || _
|d	i dddi}| || _|d	i dddi}| || _|d	i dddi}| || _|dd| _d| _| jr|di dddi}| || _n|di dd| _d| _|d	i dd| _t | _tdd| _tdd| _dS )!a  Initializes the table recognition pipeline v2.

        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TableClassificationz,config error for table_classification_model!ZWiredTableStructureRecognitionz-config error for wired_table_structure_model!Z!WirelessTableStructureRecognitionz0config error for wireless_table_structure_model!ZWiredTableCellsDetectionz3config error for wired_table_cells_detection_model!ZWirelessTableCellsDetectionz6config error for wireless_table_cells_detection_model!use_ocr_modelNZ
GeneralOCRz&config error for general_ocr_pipeline!ZTableOrientationClassifyr   )Z
batch_sizeZBGR)format )super__init__getr,   create_pipelinedoc_preprocessor_pipeliner-   create_modellayout_det_modeltable_cls_modelwired_table_rec_modelwireless_table_rec_model!wired_table_cells_detection_model$wireless_table_cells_detection_modelr.   general_ocr_pipelinegeneral_ocr_config_bak table_orientation_classify_model!table_orientation_classify_configr   _crop_by_boxesr   batch_samplerr   
img_reader)selfr*   r$   r%   r&   r'   r(   r)   kwargsZdoc_preprocessor_configZlayout_det_configZtable_cls_configZwired_table_rec_configZwireless_table_rec_configZwired_table_cells_det_configZwireless_table_cells_det_configZgeneral_ocr_config	__class__r0   z/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/table_recognition/pipeline_v2.pyr2   5   s   
z$_TableRecognitionPipelineV2.__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)rD   rI   rJ   r-   r.   r,   r0   r0   rH   get_model_settings   s   z._TableRecognitionPipelineV2.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.   )rD   rM   rN   rO   r0   r0   rH   check_model_settings_valid   s@   
z6_TableRecognitionPipelineV2.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,   rI   rJ   rI   rJ   r   
output_img)listr5   )rD   rR   rS   rI   rJ   doc_preprocessor_resdoc_preprocessor_imager0   r0   rH   predict_doc_preprocessor_res  s"   z8_TableRecognitionPipelineV2.predict_doc_preprocessor_resc           	      C   s   |dkr|d t |d  S |dkrWd}g }g }d|v rSt|d trS|d D ]*}t|trRd|v rRd|v rR|d }|d }t|trR||krR|| || q(||fS |d	kr_|d
 S d S )Nclslabel_namesscoresdet        boxesscore
coordinate
table_stru	structure)npZargmax
isinstancerV   rK   floatappend)	rD   predtask	thresholdresultZ
cell_scoreboxr`   ra   r0   r0   rH   extract_results$  s&   

z+_TableRecognitionPipelineV2.extract_results333333?c                 C   s  t |}t |}g }|dddf }|dddf }|dddf }	|dddf }
|	| |
|  }| ddd }|jdkr|d }|| t || ||dd  }t || ||dd  }t |	| |	|dd  }t |
| |
|dd  }t d|| }t d|| }|| }||| ||dd   |  }t ||kd }||d  }|jdksB||  }||  }||fS )a  
        Apply Non-Maximum Suppression (NMS) on detection results to remove redundant overlapping bounding boxes.

        Args:
            cells_det_results (list): List of bounding boxes, each box is in format [x1, y1, x2, y2].
            cells_det_scores (list): List of confidence scores corresponding to the bounding boxes.
            cells_det_threshold (float): IoU threshold for suppression. Boxes with IoU greater than this threshold
                                        will be suppressed. Default is 0.5.

        Returns:
        Tuple[list, list]: A tuple containing the list of bounding boxes and confidence scores after NMS,
                            while maintaining one-to-one correspondence.
        Nr   r   r   r   r^   )	rd   arrayargsortsizerg   maximumminimumwheretolist)rD   cells_det_resultscells_det_scoresZcells_det_thresholdr_   r\   Zpicked_indicesx1y1x2y2ZareasorderiZxx1Zyy1Zxx2Zyy2whinterZovrZindsZfinal_boxesZfinal_scoresr0   r0   rH   cells_det_results_nms9  s8   



 
z1_TableRecognitionPipelineV2.cells_det_results_nmsc                 C   s   d}|\}}}}g }|D ]3}	|	\}
}}}|
| |kr?|| |kr?|| |kr?|| |kr?|
| || || || g}| | q|S )a  Adjust the coordinates of ocr_det_boxes that are fully inside table_box relative to table_box.

        Args:
            ocr_det_boxes (list of list): List of bounding boxes [x1, y1, x2, y2] in the original image.
            table_box (list): Bounding box [x1, y1, x2, y2] of the target region in the original image.

        Returns:
            list of list: List of adjusted bounding boxes relative to table_box, for boxes fully inside table_box.
        r   )rg   )rD   ocr_det_boxes	table_boxZtolZx_min_tZy_min_tZx_max_tZy_max_tZadjusted_boxesrl   Zx_min_bZy_min_bZx_max_bZy_max_bZadjusted_boxr0   r0   rH   get_region_ocr_det_boxesr  s"   

z4_TableRecognitionPipelineV2.get_region_ocr_det_boxesc                 C   sh  dd }t ddd }t|}t|}t|}d}t||kr%|S t||kr=d}t| d| }||  }d	}	g }
|D ].}g }d}|D ]}|||}|d
kr[|| ||	kset||	krid} nqK|sq|
| qCt|
d
kr|dkr~|n| }n"|dkr|||
 |}n|t| }||
|}tj||fd
d }t|d	| kr|||}|S )ao  
        Process and filter cells_det_results based on ocr_det_results and html_pred_boxes_nums.

        Args:
            cells_det_results (List[List[float]]): List of detected cell rectangles [[x1, y1, x2, y2], ...].
            cells_det_scores (List[float]): List of confidence scores for each rectangle in cells_det_results.
            ocr_det_results (List[List[float]]): List of OCR detected rectangles [[x1, y1, x2, y2], ...].
            html_pred_boxes_nums (int): The desired number of rectangles in the final output.

        Returns:
            List[List[float]]: The processed list of rectangles.
        c           	      S   s   t | d |d }t | d |d }t| d |d }t| d |d }||ks,||kr.dS || ||  }| d | d  | d | d   }|d |d  |d |d    |t| }|S )aX  
            Compute the Intersection over Union (IoU) between two rectangles.

            Args:
                box1 (array-like): [x1, y1, x2, y2] of the first rectangle.
                box2 (array-like): [x1, y1, x2, y2] of the second rectangle.

            Returns:
                float: The IoU between the two rectangles.
            r   r   r   r   r^   )maxminrf   )	box1box2x_lefty_topx_righty_bottomintersection_area	box1_areaiour0   r0   rH   compute_iou  s     zO_TableRecognitionPipelineV2.cells_det_results_reprocessing.<locals>.compute_iour    c                    s   t  }||kr
 S tdd  D }t|ddd}||}g }t|D ]S}t||kd }t |dkr8q&t fdd|D }	t|	dddf }
t|	dddf }t|	ddd	f }t|	ddd
f }|	|
|||g q&|S )a}  
            Combine rectangles into N rectangles based on geometric proximity.

            Args:
                rectangles (list of list of int): A list of rectangles, each represented by [x1, y1, x2, y2].
                N (int): The desired number of combined rectangles.

            Returns:
                list of list of int: A list of N combined rectangles.
            c                 S   s4   g | ]}|d  |d  d |d |d  d gqS )r   r   r   r   r0   .0rectr0   r0   rH   
<listcomp>  s    zj_TableRecognitionPipelineV2.cells_det_results_reprocessing.<locals>.combine_rectangles.<locals>.<listcomp>r   auto)Z
n_clustersZrandom_stateZn_initc                       g | ]} | qS r0   r0   )r   idx
rectanglesr0   rH   r         Nr   r   r   )
lenrd   rp   r!   Zfit_predictrangeru   r   r   rg   )r   NZ	num_rectsZcentersZkmeanslabelsZcombined_rectanglesr~   indicesZcluster_rectsZx1_minZy1_minZx2_maxZy2_maxr0   r   rH   combine_rectangles  s,   

zV_TableRecognitionPipelineV2.cells_det_results_reprocessing.<locals>.combine_rectanglesFTNg333333?r   )Zaxis)	r
   rd   rp   r   rq   rv   rg   sumZconcatenate)rD   rw   rx   ocr_det_resultsZhtml_pred_boxes_numsr   r   Zmore_cells_flagZtop_indicesZiou_thresholdZocr_miss_boxesZocr_rectZmerge_ocr_box_iouZhas_large_iouZ	cell_rectr   Zfinal_resultsr   Zocr_supp_boxesr0   r0   rH   cells_det_results_reprocessing  s`   

2






z:_TableRecognitionPipelineV2.cells_det_results_reprocessingr   c              	      s  dd   fdd}dd }t |d dr|d  }n|d }|d	 }g }	g }
d
}|t|k r|| }|| }|||}t||kr||||}g }|D ]^}t|d
 t|d t|d t|d f\}}}}|| dkr|| dkr||jd
 k r||jd k rt| j|||||ddf d
 }d|v r|d }nd}nd}|| qM|		| |
	| n
|	| |
| |d7 }|t|k s0|	|d< |
|d	< |S )a  
        Split OCR bounding boxes based on table cell boundaries when they span multiple cells horizontally.

        Args:
            cells_det_results (list): List of cell bounding boxes in format [x1, y1, x2, y2]
            overall_ocr_res (dict): Dictionary containing OCR results with keys:
                                - 'rec_boxes': OCR bounding boxes (will be converted to list)
                                - 'rec_texts': OCR recognized texts
            ori_img (np.array): Original input image array
            k (int): Threshold for determining when to split (minimum number of cells spanned)

        Returns:
            dict: Modified overall_ocr_res with split boxes and texts
        c           	      S   s   t | d |d }t | d |d }t| d |d }t| d |d }||k s,||k r.dS || ||  }| d | d  | d | d   }|d |d  |d |d   }|| S )a  
            Calculate Intersection over Union (IoU) between two bounding boxes.

            Args:
                box1 (list): [x1, y1, x2, y2]
                box2 (list): [x1, y1, x2, y2]

            Returns:
                float: IoU value
            r   r   r   r   r^   )r   r   )	r   r   r   r   r   r   r   r   Z	box2_arear0   r0   rH   calculate_iou@  s     zR_TableRecognitionPipelineV2.split_ocr_bboxes_by_table_cells.<locals>.calculate_iouc                    sF   g }t  D ]\}}| |dkr|| q|j fddd |S )aM  
            Find cells that overlap significantly with the OCR box (IoU > 0.5).

            Args:
                ocr_box (list): OCR bounding box [x1, y1, x2, y2]
                cells (list): List of cell bounding boxes

            Returns:
                list: Indices of overlapping cells, sorted by x-coordinate
            g      ?c                    s    |  d S )Nr   r0   r~   cellsr0   rH   <lambda>i  s    zl_TableRecognitionPipelineV2.split_ocr_bboxes_by_table_cells.<locals>.get_overlapping_cells.<locals>.<lambda>key)	enumeraterg   sort)ocr_boxr   Zoverlappingr   cellr   r   rH   get_overlapping_cellsY  s   
zZ_TableRecognitionPipelineV2.split_ocr_bboxes_by_table_cells.<locals>.get_overlapping_cellsc              	      sn  |s| gS g } fdd|D }| d |d d k r-| | d | d |d d | d g tt|D ]H}|| }| t| d |d | d t| d |d | d g |t|d k r{||d  }|d |d k r{| |d | d |d | d g q3|d }|d | d k r| |d | d | d | d g g }	t }
|D ]}t|}||
vr|
| |	 | q|	S )aS  
            Split OCR box vertically at cell boundaries.

            Args:
                ocr_box (list): Original OCR box [x1, y1, x2, y2]
                cell_indices (list): Indices of cells to split by
                cells (list): All cell bounding boxes

            Returns:
                list: List of split boxes
            c                    r   r0   r0   )r   r~   r   r0   rH   r   {  r   zk_TableRecognitionPipelineV2.split_ocr_bboxes_by_table_cells.<locals>.split_box_by_cells.<locals>.<listcomp>r   r   r   r   ro   )rg   r   r   r   r   settupleadd)r   Zcell_indicesr   split_boxesZcells_to_splitr~   Zcurrent_cellZ	next_cell	last_cellZunique_boxesseenrl   Z	box_tupler0   r   rH   split_box_by_cellsl  sH   "

zW_TableRecognitionPipelineV2.split_ocr_bboxes_by_table_cells.<locals>.split_box_by_cells	rec_boxesrv   	rec_textsr   r   r   r   NZrec_text )
hasattrrv   r   intshaperV   r=   Ztext_rec_modelrg   extend)rD   rw   rN   ori_imgkr   r   r   Z	ocr_textsZ	new_boxesZ	new_textsr~   r   textZoverlapping_cellsr   Zsplit_textsrl   ry   rz   r{   r|   Z
ocr_resultrk   r0   r   rH   split_ocr_bboxes_by_table_cells.  s`   2
4


.z;_TableRecognitionPipelineV2.split_ocr_bboxes_by_table_cellsc           
   	   C   s   t |ts	| }g }tt|D ]9}dd || D \}}}}|| dkrJ|| dkrJt| |||||ddf d }	|d|	d  q|S )at  
        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   s   g | ]}t |qS r0   )mathceil)r   r   r0   r0   rH   r         zH_TableRecognitionPipelineV2.gen_ocr_with_table_cells.<locals>.<listcomp>r   Nr   r   r   )re   rV   rv   r   r   r=   rg   join)
rD   r   Zcells_bboxesZ
texts_listr~   ry   rz   r{   r|   Zrec_ter0   r0   rH   gen_ocr_with_table_cells  s   
(z4_TableRecognitionPipelineV2.gen_ocr_with_table_cellsc                 C   s   g }t t|D ]f}|| d || d || d || d f\}}}	}
|dkr7|||	 }}|
|| }}n)|dkrN||	 ||
 }}|| || }}n|dkr`||
 |}}|| |	}}t||||g}|| q|S )a  
        Map bounding boxes from the rotated image back to the original image.

        Parameters:
        - detections: list of numpy arrays, each containing bounding box coordinates [x1, y1, x2, y2]
        - table_angle: rotation angle in degrees (90, 180, or 270)
        - width_orig: width of the original image (img1)
        - height_orig: height of the original image (img1)

        Returns:
        - mapped_detections: list of numpy arrays with mapped bounding box coordinates
        r   r   r   r   27018090)r   r   rd   rp   rg   )rD   Z
detectionstable_angle	img_width
img_heightZmapped_detectionsr~   tbx1tby1tbx2tby2new_x1new_y1new_x2new_y2Znew_boxr0   r0   rH   map_cells_to_original_image  s&   



z7_TableRecognitionPipelineV2.map_cells_to_original_imagec                 C   sB   g d}d dd |D }td| d|}dd |D }|S )	z
        Split HTML string by keywords.

        Args:
            html_string (str): The HTML string.
        Returns:
            split_html (list): The list of html keywords.
        )0z<thead>z</thead>z<tbody>z</tbody><tr></tr>z<td><td></td>zcolspan="2"zcolspan="3"zcolspan="4"zcolspan="5"zcolspan="6"zcolspan="7"zcolspan="8"zcolspan="9"zcolspan="10"zcolspan="11"zcolspan="12"zcolspan="13"zcolspan="14"zcolspan="15"zcolspan="16"zcolspan="17"zcolspan="18"zcolspan="19"zcolspan="20"zrowspan="2"zrowspan="3"zrowspan="4"zrowspan="5"zrowspan="6"zrowspan="7"zrowspan="8"zrowspan="9"zrowspan="10"zrowspan="11"zrowspan="12"zrowspan="13"zrowspan="14"zrowspan="15"zrowspan="16"zrowspan="17"zrowspan="18"zrowspan="19"zrowspan="20"|c                 s   s    | ]}t |V  qd S N)reescape)r   keywordr0   r0   rH   	<genexpr>Z  s    zG_TableRecognitionPipelineV2.split_string_by_keywords.<locals>.<genexpr>()c                 S   s   g | ]}|r|qS r0   r0   )r   partr0   r0   rH   r   \  r   zH_TableRecognitionPipelineV2.split_string_by_keywords.<locals>.<listcomp>)r   r   split)rD   Zhtml_stringkeywordsZregex_patternZsplit_resultZ
split_htmlr0   r0   rH   split_string_by_keywords  s
   
2z4_TableRecognitionPipelineV2.split_string_by_keywordsc                 C   s   |sg S t t|}g }|d g}|dd  D ] }t||d  |kr)|| q|t|t|  |g}q|t|t|  |S )Nr   r   ro   )sortedr   absrg   r   r   )rD   Z	positions	toleranceZ	clusteredZcurrent_clusterposr0   r0   rH   cluster_positions_  s   
z-_TableRecognitionPipelineV2.cluster_positionsc               	      s  d}dd |D }dd |D }|  |||  ||dd tD }dd tD }td }td   fd	dt|D }g }	d
}
i }t|D ]\}}|\tttfddd}tttfddd}tttfddd}tttfddd}t||}t||}t||}t||}|| }|| }|d
krd}|d
krd}|	||||dd t||| D ]}t||| D ]}||f}||v rq|||< qqqOd}t|D ]v}|d7 }d
}| k rb||f}||v rV|| }
|	|
 }|d |krO|d |krO|d }|d }|dkr/d| dnd}|dkr<d| dnd}|d }|d| | d| d7 }||d 7 }n|d 7 }|d7 }| k s|d!7 }q|d"7 }| |}|S )#z
        Trans table cells bboxes to HTML.

        Args:
            cells_det_results (list): The table cells detection results.
        Returns:
            html (list): The list of html keywords.
           c                 S   &   g | ]}|d  |d fD ]}|qqS )r   r   r0   )r   r   xr0   r0   rH   r   y     & zO_TableRecognitionPipelineV2.trans_cells_det_results_to_html.<locals>.<listcomp>c                 S   r   )r   r   r0   )r   r   yr0   r0   rH   r   z  r   c                 S      i | ]\}}||qS r0   r0   )r   r~   r   r0   r0   rH   
<dictcomp>}  r   zO_TableRecognitionPipelineV2.trans_cells_det_results_to_html.<locals>.<dictcomp>c                 S   r   r0   r0   )r   r~   r   r0   r0   rH   r   ~  r   r   c                    s   g | ]}d d t  D qS )c                 S   s   g | ]}d qS r   r0   r   _r0   r0   rH   r         zZ_TableRecognitionPipelineV2.trans_cells_det_results_to_html.<locals>.<listcomp>.<listcomp>)r   r   )num_colsr0   rH   r     s    r   c                       t |    S r   r   r   )ry   x_positionsr0   rH   r     r   zM_TableRecognitionPipelineV2.trans_cells_det_results_to_html.<locals>.<lambda>r   c                    r   r   r   r   )r{   r   r0   rH   r     r   c                    r   r   r   r   )rz   y_positionsr0   rH   r     r   c                    r   r   r   r   )r|   r   r0   rH   r     r   r   )	row_start	col_startrowspancolspancontentz<table><tbody>r   r   r   r   r   z
 rowspan=""z
 colspan="r  r   r   r   z	<td></td>r   z</tbody></table>)r   r   r   r   r   r   rg   r   ) rD   rw   r   Zx_coordsZy_coordsZx_position_to_indexZy_position_to_indexZnum_rowsgridZ
cells_infoZ
cell_indexZcell_mapindexr   Zx1_idxZx2_idxZy1_idxZy2_idxr   Zcol_endr   Zrow_endr   r   rcr   htmlZ	cell_infoZrowspan_attrZcolspan_attrr  r0   )r   ry   r{   r   rz   r|   r   rH   trans_cells_det_results_to_htmln  s   




	




z;_TableRecognitionPipelineV2.trans_cells_det_results_to_htmlTr   use_e2e_wired_table_rec_model use_e2e_wireless_table_rec_model#use_wired_table_cells_trans_to_html&use_wireless_table_cells_trans_to_html use_ocr_results_with_table_cellsflag_find_nei_textc
              	   C   sp  t | |d }
| |
d}d}d}|dkrG|dkrd}n	t | |d }|dkr;d}|dkr:t | |d }n?t | j|ddd }n3|dkrz|dkrRd}n	t | |d }|dkrod}|dkrnt | |d }nt | j|ddd }|dkr| |d	\}}| ||\}}|dkr| |}n| |d
}| 	|d 
 |}| |||t|d }|dkr| jdkrt|g}t||}| |||}g }n| ||}i }ni }g }t|||||||| j}n#g }d}|d }dd |D }|dkr| ||d< t|||||}d}|	r2t|d |}t|dkr2|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_e2e_wired_table_rec_model (bool): Whether to use end-to-end wired table recognition model.
            use_e2e_wireless_table_rec_model (bool): Whether to use end-to-end wireless table recognition model.
            use_wired_table_cells_trans_to_html (bool): Whether to use wired table cells trans to HTML.
            use_wireless_table_cells_trans_to_html (bool): Whether to use wireless table cells trans to HTML.
            use_ocr_results_with_table_cells (bool): Whether to use OCR results processed by table cells.
            flag_find_nei_text (bool): Whether to find neighboring text.
        Returns:
            SingleTableRecognitionResult: single table recognition result.
        r   rZ   FZwired_tableTrn   )rj   Zwireless_tabler]   rb   r   Zbboxc                 S   s(   g | ]}|d  |d |d |d gqS )r   r   r   r   r0   r   r0   r0   rH   r   J  s    zT_TableRecognitionPipelineV2.predict_single_table_recognition_res.<locals>.<listcomp>rc   r   r   z; Zneighbor_texts)rV   r8   rm   r9   r;   r:   r<   r   r  r   rv   r   r   cells_split_ocrrd   rp   r   r   r   r   get_table_recognition_res_e2er   )rD   rR   rN   r   r	  r
  r  r  r  r  Ztable_cls_predZtable_cls_resultZuse_e2e_modelZcells_trans_to_htmlZtable_structure_predZtable_cells_predZtable_cells_resultZtable_cells_scoreZtable_structure_resultr   Ztable_box_copyZtable_ocr_predZcells_texts_listZsingle_table_recognition_resZtable_cells_result_e2eZneighbor_textZmatch_idx_listr   r0   r0   rH   $predict_single_table_recognition_res  s   	


z@_TableRecognitionPipelineV2.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_threshuse_table_orientation_classifyc           0      k   s   d| _ |dkr| jdu r| jdksJ | | j| _| ||||}| |||s/ddiV  t| |D ]\}}| |j	d }|d rTt
| j|||dd }nd|i}|d }|d	 rqt
| j|||	|
|||d
d }n | jdu r|dkr| j dks|dkr| jdksJ | | j| _|dkrd}g }d}|d s|du r|jdd \}}dd|d |d g} |dkrt
| |d d d }|dkrtj|dd}n|dkrtj|dd}n|dkrtj|dd}|dv rXt
| j|||	|
|||d
d }| d | d | d | d f\}!}"}#}$|dkr'|"||# }%}&|$||! }'}(n+|dkr?||# ||$ }%}&||! ||" }'}(n|dkrR||$ |!}%}&||" |#}'}(|%|&|'|(g} | j||| |||||dd	})||)d< |dkr|dkr|jdd \}}| |)d ||||)d< ||) |d7 }n=|d rt
| |d }|jdd \}}|d D ] }*|*d  dv r| ||*g}+|+d }+|+d } |dkr| },t
| |+d d d d }|dkrtj|+d dd|+d< tj|,dd},n/|dkrtj|+d dd|+d< tj|,dd},n|dkr)tj|+d dd|+d< tj|,dd},|dv rt
| j|,||	|
|||d
d }| d | d | d | d f\}!}"}#}$|dkrd|"||# }%}&|$||! }'}(n+|dkr|||# ||$ }%}&||! ||" }'}(n|dkr||$ |!}%}&||" |#}'}(|%|&|'|(g} | |+d || |||||})||)d< |dkr|dkr|,jdd \}-}.| |)d ||.|-|)d< ||) |d7 }q|jd |jd |||||d}/t|/V  q6dS ) a9  
        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_e2e_wired_table_rec_model (bool): Whether to use end-to-end wired table recognition model.
            use_e2e_wireless_table_rec_model (bool): Whether to use end-to-end wireless table recognition model.
            use_wired_table_cells_trans_to_html (bool): Whether to use wired table cells trans to HTML.
            use_wireless_table_cells_trans_to_html (bool): Whether to use wireless table cells trans to HTML.
            use_table_orientation_classify (bool): Whether to use table orientation classification.
            use_ocr_results_with_table_cells (bool): Whether to use OCR results processed by table cells.
            **kwargs: Additional keyword arguments.

        Returns:
            TableRecognitionResult: The predicted table recognition result.
        TNrP   z0the input params for model settings are invalid!r   r,   rT   rU   r.   )r  r  r  r  r  r  F0r   r-   r   r[   r   )r   r   r   r   )r   r   r   )r  table_region_idZcell_box_listr_   label)tablerl   Zimg)Z
input_pathZ
page_indexrW   rO   rN   table_res_listrM   )r  r?   r@   r6   rL   rQ   r   rB   rC   Z	instancesrV   r5   r=   r>   r4   r   rd   Zrot90r  r   rg   r7   lowerrA   copyZinput_pathsZpage_indexesr   )0rD   r  rI   rJ   r-   r.   rN   rO   r  r  r  r  r  r  r	  r
  r  r  r  r  rE   rM   Zimg_idZ
batch_datarR   rW   rX   r   r  r  r   r   r   r   r   r   r   r   r   r   r   Zsingle_table_rec_resZbox_infoZcrop_img_infoZdoc_preprocessor_image_copyZimg_height_copyZimg_width_copyZsingle_img_resr0   r0   rH   predictd  s  /
























 z#_TableRecognitionPipelineV2.predict)rn   )r   )FFFFTT)NNNNNNNNNNNNFFFFTT)*__name__
__module____qualname____doc__r   r   strr   r   boolr   r   r2   rK   rL   r   r   rQ   rd   Zndarrayr   r   rY   rm   r   r   r   r   r   r   r   r   r  rV   r   r  r   r   rf   r   r!  __classcell__r0   r0   rF   rH   r"   1   s2   	s
'
7

9" 
 0%A]	

 "	
r"   Zocrc                   @   s&   e Zd ZdgZedd Zdd ZdS )TableRecognitionPipelineV2Ztable_recognition_v2c                 C   s   t S r   )r"   )rD   r0   r0   rH   _pipeline_cls  s   z(TableRecognitionPipelineV2._pipeline_clsc                 C   s   dS )Nr   r0   )rD   r*   r0   r0   rH   _get_batch_size  s   z*TableRecognitionPipelineV2._get_batch_sizeN)r"  r#  r$  entitiespropertyr*  r+  r0   r0   r0   rH   r)    s
    
r)  )5r   r   typingr   r   r   r   r   r   numpyrd   utilsr	   Z
utils.depsr
   r   r   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layout_parsing.utilsr   Z
ocr.resultr   rk   r   r   Z!table_recognition_post_processingr   r  Z$table_recognition_post_processing_v2r   Zsklearn.clusterr!   Ztime_methodsr"   r)  r0   r0   r0   rH   <module>   sJ              [