o
    0j!                     @   s   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 ddlmZ ejG dd deZedddG dd deZdS )    )AnyDictListOptionalUnionN   )logging)pipeline_requires_extra   )ImageBatchSampler)	ReadImage)	HPIConfigPaddlePredictorOption)	benchmark   )(AutoParallelImageSimpleInferencePipeline)BasePipeline)rotate_image   )DocPreprocessorResultc                       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de	fddZdefddZ		dde
eee ejeej f dee	 dee	 defddZ  ZS )_DocPreprocessorPipelinezDoc Preprocessor 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}
| |
| _t|ddd| _	t
dd| _dS )a  Initializes the doc preprocessor 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_orientation_classifyTZ
SubModulesZDocOrientationClassifyZmodel_config_errorz(config error for doc_ori_classify_model!use_doc_unwarpingZDocUnwarpingz%config error for doc_unwarping_model!
batch_sizer   )r"   ZBGR)formatN )super__init__getr    Zcreate_modeldoc_ori_classify_modelr!   doc_unwarping_modelr   batch_samplerr   
img_reader)selfr   r   r   r   r   r   r   kwargsZdoc_ori_classify_configZdoc_unwarping_config	__class__r$   v/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/doc_preprocessor/pipeline.pyr&   #   s8   
z!_DocPreprocessorPipeline.__init__model_settingsc                 C   s<   |d r| j std dS |d r| jstd dS dS )aI  
        Check if the the input params for model settings are valid based on the initialized models.

        Args:
            model_settings (Dict): A dictionary containing model settings.

        Returns:
            bool: True if all required models are initialized according to the model settings, False otherwise.
        r    z`Set use_doc_orientation_classify, but the model for doc orientation classify is not initialized.Fr!   zJSet use_doc_unwarping, but the model for doc unwarping is not initialized.T)r    r   errorr!   )r,   r1   r$   r$   r0   check_model_settings_valid[   s   z3_DocPreprocessorPipeline.check_model_settings_validc                 C   s*   |du r| j }|du r| j}||d}|S )at  
        Retrieve the model settings dictionary based on input parameters.

        Args:
            use_doc_orientation_classify (bool, optional): Whether to use document orientation classification.
            use_doc_unwarping (bool, optional): Whether to use document unwarping.

        Returns:
            dict: A dictionary containing the model settings.
        Nr    r!   r4   )r,   r    r!   r1   r$   r$   r0   get_model_settingsw   s   z+_DocPreprocessorPipeline.get_model_settingsinputr    r!   c              
   c   s.   |  ||}| |sddiV  t| |D ]|\}}| |j}|d rSt| |}g }	g }
t||D ]\}}t	|d d }|	
| t||}|

| q6ndd tt|D }	|}
|d rod	d | |
D }n|
}t|j|j||	|
|D ]\}}}}}}|||||||d
}t|V  q|qdS )a!  
        Predict the preprocessing result for the input image or images.

        Args:
            input (Union[str, list[str], np.ndarray, list[np.ndarray]]): The input image(s) or path(s) to the images or pdfs.
            use_doc_orientation_classify (bool): Whether to use document orientation classification.
            use_doc_unwarping (bool): Whether to use document unwarping.
            **kwargs: Additional keyword arguments.

        Returns:
            DocPreprocessorResult: A generator yielding preprocessing results.
        r2   z0the input params for model settings are invalid!r    Zlabel_namesr   c                 S   s   g | ]}d qS )r$   ).0_r$   r$   r0   
<listcomp>   s    z4_DocPreprocessorPipeline.predict.<locals>.<listcomp>r!   c                 S   s,   g | ]}|d  dddddddf qS )Z	doctr_imgNr7   r$   )r8   itemr$   r$   r0   r:      s    )
input_path
page_indexZ	input_imgr1   anglerot_img
output_imgN)r5   r3   	enumerater*   r+   Z	instanceslistr(   zipintappendr   rangelenr)   Zinput_pathsZpage_indexesr   )r,   r6   r    r!   r1   r9   Z
batch_dataZimage_arrayspredsZanglesZrot_imgsZimgpredr>   r?   Zoutput_imgsr<   r=   Zimage_arrayr@   Zsingle_img_resr$   r$   r0   predict   sX   



		z _DocPreprocessorPipeline.predict)NN)__name__
__module____qualname____doc__r   r   strr   r   boolr   r   r&   r3   dictr5   r   npZndarrayr   rJ   __classcell__r$   r$   r.   r0   r      sP    	8
r   Zocrzocr-core)Zaltc                   @   s$   e Zd ZdZedd Zdd ZdS )DocPreprocessorPipelineZdoc_preprocessorc                 C   s   t S )N)r   )r,   r$   r$   r0   _pipeline_cls   s   z%DocPreprocessorPipeline._pipeline_clsc                 C   s   | ddS )Nr"   r   )r'   )r,   r   r$   r$   r0   _get_batch_size   s   z'DocPreprocessorPipeline._get_batch_sizeN)rK   rL   rM   entitiespropertyrU   rV   r$   r$   r$   r0   rT      s
    
rT   ) typingr   r   r   r   r   numpyrR   utilsr   Z
utils.depsr	   Zcommon.batch_samplerr   Zcommon.readerr   modelsr   r   Zutils.benchmarkr   Z	_parallelr   baser   
componentsr   resultr   Ztime_methodsr   rT   r$   r$   r$   r0   <module>   s"    
4