
    ёiZ                         S SK r S SKrS SKJrJr  S SKrS SKrS SKJ	r	  / r
SS jrSS jrSS jrS rS rS	 rS
 rS rS rS 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)    N)IterableSequence)
try_importc                    US;  a  [        SU 35      eU R                  S:X  a  U SS2SS2S4   n US:X  a&  [        R                  " U R	                  S5      5      nO[        R                  " U 5      n[        R
                  R                  R                  UR                  5      S:X  a(  [        R                  " U[        R                  5      S-  $ U$ )	a*  Converts a ``numpy.ndarray`` to paddle.Tensor.

See ``ToTensor`` for more details.

Args:
    pic (np.ndarray): Image to be converted to tensor.
    data_format (str, optional): Data format of output tensor, should be 'HWC' or
        'CHW'. Default: 'CHW'.

Returns:
    Tensor: Converted image.

)CHWHWCz&data_format should be CHW or HWC. Got    Nr   )r	   r      uint8g     o@)
ValueErrorndimpaddle	to_tensor	transposebasedata_feederconvert_dtypedtypecastnpfloat32)picdata_formatimgs      g/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/vision/transforms/functional_cv2.pyr   r      s     .(A+OPP
xx1}!Q*oes}}Y78s#{{,,SYY77B{{3

+e33
    c                    [        S5      nUR                  UR                  UR                  UR                  UR
                  S.n[        U[        5      (       d2  [        U[        5      (       a  [        U5      S:X  d  [        SU 35      eU R                  SS u  pV[        U[        5      (       ai  Xe::  a  Xa:X  d
  XV::  a  XQ:X  a  U $ Xe:  a'  Un[        X-  U-  5      nUR                  U Xx4XB   S9n	OBUn[        X-  U-  5      nUR                  U Xx4XB   S9n	OUR                  U US   US   4XB   S9n	[        U R                  5      S	:X  a-  U R                  S   S:X  a  U	SS2SS2[        R                  4   $ U	$ )
a  
Resizes the image to given size

Args:
    input (np.ndarray): Image to be resized.
    size (int|list|tuple): Target size of input data, with (height, width) shape.
    interpolation (int|str, optional): Interpolation method. when use cv2 backend,
        support method are as following:
        - "nearest": cv2.INTER_NEAREST,
        - "bilinear": cv2.INTER_LINEAR,
        - "area": cv2.INTER_AREA,
        - "bicubic": cv2.INTER_CUBIC,
        - "lanczos": cv2.INTER_LANCZOS4

Returns:
    np.array: Resized image.

cv2nearestbilinearareabicubiclanczosr	   zGot inappropriate size arg: N)dsizeinterpolationr
   r      )r   INTER_NEARESTINTER_LINEAR
INTER_AREAINTER_CUBICINTER_LANCZOS4
isinstanceintr   len	TypeErrorshaperesizer   newaxis)
r   sizer&   r   _cv2_interp_from_strhwowohoutputs
             r   r2   r2   ;   s   & U
C$$$$??%% 	4*T8"<"<Ta6tf=>>99Ra=DA$FqyafJ5BTX\"BZZh2A   F BTX\"BZZh2A   F 7DG$.=  

 399~syy|q0aBJJ&''r   c           
      V   [        S5      nUR                  UR                  UR                  UR                  S.n[        U[        R                  [        [        45      (       d  [        S5      e[        U[        R                  [        [        [        45      (       d  [        S5      e[        U[        5      (       d  [        S5      e[        U[        5      (       a)  [        U5      S;  a  [        S[        U5       S3-   5      eUS;   d   S	5       e[        U[        5      (       a  [        U5      n[        U[        5      (       a  U=n=n=p[        U[        5      (       a  [        U5      S
:X  a  US   =pgUS   =p[        U[        5      (       a#  [        U5      S:X  a  US   nUS   nUS
   nUS   n	[        U R                   5      S:X  aB  U R                   S
   S:X  a/  UR#                  U WW	WWXS   US9SS2SS2[$        R&                  4   $ UR#                  U WW	WWXS   US9$ )a  
Pads the given numpy.array on all sides with specified padding mode and fill value.

Args:
    img (np.array): Image to be padded.
    padding (int|list|tuple): Padding on each border. If a single int is provided this
        is used to pad all borders. If list/tuple of length 2 is provided this is the padding
        on left/right and top/bottom respectively. If a list/tuple of length 4 is provided
        this is the padding for the left, top, right and bottom borders
        respectively.
    fill (float, optional): Pixel fill value for constant fill. If a tuple of
        length 3, it is used to fill R, G, B channels respectively.
        This value is only used when the padding_mode is constant. Default: 0.
    padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default: 'constant'.

        - constant: pads with a constant value, this value is specified with fill

        - edge: pads with the last value on the edge of the image

        - reflect: pads with reflection of image (without repeating the last value on the edge)

                   padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
                   will result in [3, 2, 1, 2, 3, 4, 3, 2]

        - symmetric: pads with reflection of image (repeating the last value on the edge)

                     padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
                     will result in [2, 1, 1, 2, 3, 4, 4, 3]

Returns:
    np.array: Padded image.

r   )constantedgereflect	symmetriczGot inappropriate padding argzGot inappropriate fill argz"Got inappropriate padding_mode arg)r	      z9Padding must be an int or a 2, or 4 element tuple, not a z element tuplezBPadding mode should be either constant, edge, reflect or symmetricr	   r   r
   r@   r'   )topbottomleftright
borderTypevalueN)r   BORDER_CONSTANTBORDER_REPLICATEBORDER_REFLECT_101BORDER_REFLECTr-   numbersNumberlisttupler0   strr   r/   r   r.   r1   copyMakeBorderr   r3   )
r   paddingfillpadding_moder   _cv2_pad_from_strpad_left	pad_rightpad_top
pad_bottoms
             r   padrY   }   s:   D U
C''$$))''	 ge<==788dW^^S$>??455lC((<=='8$$WV)CGWn-.
 	

    L
 LL  '4  .'36===9=w'8$$W):&qz)&qz)'8$$W):1:!*AJ	QZ

399~syy|q0!!(6 " 
 Q


 	 !!(6 " 
 	
r   c                 $    XX-   2X"U-   2SS24   $ )a  Crops the given image.

Args:
    img (np.array): Image to be cropped. (0,0) denotes the top left
        corner of the image.
    top (int): Vertical component of the top left corner of the crop box.
    left (int): Horizontal component of the top left corner of the crop box.
    height (int): Height of the crop box.
    width (int): Width of the crop box.

Returns:
    np.array: Cropped image.

N )r   rA   rC   heightwidths        r   cropr^      s"      S\!4,#69::r   c                    [        U[        R                  5      (       a  [        U5      [        U5      4nU R                  SS u  p#Uu  pE[        [        X$-
  S-  5      5      n[        [        X5-
  S-  5      5      n[        XXtU5      $ )a  Crops the given image and resize it to desired size.

Args:
    img (np.array): Image to be cropped. (0,0) denotes the top left corner of the image.
    output_size (sequence or int): (height, width) of the crop box. If int,
        it is used for both directions
    backend (str, optional): The image process backend type. Options are `pil`, `cv2`. Default: 'pil'.

Returns:
    np.array: Cropped image.

r   r	          @)r-   rK   rL   r.   r1   roundr^   )r   output_sizer6   r7   thtwijs           r   center_croprg      sz     +w~~..;'[)9:99Qq>DAFBE16S.!"AE16S.!"Ar""r   c                 <    [        S5      nUR                  U S5      $ )zHorizontally flips the given image.

Args:
    img (np.array): Image to be flipped.

Returns:
    np.array:  Horizontally flipped image.

r   r
   )r   flipr   r   s     r   hfliprk   	  s     U
C88Cr   c                     [        S5      n[        U R                  5      S:X  a=  U R                  S   S:X  a*  UR                  U S5      SS2SS2[        R
                  4   $ UR                  U S5      $ )zVertically flips the given np.array.

Args:
    img (np.array): Image to be flipped.

Returns:
    np.array:  Vertically flipped image.

r   r'   r	   r
   r   N)r   r/   r1   ri   r   r3   rj   s     r   vfliprm     s_     U
C
399~syy|q0xxQ1bjj 011xxQr   c                    [        S5      n[        R                  " [        SS5       Vs/ s H  o3U-  PM	     sn5      R	                  SS5      R                  S5      n[        U R                  5      S:X  a<  U R                  S   S:X  a)  UR                  X5      S	S	2S	S	2[        R                  4   $ UR                  X5      $ s  snf )
a]  Adjusts brightness of an image.

Args:
    img (np.array): 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:
    np.array: Brightness adjusted image.

r   r         r   r'   r	   r
   N
r   r   arrayrangeclipastyper/   r1   LUTr3   )r   brightness_factorr   re   tables        r   adjust_brightnessry   *  s     U
C 	q#?A''?@	a	 
 399~syy|q0wws"1a#344wws"" @s   Cc                    [        S5      n[        R                  " [        SS5       Vs/ s H  o3S-
  U-  S-   PM     sn5      R	                  SS5      R                  S5      n[        U R                  5      S:X  a<  U R                  S   S	:X  a)  UR                  X5      S
S
2S
S
2[        R                  4   $ UR                  X5      $ s  snf )aW  Adjusts contrast of an image.

Args:
    img (np.array): 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:
    np.array: Contrast adjusted image.

r   r   ro   J   rp   r   r'   r	   r
   Nrq   )r   contrast_factorr   re   rx   s        r   adjust_contrastr}   E  s     U
C 	5C=I=ar6_,r1=IJ	a	 

 399~syy|q0wws"1a#344wws"" Js   C
c                    [        S5      nU R                  nU R                  [        R                  5      n [        R
                  R                  [        SSU-
  5      SU-   5      nUR                  XR                  5      nUS[        R                  4   nX-  USU-
  -  -   n U R                  SS5      R                  U5      $ )aX  Adjusts color saturation of an image.

Args:
    img (np.array): 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:
    np.array: Saturation adjusted image.

r   r   r
   .rp   )r   r   ru   r   r   randomuniformmaxcvtColorCOLOR_BGR2GRAYr3   rt   )r   saturation_factorr   r   alphagray_imgs         r   adjust_saturationr   _  s     U
CIIE
**RZZ
 CIIAq$$%q+<'<E ||C!3!34HRZZ(H
+AI.
.C88As""5))r   c                 *   [        S5      nSUs=::  a  S::  d  O  [        SU S35      eU R                  nU R                  [        R
                  5      n UR                  XR                  5      nUR                  U5      u  pVnUnUR                  [        R                  5      nU[        US-  5      -   S-  nUR                  [        R
                  5      nUR                  XVU/5      nUR                  XBR                  5      R                  U5      $ )a  Adjusts 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]`.

Args:
    img (np.array): 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:
    np.array: Hue adjusted image.

r   g            ?zhue_factor:z is not in [-0.5, 0.5].rp   ro   )r   r   r   ru   r   r   r   COLOR_BGR2HSV_FULLsplitint32r.   mergeCOLOR_HSV2BGR_FULL)	r   
hue_factorr   r   hsv_imgr6   svr   s	            r   
adjust_huer   y  s    , U
CJ%#%;zl2IJKKIIE
**RXX
Cll3 6 67Gii GA!E	A	
S	$A	Aiiq	"G<<!7!78??FFr   c                 R   [        S5      nUR                  UR                  UR                  UR                  UR
                  S.n	U R                  SS u  p[        U[        5      (       a  [        U/S-  5      nUc
  US-  U
S-  4n[        R                  " SS/5      nUR                  XUS9n[        R                  " US   [        R                  -  S	-  5      n[        R                  " US
   [        R                  -  S	-  5      nUS   XS
   -  -   US'   US
   XS   -  -   US
'   Uu  nnUUS'   UUS'   [!        U R                  5      S:X  aA  U R                  S   S
:X  a.  UR#                  U UX4X   US9SS2SS2[        R$                  4   $ UR#                  U UX4X   US9$ )a]  Affine the image by matrix.

Args:
    img (PIL.Image): Image to be affined.
    translate (sequence or int): horizontal and vertical translations
    scale (float): overall scale ratio
    shear (sequence or float): shear angle value in degrees between -180 to 180, clockwise direction.
        If a sequence is specified, the first value corresponds to a shear parallel to the x axis, while
        the second value corresponds to a shear parallel to the y axis.
    interpolation (int|str, optional): Interpolation method. If omitted, or if the
        image has only one channel, it is set to cv2.INTER_NEAREST.
        when use cv2 backend, support method are as following:
        - "nearest": cv2.INTER_NEAREST,
        - "bilinear": cv2.INTER_LINEAR,
        - "bicubic": cv2.INTER_CUBIC
    fill (3-tuple or int): RGB pixel fill value for area outside the affined image.
        If int, it is used for all channels respectively.
    center (sequence, optional): Optional center of rotation. Origin is the upper left corner.
        Default is the center of the image.

Returns:
    np.array: Affined image.

r   r   r   r	   r'   Nr`   )anglecenterscale   r
   r   r	   r
   r	   r%   flagsborderValue)r   r(   r)   r*   r+   r,   r1   r-   r.   rN   r   onesgetRotationMatrix2Dmathtanpir/   
warpAffiner3   )r   r   	translater   shearr&   rR   r   r   r5   r6   r7   MRsxsytxtys                     r   affiner     s   D U
C$$$$??%% 99Qq>DA$dVaZ ~c'1s7#
AAe%HA 
%(TWW$s*	+B	%(TWW$s*	+BQ4"t)AaDQ4"t)AaD FBAdGAdG
399~syy|q0~~&&5  
 Q


 	 ~~&&5  
 	
r   c           	         [        S5      nUR                  UR                  UR                  UR                  UR
                  S.nU R                  SS u  pUc
  U	S-  US-  4nUR                  XAS5      n
U(       Ga  S n/ n/ n[        R                  " U5      * n[        [        R                  " U5      S	5      [        [        R                  " U5      S	5      S
[        [        R                  " U5      * S	5      [        [        R                  " U5      S	5      S
/nSnU" US   * US   -
  US   * US   -
  U5      u  US'   US'   US==   US   -  ss'   US==   US   -  ss'   SU	S4X4SU44 H5  u  nnU" UUU5      u  nnUR                  U5        UR                  U5        M7     [        R                  " [        U5      5      [        R                   " [#        U5      5      -
  n[        R                  " [        U5      5      [        R                   " [#        U5      5      -
  nU
S==   UU	-
  S-  -  ss'   U
S==   UU-
  S-  -  ss'   [%        U5      [%        U5      p['        U R                  5      S:X  aA  U R                  S   S:X  a.  UR)                  U U
X4Xr   US9SS2SS2[*        R,                  4   $ UR)                  U U
X4Xr   US9$ )am  Rotates the image by angle.

Args:
    img (np.array): Image to be rotated.
    angle (float or int): In degrees degrees counter clockwise order.
    interpolation (int|str, optional): Interpolation method. If omitted, or if the
        image has only one channel, it is set to cv2.INTER_NEAREST.
        when use cv2 backend, support method are as following:
        - "nearest": cv2.INTER_NEAREST,
        - "bilinear": cv2.INTER_LINEAR,
        - "bicubic": cv2.INTER_CUBIC
    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.
    fill (3-tuple or int): RGB pixel fill value for area outside the rotated image.
        If int, it is used for all channels respectively.

Returns:
    np.array: Rotated image.

r   r   r   r	   Nr`   r
   c                 >    Uu  p4pVpxX0-  XA-  -   U-   X`-  Xq-  -   U-   4$ )Nr[   )	xymatrixabcdefs	            r   	transformrotate.<locals>.transform!  s3    !'Q1515=1$aeaema&777r      g        )r   r      r   r   r   r'   )r   r   )r   r(   r)   r*   r+   r,   r1   r   r   radiansra   cossinappendceilr   floorminr.   r/   r   r   r3   )r   r   r&   expandr   rR   r   r5   r6   r7   r   r   xxyyexpand_matrix
post_transr   r   nwnhs                       r   rotater     s   8 U
C$$$$??%% 99Qq>DA~c'1s7#q1A	8
 e$$$((5/2&$((5/2&488E?"B'$((5/2&
 
-6AYJA&AYJA&.
*a-*
 	aF1I%aF1I%aVaVaV4DAqQ=1DAqIIaLIIaL 5 YYs2w$**SW"55YYs2w$**SW"55	$BFc>!	$BFc>!2wB1
399~syy|q0~~F&5  
 Q


 	 ~~F&5  
 	
r   c                    [        S5      nUR                  UR                  UR                  UR                  UR
                  S.nU R                  SS u  px[        R                  " USS9n[        R                  " USS9nUR                  X5      n	[        U R                  5      S:X  aA  U R                  S   S:X  a.  UR                  U U	X4Xc   US	9S
S
2S
S
2[        R                  4   $ UR                  U U	X4Xc   US	9$ )a)  Perspective the image.

Args:
    img (np.array): Image to be perspectived.
    startpoints (list[list[int]]): [top-left, top-right, bottom-right, bottom-left] of the original image,
    endpoints (list[list[int]]): [top-left, top-right, bottom-right, bottom-left] of the transformed image.
    interpolation (int|str, optional): Interpolation method. If omitted, or if the
        image has only one channel, it is set to cv2.INTER_NEAREST.
        when use cv2 backend, support method are as following:
        - "nearest": cv2.INTER_NEAREST,
        - "bilinear": cv2.INTER_LINEAR,
        - "bicubic": cv2.INTER_CUBIC
    fill (3-tuple or int): RGB pixel fill value for area outside the rotated image.
        If int, it is used for all channels respectively.

Returns:
    np.array: Perspectived image.

r   r   r   r	   r   )r   r'   r
   r   N)r   r(   r)   r*   r+   r,   r1   r   rr   getPerspectiveTransformr/   warpPerspectiver3   )
r   startpoints	endpointsr&   rR   r   r5   r6   r7   r   s
             r   perspectiver   Z  s   ( U
C$$$$??%% 99Qq>DA((;i8K)4I((@F
399~syy|q0""&&5 # 
 Q


 	 ""&&5 # 
 	
r   c                 X   [        S5      nUS:X  a5  UR                  XR                  5      SS2SS2[        R                  4   n U $ US:X  aT  [        R
                  " UR                  XR                  5      SS2SS2[        R                  4   U R                  5      n U $ [        S5      e)a=  Converts image to grayscale version of image.

Args:
    img (np.array): Image to be converted to grayscale.

Returns:
    np.array: 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

r   r
   Nr'   z+num_output_channels should be either 1 or 3)r   r   COLOR_RGB2GRAYr   r3   broadcast_tor1   r   )r   num_output_channelsr   s      r   to_grayscaler     s     U
Call3 2 23Aq"**4DE J 
	!ooLL001!Q

2BCSYY
 J FGGr   c                    US:X  aw  [         R                  " [         R                  " U5      R                  SSS5      5      n[         R                  " [         R                  " U5      R                  SSS5      5      nOv[         R                  " [         R                  " U5      R                  SSS5      5      n[         R                  " [         R                  " U5      R                  SSS5      5      nU(       a
  U SSSS24   n X-
  U-  n U $ )a  Normalizes a ndarray image or image with mean and standard deviation.

Args:
    img (np.array): input data to be normalized.
    mean (list|tuple): Sequence of means for each channel.
    std (list|tuple): Sequence of standard deviations for each channel.
    data_format (str, optional): Data format of img, should be 'HWC' or
        'CHW'. Default: 'CHW'.
    to_rgb (bool, optional): Whether to convert to rgb. Default: False.

Returns:
    np.array: Normalized mage.

r   r
   .N)r   r   rr   reshape)r   meanstdr   to_rgbs        r   	normalizer     s      ezz"((4.00Q:;jj#..r1a89zz"((4.00Ar:;jj#..q!R89#tt)n:
CJr   c                 R    U(       d  U R                  5       n XPXU-   2X"U-   2S4'   U $ )a.  Erase the pixels of selected area in input image array with given value.

Args:
     img (np.array): input image array, which shape is (H, W, C).
     i (int): y coordinate of the top-left point of erased region.
     j (int): x coordinate of the top-left point of erased region.
     h (int): Height of the erased region.
     w (int): Width of the erased region.
     v (np.array): value used to replace the pixels in erased region.
     inplace (bool, optional): Whether this transform is inplace. Default: False.

 Returns:
     np.array: Erased image.

.)copy)r   re   rf   r6   r7   r   inplaces          r   eraser     s2      hhj%&E	11u9c!"Jr   )r   )r!   )r   r<   )r    r   N)r    FNr   )r    r   )r
   )r   F)F)r   rK   collections.abcr   r   numpyr   r   paddle.utilsr   __all__r   r2   rY   r^   rg   rk   rm   ry   r}   r   r   r   r   r   r   r   r   r[   r   r   <module>r      s      .   #
@?D^
B;&#0 $#6#4*4%G\ 	
Q
j JKb
J1
h8<r   