
    h3O                     P   S SK Jr  S SKrS SKJrJrJr   S SKrS SK	r
S SKrS SKrS SKrS SKrS rS rS rS rSS jrS	 r\R*                  4S
 jrS rSS jrS rS r\R*                  4S jrS rS rS rSS jrS r S r!S r"S r#SS jr$SS jr%SS jr&g! \ a    Sr Nf = f)     )divisionN)ImageImageOpsImageEnhancec                     [         b*  [        U [        R                  [         R                  45      $ [        U [        R                  5      $ N)accimage
isinstancer   imgs    W/var/www/fran/franai/plugins/Silent-Face-Anti-Spoofing-master/src/data_io/functional.py_is_pil_imager      s3    #X^^<==#u{{++    c                 b    [         R                  " U 5      =(       a    U R                  5       S:H  $ )N   )torch	is_tensor
ndimensionr   s    r   _is_tensor_imager      s"    ??39CNN$4$99r   c                 b    [        U [        R                  5      =(       a    U R                  S;   $ )N>      r   )r
   npndarrayndimr   s    r   _is_numpy_imager   !   s!    c2::&?CHH,>?r   c                    [        U 5      (       d3  [        U 5      (       d#  [        SR                  [	        U 5      5      5      e[        U [        R                  5      (       as  U R                  S:X  a.  U R                  U R                  S   U R                  S   S45      n [        R                  " U R                  S5      5      nUR                  5       $ [        b  [        U [        R                   5      (       ak  [        R"                  " U R$                  U R&                  U R(                  /[        R*                  S9nU R-                  U5        [        R                  " U5      $ U R.                  S:X  a9  [        R                  " [        R0                  " U [        R2                  SS	95      nOU R.                  S
:X  a9  [        R                  " [        R0                  " U [        R4                  SS	95      nOA[        R6                  " [        R8                  R;                  U R=                  5       5      5      nU R.                  S:X  a  SnO(U R.                  S
:X  a  SnO[?        U R.                  5      nURA                  U RB                  S   U RB                  S   U5      nUR                  SS5      R                  SS5      RE                  5       n[        U[        R6                  5      (       a  UR                  5       $ U$ )zConvert a ``PIL Image`` or ``numpy.ndarray`` to tensor.

See ``ToTensor`` for more details.

Args:
    pic (PIL Image or numpy.ndarray): Image to be converted to tensor.

Returns:
    Tensor: Converted image.
z*pic should be PIL Image or ndarray. Got {}r   r      )r   r   r   dtypeIF)copyI;16YCbCrr   )#r   r   	TypeErrorformattyper
   r   r   r   reshapeshaper   
from_numpy	transposefloatr	   r   zeroschannelsheightwidthfloat32copytomodearrayint32int16
ByteTensorByteStoragefrom_buffertobyteslenviewsize
contiguous)picr   nppicnchannels       r   	to_tensorrA   %   s    !5!5DKKDQTIVWW#rzz"" 88q=++syy|SYYq\1=>Cs}}Y78 yy{
3 ? ?#,,

CII>bjjQ

5&& xx3rxxRXXEBC	V	rxxRXXEBCu00<<S[[]KL
xx7	V	sxx=
((388A;X
6C --1

'
'1
-
8
8
:C#u''((yy{
r   c                    [        U 5      (       d3  [        U 5      (       d#  [        SR                  [	        U 5      5      5      eU n[        U [        R                  5      (       a  U R                  S5      R                  5       n [        R                  " U 5      (       a%  [        R                  " U R                  5       S5      n[        U[        R                  5      (       d&  [        SSR                  [	        U5      5      -   5      eUR                  S   S:X  a  SnUSS2SS2S	4   nUR                   [        R"                  :X  a  S
nUR                   [        R$                  :X  a  SnUR                   [        R&                  :X  a  SnO UR                   [        R(                  :X  a  SnUb/  X:w  a*  [+        SR                  U[        R                   U5      5      eUnOUR                  S   S:X  aJ  SS/nUb  X;  a  [+        SR                  U5      5      eUc   UR                   [        R"                  :X  a  SnOI/ SQnUb  X;  a  [+        SR                  U5      5      eUc   UR                   [        R"                  :X  a  SnUc$  [        SR                  UR                   5      5      e[,        R.                  " X!S9$ )a  Convert a tensor or an ndarray to PIL Image.

See :class:`~torchvision.transforms.ToPIlImage` for more details.

Args:
    pic (Tensor or numpy.ndarray): Image to be converted to PIL Image.
    mode (`PIL.Image mode`_): color space and pixel depth of input data (optional).

.. _PIL.Image mode: http://pillow.readthedocs.io/en/3.4.x/handbook/concepts.html#modes

Returns:
    PIL Image: Image converted to PIL Image.
z(pic should be Tensor or ndarray. Got {}.   )r   r   r   z3Input pic must be a torch.Tensor or NumPy ndarray, znot {}r   r   Nr   Lr"   r    Fz<Incorrect mode ({}) supplied for input type {}. Should be {}   RGBACMYKz)Only modes {} are supported for 4D inputs)RGBr#   HSVz)Only modes {} are supported for 3D inputsrI   zInput type {} is not supported)r2   )r   r   r$   r%   r&   r
   r   FloatTensormulbyter   r   r*   numpyr   r(   r   uint8r5   r4   r0   
ValueErrorr   	fromarray)r>   r2   npimgexpected_modepermitted_4_channel_modespermitted_3_channel_modess         r   to_pil_imagerV   [   s5    3#3C#8#8BII$s)TUUE#u(())ggcl!sSYY[)4eRZZ((M U45 6 	6 {{1~aAg;;"(("M;;"((""M;;"(("M[[BJJ&M 5[$fT288]CE E	Q1	%+V$4! EHOOPijkk<EKK2883D$;! EHOOPijkk<EKK2883D|8??LMM??5,,r   c                     [        U 5      (       d  [        S5      e[        XU5       H&  u  p4nUR                  U5      R	                  U5        M(     U $ )a\  Normalize a tensor image with mean and standard deviation.

See ``Normalize`` for more details.

Args:
    tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
    mean (sequence): Sequence of means for each channel.
    std (sequence): Sequence of standard deviations for each channely.

Returns:
    Tensor: Normalized Tensor image.
ztensor is not a torch image.)r   r$   zipsub_div_)tensormeanstdtmss         r   	normalizera      sI     F##677vS)a	q	q *Mr   c                 b   [        U 5      (       d#  [        SR                  [        U 5      5      5      e[	        U[
        5      (       dH  [	        U[        R                  5      (       a  [        U5      S:X  d  [        SR                  U5      5      e[	        U[
        5      (       as  U R                  u  p4X4::  a  X1:X  d
  XC::  a  XA:X  a  U $ X4:  a%  Un[        X-  U-  5      nU R                  XV4U5      $ Un[        X-  U-  5      nU R                  XV4U5      $ U R                  USSS2   U5      $ )aI  Resize the input PIL Image to the given size.

Args:
    img (PIL Image): Image to be resized.
    size (sequence or int): Desired output size. If size is a sequence like
        (h, w), the output size will be matched to this. If size is an int,
        the smaller edge of the image will be matched to this number maintaing
        the aspect ratio. i.e, if height > width, then image will be rescaled to
        (size * height / width, size)
    interpolation (int, optional): Desired interpolation. Default is
        ``PIL.Image.BILINEAR``

Returns:
    PIL Image: Resized image.
img should be PIL Image. Got {}r   zGot inappropriate size arg: {}N)r   r$   r%   r&   r
   intcollectionsIterabler:   r<   resize)r   r<   interpolationwhowohs          r   rh   rh      s     9@@cKLLtS!!j{7K7K&L&LQTUYQZ^_Q_8??EFF$xxFqyafJ5BTX\"B::rh66BTX\"B::rh66zz$tt*m44r   c                  D    [         R                  " S5        [        U 0 UD6$ )Nz^The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.)warningswarnrh   )argskwargss     r   scalers      s#    MM : ;4"6""r   c                    [        U 5      (       d#  [        SR                  [        U 5      5      5      e[	        U[
        R                  [        45      (       d  [        S5      e[	        U[
        R                  [        [        45      (       d  [        S5      e[	        U[        R                  5      (       a5  [        U5      S;  a&  [        SSR                  [        U5      5      -   5      e[        R                  " XUS9$ )av  Pad the given PIL Image on all sides with the given "pad" value.

Args:
    img (PIL Image): Image to be padded.
    padding (int or tuple): Padding on each border. If a single int is provided this
        is used to pad all borders. If tuple of length 2 is provided this is the padding
        on left/right and top/bottom respectively. If a tuple of length 4 is provided
        this is the padding for the left, top, right and bottom borders
        respectively.
    fill: Pixel fill value. Default is 0. If a tuple of
        length 3, it is used to fill R, G, B channels respectively.

Returns:
    PIL Image: Padded image.
rc   zGot inappropriate padding argzGot inappropriate fill arg)r   rF   z9Padding must be an int or a 2, or 4 element tuple, not a z{} element tuple)borderfill)r   r$   r%   r&   r
   numbersNumbertuplestrrf   Sequencer:   rP   r   expand)r   paddingrv   s      r   padr~      s      9@@cKLLg677788dW^^S%899455';//00S\5OT+223w<@A B 	B ??3T::r   c                     [        U 5      (       d#  [        SR                  [        U 5      5      5      eU R	                  X!X$-   X-   45      $ )zCrop the given PIL Image.

Args:
    img (PIL Image): Image to be cropped.
    i: Upper pixel coordinate.
    j: Left pixel coordinate.
    h: Height of the cropped image.
    w: Width of the cropped image.

Returns:
    PIL Image: Cropped image.
rc   )r   r$   r%   r&   crop)r   ijrk   rj   s        r   r   r      sD     9@@cKLL88Q15!%())r   c                    [        U[        R                  5      (       a  [        U5      [        U5      4nU R                  u  p#Uu  pE[        [        X4-
  S-  5      5      n[        [        X%-
  S-  5      5      n[        XXtU5      $ )Ng       @)r
   rw   rx   re   r<   roundr   )r   output_sizerj   rk   thtwr   r   s           r   center_cropr     sr    +w~~..;'[)9:88DAFBE16R- !AE16R- !Ar""r   c                 f    [        U 5      (       d   S5       e[        XX#U5      n [        XU5      n U $ )a  Crop the given PIL Image and resize it to desired size.

Notably used in RandomResizedCrop.

Args:
    img (PIL Image): Image to be cropped.
    i: Upper pixel coordinate.
    j: Left pixel coordinate.
    h: Height of the cropped image.
    w: Width of the cropped image.
    size (sequence or int): Desired output size. Same semantics as ``scale``.
    interpolation (int, optional): Desired interpolation. Default is
        ``PIL.Image.BILINEAR``.
Returns:
    PIL Image: Cropped image.
zimg should be PIL Image)r   r   rh   )r   r   r   rk   rj   r<   ri   s          r   resized_cropr     s:    " 888
sqQ
C
M
*CJr   c                     [        U 5      (       d#  [        SR                  [        U 5      5      5      eU R	                  [
        R                  5      $ )zHorizontally flip the given PIL Image.

Args:
    img (PIL Image): Image to be flipped.

Returns:
    PIL Image:  Horizontall flipped image.
rc   )r   r$   r%   r&   r*   r   FLIP_LEFT_RIGHTr   s    r   hflipr   -  ?     9@@cKLL==..//r   c                     [        U 5      (       d#  [        SR                  [        U 5      5      5      eU R	                  [
        R                  5      $ )zVertically flip the given PIL Image.

Args:
    img (PIL Image): Image to be flipped.

Returns:
    PIL Image:  Vertically flipped image.
rc   )r   r$   r%   r&   r*   r   FLIP_TOP_BOTTOMr   s    r   vflipr   <  r   r   c                    [        U[        R                  5      (       a  [        U5      [        U5      4nO[	        U5      S:X  d   S5       eU R
                  u  p#Uu  pEXR:  d  XC:  a  [        SR                  UX245      5      eU R                  SSXT45      nU R                  X%-
  SX$45      nU R                  SX4-
  XS45      nU R                  X%-
  X4-
  X#45      n	[        XU45      n
XgXU
4$ )a  Crop the given PIL Image into four corners and the central crop.

.. Note::
    This transform returns a tuple of images and there may be a
    mismatch in the number of inputs and targets your ``Dataset`` returns.

Args:
   size (sequence or int): Desired output size of the crop. If size is an
       int instead of sequence like (h, w), a square crop (size, size) is
       made.
Returns:
    tuple: tuple (tl, tr, bl, br, center) corresponding top left,
        top right, bottom left, bottom right and center crop.
r   3Please provide only two dimensions (h, w) for size.z3Requested crop size {} is bigger than input size {}r   )
r
   rw   rx   re   r:   r<   rP   r%   r   r   )r   r<   rj   rk   crop_hcrop_wtltrblbrcenters              r   	five_cropr   K  s     $''D	3t9%4yA~TTT~88DANFzVZNUUVZWXV\^ _ 	_	1a(	)B	1:q!,	-B	1aj&,	-B	1:qz10	1Bv./FBF##r   c                 
   [        U[        R                  5      (       a  [        U5      [        U5      4nO[	        U5      S:X  d   S5       e[        X5      nU(       a  [        U 5      n O[        U 5      n [        X5      nX4-   $ )a#  Crop the given PIL Image into four corners and the central crop plus the
   flipped version of these (horizontal flipping is used by default).

.. Note::
    This transform returns a tuple of images and there may be a
    mismatch in the number of inputs and targets your ``Dataset`` returns.

   Args:
       size (sequence or int): Desired output size of the crop. If size is an
           int instead of sequence like (h, w), a square crop (size, size) is
           made.
       vertical_flip (bool): Use vertical flipping instead of horizontal

    Returns:
        tuple: tuple (tl, tr, bl, br, center, tl_flip, tr_flip, bl_flip,
            br_flip, center_flip) corresponding top left, top right,
            bottom left, bottom right and center crop and same for the
            flipped image.
r   r   )r
   rw   rx   re   r:   r   r   r   )r   r<   vertical_flip
first_fivesecond_fives        r   ten_cropr   l  so    ( $''D	3t9%4yA~TTT~3%JCjCjC&K##r   c                     [        U 5      (       d#  [        SR                  [        U 5      5      5      e[        R
                  " U 5      nUR                  U5      n U $ )aa  Adjust brightness of an Image.

Args:
    img (PIL Image): PIL Image to be adjusted.
    brightness_factor (float):  How much to adjust the brightness. Can be
        any non negative number. 0 gives a black image, 1 gives the
        original image while 2 increases the brightness by a factor of 2.

Returns:
    PIL Image: Brightness adjusted image.
rc   )r   r$   r%   r&   r   
Brightnessenhance)r   brightness_factorenhancers      r   adjust_brightnessr     sP     9@@cKLL&&s+H


,
-CJr   c                     [        U 5      (       d#  [        SR                  [        U 5      5      5      e[        R
                  " U 5      nUR                  U5      n U $ )a[  Adjust contrast of an Image.

Args:
    img (PIL Image): PIL Image to be adjusted.
    contrast_factor (float): How much to adjust the contrast. Can be any
        non negative number. 0 gives a solid gray image, 1 gives the
        original image while 2 increases the contrast by a factor of 2.

Returns:
    PIL Image: Contrast adjusted image.
rc   )r   r$   r%   r&   r   Contrastr   )r   contrast_factorr   s      r   adjust_contrastr     sO     9@@cKLL$$S)H


?
+CJr   c                     [        U 5      (       d#  [        SR                  [        U 5      5      5      e[        R
                  " U 5      nUR                  U5      n U $ )a\  Adjust color saturation of an image.

Args:
    img (PIL Image): PIL Image to be adjusted.
    saturation_factor (float):  How much to adjust the saturation. 0 will
        give a black and white image, 1 will give the original image while
        2 will enhance the saturation by a factor of 2.

Returns:
    PIL Image: Saturation adjusted image.
rc   )r   r$   r%   r&   r   Colorr   )r   saturation_factorr   s      r   adjust_saturationr     sP     9@@cKLL!!#&H


,
-CJr   c                    SUs=::  a  S::  d  O  [        SR                  U5      5      e[        U 5      (       d#  [        SR                  [	        U 5      5      5      eU R
                  nUS;   a  U $ U R                  S5      R                  5       u  p4n[        R                  " U[        R                  S9n[        R                  " SS	9   U[        R                  " US
-  5      -  nSSS5        [        R                  " US5      n[        R                  " SX4U45      R                  U5      n U $ ! , (       d  f       NO= f)a0  Adjust hue of an image.

The image hue is adjusted by converting the image to HSV and
cyclically shifting the intensities in the hue channel (H).
The image is then converted back to original image mode.

`hue_factor` is the amount of shift in H channel and must be in the
interval `[-0.5, 0.5]`.

See https://en.wikipedia.org/wiki/Hue for more details on Hue.

Args:
    img (PIL Image): PIL Image to be adjusted.
    hue_factor (float):  How much to shift the hue channel. Should be in
        [-0.5, 0.5]. 0.5 and -0.5 give complete reversal of hue channel in
        HSV space in positive and negative direction respectively.
        0 means no shift. Therefore, both -0.5 and 0.5 will give an image
        with complementary colors while 0 gives the original image.

Returns:
    PIL Image: Hue adjusted image.
g      g      ?z!hue_factor is not in [-0.5, 0.5].rc   >   1rE   r    rD   rJ   r   ignore)overrC   NrD   )rP   r%   r   r$   r&   r2   convertsplitr   r3   rO   errstater   rQ   merge)r   
hue_factor
input_moderk   r`   vnp_hs          r   
adjust_huer     s    . :$$<CCJOPP9@@cKLLJ))
kk% &&(GA!88ARXX&D	(	#c)** 
$c"A
++eaAY
'
/
/

;CJ 
$	#s   
D00
D>c                    [        U 5      (       d#  [        SR                  [        U 5      5      5      eUS:  a  [	        S5      eU R
                  nU R                  S5      n [        R                  " U [        R                  S9nSU-  US-  U-  -  n[        R                  " [        R                  " USS5      5      n[        R                  " US5      R                  U5      n U $ )a	  Perform gamma correction on an image.

Also known as Power Law Transform. Intensities in RGB mode are adjusted
based on the following equation:

    I_out = 255 * gain * ((I_in / 255) ** gamma)

See https://en.wikipedia.org/wiki/Gamma_correction for more details.

Args:
    img (PIL Image): PIL Image to be adjusted.
    gamma (float): Non negative real number. gamma larger than 1 make the
        shadows darker, while gamma smaller than 1 make dark regions
        lighter.
    gain (float): The constant multiplier.
rc   r   z*Gamma should be a non-negative real numberrI   r   rC   )r   r$   r%   r&   rP   r2   r   r   r3   r0   rO   clipr   rQ   )r   gammagainr   np_imgs        r   adjust_gammar     s    " 9@@cKLLqyEFFJ
++e
CXXc,F4ZFSLU23FXXbggfa-.F
//&%
(
0
0
<CJr   c                     [        U 5      (       d#  [        SR                  [        U 5      5      5      eU R	                  XX45      $ )a  Rotate the image by angle and then (optionally) translate it by (n_columns, n_rows)


Args:
    img (PIL Image): PIL Image to be rotated.
    angle ({float, int}): In degrees degrees counter clockwise order.
    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 image 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.
rc   )r   r$   r%   r&   rotate)r   angleresampler|   r   s        r   r   r     s;    ( 9@@cKLL::ev66r   c                    [        U 5      (       d#  [        SR                  [        U 5      5      5      eUS:X  a  U R	                  S5      n U $ US:X  ae  U R	                  S5      n [
        R                  " U [
        R                  S9n[
        R                  " X"U/5      n[        R                  " US5      n U $ [        S5      e)aQ  Convert image to grayscale version of image.

Args:
    img (PIL Image): Image to be converted to grayscale.

Returns:
    PIL Image:  Grayscale version of the image.
                if num_output_channels == 1 : returned image is single channel
                if num_output_channels == 3 : returned image is 3 channel with r == g == b
rc   r   rD   r   r   rI   z+num_output_channels should be either 1 or 3)r   r$   r%   r&   r   r   r3   rO   dstackr   rQ   rP   )r   num_output_channelsr   s      r   to_grayscaler   5  s     9@@cKLLakk# J 
	!kk##RXX.FF34oofe, J FGGr   r   )r   )F)r   )FFN)'
__future__r   r   PILr   r   r   r	   ImportErrorrN   r   rw   typesrf   ro   r   r   r   rA   rV   ra   BILINEARrh   rs   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   <module>r      s       - -     ,:@3l<-~* %*NN "5J#;>*&# 7<nn .00$B!$H(((*ZD74Q  Hs   B B%$B%