o
    Qe  ã                   @   s~   d dl Zd dlmZ d dlmZ g Zdd„ Zdd„ Z	dd	d
„Z
dd„ Zddd„Zddd„Zddd„Zdd„ ZG dd„ dƒZdS )é    N)ÚImage)Ú	cStringIOc                 C   s\   |t t| jd | jd ƒƒ }tt| jd | ƒƒtt| jd | ƒƒf}|  |tj¡} | S )z¢
    Resize an image so that the shorter edge has length target_size.
    img: the input image to be resized.
    target_size: the target resized image size.
    r   é   )ÚfloatÚminÚsizeÚintÚroundÚresizer   Z	ANTIALIAS)ÚimgZtarget_sizeÚpercentZresized_size© r   úGD:\Projects\ConvertPro\env\Lib\site-packages\paddle/utils/image_util.pyÚresize_image   s   ÿr   c                 C   s@   t | jƒdkr| dd…dd…ddd…f S | dd…ddd…f S )z
    Return the flipped image.
    Flip an image along the horizontal direction.
    im: input image, (K x H x W) ndarrays
    é   Néÿÿÿÿ)ÚlenÚshape)Úimr   r   r   Úflip#   s   r   Tc                 C   sÔ  |rJt || jd ƒt || jd ƒ}}t d||f¡}|| jd  d }|| jd  d }|| jd  || jd  }	}
| |dd…||	…||
…f< nH|  d¡} t || jd ƒt || jd ƒ}}t ||f¡}|| jd  d }|| jd  d }|| jd  || jd  }	}
| |||	…||
…f< |r¡|| d }|| d }ntj d|| d ¡}tj d|| d ¡}|| || }	}
|rÐ|dd…||	…||
…f }n
|||	…||
…f }|sètj d¡dkrèt|ƒ}|S )aE  
    Return cropped image.
    The size of the cropped image is inner_size * inner_size.
    im: (K x H x W) ndarrays
    inner_size: the cropped image size.
    color: whether it is color image.
    test: whether in test mode.
      If False, does random cropping and flipping.
      If True, crop the center of images.
    r   é   r   NÚfloat32r   )Úmaxr   ÚnpZzerosÚastypeÚrandomÚrandintr   )r   Z
inner_sizeÚcolorÚtestÚheightÚwidthZ	padded_imZstartYZstartXZendYZendXÚpicr   r   r   Úcrop_img/   sD   ÿÿ
ÿÿr"   c                 C   s2   t  t t| ƒ¡¡}t|jƒdkrt  |d¡}|S )Nr   )r   r   r   )r   Úarrayr   ÚopenÚStringIOr   r   Ú	transpose)Zjpeg_stringZnp_arrayr   r   r   Údecode_jpeg[   s   r'   c                 C   s.   |   d¡} | }t| |||ƒ}||8 }| ¡ S )zì
    Does data augmentation for images.
    If is_train is false, cropping the center region from the image.
    If is_train is true, randomly crop a region from the image,
    and random does flipping.
    im: (K x H x W) ndarrays
    r   )r   r"   Úflatten)r   Zimg_meanÚ	crop_sizeZis_trainr   r   r!   r   r   r   Úpreprocess_imgb   s
   
r*   c                 C   sº   t  | ¡d }|| d }|r9|| d |jd ksJ ‚| d||¡}|dd…||| …||| …f  d¡}|S || |jd ksDJ ‚| ||¡}|||| …||| …f  d¡}|S )zâ
    Return the loaded meta file.
    Load the meta image, which is the mean of the images in the dataset.
    The mean image is subtracted from every input image so that the expected mean
    of each input image is zero.
    Z	data_meanr   r   r   Nr   )r   Úloadr   Zreshaper   )Ú	meta_pathZmean_img_sizer)   r   ÚmeanÚborderr   r   r   Ú	load_metaq   s&   
ÿÿü
ÿÿr/   c                 C   s   t  | ¡}| ¡  |S )z_
    Load image and return.
    img_path: image path.
    is_color: is color image or not.
    )r   r$   r+   )Zimg_pathÚis_colorr   r   r   r   Ú
load_image‡   s   
r1   c                 C   s„  t  | d j¡}t  |¡}|dd… d }d|d |d  f}d|d |d  f}t jdtd}d}|D ]}|D ]}	||	||d  |	|d  f||< |d7 }q8q4t  |d¡t  | d |d g¡ |d	< t  |d
¡}t jdt| ƒ |d |d |d ft jd}
d}| D ];}|D ]}||d |d …|d |d …dd…f |
|< |d7 }qˆ|
|d |…dd…ddd…dd…f |
|d |…< q„|
S )zý
    image : iterable of (H x W x K) ndarrays
    crop_dims: (height, width) tuple for the crops.
    Returned data contains ten crops of input image, namely,
    four corner patches and the center patch as well as their
    horizontal reflections.
    r   Nr   g       @r   )é   é   )Zdtype)r   r   r3   )r   r   é
   r   r   r2   )	r   r#   r   Úemptyr   ZtileZconcatenater   r   )r   Z	crop_dimsZim_shapeZ	im_centerZ	h_indicesZ	w_indicesZcrops_ixÚcurrÚiÚjZcropsZixr   Úcropr   r   r   Ú
oversample’   s4   	
 
þ
ÿ"ÿ.
4r:   c                   @   s>   e Zd Z				ddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚImageTransformerNTc                 C   s(   || _ |  |¡ |  |¡ |  |¡ d S ©N)r0   Úset_transposeÚset_channel_swapÚset_mean)Úselfr&   Úchannel_swapr-   r0   r   r   r   Ú__init__º   s   

zImageTransformer.__init__c                 C   ó(   |d ur| j rdt|ƒksJ ‚|| _d S ©Nr   )r0   r   r&   ©r@   Úorderr   r   r   r=   Ä   ó   
zImageTransformer.set_transposec                 C   rC   rD   )r0   r   rA   rE   r   r   r   r>   Ê   rG   z!ImageTransformer.set_channel_swapc                 C   sL   |d ur!|j dkr|d d …tjtjf }n| jr!t|jƒdks!J ‚|| _d S )Nr   r   )Úndimr   Znewaxisr0   r   r   r-   )r@   r-   r   r   r   r?   Ð   s   

zImageTransformer.set_meanc                 C   sP   | j d ur|  | j ¡}| jd ur|| jd d …d d …f }| jd ur&|| j8 }|S r<   )r&   rA   r-   )r@   Údatar   r   r   ÚtransformerÛ   s   



zImageTransformer.transformer)NNNT)Ú__name__Ú
__module__Ú__qualname__rB   r=   r>   r?   rJ   r   r   r   r   r;   ¸   s    
ü
r;   )TT)T)Únumpyr   ZPILr   Z	six.movesr   r%   Ú__all__r   r   r"   r'   r*   r/   r1   r:   r;   r   r   r   r   Ú<module>   s   
,


&