o
    rqiF                     @   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
   Z	transform r   f/home/app/PaddleOCR-VL/.venv_paddleocr/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   



r0   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   r6   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 r4   )r	   r   r6   listr0   r   r   keyr   r   r   r   k      


zToTensor.__call__c                 C      | j jd| j d S Nz(keys=)r   r   r6   r   r   r   r   r!   x      zToTensor.__repr__r4   r"   r   r   r   r   r2   ^   
    
r2   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   r3   r4   )reserved_keys)r   rF   r   r   r   r      r8   zFilter.__init__c                 C   s6   t |tsJ i }| jD ]}||v r|| ||< q|S r4   )r	   r   rF   )r   r   Zreserved_datar<   r   r   r   r      s   
zFilter.__call__c                 C   r>   r?   )r   r   rF   rB   r   r   r   r!      rC   zFilter.__repr__Nr"   r   r   r   r   rE   |   s
    
rE   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)zConvert 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   r/   r   r   r   to_numpy   s   


rH   c                   @   r1   )
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   r3   r4   r5   r7   r   r   r   r      r8   zToNumpy.__init__c                 C   r9   r4   )r	   r   r6   r:   rH   r;   r   r   r   r      r=   zToNumpy.__call__c                 C   r>   r?   rA   rB   r   r   r   r!      rC   zToNumpy.__repr__r4   r"   r   r   r   r   rI      rD   rI   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 r4   )
input_keysoutput_keys)r   rK   rL   r   r   r   r      s   
zRename.__init__c                 C   sL   t |tr$t| j| jD ]\}}||v r#||vr#|| ||< || q|S r4   )r	   r   ziprK   rL   pop)r   r   Zin_keyZout_keyr   r   r   r      s   

zRename.__call__c                 C   r>   r?   rA   rB   r   r   r   r!      rC   zRename.__repr__Nr"   r   r   r   r   rJ      s
    rJ   c                   @   s   e Zd Zdd Zdd ZdS )Identityc                 C   s   d S r4   r   rB   r   r   r   r         zIdentity.__init__c                 C   s   |S r4   r   )r   itemr   r   r   r      rP   zIdentity.__call__N)r   r#   r$   r   r   r   r   r   r   rO      s    rO   )r   collections.abcr   typingr   rG   r*   r(   Zmodelscope.utils.registryr   Zbuilderr   r   Zregister_moduleobjectr   r0   r2   rE   rH   rI   rJ   rO   r   r   r   r   <module>   s*   6