
    j                        d dl Z d dlZd dlmZ d dlmZ d dlZd dlm	Z	 ddl
mZ g dZ G d d	e	          Z G d
 de	          Z G d de	          Z G d de	          Z G d de	          Z G d de	          Z G d de	          ZdS )    N)Callable)Any)NestedObject   )
functional)SampleComposeImageTransformColorInversionOneOfRandomApplyRandomRotate
RandomCropc                       e Zd ZU dZdgZee         ed<   deee	e	ge
e	e	f         f                  ddfdZde	d	e	de
e	e	f         fd
ZdS )r   a  Implements a wrapper that will apply transformations sequentially on both image and target

    .. code:: python

        >>> import numpy as np
        >>> import torch
        >>> from doctr.transforms import SampleCompose, ImageTransform, ColorInversion, RandomRotate
        >>> transfos = SampleCompose([ImageTransform(ColorInversion((32, 32))), RandomRotate(30)])
        >>> out, out_boxes = transfos(torch.rand(8, 64, 64, 3), np.zeros((2, 4)))

    Args:
        transforms: list of transformation modules
    sample_transforms_children_names
transformsreturnNc                     || _         d S Nr   selfr   s     `/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/transforms/modules/base.py__init__zSampleCompose.__init__%   s    !+    xtargetc                 <    | j         D ]} |||          \  }}||fS r   r   )r   r   r   ts       r   __call__zSampleCompose.__call__(   s3    ' 	% 	%A!VIAvv&yr   )__name__
__module____qualname____doc__r   liststr__annotations__r   r   tupler   r     r   r   r   r      s           #6!6OT#Y666,4#sU38_1L(M#N ,SW , , , ,# s uS#X      r   r   c                   v    e Zd ZU dZdgZee         ed<   dee	ge	f         ddfdZ
de	d	e	dee	e	f         fd
ZdS )r	   a  Implements a transform wrapper to turn an image-only transformation into an image+target transform

    .. code:: python

        >>> import torch
        >>> from doctr.transforms import ImageTransform, ColorInversion
        >>> transfo = ImageTransform(ColorInversion((32, 32)))
        >>> out, _ = transfo(torch.rand(8, 64, 64, 3), None)

    Args:
        transform: the image transformation module to wrap
    img_transformr   	transformr   Nc                     || _         d S r   r+   )r   r,   s     r   r   zImageTransform.__init__?   s    &r   imgr   c                 4    |                      |          }||fS r   r.   r   r/   r   s      r   r    zImageTransform.__call__B   s      %%F{r   )r!   r"   r#   r$   r   r%   r&   r'   r   r   r   r(   r    r)   r   r   r	   r	   /   s           #2!2OT#Y222'(C5#:"6 '4 ' ' ' 'C  sCx      r   r	   c                   @    e Zd ZdZd
deddfdZdefdZdedefd	Z	dS )r
   a  Applies the following tranformation to a tensor (image or batch of images):
    convert to grayscale, colorize (shift 0-values randomly), and then invert colors

    .. code:: python

        >>> import torch
        >>> from doctr.transforms import ColorInversion
        >>> transfo = ColorInversion(min_val=0.6)
        >>> out = transfo(torch.rand(8, 64, 64, 3))

    Args:
        min_val: range [min_val, 1] to colorize RGB pixels
          ?min_valr   Nc                     || _         d S r   r4   )r   r4   s     r   r   zColorInversion.__init__V   s    r   c                     d| j          S )Nzmin_val=r6   r   s    r   
extra_reprzColorInversion.extra_reprY   s    ($,(((r   r/   c                 6    t          j        || j                  S r   )Finvert_colorsr4   )r   r/   s     r   r    zColorInversion.__call__\   s    sDL111r   r3   )
r!   r"   r#   r$   floatr   r&   r9   r   r    r)   r   r   r
   r
   G   s               )C ) ) ) )2C 2C 2 2 2 2 2 2r   r
   c            
           e Zd ZU dZdgZee         ed<   deee	ge	f                  ddfdZ
d
de	dej        dz  de	ee	ej        f         z  fd	ZdS )r   aM  Randomly apply one of the input transformations

    .. code:: python

        >>> import torch
        >>> from doctr.transforms import OneOf
        >>> transfo = OneOf([JpegQuality(), Gamma()])
        >>> out = transfo(torch.rand(1, 64, 64, 3))

    Args:
        transforms: list of transformations, one only will be picked
    r   r   r   Nc                     || _         d S r   )r   r   s     r   r   zOneOf.__init__p   s    $r   r/   r   c                     | j         t          t          j                    t          | j                   z                     }| ||          n |||          S r   )r   intrandomlen)r   r/   r   transfos       r   r    zOneOf.__call__s   sM    /#fmooDO8L8L&L"M"MN%~wws|||7733G3GGr   r   )r!   r"   r#   r$   r   r%   r&   r'   r   r   r   npndarrayr(   r    r)   r   r   r   r   `   s           #/OT#Y///%4#(<#= %$ % % % %H HC Hd): HcERUWYWaRaLbFb H H H H H Hr   r   c            
           e Zd ZdZddeegef         deddfdZdefdZ	dd	ed
e
j        dz  deeee
j        f         z  fdZdS )r   aX  Apply with a probability p the input transformation

    .. code:: python

        >>> import torch
        >>> from doctr.transforms import RandomApply
        >>> transfo = RandomApply(Gamma(), p=.5)
        >>> out = transfo(torch.rand(1, 64, 64, 3))

    Args:
        transform: transformation to apply
        p: probability to apply
    r3   r,   pr   Nc                 "    || _         || _        d S r   r,   rI   )r   r,   rI   s      r   r   zRandomApply.__init__   s    "r   c                 &    d| j          d| j         S )Nz
transform=z, p=rK   r8   s    r   r9   zRandomApply.extra_repr   s    8DN88888r   r/   r   c                     t          j                     | j        k     r-||                     |          n|                     ||          S ||n||fS r   )rC   rI   r,   r1   s      r   r    zRandomApply.__call__   sQ    =??TV##*0.4>>#&&&dnnSRX>Y>YYnss3-7r   r=   r   )r!   r"   r#   r$   r   r   r>   r   r&   r9   rF   rG   r(   r    r)   r   r   r   r   z   s          (C5#:"6 5 4    9C 9 9 9 98 8C 8d): 8cERUWYWaRaLbFb 8 8 8 8 8 8r   r   c                   l    e Zd ZdZddededdfdZdefd	Zd
e	de
j        dee	e
j        f         fdZdS )r   aq  Randomly rotate a tensor image and its boxes

    .. image:: https://doctr-static.mindee.com/models?id=v0.4.0/rotation_illustration.png&src=0
        :align: center

    Args:
        max_angle: maximum angle for rotation, in degrees. Angles will be uniformly picked in [-max_angle, max_angle]
        expand: whether the image should be padded before the rotation
          @F	max_angleexpandr   Nc                 "    || _         || _        d S r   rP   rQ   )r   rP   rQ   s      r   r   zRandomRotate.__init__   s    "r   c                 &    d| j          d| j         S )Nz
max_angle=z	, expand=rS   r8   s    r   r9   zRandomRotate.extra_repr   s    BDNBBT[BBBr   r/   r   c                    t          j        | j         | j                  }t          j        |||| j                  \  }}|                    d          |                    d          k                        d          dk    }|||         fS )N   r   )	rC   uniformrP   r;   rotate_samplerQ   maxminsum)r   r/   r   angler_imgr_polysis_kepts          r   r    zRandomRotate.__call__   su    ??feT[IIw;;q>>GKKNN277::a?gg&&&r   )rO   F)r!   r"   r#   r$   r>   boolr   r&   r9   r   rF   rG   r(   r    r)   r   r   r   r      s          % t     CC C C C C'C ' 'c2:o8N ' ' ' ' ' 'r   r   c                       e Zd ZdZddeeef         deeef         ddfdZdefd	Zd
e	de
j        dee	e
j        f         fdZdS )r   zRandomly crop a tensor image and its boxes

    Args:
        scale: tuple of floats, relative (min_area, max_area) of the crop
        ratio: tuple of float, relative (min_ratio, max_ratio) where ratio = h/w
    g{Gz?g      ?g      ?gHzG?scaleratior   Nc                 "    || _         || _        d S r   rd   re   )r   rd   re   s      r   r   zRandomCrop.__init__   s    



r   c                 &    d| j          d| j         S )Nzscale=z, ratio=rg   r8   s    r   r9   zRandomCrop.extra_repr   s    8
88DJ888r   r/   r   c                    t          j        | j        d         | j        d                   }t          j        | j        d         | j        d                   }|j        dd          \  }}||z  |z  }|||z  z  }t          t          t          j        ||z                                }	t          t          t          j        ||z                                }
t          |	|          }	t          |
|          }
t          j
        d||	z
            }t          j
        d||
z
            }||z  ||z  ||	z   |z  ||
z   |z  f}|j        dd          dk    rEt          j	        |d          }t          j        |d          }t          j        ||fd          }n|}t          j        |||          \  }}|j        d         dk    r||fS |t          j        |dd          fS )Nr   rV   )   r   )axis)rC   rW   rd   re   shaperB   roundmathsqrtrZ   randintrF   rY   concatenater;   crop_detectionclip)r   r/   r   rd   re   heightwidth	crop_areaaspect_ratio
crop_widthcrop_heightr   ycrop_boxmin_xymax_xy_target
croped_img
crop_boxess                      r   r    zRandomCrop.__call__   s   tz!}djm<<tz!}djm<<	"## EMF*	/ty\)ABBCCDD
%	)l*B C CDDEE U++
+v.. N1ej011N1f{233 Iq6zA
Ne+Ca+oY_E_`<v%%VF+++FVF+++Fnff%5A>>>GGG "#!1#w!I!I
JA!##;27:q!4444r   )rb   rc   )r!   r"   r#   r$   r(   r>   r   r&   r9   r   rF   rG   r    r)   r   r   r   r      s          eE5L1 eUZlH[ os    9C 9 9 9 9#5C #5 #5c2:o8N #5 #5 #5 #5 #5 #5r   r   )ro   rC   collections.abcr   typingr   numpyrF   doctr.utils.reprr    r   r;   __all__r   r	   r
   r   r   r   r   r)   r   r   <module>r      s     $ $ $ $ $ $           ) ) ) ) ) )      
u
u
u    L   6    \   02 2 2 2 2\ 2 2 22H H H H HL H H H48 8 8 8 8, 8 8 88' ' ' ' '< ' ' '425 25 25 25 25 25 25 25 25 25r   