
    |j                        U d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZ er:d dlmZ d dlmZ d dlmZ ed         Zd	ed
<   eeeej        e         f         Zd	ed<   g Zdad
ed<   ddZddZ	 dddZdS )    )annotations)TYPE_CHECKINGAnyLiteralUnion)Image)	TypeAlias)
try_importN)Tensorpilcv2tensorr	   _ImageBackend_ImageDataTyper   _image_backendbackendreturnNonec                6    | dvrt          d|            | adS )ab  
    Specifies the backend used to load images in class :ref:`api_paddle_datasets_ImageFolder`
    and :ref:`api_paddle_datasets_DatasetFolder` . Now support backends are pillow and opencv.
    If backend not set, will use 'pil' as default.

    Args:
        backend (str): Name of the image load backend, should be one of {'pil', 'cv2'}.

    Examples:

        .. code-block:: python

            >>> import os
            >>> import shutil
            >>> import tempfile
            >>> import numpy as np
            >>> from PIL import Image

            >>> from paddle.vision import DatasetFolder
            >>> from paddle.vision import set_image_backend

            >>> set_image_backend('pil')

            >>> def make_fake_dir():
            ...     data_dir = tempfile.mkdtemp()
            ...
            ...     for i in range(2):
            ...         sub_dir = os.path.join(data_dir, 'class_' + str(i))
            ...         if not os.path.exists(sub_dir):
            ...             os.makedirs(sub_dir)
            ...         for j in range(2):
            ...             fake_img = Image.fromarray((np.random.random((32, 32, 3)) * 255).astype('uint8'))
            ...             fake_img.save(os.path.join(sub_dir, str(j) + '.png'))
            ...     return data_dir

            >>> temp_dir = make_fake_dir()

            >>> pil_data_folder = DatasetFolder(temp_dir)

            >>> for items in pil_data_folder:
            ...     break

            >>> print(type(items[0]))
            <class 'PIL.Image.Image'>

            >>> # use opencv as backend
            >>> set_image_backend('cv2')

            >>> cv2_data_folder = DatasetFolder(temp_dir)

            >>> for items in cv2_data_folder:
            ...     break

            >>> print(type(items[0]))
            <class 'numpy.ndarray'>

            >>> shutil.rmtree(temp_dir)
    r   >Expected backend are one of ['pil', 'cv2', 'tensor'], but got N)
ValueErrorr   )r   s    c/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/vision/image.pyset_image_backendr   &   s9    x ...VWVV
 
 	
 NNN    c                     t           S )a7  
    Gets the name of the package used to load images

    Returns:
        str: backend of image load.

    Examples:

        .. code-block:: python

            >>> from paddle.vision import get_image_backend

            >>> backend = get_image_backend()
            >>> print(backend)
            pil

    )r    r   r   get_image_backendr   i   s
    $ r   pathstr_ImageBackend | None_ImageDataType | Nonec                    |t           }|dvrt          d|           |dk    rt          j        |           S |dk    r$t	          d          }|                    |           S dS )ar  Load an image.

    Args:
        path (str): Path of the image.
        backend (str, optional): The image decoding backend type. Options are
            `cv2`, `pil`, `None`. If backend is None, the global _imread_backend
            specified by :ref:`api_paddle_vision_set_image_backend` will be used. Default: None.

    Returns:
        PIL.Image or np.array: Loaded image.

    Examples:

        .. code-block:: python

            >>> import numpy as np
            >>> from PIL import Image
            >>> from paddle.vision import image_load, set_image_backend

            >>> fake_img = Image.fromarray((np.random.random((32, 32, 3)) * 255).astype('uint8'))

            >>> path = 'temp.png'
            >>> fake_img.save(path)

            >>> set_image_backend('pil')

            >>> pil_img = image_load(path).convert('RGB')  # type: ignore

            >>> print(type(pil_img))
            <class 'PIL.Image.Image'>

            >>> # use opencv as backend
            >>> set_image_backend('cv2')

            >>> np_img = image_load(path)
            >>> print(type(np_img))
            <class 'numpy.ndarray'>

    Nr   r   r   r   )r   r   r   openr
   imread)r   r   r   s      r   
image_loadr&   ~   s    V  ...VWVV
 
 	
 %z$	E		zz$ 
	r   )r   r   r   r   )r   r   )N)r   r    r   r!   r   r"   )
__future__r   typingr   r   r   r   PILr   typing_extensionsr	   paddle.utilsr
   numpy.typingnpt	PIL.ImagePILImagepaddler   r   __annotations__NDArrayr   __all__r   r   r   r&   r   r   r   <module>r4      s[   # " " " " " " 5 5 5 5 5 5 5 5 5 5 5 5       ' ' ' ' ' ' # # # # # # J++++++&'=>M>>>> %fhC8H&H INIIII
 % % % % %@ @ @ @F   , 046  6  6  6  6  6  6 r   