
    j                     |   d dl mZ d dlZd dlZd dlmZ d dlmZ	 d dl
mZ ddlmZmZ g dZdd
ej        dedej        fdZ	 dd
ej        dej        dededeej        ej        f         f
dZd
ej        dej        deeeeef         deej        ej        f         fdZd
ej        deeef         dej        fdZdS )    )deepcopyN)gaussian_filter)
functional)rotate_abs_geoms   )create_shadow_mask
crop_boxes)invert_colorsrotate_samplecrop_detectionrandom_shadow333333?imgmin_valreturnc                    t          j        | d          }| j        dk    r| j        d         dddgng d}|d|z
  t	          j        |          z  z   }|j        t          j        k    r=|                    |j                  |z                      t          j                  }n||                    |j                  z  }|j        t          j        k    rd|z
  nd|z
  }|S )	zInvert the colors of an image

    Args:
        img : torch.Tensor, the image to invert
        min_val : minimum value of the random shift

    Returns:
        the inverted image
       )num_output_channels   r   r   )r   r   r   )dtype   )	Frgb_to_grayscalendimshapetorchrandr   uint8to)r   r   outshift_shape	rgb_shifts        f/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/transforms/functional/pytorch.pyr
   r
      s     
Sa
8
8
8C-0X]]39Q<Aq))			K1w;%*[*A*AAAI
yEKvvIOv,,y8<<5;<OOILLsyL111yEK//#))QWCJ    Fgeomsangleexpandc                 V   t          j        | |d|          }|dd         }t          |          }|j        dd         dk    r[t	          j        |          dk    rB|ddddgfxx         | j        d         z  cc<   |ddddgfxx         | j        d	         z  cc<   nq|j        dd         d
k    rOt	          j        |          dk    r6|dxx         | j        d         z  cc<   |dxx         | j        d	         z  cc<   nt          d          t          ||| j        dd         |                              t          j	                  }|d         |j        d         z  |d<   |d         |j        d         z  |d<   |t	          j
        t	          j        |d          dd          fS )a  Rotate image around the center, interpolation=NEAREST, pad with 0 (black)

    Args:
        img: image to rotate
        geoms: array of geometries of shape (N, 4) or (N, 4, 2)
        angle: angle in degrees. +: counter-clockwise, -: clockwise
        expand: whether the image should be padded before the rotation

    Returns:
        A tuple of rotated img (tensor), rotated geometries of shape (N, 4, 2)
    r   )r&   fillr'   Nr   r   )r      )r   r*   ).r   ).r   zinvalid format for arg `geoms`   )decimals)r   rotater   r   npmaxAssertionErrorr   astypefloat32cliparound)r   r%   r&   r'   rotated_img_geomsrotated_geomss          r#   r   r   ,   s   " (3e!FCCCKbqb/Ke__F|ABB46&>>Q111q!f92.111q!f92.	abb	V	#	#6&>>Q6NNNcim+NNN6NNNcim+NNN=>>> !1	!""	! !
 fRZ  *&1K4Ea4HHM&)&1K4Ea4HHM&	-" E E Eq!LLLLr$   boxescrop_boxc                 V   t          d |D                       rt          d          | j        dd         \  }}t          t	          |d         |dz
  z                      t          t	          |d         |dz
  z                      }}t          t	          |d         |dz
  z                      t          t	          |d         |dz
  z                      }}t          j        | ||||z
  ||z
            }	t          ||                                dk    r|n||||f          }|	|fS )	aK  Crop and image and associated bboxes

    Args:
        img: image to crop
        boxes: array of boxes to clip, absolute (int) or relative (float)
        crop_box: box (xmin, ymin, xmax, ymax) to crop the image. Relative coords.

    Returns:
        A tuple of cropped image, cropped boxes, where the image is not resized.
    c              3   .   K   | ]}|d k     p|dk    V  dS )r   r   N ).0vals     r#   	<genexpr>z!crop_detection.<locals>.<genexpr>h   s/      
2
2#37cAg
2
2
2
2
2
2r$   z0coordinates of arg `crop_box` should be relativer,   Nr   r   r*   r   )	anyr2   r   introundr   cropr	   r1   )
r   r:   r;   hwxminyminxmaxymaxcropped_imgs
             r#   r   r   [   s'    
2
2
2
2
222 QOPPP9RSS>DAqU8A;!a%01122ChqkQQRU>S8T8T4U4U$DU8A;!a%01122ChqkQQRU>S8T8T4U4U$D&dD$+td{CCKu%))++*:*:hhtTSW@XYYEr$   opacity_rangec                    t          | j        dd         fi |}t          j        j        | }t          j                            dd          }t          ||          }dt          j        |                                          z
  }|	                    | j
                                      d          }||z  | z  d|z
  | z  z   S )at  Apply a random shadow effect to an image using NumPy for blurring.

    Args:
        img: Image to modify (C, H, W) as a PyTorch tensor.
        opacity_range: The minimum and maximum desired opacity of the shadow.
        **kwargs: Additional arguments to pass to `create_shadow_mask`.

    Returns:
        Shadowed image as a PyTorch tensor (same shape as input).
    r   Ng      ?g      @)sigmar   )r   r   r0   randomuniformr   r   
from_numpyfloatr   device	unsqueeze)r   rM   kwargsshadow_maskopacityrO   blurred_maskshadow_tensors           r#   r   r   t   s     %SYqrr]==f==Ki/G Ic3''E";e<<<L(66<<>>>M!$$SZ00::1==M]"S(AK3+>>>r$   )r   )F)copyr   numpyr0   r   scipy.ndimager   torchvision.transformsr   r   doctr.utils.geometryr   baser   r	   __all__TensorrS   r
   ndarraybooltupler   r   r   r>   r$   r#   <module>rf      s              ) ) ) ) ) ) 2 2 2 2 2 2 1 1 1 1 1 1 0 0 0 0 0 0 0 0
O
O
O u| e el    8 	,M ,M	,M:,M ,M 	,M
 5<#$,M ,M ,M ,M^	 j49%u:T4U
5<#$   2?u| ?E%,4G ?V[Vb ? ? ? ? ? ?r$   