o
    + iH                     @  s  U d dl mZ d dlmZmZmZmZ d dlmZ er;d dl	m
Z
 d dlmZ d dlmZ ddlmZ ed	 Zd
ed< d dlZd dlmZ d dlZd dlmZ d dlmZ g Zd%ddZd&ddZG dd deedef  Z d	Z!dd Z"dd  Z#d!d" Z$G d#d$ d$ee%d  Z&dS )'    )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_importfilenamestr
extensionsSequence[str]returnboolc                 C  s6   t |ttfsJ dtdd |D }|  |S )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.c                 S  s   g | ]}|  qS  )lower).0xr   r   i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/vision/datasets/folder.py
<listcomp>A       z'has_valid_extension.<locals>.<listcomp>)
isinstancelisttupler   endswith)r   r   r   r   r   has_valid_extension4   s
   
r"   c                   s   g }t j| }  d ur fdd}t| D ]<}t j| |}t j|s(qtt j|ddD ]"\}}}	t|	D ]}
t j||
}||rR||| f}|| q:q1q|S )Nc                   
   t |  S Nr"   r   r   r   r   is_valid_fileK      
z#make_dataset.<locals>.is_valid_fileTfollowlinks)	ospath
expandusersortedkeysjoinisdirwalkappend)dirclass_to_idxr   r(   Zimagestargetdroot_fnamesfnamer-   itemr   r'   r   make_datasetE   s$   
r>   c                   @  s   e Zd ZU dZded< ded< ded< ded	< d
ed< ded< ded< ded< 				d%d&ddZd'ddZd(d!d"Zd#d$ ZdS ))DatasetFoldera"  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]r6   zlist[tuple[str, int]]samplestargetsr	   dtypeNr9   r   r(   _ImageDataType | Noner   Nonec           	      C  s   || _ || _|d u rt}| | j \}}t| j |||}t|dkr0td| j  d d| |d u r6tn|| _	|| _
|| _|| _|| _dd |D | _t | _d S )Nr   z&Found 0 directories in subfolders of: 
Supported extensions are: ,c                 S  s   g | ]}|d  qS )   r   )r   sr   r   r   r     r   z*DatasetFolder.__init__.<locals>.<listcomp>)r9   rD   IMG_EXTENSIONS_find_classesr>   lenRuntimeErrorr1   default_loaderrA   r   rF   r6   rG   rH   paddleZget_default_dtyperI   )	selfr9   rA   r   rD   r(   rF   r6   rG   r   r   r   __init__   s,   
zDatasetFolder.__init__r5    tuple[list[str], dict[str, int]]c                   s>   dd t |D      fddtt D } |fS )a  
        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.

        c                 S  s   g | ]	}|  r|jqS r   )is_dirname)r   r8   r   r   r   r     s    z/DatasetFolder._find_classes.<locals>.<listcomp>c                   s   i | ]} | |qS r   r   )r   irF   r   r   
<dictcomp>!  s    z/DatasetFolder._find_classes.<locals>.<dictcomp>)r,   scandirsortrangerR   )rV   r5   r6   r   r\   r   rQ     s   zDatasetFolder._find_classesindexinttuple[_ImageDataType, int]c                 C  s4   | j | \}}| |}| jdur| |}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (sample, target) where target is class_index of the target class.
        NrG   rA   rD   )rV   ra   r-   r7   sampler   r   r   __getitem__$  s
   


zDatasetFolder.__getitem__c                 C  
   t | jS r$   rR   rG   rV   r   r   r   __len__3  r)   zDatasetFolder.__len__NNNNr9   r   rA   r@   r   rB   rD   rC   r(   rJ   r   rK   )r5   r   r   rX   )ra   rb   r   rc   )	__name__
__module____qualname____doc____annotations__rW   rQ   rf   rj   r   r   r   r   r?   \   s&   
  
"
r?   r   c                 C  sB   t | d}t |}|dW  d    S 1 sw   Y  d S )NrbRGB)openr   convert)r-   fimgr   r   r   
pil_loaderD  s   
$rx   c                 C  s   t d}||| |jS )Ncv2)r   ZcvtColorZimreadZCOLOR_BGR2RGB)r-   ry   r   r   r   
cv2_loaderJ  s   rz   c                 C  s&   ddl m} | dkrt| S t| S )Nr   )get_image_backendry   )Zpaddle.visionr{   rz   rx   )r-   r{   r   r   r   rT   O  s   
rT   c                   @  sZ   e Zd ZU dZded< ded< ded< ded	< 	
	
	
	
ddddZdddZdddZd
S )ImageFolderam  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@   rA   rB   r   rE   rG   rC   rD   Nr9   r   r(   rJ   r   rK   c                   s   || _  d u r	t g }tj|} d ur fdd}ttj|ddD ]\}}}	t|	D ]}
tj||
}||r?|| q-q$t	|dkrUt
d| j  d d  |d u r[tn|| _ | _|| _|| _d S )	Nc                   r#   r$   r%   r&   r'   r   r   r(     r)   z+ImageFolder.__init__.<locals>.is_valid_fileTr*   r   z Found 0 files in subfolders of: rL   rM   )r9   rP   r,   r-   r.   r/   r3   r1   r4   rR   rS   rT   rA   r   rG   rD   )rV   r9   rA   r   rD   r(   rG   r-   r:   r;   r<   rv   r   r'   r   rW     s2   

zImageFolder.__init__ra   rb   list[_ImageDataType]c                 C  s.   | j | }| |}| jdur| |}|gS )zn
        Args:
            index (int): Index

        Returns:
            sample of specific index.
        Nrd   )rV   ra   r-   re   r   r   r   rf     s
   



zImageFolder.__getitem__c                 C  rg   r$   rh   ri   r   r   r   rj     r)   zImageFolder.__len__rk   rl   )ra   rb   r   r}   )r   rb   )rm   rn   ro   rp   rq   rW   rf   rj   r   r   r   r   r|   X  s   
 z
'r|   )r   r   r   r   r   r   r$   )'
__future__r   typingr   r   r   r   Ztyping_extensionsr   collections.abcr   Zpaddle._typing.dtype_liker	   Z#paddle.vision.transforms.transformsr
   imager   r   rq   r,   ZPILr   rU   Z	paddle.ior   Zpaddle.utilsr   __all__r"   r>   r    rb   r?   rP   rx   rz   rT   r   r|   r   r   r   r   <module>   s4   

 \	