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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ResizeByShortToBatch
ToCHWImage)RunnerPredictor   NormalizeFeaturesIdentityResultc                       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g dg ddddfddZeddd  Zed!d"d# Z  ZS )%ImageFeatureRunnerPredictorz?ImageFeatureRunnerPredictor that inherits from RunnerPredictor.argskwargsreturnNc                    s&   t  j|i | |  \| _| _dS )zInitializes ClasPredictor.

        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__ q/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/models/image_feature/predictor.pyr   "   s   z$ImageFeatureRunnerPredictor.__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   z0ImageFeatureRunnerPredictor._build_batch_samplerc                 C   s   t S )zpReturns the result class, IdentityResult.

        Returns:
            type: The IdentityResult class.
        r   r'   r%   r%   r&   _get_result_class4   s   z-ImageFeatureRunnerPredictor._get_result_classc           
      C   s   dt ddi}| jd d D ]6}t| d }| j| }||i }|dur0d|v r0|d |r:|| fi |n|| \}}|||< qt |d	< i }| jd
 D ]%}	| j|	}| jd
 |	i }|rl|| fi |n|| \}}|||< qQ||fS )zBuild the preprocessors and postprocessors based on the configuration.

        Returns:
            tuple: A tuple containing the preprocessors and postprocessors.
        ReadRGB)formatZ
PreProcessZtransform_opsr   NZreturn_numpyr   ZPostProcess)r   configlistkeys	_FUNC_MAPgetpopr   )
r"   r    cfgZtf_keyfuncr   nameopr!   keyr%   r%   r&   r   <   s"   

 

 
z"ImageFeatureRunnerPredictor._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 |}|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   r   ToCHWr   )xr   )Z
input_pathZ
page_indexZ	input_imgfeature)r    Z	instancesrunnerr!   Zinput_pathsZpage_indexes)r"   r8   Zbatch_raw_imgsZ
batch_imgsr:   Zbatch_predsfeaturesr%   r%   r&   processT   s   
z#ImageFeatureRunnerPredictor.processZResizeImagecv2LINEARc                 C   s8   |s|sJ |rt |d dd}d|fS t|d}d|fS )Nr@   )Ztarget_short_edgeZsize_divisorinterp)Ztarget_sizer   )r   r   )r"   Zresize_shortsizebackendinterpolationr6   r%   r%   r&   build_resizel   s   
z(ImageFeatureRunnerPredictor.build_resizeZNormalizeImage)g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?gp? r	   c                 C   s   |dksJ dt |||dfS )Nr	   r   )scalemeanstd)r   )r"   rH   rI   rG   orderZchannel_numr%   r%   r&   build_normalizez   s   	z+ImageFeatureRunnerPredictor.build_normalizer   c                 C   
   dt  fS )Nr9   )r   r'   r%   r%   r&   build_to_chw      
z(ImageFeatureRunnerPredictor.build_to_chwr   c                 C   rL   )Nr   r   r'   r%   r%   r&   build_normalize_features   rN   z4ImageFeatureRunnerPredictor.build_normalize_features)NNr?   r@   )__name__
__module____qualname____doc__r0   r   registerr   r   r   r   r(   typer)   r   r   r   strnpZndarrayr   r>   rE   rK   rM   rO   __classcell__r%   r%   r#   r&   r      s.    
(
r   )typingr   r   r   r   r   numpyrW   Zutils.func_registerr   Zcommon.batch_samplerr   Zcommon.readerr   commonr   r   r   r   r   Z
predictorsr   Z
processorsr   resultr   r   r%   r%   r%   r&   <module>   s   