
    j                     d   d dl mZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
 g dZej        fdej        dej        d	ej        fd
Zej        fde
dej        d	ej        fdZej        fdedej        d	ej        fdZej        fdej        dej        d	ej        fdZdej        d	eeef         fdZdS )    )BytesION)Image)	to_tensor)AbstractPath)tensor_from_pilread_img_as_tensordecode_img_as_tensortensor_from_numpyget_img_shapepil_imgdtypereturnc                     |t           j        k    rt          |           }n/t          t	          j        | t          j        d          |          }|S )zConvert a PIL Image to a PyTorch tensor

    Args:
        pil_img: a PIL image
        dtype: the output tensor data type

    Returns:
        decoded image as tensor
    T)copy)torchfloat32r   r
   nparrayuint8)r   r   imgs      Y/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/io/image/pytorch.pyr   r      sG       "( F F FNNJ    img_pathc                    |t           j        t           j        t           j        fvrt	          d          t          j        | d          5 }t          |                    d          |          cddd           S # 1 swxY w Y   dS )a  Read an image file as a PyTorch tensor

    Args:
        img_path: location of the image file
        dtype: the desired data type of the output tensor. If it is float-related, values will be divided by 255.

    Returns:
        decoded image as a tensor
    insupported value for dtypermodeRGBN)	r   r   float16r   
ValueErrorr   openr   convert)r   r   r   s      r   r   r   $   s     U[%-???6777	H3	'	'	' >7wu55u==> > > > > > > > > > > > > > > > > >s   #A;;A?A?img_contentc                 *   |t           j        t           j        t           j        fvrt	          d          t          j        t          |           d          5 }t          |	                    d          |          cddd           S # 1 swxY w Y   dS )a  Read a byte stream as a PyTorch tensor

    Args:
        img_content: bytes of a decoded image
        dtype: the desired data type of the output tensor. If it is float-related, values will be divided by 255.

    Returns:
        decoded image as a tensor
    r   r   r   r   N)
r   r   r    r   r!   r   r"   r   r   r#   )r$   r   r   s      r   r	   r	   5   s     U[%-???6777	GK((s	3	3	3 >wwu55u==> > > > > > > > > > > > > > > > > >s   #BBBnpy_imgc                    |t           j        t           j        t           j        fvrt	          d          |t           j        k    rt          |           }n~t          j        |           }|                    d                                          }|t           j        k    r3|	                    t           j                  
                    d          }|S )a7  Read an image file as a PyTorch tensor

    Args:
        npy_img: image encoded as a numpy array of shape (H, W, C) in np.uint8
        dtype: the desired data type of the output tensor. If it is float-related, values will be divided by 255.

    Returns:
        same image as a tensor of shape (C, H, W)
    r   )   r      )r      )r   r   r    r   r!   r   
from_numpypermute
contiguoustodiv)r&   r   r   s      r   r
   r
   F   s     U[%-???6777  w''kk)$$//11EM!!&&u}&--11#66CJr   r   c                      | j         dd         S )zGet the shape of an imageN)shape)r   s    r   r   r   `   s    9RSS>r   )ior   numpyr   r   PILr   !torchvision.transforms.functionalr   doctr.utils.common_typesr   __all__r   r   Tensorr   r   bytesr	   ndarrayr
   tupleintr    r   r   <module>r?      s                    7 7 7 7 7 7 1 1 1 1 1 1
q
q
q @E}  U[  QVQ]    $ EJM > > >ek >V[Vb > > > >" CH- > >e >EK >TYT` > > > >" AF  rz %+ RWR^    4u| c3h      r   