o
    0jw                     @   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 ddlmZmZ ddlmZ G dd deZdS )    )AnyDictListTupleUnionN   )FuncRegister   ImageBatchSampler)	ReadImage   )	NormalizeToBatch
ToCHWImage)RunnerPredictor   )ResizeSegPostProcess	SegResultc                	       s   e Zd ZdZi ZeeZ	ddeee	e df 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de
eeejf  deee	e df deeef fddZed		dddZed				dddZ  ZS ) SegRunnerPredictorz6SegRunnerPredictor that inherits from RunnerPredictor.Ntarget_sizeargskwargsreturnc                    s,   t  j|i | || _|  \| _| _dS )a  Initializes SegPredictor.

        Args:
            target_size: Image size used for inference.
            *args: Arbitrary positional arguments passed to the superclass.
            **kwargs: Arbitrary keyword arguments passed to the superclass.
        N)super__init__r   _buildpreprocessorspostprocessers)selfr   r   r   	__class__ y/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/models/semantic_segmentation/predictor.pyr   "   s   zSegRunnerPredictor.__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_sampler3   s   z'SegRunnerPredictor._build_batch_samplerc                 C   s   t S )zfReturns the result class, SegResult.

        Returns:
            type: The SegResult class.
        r   r&   r$   r$   r%   _get_result_class;   s   z$SegRunnerPredictor._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	|vrN| jd	 | d
d\}}||d	< | jdurc| jd	 | | jd\}}||d	< t }	||	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   r   )r   N)r   r   configpop	_FUNC_MAPr   r   r   )
r!   r   cfgZtf_keyfuncr   nameop_r    r$   r$   r%   r   C   s"   


 


zSegRunnerPredictor._build
batch_datac                 C   s   | j d |jd}| j d ||d}| j d |d}| j d |d}| j d |d}| j|d}t|d	krBt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).
            target_size: Image size used for inference.

        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)   )imgsr   )r8   r   r   r,   r   )xr   r   )Zaxis)Z
input_pathZ
page_indexZ	input_imgpred)	r   Z	instancesrunnerlennpsplitr    Zinput_pathsZpage_indexes)r!   r7   r   Zbatch_raw_imgsZ
batch_imgsr9   Zbatch_predsr$   r$   r%   process]   s    zSegRunnerPredictor.processr         ?c                 C   s   t ||d}d|fS )N)meanstdr   )r   )r!   rA   rB   r5   r$   r$   r%   build_normalize   s   z"SegRunnerPredictor.build_normalizer   r.   T    LINEARc                 C   s   t ||||d}d|fS )N)r   
keep_ratiosize_divisorinterpr   )r   )r!   r   rF   rG   rH   r5   r$   r$   r%   build_resize   s   zSegRunnerPredictor.build_resize)N)r@   r@   )r.   TrD   rE   )__name__
__module____qualname____doc__r1   r   registerr   intr   r   r   r   r   r'   r-   r(   r   strr=   Zndarrayr   r?   rC   rI   __classcell__r$   r$   r"   r%   r      sF    

$r   )typingr   r   r   r   r   numpyr=   Zutils.func_registerr   Zcommon.batch_samplerr   Zcommon.readerr   commonr   r   r   Z
predictorsr   Z
processorsr   r   resultr   r   r$   r$   r$   r%   <module>   s   