
    ΑiV+                         S r SSKrSSKrSSKr SSKrSSKr/ rS r	 SS jr
SS jrSS jrS rSS jrSS	 jrSS
 jrSS jr SS jr SS jrg! \ a    Sr NGf = f)at  
This file contains some common interfaces for image preprocess.
Many users are confused about the image layout. We introduce
the image layout as follows.

- CHW Layout

  - The abbreviations: C=channel, H=Height, W=Width
  - The default layout of image opened by cv2 or PIL is HWC.
    PaddlePaddle only supports the CHW layout. And CHW is simply
    a transpose of HWC. It must transpose the input image.

- Color format: RGB or BGR

  OpenCV use BGR color format. PIL use RGB color format. Both
  formats can be used for training. Noted that, the format should
  be keep consistent between the training and inference period.
    Nc                  R    [         c   SS Kn U R                  R                  S5        gg)Nr   zWarning with paddle image module: opencv-python should be imported,
         or paddle image module could NOT work; please install opencv-python first.FT)cv2sysstderrwrite)r   s    T/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/dataset/image.py
_check_cv2r	   /   s*    
{

W	
     c           	        ^ U S-   nU SU S[         R                  " 5        3mU SU S[         R                  " 5        S3n[         R                  R                  T5      (       a  U$ [         R                  " T5        [
        R                  " U 5      nUR                  5       n/ n/ n	Sn
U H  nUR                  U;   d  M  UR                  UR                  U5      R                  5       5        U	R                  X+R                     5        [        U5      U:X  d  Mq  XS.n[        R                  " U[        T SU
 3S5      S	S
9  U
S-  n
/ n/ n	M     [        U5      S:  a)  XS.n[        R                  " U[        T SU
 3S5      S	S
9  [        USS9 nUR                  U4S j[         R                   " T5       5       5        SSS5        U$ ! , (       d  f       U$ = f)a  
Read images from tar file and batch them into batch file.

:param data_file: path of image tar file
:type data_file: string
:param dataset_name: 'train','test' or 'valid'
:type dataset_name: string
:param img2label: a dict with image file name as key
                and image's label as value
:type img2label: dict
:param num_per_batch: image number per batch file
:type num_per_batch: int
:return: path of list file containing paths of batch file
:rtype: string
_batch/_z.txtr   )labeldataz/batch_wb   )protocol   a)modec              3   r   >#    U  H,  n[         R                  R                  T S U 35      S-   v   M.     g7f)r   
N)ospathabspath).0fileout_paths     r   	<genexpr>(batch_images_from_tar.<locals>.<genexpr>q   s5      
, GGOOxj$01D8,s   47N)r   getpidr   existsmakedirstarfileopen
getmembersnameappendextractfilereadlenpickledump
writelineslistdir)	data_filedataset_name	img2labelnum_per_batch	batch_dir	meta_filetfmemsr   labelsfile_idmemoutputmetar   s                 @r   batch_images_from_tarr=   <   s   $ H$IAl^1RYY[M:H+Q|nAbiik]$?I	ww~~h
H	i	 B==?DDFG88y KKs+0023MM)HH-.4yM)#)8H:WWI6=
 1  4y1}!0DH:WWI6=	
 
ic	"d 


8,
 	
 
#
  
#	"
 s   0G
Gc                     [        5       SL d   eU(       a  SOSn[        R                  " [        U 5      [        R                  S9n[
        R                  " X25      nU$ )a  
Load an color or gray image from bytes array.

Example usage:

.. code-block:: python

    >>> with open('cat.jpg') as f:
    ...     im = load_image_bytes(f.read())
    ...
:param bytes: the input image bytes array.
:type bytes: str
:param is_color: If set is_color True, it will load and
                 return a color image. Otherwise, it will
                 load and return a gray image.
:type is_color: bool
Tr   r   dtype)r	   npasarray	bytearrayuint8r   imdecode)bytesis_colorflag
file_bytesimgs        r   load_image_bytesrK   x   sI    $ <41aDIe,BHH=J
,,z
(CJr
   c                     [        5       SL d   eU(       a  SOSn[        R                  " U R                  S5      R	                  S5      U5      nU$ )aj  
Load an color or gray image from the file path.

Example usage:

.. code-block:: python

    >>> im = load_image('cat.jpg')

:param file: the input image path.
:type file: string
:param is_color: If set is_color True, it will load and
                 return a color image. Otherwise, it will
                 load and return a gray image.
:type is_color: bool
Tr   r   zutf-8)r	   r   imreadencodedecode)r   rG   rH   ims       r   
load_imagerQ      sH    " <4 1aD	DKK(//8$	?BIr
   c                     [        5       SL d   eU R                  SS u  p#XpTX#:  a  X-  U-  nOX-  U-  n[        R                  " XU4[        R                  S9n U $ )a7  
Resize an image so that the length of shorter edge is size.

Example usage:

.. code-block:: python

    >>> im = load_image('cat.jpg')
    >>> im = resize_short(im, 256)

:param im: the input image with HWC layout.
:type im: ndarray
:param size: the shorter edge size of image after resizing.
:type size: int
TNr   )interpolation)r	   shaper   resizeINTER_CUBIC)rP   sizehwh_neww_news         r   resize_shortr\      sb      <488BQ<DA5uAA	Bcoo	FBIr
   c                 p    [        U R                  5      [        U5      :X  d   eU R                  U5      n U $ )a  
Transpose the input image order. The image layout is HWC format
opened by cv2 or PIL. Transpose the input image to CHW layout
according the order (2,0,1).

Example usage:

.. code-block:: python

    >>> im = load_image('cat.jpg')
    >>> im = resize_short(im, 256)
    >>> im = to_chw(im)

:param im: the input image with HWC layout.
:type im: ndarray
:param order: the transposed order.
:type order: tuple|list
)r+   rT   	transpose)rP   orders     r   to_chwr`      s1    & rxx=CJ&&&	e	BIr
   c                     U R                   SS u  p4X1-
  S-  nXA-
  S-  nXQ-   Xa-   pU(       a  XU2Xh2SS24   n U $ XU2Xh24   n U $ )aK  
Crop the center of image with size.

Example usage:

.. code-block:: python

    >>> im = load_image('cat.jpg')
    >>> im = center_crop(im, 224)

:param im: the input image with HWC layout.
:type im: ndarray
:param size: the cropping size.
:type size: int
:param is_color: whether the image is color or not.
:type is_color: bool
Nr   )rT   	rP   rW   rG   rX   rY   h_startw_starth_endw_ends	            r   center_croprg      so    $ 88BQ<DAxAoGxAoG>7>5w}a/0 I w},-Ir
   c                    U R                   SS u  p4[        R                  R                  SX1-
  S-   5      n[        R                  R                  SXA-
  S-   5      nXQ-   Xa-   pU(       a  XU2Xh2SS24   n U $ XU2Xh24   n U $ )aL  
Randomly crop input image with size.

Example usage:

.. code-block:: python

    >>> im = load_image('cat.jpg')
    >>> im = random_crop(im, 224)

:param im: the input image with HWC layout.
:type im: ndarray
:param size: the cropping size.
:type size: int
:param is_color: whether the image is color or not.
:type is_color: bool
Nr   r   r   )rT   rA   randomrandintrb   s	            r   random_croprk      s    $ 88BQ<DAii18a<0Gii18a<0G>7>5w}a/0 I w},-Ir
   c                 x    [        U R                  5      S:X  a  U(       a  U SS2SSS2SS24   $ U SS2SSS24   $ )aV  
Flip an image along the horizontal direction.
Return the flipped image.

Example usage:

.. code-block:: python

    >>> im = load_image('cat.jpg')
    >>> im = left_right_flip(im)

:param im: input image with HWC layout or HW layout for gray image
:type im: ndarray
:param is_color: whether input image is color or not
:type is_color: bool
   N)r+   rT   )rP   rG   s     r   left_right_flipro     s<    " 288}h!TrT1*~!TrT'{r
   c                 X   [        X5      n U(       a9  [        XUS9n [        R                  R	                  S5      S:X  a  [        X5      n O
[        XUS9n [        U R                  5      S:X  a  [        U 5      n U R                  S5      n Ub  [        R                  " U[        R                  S9nUR                  S:X  a.  U(       a'  USS2[        R                  [        R                  4   nO7UR                  S:X  a  UnO$[        UR                  5      [        U 5      :X  d   eX-  n U $ )	a  
Simply data argumentation for training. These operations include
resizing, cropping and flipping.

Example usage:

.. code-block:: python

    >>> im = load_image('cat.jpg')
    >>> im = simple_transform(im, 256, 224, True)

:param im: The input image with HWC layout.
:type im: ndarray
:param resize_size: The shorter edge length of the resized image.
:type resize_size: int
:param crop_size: The cropping size.
:type crop_size: int
:param is_train: Whether it is training or not.
:type is_train: bool
:param is_color: whether the image is color or not.
:type is_color: bool
:param mean: the mean values, which can be element-wise mean values or
             mean values per channel.
:type mean: numpy array | list
)rG   r   r   rm   float32Nr?   r   )r\   rk   rA   ri   rj   ro   rg   r+   rT   r`   astypearrayrq   ndimnewaxis)rP   resize_size	crop_sizeis_trainrG   means         r   simple_transformrz   4  s    8 
b	&B:99Q1$ .B:
288}BZ	9	BxxBJJ/99>h2::rzz12DYY!^D tzz?c"g---

Ir
   c                 6    [        X5      n[        XaX#XE5      nU$ )a  
Load image from the input file `filename` and transform image for
data argumentation. Please refer to the `simple_transform` interface
for the transform operations.

Example usage:

.. code-block:: python

    >>> im = load_and_transform('cat.jpg', 256, 224, True)

:param filename: The file name of input image.
:type filename: string
:param resize_size: The shorter edge length of the resized image.
:type resize_size: int
:param crop_size: The cropping size.
:type crop_size: int
:param is_train: Whether it is training or not.
:type is_train: bool
:param is_color: whether the image is color or not.
:type is_color: bool
:param mean: the mean values, which can be element-wise mean values or
             mean values per channel.
:type mean: numpy array | list
)rQ   rz   )filenamerv   rw   rx   rG   ry   rP   s          r   load_and_transformr}   j  s!    8 
H	'B	"9	OBIr
   )i   )T))r   r   r   )TN)__doc__r   r$   numpyrA   r   ImportErrorr,   __all__r	   r=   rK   rQ   r\   r`   rg   rk   ro   rz   r}    r
   r   <module>r      s   & 
   

 7;9x2<80::0 ?C3n EIE
  
Cs   A AA