
    h,                     D   S SK Jr  S SKrS SKrS SKJr   S SKrS SKr	S SK
r
S SKrS SKJr  / SQr " S S\5      r " S S	\5      r " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg! \ a    Sr Nf = f)    )divisionN)Image)
functional)	ComposeToTensor
ToPILImage	NormalizeRandomHorizontalFlipLambdaRandomResizedCropColorJitterRandomRotationc                   $    \ rS rSrSrS rS rSrg)r      zComposes several transforms together.

Args:
    transforms (list of ``Transform`` objects): list of transforms to compose.

Example:
    >>> transforms.Compose([
    >>>     transforms.CenterCrop(10),
    >>>     transforms.ToTensor(),
    >>> ])
c                     Xl         g N
transforms)selfr   s     V/var/www/fran/franai/plugins/Silent-Face-Anti-Spoofing-master/src/data_io/transform.py__init__Compose.__init__'   s    $    c                 <    U R                    H  nU" U5      nM     U$ r   r   )r   imgts      r   __call__Compose.__call__*   s    AC&C !
r   r   N__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__ r   r   r   r      s    
%r   r   c                       \ rS rSrSrS rSrg)r   0   zConvert a ``PIL Image`` or ``numpy.ndarray`` to tensor.

Converts a PIL Image or numpy.ndarray (H x W x C) in the range
[0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].
c                 .    [         R                  " U5      $ )zu
Args:
    pic (PIL Image or numpy.ndarray): Image to be converted to tensor.

Returns:
    Tensor: Converted image.
)F	to_tensorr   pics     r   r   ToTensor.__call__8   s     {{3r   r&   Nr    r!   r"   r#   r$   r   r%   r&   r   r   r   r   0   s     r   r   c                   $    \ rS rSrSrS rS rSrg)r   C   zsApply a user-defined lambda as a transform.

Args:
    lambd (function): Lambda/function to be used for transform.
c                 R    [        U[        R                  5      (       d   eXl        g r   )
isinstancetypes
LambdaTypelambd)r   r6   s     r   r   Lambda.__init__J   s     %!1!12222
r   c                 $    U R                  U5      $ r   r6   r   r   s     r   r   Lambda.__call__N   s    zz#r   r9   Nr   r&   r   r   r   r   C   s    r   r   c                   (    \ rS rSrSrSS jrS rSrg)r   R   a  Convert a tensor or an ndarray to PIL Image.

Converts a torch.*Tensor of shape C x H x W or a numpy ndarray of shape
H x W x C to a PIL Image while preserving the value range.

Args:
    mode (`PIL.Image mode`_): color space and pixel depth of input data (optional).
        If ``mode`` is ``None`` (default) there are some assumptions made about the input data:
        1. If the input has 3 channels, the ``mode`` is assumed to be ``RGB``.
        2. If the input has 4 channels, the ``mode`` is assumed to be ``RGBA``.
        3. If the input has 1 channel, the ``mode`` is determined by the data type (i,e,
        ``int``, ``float``, ``short``).

.. _PIL.Image mode: http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#modes
Nc                     Xl         g r   mode)r   r@   s     r   r   ToPILImage.__init__b   s    	r   c                 B    [         R                  " XR                  5      $ )z
Args:
    pic (Tensor or numpy.ndarray): Image to be converted to PIL Image.

Returns:
    PIL Image: Image converted to PIL Image.

)r*   to_pil_imager@   r,   s     r   r   ToPILImage.__call__e   s     ~~c99--r   r?   r   r   r&   r   r   r   r   R   s    	.r   r   c                   $    \ rS rSrSrS rS rSrg)r	   q   a  Normalize an tensor image with mean and standard deviation.
Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels, this transform
will normalize each channel of the input ``torch.*Tensor`` i.e.
``input[channel] = (input[channel] - mean[channel]) / std[channel]``

Args:
    mean (sequence): Sequence of means for each channel.
    std (sequence): Sequence of standard deviations for each channel.
c                     Xl         X l        g r   meanstd)r   rI   rJ   s      r   r   Normalize.__init__|   s    	r   c                 X    [         R                  " XR                  U R                  5      $ )z|
Args:
    tensor (Tensor): Tensor image of size (C, H, W) to be normalized.

Returns:
    Tensor: Normalized Tensor image.
)r*   	normalizerI   rJ   )r   tensors     r   r   Normalize.__call__   s     {{699dhh77r   rH   Nr   r&   r   r   r	   r	   q   s    8r   r	   c                       \ rS rSrSrS rSrg)r
      zIHorizontally flip the given PIL Image randomly with a probability of 0.5.c                 d    [         R                   " 5       S:  a  [        R                  " U5      $ U$ )zb
Args:
    img (PIL Image): Image to be flipped.

Returns:
    PIL Image: Randomly flipped image.
      ?)randomr*   hflipr:   s     r   r   RandomHorizontalFlip.__call__   s%     ==?S 773<
r   r&   Nr/   r&   r   r   r
   r
      s
    S
r   r
   c                   R    \ rS rSrSrSS\R                  4S jr\S 5       r	S r
Srg	)
r      a  Crop the given PIL Image to random size and aspect ratio.

A crop of random size (default: of 0.08 to 1.0) of the original size and a random
aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop
is finally resized to given size.
This is popularly used to train the Inception networks.

Args:
    size: expected output size of each edge
    scale: range of size of the origin size cropped
    ratio: range of aspect ratio of the origin aspect ratio cropped
    interpolation: Default: PIL.Image.BILINEAR
)g{Gz?g      ?)g      ?gUUUUUU?c                 p    [        U[        5      (       a  Xl        OX4U l        X@l        X l        X0l        g r   )r3   tuplesizeinterpolationscaleratio)r   r[   r]   r^   r\   s        r   r   RandomResizedCrop.__init__   s-    dE""IDI*

r   c           	      2   [        S5       GH4  nU R                  S   U R                  S   -  n[        R                  " U6 U-  n[        R                  " U6 n[	        [        [        R                  " XV-  5      5      5      n[	        [        [        R                  " XV-  5      5      5      n[        R                  " 5       S:  a  XpXpR                  S   ::  d  M  XR                  S   ::  d  M  [        R                  " SU R                  S   U-
  5      n	[        R                  " SU R                  S   U-
  5      n
XX4s  $    [        U R                  S   U R                  S   5      nU R                  S   U-
  S-  n	U R                  S   U-
  S-  n
XXw4$ )aQ  Get parameters for ``crop`` for a random sized crop.

Args:
    img (PIL Image): Image to be cropped.
    scale (tuple): range of size of the origin size cropped
    ratio (tuple): range of aspect ratio of the origin aspect ratio cropped

Returns:
    tuple: params (i, j, h, w) to be passed to ``crop`` for a random
        sized crop.

   r      rS      )
ranger[   rT   uniformintroundmathsqrtrandintmin)r   r]   r^   attemptareatarget_areaaspect_ratiowhijs              r   
get_paramsRandomResizedCrop.get_params   sG    RyG88A;!,D ..%047K!>>51LE$))K$>?@AAE$))K$>?@AA}}$1HHQKA!$4NN1chhqkAo6NN1chhqkAo6Qz! !" SXXa[)XXa[1_"XXa[1_"Qzr   c           	          U R                  XR                  U R                  5      u  p#pE[        R                  " XX4XPR
                  U R                  5      $ )zm
Args:
    img (PIL Image): Image to be flipped.

Returns:
    PIL Image: Randomly cropped and resize image.
)rt   r]   r^   r*   resized_cropr[   r\   )r   r   rr   rs   rq   rp   s         r   r   RandomResizedCrop.__call__   s?     __S**djjA
a~~caAyy$:L:LMMr   )r\   r^   r]   r[   N)r    r!   r"   r#   r$   r   BILINEARr   staticmethodrt   r   r%   r&   r   r   r   r      s8     $/6HX]XfXf     D	Nr   r   c                   8    \ rS rSrSrSS jr\S 5       rS rSr	g)	r      ay  Randomly change the brightness, contrast and saturation of an image.

Args:
    brightness (float): How much to jitter brightness. brightness_factor
        is chosen uniformly from [max(0, 1 - brightness), 1 + brightness].
    contrast (float): How much to jitter contrast. contrast_factor
        is chosen uniformly from [max(0, 1 - contrast), 1 + contrast].
    saturation (float): How much to jitter saturation. saturation_factor
        is chosen uniformly from [max(0, 1 - saturation), 1 + saturation].
    hue(float): How much to jitter hue. hue_factor is chosen uniformly from
        [-hue, hue]. Should be >=0 and <= 0.5.
c                 4    Xl         X l        X0l        X@l        g r   )
brightnesscontrast
saturationhue)r   r~   r   r   r   s        r   r   ColorJitter.__init__   s    $ $r   c                   ^^^^	 / nU S:  aN  [         R                  R                  [        SSU -
  5      SU -   5      mUR	                  [        U4S j5      5        US:  aN  [         R                  R                  [        SSU-
  5      SU-   5      mUR	                  [        U4S j5      5        US:  aN  [         R                  R                  [        SSU-
  5      SU-   5      m	UR	                  [        U	4S j5      5        US:  a?  [         R                  R                  U* U5      mUR	                  [        U4S j5      5        [         R                  R                  U5        [        U5      nU$ )zGet a randomized transform to be applied on image.

Arguments are same as that of __init__.

Returns:
    Transform which randomly adjusts brightness, contrast and
    saturation in a random order.
r   rb   c                 2   > [         R                  " U T5      $ r   )r*   adjust_brightness)r   brightness_factors    r   <lambda>(ColorJitter.get_params.<locals>.<lambda>      1D1DSJ[1\r   c                 2   > [         R                  " U T5      $ r   )r*   adjust_contrast)r   contrast_factors    r   r   r     s    1B1B31Xr   c                 2   > [         R                  " U T5      $ r   )r*   adjust_saturation)r   saturation_factors    r   r   r   
  r   r   c                 2   > [         R                  " U T5      $ r   )r*   
adjust_hue)r   
hue_factors    r   r   r     s    c:1Nr   )nprT   re   maxappendr   shuffler   )
r~   r   r   r   r   	transformr   r   r   r   s
         @@@@r   rt   ColorJitter.get_params   s!    
> "		 1 1#aZ2H!j. Yf%\]^a< ii//Aq8|0Da(lSOf%XYZ> "		 1 1#aZ2H!j. Yf%\]^7**C45Jf%NOP
		*%J'	r   c                     U R                  U R                  U R                  U R                  U R                  5      nU" U5      $ )zX
Args:
    img (PIL Image): Input image.

Returns:
    PIL Image: Color jittered image.
)rt   r~   r   r   r   )r   r   r   s      r   r   ColorJitter.__call__  s5     OODOOT]]$(OOTXX?	~r   )r~   r   r   r   N)r   r   r   r   
r    r!   r"   r#   r$   r   rz   rt   r   r%   r&   r   r   r   r      s%      >
r   r   c                   8    \ rS rSrSrSS jr\S 5       rS rSr	g)	r   i"  a  Rotate the image by angle.

Args:
    degrees (sequence or float or int): Range of degrees to select from.
        If degrees is a number instead of sequence like (min, max), the range of degrees
        will be (-degrees, +degrees).
    resample ({PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC}, optional):
        An optional resampling filter.
        See http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#filters
        If omitted, or if the image has mode "1" or "P", it is set to PIL.Image.NEAREST.
    expand (bool, optional): Optional expansion flag.
        If true, expands the output to make it large enough to hold the entire rotated image.
        If false or omitted, make the output image the same size as the input image.
        Note that the expand flag assumes rotation around the center and no translation.
    center (2-tuple, optional): Optional center of rotation.
        Origin is the upper left corner.
        Default is the center of the image.
Nc                     [        U[        R                  5      (       a  US:  a  [        S5      eU* U4U l        O [        U5      S:w  a  [        S5      eXl        X l        X0l        X@l        g )Nr   z3If degrees is a single number, it must be positive.rc   z.If degrees is a sequence, it must be of len 2.)	r3   numbersNumber
ValueErrordegreeslenresampleexpandcenter)r   r   r   r   r   s        r   r   RandomRotation.__init__6  s`    gw~~..{ !VWW$Hg.DL7|q  !QRR"L r   c                 R    [         R                  R                  U S   U S   5      nU$ )zGet parameters for ``rotate`` for a random rotation.

Returns:
    sequence: params to be passed to ``rotate`` for random rotation.
r   rb   )r   rT   re   )r   angles     r   rt   RandomRotation.get_paramsD  s'     		!!'!*gaj9r   c                     U R                  U R                  5      n[        R                  " XU R                  U R
                  U R                  5      $ )zS
    img (PIL Image): Image to be rotated.

Returns:
    PIL Image: Rotated image.
)rt   r   r*   rotater   r   r   )r   r   r   s      r   r   RandomRotation.__call__O  s7     -xxDMM4;;LLr   )r   r   r   r   )FFNr   r&   r   r   r   r   "  s&    &  
Mr   r   )
__future__r   rh   rT   PILr   accimageImportErrornumpyr   r   r4   src.data_ior   r*   __all__objectr   r   r   r   r	   r
   r   r   r   r&   r   r   <module>r      s            'Kf , v  &V . .>8 846  DN DNN=& =@7MV 7Mi  Hs   B BB