o
    0j9                     @   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mZ dd
lmZ ddlmZ ddlmZ G dd deZdS )    )AnyDictListTupleUnionN   )FuncRegister   ImageBatchSampler)	ReadImage   )	NormalizeResizeToBatch
ToCHWImage)RunnerPredictor   	MapToMask	UadResultc                       s   e Zd ZdZi Zee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ed	dddZed		dddZeddd Z  ZS )UadRunnerPredictorz6UadRunnerPredictor that inherits from RunnerPredictor.argskwargsreturnNc                    s&   t  j|i | |  \| _| _dS )zInitializes UadPredictor.

        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__ u/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/models/anomaly_detection/predictor.pyr   "   s   zUadRunnerPredictor.__init__c                 C   s   t  S )zBuilds and returns an ImageBatchSampler instance.

        Returns:
            ImageBatchSampler: An instance of ImageBatchSampler.
        r
   r!   r$   r$   r%   _build_batch_sampler,   s   z'UadRunnerPredictor._build_batch_samplerc                 C   s   t S )zfReturns the result class, UadResult.

        Returns:
            type: The UadResult class.
        r   r&   r$   r$   r%   _get_result_class4   s   z$UadRunnerPredictor._get_result_classc           	      C   s   dt ddi}t |d< | jd d D ]"}|d}| j| }|}|r+|| fi |n|| \}}|||< q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RGB)formatToCHWZDeployZ
transformstyper   Map_to_mask)r   r   configpop	_FUNC_MAPr   r   )	r!   r   cfgZtf_keyfuncr   nameopr    r$   r$   r%   r   <   s   


 


zUadRunnerPredictor._build
batch_datac                 C   s   | j d |jd}| j d |d}| j d |d}| j d |d}| j d |d}| j|d}| jd |d	}t|d
krItj|d t|d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, and predicted segmentation maps for every instance of the batch. Keys include 'input_path', 'input_img', and 'pred'.
        r)   )Zimgsr   r   r,   r   )xr.   )predsr   r   )Zaxis)Z
input_pathZ
page_indexZ	input_imgpred)	r   Z	instancesrunnerr    lennpsplitZinput_pathsZpage_indexes)r!   r6   Zbatch_raw_imgsZ
batch_imgsr7   Zbatch_predsr$   r$   r%   processO   s   
zUadRunnerPredictor.processr   FLINEARc                 C   s    |sJ t ||||d}d|fS )N)target_size
keep_ratiosize_divisorinterpr   )r   )r!   r@   rA   rB   rC   r5   r$   r$   r%   build_resizej   s   zUadRunnerPredictor.build_resizer         ?c                 C   s   t ||d}d|fS )N)meanstdr   )r   )r!   rF   rG   r5   r$   r$   r%   build_normalizew   s   z"UadRunnerPredictor.build_normalizer.   c                 C   s   t  }d|fS )Nr.   r   )r!   Zmask_mapr5   r$   r$   r%   map_to_mask   s   zUadRunnerPredictor.map_to_mask)FNr?   )rE   rE   )__name__
__module____qualname____doc__r1   r   registerr   r   r   r   r'   r-   r(   r   r   r   strr<   Zndarrayr   r>   rD   rH   rI   __classcell__r$   r$   r"   r%   r      s$    
(r   )typingr   r   r   r   r   numpyr<   Zutils.func_registerr   Zcommon.batch_samplerr   Zcommon.readerr   commonr   r   r   r   Z
predictorsr   Z
processorsr   resultr   r   r$   r$   r$   r%   <module>   s   