o
    *ÎjF  ã                   @   sð   d dl Z d dlmZ d dlmZ d dlZd dlZd dlm	Z	 ddl
mZmZ e ¡ G dd„ deƒƒZd	d
„ Ze ¡ G dd„ deƒƒZe ¡ G dd„ deƒƒZdd„ Ze ¡ G dd„ deƒƒZe ¡ G dd„ deƒƒZe ¡ G dd„ deƒƒZdS )é    N)ÚSequence)ÚMapping)Údefault_groupé   )ÚPREPROCESSORSÚbuild_preprocessorc                   @   s*   e Zd ZdZd
dd„Zdd„ Zdd	„ ZdS )ÚComposea(  Compose a data pipeline with a sequence of transforms.
    Args:
        transforms (list[dict | callable]):
            Either config dicts of transforms or transform objects.
        profiling (bool, optional): If set True, will profile and
            print preprocess time for each step.
    NFc              	   C   s¢   t |tƒsJ ‚|| _g | _|| _|D ]<}t |tƒr:| jd u r$t|tƒ}n$zt||ƒ}W n ty9   t|tƒ}Y nw t	|ƒr?n	t
dt|ƒ› ƒ‚| j |¡ qd S )Nz.transform must be callable or a dict, but got )Ú
isinstancer   Ú	profilingÚ
transformsÚ
field_nameÚdictr   r   ÚKeyErrorÚcallableÚ	TypeErrorÚtypeÚappend)Úselfr   r   r
   Ú	transform© r   ú`/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/preprocessors/common.pyÚ__init__   s,   

ÿÿÿðzCompose.__init__c                 C   sT   | j D ]$}| jrt ¡ }||ƒ}| jr t|› dt ¡ | › ƒ |d u r' d S q|S )Nz time )r   r
   ÚtimeÚprint)r   ÚdataÚtÚstartr   r   r   Ú__call__/   s   
ÿzCompose.__call__c                 C   s2   | j jd }| jD ]	}|d|› 7 }q	|d7 }|S )Nú(z
    z
))Ú	__class__Ú__name__r   )r   Úformat_stringr   r   r   r   Ú__repr__=   s
   
zCompose.__repr__)NF©r    Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r   r   r   r   r      s
    
r   c                 C   s„   t | tjƒr| S t | tjƒrt | ¡S t | tƒr"t | tƒs"t | ¡S t | t	ƒr-t 
| g¡S t | tƒr8t | g¡S tdt| ƒ› dƒ‚)a8  Convert objects of various python types to :obj:`torch.Tensor`.

    Supported types are: :class:`numpy.ndarray`, :class:`torch.Tensor`,
    :class:`Sequence`, :class:`int` and :class:`float`.

    Args:
        data (torch.Tensor | numpy.ndarray | Sequence | int | float): Data to
            be converted.
    útype ú cannot be converted to tensor.)r	   ÚtorchÚTensorÚnpÚndarrayZ
from_numpyr   ÚstrZtensorÚintZ
LongTensorÚfloatZFloatTensorr   r   ©r   r   r   r   Ú	to_tensorE   s   



r1   c                   @   ó*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚToTensoru  Convert target object to tensor.

    Args:
        keys (Sequence[str]): Key of data to be converted to Tensor.
            Only valid when data is type of `Mapping`. If `keys` is None,
            all values of keys â€‹â€‹will be converted to tensor by default.
    Nc                 C   ó
   || _ d S ©N©Úkeys©r   r7   r   r   r   r   h   ó   
zToTensor.__init__c                 C   óV   t |tƒr%| jd u rt| ¡ ƒ| _| jD ]}||v r"t|| ƒ||< q|S t|ƒ}|S r5   )r	   r   r7   Úlistr1   ©r   r   Úkeyr   r   r   r   k   ó   


€þzToTensor.__call__c                 C   ó   | j jd| j› d S ©Nz(keys=ú)©r   r    r7   ©r   r   r   r   r"   x   ó   zToTensor.__repr__r5   r#   r   r   r   r   r3   ^   ó
    
r3   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚFiltera  This is usually the last stage of the dataloader transform.
    Only data of reserved keys will be kept and passed directly to the model, others will be removed.

    Args:
        keys (Sequence[str]): Keys of data to be reserved, others will be removed.
    c                 C   r4   r5   )Úreserved_keys)r   rG   r   r   r   r   …   r9   zFilter.__init__c                 C   s6   t |tƒsJ ‚i }| jD ]}||v r|| ||< q|S r5   )r	   r   rG   )r   r   Zreserved_datar=   r   r   r   r   ˆ   s   
€zFilter.__call__c                 C   r?   r@   )r   r    rG   rC   r   r   r   r"   ’   rD   zFilter.__repr__Nr#   r   r   r   r   rF   |   s
    
rF   c                 C   sŠ   t | tjƒr
|  ¡ S t | tjƒr| S t | tƒr!t | tƒs!t | ¡S t | t	ƒr.tj| tj
dS t | tƒr;tj| tjdS tdt| ƒ› dƒ‚)z³Convert objects of various python types to `numpy.ndarray`.

    Args:
        data (torch.Tensor | numpy.ndarray | Sequence | int | float): Data to
            be converted.
    )Zdtyper'   r(   )r	   r)   r*   Únumpyr+   r,   r   r-   Zasarrayr.   Zint64r/   Zfloat64r   r   r0   r   r   r   Úto_numpy–   s   


rI   c                   @   r2   )
ÚToNumpyu!  Convert target object to numpy.ndarray.

    Args:
        keys (Sequence[str]): Key of data to be converted to numpy.ndarray.
            Only valid when data is type of `Mapping`. If `keys` is None,
            all values of keys â€‹â€‹will be converted to numpy.ndarray by default.
    Nc                 C   r4   r5   r6   r8   r   r   r   r   ¶   r9   zToNumpy.__init__c                 C   r:   r5   )r	   r   r7   r;   rI   r<   r   r   r   r   ¹   r>   zToNumpy.__call__c                 C   r?   r@   rB   rC   r   r   r   r"   Æ   rD   zToNumpy.__repr__r5   r#   r   r   r   r   rJ   ¬   rE   rJ   c                   @   s.   e Zd ZdZg g fdd„Zdd„ Zdd„ ZdS )	ÚRenamezDChange the name of the input keys to output keys, respectively.
    c                 C   s   || _ || _d S r5   )Ú
input_keysÚoutput_keys)r   rL   rM   r   r   r   r   Ï   s   
zRename.__init__c                 C   sL   t |tƒr$t| j| jƒD ]\}}||v r#||vr#|| ||< | |¡ q|S r5   )r	   r   ÚziprL   rM   Úpop)r   r   Zin_keyZout_keyr   r   r   r   Ó   s   

€zRename.__call__c                 C   r?   r@   rB   rC   r   r   r   r"   Û   rD   zRename.__repr__Nr#   r   r   r   r   rK   Ê   s
    rK   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚIdentityc                 C   s   d S r5   r   rC   r   r   r   r   â   ó   zIdentity.__init__c                 C   s   |S r5   r   )r   Úitemr   r   r   r   å   rQ   zIdentity.__call__N)r    r$   r%   r   r   r   r   r   r   rP   ß   s    rP   )r   Úcollections.abcr   Útypingr   rH   r+   r)   Zmodelscope.utils.registryr   Úbuilderr   r   Zregister_moduleÚobjectr   r1   r3   rF   rI   rJ   rK   rP   r   r   r   r   Ú<module>   s*   6