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mZmZ dd	lmZmZ d
dlmZ d
dlmZ dgZG dd deZG dd deZdS )    )AnyDictListTupleUnionN)Image   ImageBatchSampler)	ReadImage   )	NormalizeToBatch
ToCHWImage)RunnerPredictorTransformersPredictor   )DocTrPostProcessDocTrResultZUVDocc                       s   e Zd ZdZdededdf fddZdefdd	Zde	fd
dZ
defddZdeeeejf  deeef fddZ  ZS )WarpRunnerPredictorz7WarpRunnerPredictor that inherits from RunnerPredictor.argskwargsreturnNc                    s&   t  j|i | |  \| _| _dS )zInitializes WarpPredictor.

        Args:
            *args: Arbitrary positional arguments passed to the superclass.
            **kwargs: Arbitrary keyword arguments passed to the superclass.
        N)super__init___buildpreprocessorspostprocessorsselfr   r   	__class__ s/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/models/image_unwarping/predictor.pyr   !   s   zWarpRunnerPredictor.__init__c                 C      t  S )zBuilds and returns an ImageBatchSampler instance.

        Returns:
            ImageBatchSampler: An instance of ImageBatchSampler.
        r	   r    r#   r#   r$   _build_batch_sampler+   s   z(WarpRunnerPredictor._build_batch_samplerc                 C      t S )zgReturns the warpping result, DocTrResult.

        Returns:
            type: The DocTrResult.
        r   r&   r#   r#   r$   _get_result_class3   s   z%WarpRunnerPredictor._get_result_classc                 C   sF   dt ddi}tdddd|d< t |d	< t |d
< dt i}||fS )zBuild the preprocessors and postprocessors based on the configuration.

        Returns:
            tuple: A tuple containing the preprocessors and postprocessors.
        ReadBGRformatg        g      ?gp?)meanZstdscaler   ToCHWr   r   )r   r   r   r   r   )r    r   r   r#   r#   r$   r   ;   s   


zWarpRunnerPredictor._build
batch_datac                 C   sn   | j d |jd}| j d |d}| j d |d}| j d |d}| j|d}| jd |}|j|j||dS )	a  
        Process a batch of data through the preprocessing, inference, and postprocessing.

        Args:
            batch_data (List[Union[str, np.ndarray], ...]): A batch of input data (e.g., image file paths).

        Returns:
            dict: A dictionary containing the input path, raw image, class IDs, scores, and label names for every instance of the batch. Keys include 'input_path', 'input_img', 'class_ids', 'scores', and 'label_names'.
        r*   Zimgsr   r0   r   )xr   Z
input_pathZ
page_indexZ	input_imgZ	doctr_img)r   	instancesrunnerr   input_pathspage_indexes)r    r1   batch_raw_imgsZ
batch_imgsr3   Zbatch_predsbatch_warp_predsr#   r#   r$   processH   s   
zWarpRunnerPredictor.process)__name__
__module____qualname____doc__r   r   r   r
   r'   typer)   r   r   r   strnpndarrayr   r;   __classcell__r#   r#   r!   r$   r      s    
0r   c                       s   e Zd Zdededdf fddZdefddZdefd	d
Z	de
fddZdeeeejf  deeef fddZdd Z  ZS )WarpTransformersPredictorr   r   r   Nc                    s2   t  j|i | tdd| _|  \| _| _d S )Nr+   r,   )r   r   r   read_opr   image_processorZinferr   r!   r#   r$   r   c   s   z"WarpTransformersPredictor.__init__c                 C   r%   Nr	   r&   r#   r#   r$   r'   h   s   z.WarpTransformersPredictor._build_batch_samplerc                 C   r(   rH   r   r&   r#   r#   r$   r)   k   s   z+WarpTransformersPredictor._get_result_classc                 C   s,   ddl m}m} | |}| |}||fS )Nr   )AutoImageProcessor	AutoModel)ZtransformersrI   rJ   Z_load_pretrained_processorZ_load_pretrained_model)r    rI   rJ   rG   modelr#   r#   r$   r   n   s   

z WarpTransformersPredictor._buildr1   c                 C   s\   | j |jd}dd |D }| j|d}|d}| |}| j||d}|j|j||dS )Nr2   c              	   S   s$   g | ]}t |d dddf qS ).N)r   Z	fromarray).0Zimgr#   r#   r$   
<listcomp>w   s   $ z5WarpTransformersPredictor.process.<locals>.<listcomp>)imagesoriginal_imagesrP   r4   )rF   r5   Zpreprocess_imagespopforwardpostprocessr7   r8   )r    r1   r9   rO   Zmodel_inputsrP   outputsr:   r#   r#   r$   r;   u   s   

z!WarpTransformersPredictor.processc                K   sF   | j j|j|d}g }|D ]}|d }|   }|| q|S )NrQ   rO   )rG   Z#post_process_document_rectificationZlast_hidden_statedetachcpunumpyappend)r    rU   rP   r   resultsr:   resZwarpedr#   r#   r$   rT      s   z%WarpTransformersPredictor.postprocess)r<   r=   r>   r   r   r   r
   r'   r@   r)   r   r   r   rA   rB   rC   r   r;   rT   rD   r#   r#   r!   r$   rE   a   s    (rE   )typingr   r   r   r   r   rX   rB   ZPILr   Zcommon.batch_samplerr
   Zcommon.readerr   commonr   r   r   Z
predictorsr   r   Z
processorsr   resultr   ZWARP_TRANSFORMERS_MODELSr   rE   r#   r#   r#   r$   <module>   s   C