
    ёiH                    2   % S SK Jr  S SKJrJrJrJr  S SKJr  \(       a"  S SK	J
r
  S SKJr  S SKJr  SSKJr  \S	   rS
\S'   S SKrS SKJr  S SKrS SKJr  S SKJr  / rSS jrSS jr " S S\\S\4      5      r S	r!S r"S r#S r$ " S S\\%S      5      r&g)    )annotations)TYPE_CHECKINGAnyCallableLiteral)	TypeAlias)Sequence)_DTypeLiteral)
_Transform   )_ImageDataType)	z.jpgz.jpegz.pngz.ppmz.bmpz.pgmz.tifz.tiffz.webpr   _AllowedExtensionsN)Image)Dataset)
try_importc                    [        U[        [        45      (       d   S5       e[        U Vs/ s H  o"R                  5       PM     sn5      nU R                  5       R	                  U5      $ s  snf )zChecks if a file is a valid extension.

Args:
    filename (str): path to a file
    extensions (list[str]|tuple[str]): extensions to consider

Returns:
    bool: True if the filename ends with one of given extensions
z#`extensions` must be list or tuple.)
isinstancelisttuplelowerendswith)filename
extensionsxs      ]/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/vision/datasets/folder.pyhas_valid_extensionr   4   sa     j4-00 -0 :6:a	:67J>>$$Z00 7s   A+c                $  ^ / n[         R                  R                  U 5      n Tb  U4S jn[        UR	                  5       5       H  n[         R                  R                  X5      n[         R                  R                  U5      (       d  MH  [        [         R                  " USS95       H]  u  pxn	[        U	5       HH  n
[         R                  R                  Xz5      nU" U5      (       d  M1  XU   4nUR                  U5        MJ     M_     M     U$ )Nc                   > [        U T5      $ Nr   r   r   s    r   is_valid_file#make_dataset.<locals>.is_valid_fileK   s    &q*55    Tfollowlinks)	ospath
expandusersortedkeysjoinisdirwalkappend)dirclass_to_idxr   r"   imagestargetdroot_fnamesfnamer(   items     `          r   make_datasetr:   E   s    F
''

S
!C	6 **,-GGLL%ww}}Q%bggaT&BCODVww||D0 && v"67DMM$'	 (  D	 . Mr$   c                      \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   S\S'   S\S'   S	\S'   S\S'       S           SS jjrSS jrSS jrS r	Sr
g)DatasetFolder\   aN  A generic data loader where the samples are arranged in this way:

.. code-block:: text

    root/class_a/1.ext
    root/class_a/2.ext
    root/class_a/3.ext

    root/class_b/123.ext
    root/class_b/456.ext
    root/class_b/789.ext

Args:
    root (str): Root directory path.
    loader (Callable|None, optional): A function to load a sample given its path. Default: None.
    extensions (list[str]|tuple[str]|None, optional): A list of allowed extensions.
        Both :attr:`extensions` and :attr:`is_valid_file` should not be passed.
        If this value is not set, the default is to use ('.jpg', '.jpeg', '.png',
        '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp'). Default: None.
    transform (Callable|None, optional): A function/transform that takes in
        a sample and returns a transformed version. Default: None.
    is_valid_file (Callable|None, optional): A function that takes path of a file
        and check if the file is a valid file. Both :attr:`extensions` and
        :attr:`is_valid_file` should not be passed. Default: None.

Returns:
    :ref:`api_paddle_io_Dataset`. An instance of DatasetFolder.

Attributes:
    classes (list[str]): List of the class names.
    class_to_idx (dict[str, int]): Dict with items (class_name, class_index).
    samples (list[tuple[str, int]]): List of (sample_path, class_index) tuples.
    targets (list[int]): The class_index value for each image in the dataset.

Example:

    .. code-block:: python

        >>> import shutil
        >>> import tempfile
        >>> import cv2
        >>> import numpy as np
        >>> import paddle.vision.transforms as T
        >>> from pathlib import Path
        >>> from paddle.vision.datasets import DatasetFolder

        >>> def make_fake_file(img_path: str):
        ...     if img_path.endswith((".jpg", ".png", ".jpeg")):
        ...         fake_img = np.random.randint(0, 256, (32, 32, 3), dtype=np.uint8)
        ...         cv2.imwrite(img_path, fake_img)
        ...     elif img_path.endswith(".txt"):
        ...         with open(img_path, "w") as f:
        ...             f.write("This is a fake file.")

        >>> def make_directory(root, directory_hierarchy, file_maker=make_fake_file):
        ...     root = Path(root)
        ...     root.mkdir(parents=True, exist_ok=True)
        ...     for subpath in directory_hierarchy:
        ...         if isinstance(subpath, str):
        ...             filepath = root / subpath
        ...             file_maker(str(filepath))
        ...         else:
        ...             dirname = list(subpath.keys())[0]
        ...             make_directory(root / dirname, subpath[dirname])

        >>> directory_hierarchy = [
        ...     {"class_0": [
        ...         "abc.jpg",
        ...         "def.png"]},
        ...     {"class_1": [
        ...         "ghi.jpeg",
        ...         "jkl.png",
        ...         {"mno": [
        ...             "pqr.jpeg",
        ...             "stu.jpg"]}]},
        ...     "this_will_be_ignored.txt",
        ... ]

        >>> # You can replace this with any directory to explore the structure
        >>> # of generated data. e.g. fake_data_dir = "./temp_dir"
        >>> fake_data_dir = tempfile.mkdtemp()
        >>> make_directory(fake_data_dir, directory_hierarchy)
        >>> data_folder_1 = DatasetFolder(fake_data_dir)
        >>> print(data_folder_1.classes)
        ['class_0', 'class_1']
        >>> print(data_folder_1.class_to_idx)
        {'class_0': 0, 'class_1': 1}
        >>> print(data_folder_1.samples)
        >>> # doctest: +SKIP(it's different with windows)
        [('./temp_dir/class_0/abc.jpg', 0), ('./temp_dir/class_0/def.png', 0),
         ('./temp_dir/class_1/ghi.jpeg', 1), ('./temp_dir/class_1/jkl.png', 1),
         ('./temp_dir/class_1/mno/pqr.jpeg', 1), ('./temp_dir/class_1/mno/stu.jpg', 1)]
        >>> # doctest: -SKIP
        >>> print(data_folder_1.targets)
        [0, 0, 1, 1, 1, 1]
        >>> print(len(data_folder_1))
        6

        >>> for i in range(len(data_folder_1)):
        ...     img, label = data_folder_1[i]
        ...     # do something with img and label
        ...     print(type(img), img.size, label)
        ...     # <class 'PIL.Image.Image'> (32, 32) 0


        >>> transform = T.Compose(
        ...     [
        ...         T.Resize(64),
        ...         T.ToTensor(),
        ...         T.Normalize(
        ...             mean=[0.5, 0.5, 0.5],
        ...             std=[0.5, 0.5, 0.5],
        ...             to_rgb=True,
        ...         ),
        ...     ]
        ... )

        >>> data_folder_2 = DatasetFolder(
        ...     fake_data_dir,
        ...     loader=lambda x: cv2.imread(x),  # load image with OpenCV
        ...     extensions=(".jpg",),  # only load *.jpg files
        ...     transform=transform,  # apply transform to every image
        ... )

        >>> print([img_path for img_path, label in data_folder_2.samples])
        >>> # doctest: +SKIP(it's different with windows)
        ['./temp_dir/class_0/abc.jpg', './temp_dir/class_1/mno/stu.jpg']
        >>> # doctest: -SKIP
        >>> print(len(data_folder_2))
        2

        >>> for img, label in iter(data_folder_2):
        ...     # do something with img and label
        ...     print(type(img), img.shape, label)  # type: ignore
        ...     # <class 'paddle.Tensor'> [3, 64, 64] 0

        >>> shutil.rmtree(fake_data_dir)
$Callable[..., _ImageDataType] | Noneloader#Sequence[_AllowedExtensions] | Noner   _Transform[Any, Any] | None	transform	list[str]classeszdict[str, int]r1   zlist[tuple[str, int]]samplestargetsr
   dtypeNc                   Xl         X@l        Uc  [        nU R                  U R                   5      u  pg[	        U R                   XsU5      n[        U5      S:X  a-  [        SU R                   -   S-   SR                  U5      -   5      eUc  [        OUU l	        X0l
        X`l        Xpl        Xl        U V	s/ s H  oS   PM	     sn	U l        [        R                   " 5       U l        g s  sn	f )Nr   z&Found 0 directories in subfolders of: 
Supported extensions are: ,   )r5   rB   IMG_EXTENSIONS_find_classesr:   lenRuntimeErrorr,   default_loaderr?   r   rD   r1   rE   rF   paddleget_default_dtyperG   )
selfr5   r?   r   rB   r"   rD   r1   rE   ss
             r   __init__DatasetFolder.__init__   s     	"'J $ 2 2499 =II|
 w<1<tyyH L1 13688J3GH )/nF$(&-.g!g.--/
 /s   2C#c                   [         R                  " U5       Vs/ s H%  o"R                  5       (       d  M  UR                  PM'     nnUR	                  5         [        [        U5      5       Vs0 s H  oCU   U_M
     nnX54$ s  snf s  snf )z
Finds the class folders in a dataset.

Args:
    dir (string): Root directory path.

Returns:
    tuple: (classes, class_to_idx) where classes are relative to (dir),
            and class_to_idx is a dictionary.

)r'   scandiris_dirnamesortrangerN   )rS   r0   r4   rD   ir1   s         r   rM   DatasetFolder._find_classes  sl     $&::c?A?ahhj6166?A/4S\/BC/B!
A/BC$$ BCs   BB-Bc                    U R                   U   u  p#U R                  U5      nU R                  b  U R                  U5      nXC4$ )zu
Args:
    index (int): Index

Returns:
    tuple: (sample, target) where target is class_index of the target class.
rE   r?   rB   )rS   indexr(   r3   samples        r   __getitem__DatasetFolder.__getitem__$  sB     ||E*T">>%^^F+F~r$   c                ,    [        U R                  5      $ r   rN   rE   rS   s    r   __len__DatasetFolder.__len__3      4<<  r$   )	r1   rD   rG   r   r?   r5   rE   rF   rB   NNNNr5   strr?   r>   r   r@   rB   rA   r"   z_ImageDataType | NonereturnNone)r0   rm   rn   z tuple[list[str], dict[str, int]])ra   intrn   ztuple[_ImageDataType, int])__name__
__module____qualname____firstlineno____doc____annotations__rU   rM   rc   rh   __static_attributes__ r$   r   r<   r<   \   s    IV 1033**  ""
 8<:>15/3 0 0 5 0 8	 0
 / 0 - 0 
 0D%"!r$   r<   r   c                    [        U S5       n[        R                   " U5      nUR                  S5      sS S S 5        $ ! , (       d  f       g = f)NrbRGB)openr   convert)r(   fimgs      r   
pil_loaderr   D  s3    	dD	Qjjm{{5! 
		s	   '>
Ac                n    [        S5      nUR                  UR                  U 5      UR                  5      $ )Ncv2)r   cvtColorimreadCOLOR_BGR2RGB)r(   r   s     r   
cv2_loaderr   J  s,    
U
C<<

4(#*;*;<<r$   c                P    SSK Jn  U" 5       S:X  a  [        U 5      $ [        U 5      $ )Nr   )get_image_backendr   )paddle.visionr   r   r   )r(   r   s     r   rP   rP   O  s&    /e#$$r$   c                      \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'       S           SS jjrSS jrSS jrSr	g)ImageFolderiX  a  A generic data loader where the samples are arranged in this way:

.. code-block:: text

    root/1.ext
    root/2.ext
    root/sub_dir/3.ext

Args:
    root (str): Root directory path.
    loader (Callable|None, optional): A function to load a sample given its path. Default: None.
    extensions (list[str]|tuple[str]|None, optional): A list of allowed extensions.
        Both :attr:`extensions` and :attr:`is_valid_file` should not be passed.
        If this value is not set, the default is to use ('.jpg', '.jpeg', '.png',
        '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp'). Default: None.
    transform (Callable|None, optional): A function/transform that takes in
        a sample and returns a transformed version. Default: None.
    is_valid_file (Callable|None, optional): A function that takes path of a file
        and check if the file is a valid file. Both :attr:`extensions` and
        :attr:`is_valid_file` should not be passed. Default: None.

Returns:
    :ref:`api_paddle_io_Dataset`. An instance of ImageFolder.

Attributes:
    samples (list[str]): List of sample path.

Example:

    .. code-block:: python

        >>> import shutil
        >>> import tempfile
        >>> import cv2
        >>> import numpy as np
        >>> import paddle.vision.transforms as T
        >>> from pathlib import Path
        >>> from paddle.vision.datasets import ImageFolder

        >>> def make_fake_file(img_path: str):
        ...     if img_path.endswith((".jpg", ".png", ".jpeg")):
        ...         fake_img = np.random.randint(0, 256, (32, 32, 3), dtype=np.uint8)
        ...         cv2.imwrite(img_path, fake_img)
        ...     elif img_path.endswith(".txt"):
        ...         with open(img_path, "w") as f:
        ...             f.write("This is a fake file.")

        >>> def make_directory(root, directory_hierarchy, file_maker=make_fake_file):
        ...     root = Path(root)
        ...     root.mkdir(parents=True, exist_ok=True)
        ...     for subpath in directory_hierarchy:
        ...         if isinstance(subpath, str):
        ...             filepath = root / subpath
        ...             file_maker(str(filepath))
        ...         else:
        ...             dirname = list(subpath.keys())[0]
        ...             make_directory(root / dirname, subpath[dirname])

        >>> directory_hierarchy = [
        ...     "abc.jpg",
        ...     "def.png",
        ...     {"ghi": [
        ...         "jkl.jpeg",
        ...         {"mno": [
        ...             "pqr.jpg"]}]},
        ...     "this_will_be_ignored.txt",
        ... ]

        >>> # You can replace this with any directory to explore the structure
        >>> # of generated data. e.g. fake_data_dir = "./temp_dir"
        >>> fake_data_dir = tempfile.mkdtemp()
        >>> make_directory(fake_data_dir, directory_hierarchy)
        >>> image_folder_1 = ImageFolder(fake_data_dir)
        >>> print(image_folder_1.samples)
        >>> # doctest: +SKIP(it's different with windows)
        ['./temp_dir/abc.jpg', './temp_dir/def.png',
         './temp_dir/ghi/jkl.jpeg', './temp_dir/ghi/mno/pqr.jpg']
        >>> # doctest: -SKIP
        >>> print(len(image_folder_1))
        4

        >>> for i in range(len(image_folder_1)):
        ...     (img,) = image_folder_1[i]
        ...     # do something with img
        ...     print(type(img), img.size)
        ...     # <class 'PIL.Image.Image'> (32, 32)


        >>> transform = T.Compose(
        ...     [
        ...         T.Resize(64),
        ...         T.ToTensor(),
        ...         T.Normalize(
        ...             mean=[0.5, 0.5, 0.5],
        ...             std=[0.5, 0.5, 0.5],
        ...             to_rgb=True,
        ...         ),
        ...     ]
        ... )

        >>> image_folder_2 = ImageFolder(
        ...     fake_data_dir,
        ...     loader=lambda x: cv2.imread(x),  # load image with OpenCV
        ...     extensions=(".jpg",),  # only load *.jpg files
        ...     transform=transform,  # apply transform to every image
        ... )

        >>> print(image_folder_2.samples)
        >>> # doctest: +SKIP(it's different with windows)
        ['./temp_dir/abc.jpg', './temp_dir/ghi/mno/pqr.jpg']
        >>> # doctest: -SKIP
        >>> print(len(image_folder_2))
        2

        >>> for (img,) in iter(image_folder_2):
        ...     # do something with img
        ...     print(type(img), img.shape)  # type: ignore
        ...     # <class 'paddle.Tensor'> [3, 64, 64]

        >>> shutil.rmtree(fake_data_dir)
r>   r?   r@   r   rC   rE   rA   rB   Nc                (  ^ Xl         Tc  [        m/ n[        R                  R	                  U5      nTb  U4S jn[        [        R                  " USS95       HW  u  pn	[        U	5       HB  n
[        R                  R                  X5      nU" U5      (       d  M1  UR                  U5        MD     MY     [        U5      S:X  a-  [        SU R                   -   S-   SR                  T5      -   5      eUc  [        OUU l        TU l        X`l        X@l        g )Nc                   > [        U T5      $ r   r    r!   s    r   r"   +ImageFolder.__init__.<locals>.is_valid_file  s    *1j99r$   Tr%   r   z Found 0 files in subfolders of: rI   rJ   )r5   rL   r'   r(   r)   r*   r.   r,   r/   rN   rO   rP   r?   r   rE   rB   )rS   r5   r?   r   rB   r"   rE   r(   r6   r7   r8   r~   s      `        r   rU   ImageFolder.__init__  s     	'Jww!!$'!:  &bggd&EFODVGGLL- ##NN1% (  G w<16B F1 13688J3GH )/nF$"r$   c                    U R                   U   nU R                  U5      nU R                  b  U R                  U5      nU/$ )zF
Args:
    index (int): Index

Returns:
    sample of specific index.
r`   )rS   ra   r(   rb   s       r   rc   ImageFolder.__getitem__  s?     ||E"T">>%^^F+Fxr$   c                ,    [        U R                  5      $ r   rf   rg   s    r   rh   ImageFolder.__len__  rj   r$   )r   r?   r5   rE   rB   rk   rl   )ra   rp   rn   zlist[_ImageDataType])rn   rp   )
rq   rr   rs   rt   ru   rv   rU   rc   rh   rw   rx   r$   r   r   r   X  s{    xt 1033**
 8<:>15/3%#%# 5%# 8	%#
 /%# -%# 
%#N!r$   r   )r   rm   r   zSequence[str]rn   boolr   )'
__future__r   typingr   r   r   r   typing_extensionsr   collections.abcr	   paddle._typing.dtype_liker
   #paddle.vision.transforms.transformsr   imager   r   rv   r'   PILr   rQ   	paddle.ior   paddle.utilsr   __all__r   r:   r   rp   r<   rL   r   r   rP   r   r   rx   r$   r   <module>r      s    # 8 8 '(7>&$+	
%	 
 
    #
1".X!GE"2C"789 X!v
"=
 v!'$/01 v!r$   