o
    0j~7                     @   s   d dl mZmZmZmZmZ d dlZddlm	Z	m
Z
 ddlmZ e
dr(d dlZeje	dG dd	 d	ZejG d
d dZejG dd dZeje	dG dd dZejG dd dZejG dd dZdS )    )ListOptionalSequenceTupleUnionN   )class_requires_depsis_dep_available   )	benchmarkzopencv-contrib-pythonc                       s   e Zd ZdZ			ddededeedf ded	df
 fd
dZdee	j
 d	ee	j
 fddZdee	j
 d	ee	j
 fddZ  ZS )ScalezScale images.TNF
short_sizefixed_ratio
keep_ratiodo_roundreturnc                    s:   t    || _|r|r|rJ d|| _|| _|| _dS )a  
        Initializes the Scale class.

        Args:
            short_size (int): The target size for the shorter side of the image.
            fixed_ratio (bool): Whether to maintain a fixed aspect ratio of 4:3.
            keep_ratio (Union[bool, None]): Whether to keep the aspect ratio. Cannot be True if fixed_ratio is True.
            do_round (bool): Whether to round the scaling factor.
        z:fixed_ratio and keep_ratio cannot be true at the same timeN)super__init__r   r   r   r   )selfr   r   r   r   	__class__ y/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/models/video_classification/processors.pyr       s   

zScale.__init__videoc                 C   s  |}g }t t|D ]}|| }t|tjr|j\}}}nt||kr(|| jks1||kr7|| jkr7|| q
||kr| j}	| j	rKt
| jd d }
ny| jdu rT| j}
np| j| }| jrft
|t| d nt
|| j | }
| jr|t
|t| d n| j}	nD| j}
| j	rt
| jd d }	n4| jdu r| j}	n+| j| }| jrt
|t| d n| j}
| jrt
|t| d nt
|| j | }	|tj||	|
ftjd q
|}|S )aZ  
        Performs resize operations on a sequence of images.

        Args:
            video (List[np.ndarray]): List where each item is an image,  as a numpy array.
             For example, [np.ndarray0, np.ndarray1, np.ndarray2, ...]

        Returns:
            List[np.ndarray]: List where each item is a np.ndarray after scaling.
        g      @g      @Fg      ?)interpolation)rangelen
isinstancenpndarrayshapeNotImplementedErrorr   appendr   intr   r   floatcv2resizeZINTER_LINEAR)r   r   imgsZresized_imgsiimghw_owZohZscale_factorr   r   r   scale9   sX   $




zScale.scalevideosc                        fdd|D S )a6  
        Apply the scaling operation to a list of videos.

        Args:
            videos (List[np.ndarray]): A list of videos, where each video is a sequence
            of images.

        Returns:
            List[np.ndarray]: A list of videos after scaling, where each video is a list of images.
        c                       g | ]}  |qS r   )r.   .0r   r   r   r   
<listcomp>       z"Scale.__call__.<locals>.<listcomp>r   r   r/   r   r4   r   __call__|   s   zScale.__call__)TNF)__name__
__module____qualname____doc__r#   boolr   r   r   r   r   r.   r8   __classcell__r   r   r   r   r      s$    
&Cr   c                       sl   e Zd ZdZddededdf fddZd	eej	 deej	 fd
dZ
deej	 deej	 fddZ  ZS )
CenterCropzCenter crop images.Ttarget_sizer   r   Nc                    s   t    || _|| _dS )z
        Initializes the CenterCrop class.

        Args:
            target_size (int): The size of the cropped area.
            do_round (bool): Whether to round the crop coordinates.
        N)r   r   r@   r   )r   r@   r   r   r   r   r      s   

zCenterCrop.__init__r'   c                 C   s   g }| j | j }}|D ]S}|j\}}}|| j kr|| j ks'J d||| j | jr4tt|| d n|| d }	| jrGtt|| d n|| d }
|||
|
| |	|	| f  q|S )a  
        Performs center crop operations on images.

        Args:
            imgs (List[np.ndarray]): A sequence of images (a numpy array).

        Returns:
            List[np.ndarray]: A list of images after center cropping or a cropped numpy array.
        z>image width({}) and height({}) should be larger than crop sizeg       @   )r@   r    formatr   r#   roundr"   )r   r'   Z	crop_imgsthZtwr)   r*   r+   r,   x1y1r   r   r   center_crop   s   
&&$zCenterCrop.center_cropr/   c                    r0   )a  
        Apply the center crop operation to a list of videos.

        Args:
            videos (List[np.ndarray]): A list of videos, where each video is a sequence of images.

        Returns:
            List[np.ndarray]: A list of videos after center cropping.
        c                    r1   r   )rG   r2   r4   r   r   r5      r6   z'CenterCrop.__call__.<locals>.<listcomp>r   r7   r   r4   r   r8         
zCenterCrop.__call__)T)r9   r:   r;   r<   r#   r=   r   r   r   r   rG   r8   r>   r   r   r   r   r?      s
    &r?   c                       sh   e Zd ZdZddededdf fdd	Zd
eej	 dej	fddZ
deej	 deej	 fddZ  ZS )Image2ArrayzJConvert a sequence of images to a numpy array with optional transposition.Ttchw	transposedata_formatr   Nc                    s0   t    |dv sJ d| || _|| _dS )aC  
        Initializes the Image2Array class.

        Args:
            transpose (bool): Whether to transpose the resulting numpy array.
            data_format (str): The format to transpose to, either 'tchw' or 'cthw'.

        Raises:
            AssertionError: If data_format is not one of the allowed values.
        )rJ   Zcthwz0Target format must in ['tchw', 'cthw'], but got N)r   r   rK   rL   )r   rK   rL   r   r   r   r      s   


zImage2Array.__init__r'   c                 C   sD   t |d}| jr | jdkr|g d}|S |g d}|S )a  
        Converts a sequence of images to a numpy array and optionally transposes it.

        Args:
            imgs (List[np.ndarray]): A list of images to be converted to a numpy array.

        Returns:
            np.ndarray: A numpy array representation of the images.
        float32rJ   )r   r
      rA   )r
   r   rN   rA   )r   stackastyperK   rL   )r   r'   Zt_imgsr   r   r   	img2array   s   

zImage2Array.img2arrayr/   c                    r0   )a"  
        Apply the image to array conversion to a list of videos.

        Args:
            videos (List[Sequence[np.ndarray]]): A list of videos, where each video is a sequence of images.

        Returns:
            List[np.ndarray]: A list of numpy arrays, one for each video.
        c                    r1   r   )rQ   r2   r4   r   r   r5      r6   z(Image2Array.__call__.<locals>.<listcomp>r   r7   r   r4   r   r8      rH   zImage2Array.__call__)TrJ   )r9   r:   r;   r<   r=   strr   r   r   r   rQ   r8   r>   r   r   r   r   rI      s
    &rI   c                       s   e Zd ZdZg ddfdee dee dee dedd	f
 fd
dZde	j
de	j
fddZdee	j
 dee	j
 fddZ  ZS )NormalizeVideoz`
    Normalize video frames by subtracting the mean and dividing by the standard deviation.
    )r
   rN   rN   Fmeanstdtensor_shapeinplacer   Nc                    st   t    || _|s&t||tj| _t||tj| _	dS tj|tjd| _tj|tjd| _	dS )aw  
        Initializes the NormalizeVideo class.

        Args:
            mean (Sequence[float]): The mean values for each channel.
            std (Sequence[float]): The standard deviation values for each channel.
            tensor_shape (Sequence[int]): The shape of the mean and std tensors.
            inplace (bool): Whether to perform normalization in place.
        dtypeN)
r   r   rW   r   arrayreshaperP   rM   rT   rU   )r   rT   rU   rV   rW   r   r   r   r      s   
zNormalizeVideo.__init__r'   c                 C   s   | j rRt|}|d j\}}}tj||||ftjd}t|D ]\}}|||< q|D ]&}t| j	dd}	dt| j
	dd }
t||	| t||
| q*n|}|d }|| j8 }|| j
 }|}tj|dd }|S )z
        Normalizes a sequence of images.

        Args:
            imgs (np.ndarray): A numpy array of images to be normalized.

        Returns:
            np.ndarray: The normalized images as a numpy array.
        r   rX   rN   g     o@axis)rW   r   r    r   emptyrM   	enumerateZfloat64rT   r[   rU   r%   subtractmultiplyZexpand_dimscopy)r   r'   nr*   r+   cZ	norm_imgsr(   r)   rT   Zstdinvr   r   r   normalize_video  s&   


zNormalizeVideo.normalize_videor/   c                    r0   )a  
        Apply normalization to a list of videos.

        Args:
            videos (List[np.ndarray]): A list of videos, where each video is a numpy array of images.

        Returns:
            List[np.ndarray]: A list of normalized videos as numpy arrays.
        c                    r1   r   )rf   r2   r4   r   r   r5   B  r6   z+NormalizeVideo.__call__.<locals>.<listcomp>r   r7   r   r4   r   r8   8  rH   zNormalizeVideo.__call__)r9   r:   r;   r<   r   r$   r#   r=   r   r   r   rf   r   r8   r>   r   r   r   r   rS      s"    &!rS   c                       s   e Zd ZdZddeeeeef   ddf fddZ	de
jde
jfdd	Zdeeeeef   dee fd
dZ	dde
jdedee
jee
j eee  f fddZ  ZS )VideoClasTopkzCApplies a top-k transformation on video classification predictions.N	class_idsr   c                    s   t    | || _dS )z
        Initializes the VideoClasTopk class.

        Args:
            class_ids (Optional[Sequence[Union[str, int]]]): A list of class labels corresponding to class indices.
        N)r   r   _parse_class_id_mapclass_id_map)r   rh   r   r   r   r   I  s   
zVideoClasTopk.__init__datac                 C   s2   t j|ddd}t || }|t j|ddd S )z
        Applies the softmax function to an array of data.

        Args:
            data (np.ndarray): An array of data for which to compute softmax.

        Returns:
            np.ndarray: The softmax-transformed data.
        r\   T)r^   Zkeepdims)r   maxexpsum)r   rk   Zx_maxZe_xr   r   r   softmaxS  s   
zVideoClasTopk.softmaxc                 C   s"   |du rdS dd t |D }|S )aB  
        Parses a list of class IDs into a mapping from class index to class label.

        Args:
            class_ids (Optional[Sequence[Union[str, int]]]): A list of class labels.

        Returns:
            Optional[dict]: A dictionary mapping class indices to labels, or None if no class_ids are provided.
        Nc                 S   s   i | ]	\}}|t |qS r   )rR   )r3   idZlbr   r   r   
<dictcomp>o  s    z5VideoClasTopk._parse_class_id_map.<locals>.<dictcomp>)r`   )r   rh   rj   r   r   r   ri   a  s   z!VideoClasTopk._parse_class_id_map   predstopkc                    s     |d |d< |d jdddd| df dddddf d}dd t|d |D } fd	d|D }|||fS )
a  
        Selects the top-k predictions from the classification output.

        Args:
            preds (np.ndarray): A 2D array of prediction scores.
            topk (int): The number of top predictions to return.

        Returns:
            Tuple[np.ndarray, List[np.ndarray], List[List[str]]]: A tuple containing:
                - An array of indices of the top-k predictions.
                - A list of arrays of scores for the top-k predictions.
                - A list of lists of label names for the top-k predictions.
        r   rN   r]   Nr\   Zint32c                 S   s&   g | ]\}}t tj|| d dqS )rr   )Zdecimals)listr   around)r3   predindexr   r   r   r5     s    z*VideoClasTopk.__call__.<locals>.<listcomp>c                    s   g | ]} fd d|D qS )c                    s   g | ]} j | qS r   )rj   )r3   r(   r4   r   r   r5     r6   z5VideoClasTopk.__call__.<locals>.<listcomp>.<listcomp>r   )r3   rx   r4   r   r   r5     s    )ro   ZargsortrP   zip)r   rs   rt   ZindexesZscoresZlabel_namesr   r4   r   r8   r  s   :
zVideoClasTopk.__call__)N)rr   )r9   r:   r;   r<   r   r   r   rR   r#   r   r   r   ro   dictri   r   r   r8   r>   r   r   r   r   rg   E  s"    (

rg   c                   @   s.   e Zd ZdZdeej deej fddZdS )ToBatchzA class for batching videos.r/   r   c                 C   s   t j|ddjt jddgS )zCall method to stack videos into a batch.

        Args:
            videos (list of np.ndarrays): List of videos to process.

        Returns:
            list of np.ndarrays: List containing a stacked tensor of the videos.
        r   r]   F)rY   rc   )r   ZconcatenaterP   rM   r7   r   r   r   r8     s   	zToBatch.__call__N)r9   r:   r;   r<   r   r   r   r8   r   r   r   r   r{     s    "r{   )typingr   r   r   r   r   numpyr   Z
utils.depsr   r	   Zutils.benchmarkr   r%   Ztimeitr   r?   rI   rS   rg   r{   r   r   r   r   <module>   s(   m55MF