
    haG                        S r SSKJr  SSKJrJrJr  SSKrSSK	J
r
  SSKJr  SSKJr  SSKJrJrJrJrJr  SSKJr  SS	KJrJrJr  SS
KJr  SSKJrJ r   SSK!J"r"J#r#  S/r$ " S S\ 5      r% " S S\ 5      r&g)a  Transform classes for dropout-based augmentations.

This module contains transform classes for various dropout techniques used in image
augmentation. It provides the base dropout class and specialized implementations like
PixelDropout. These transforms randomly remove or modify pixels, channels, or regions
in images, which can help models become more robust to occlusions and missing information.
    )annotations)AnyLiteralcastN)get_num_channels)Field)
functional)cutoutcutout_on_volumecutout_on_volumesfilter_bboxes_by_holesfilter_keypoints_in_holes)BboxProcessordenormalize_bboxesnormalize_bboxes)KeypointsProcessor)BaseTransformInitSchemaDualTransform)ALL_TARGETSTargetsPixelDropoutc                     ^  \ rS rSr% Sr\rS\S'    " S S\5      r	      SU 4S jj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SS jr        SS jr        SS jrSS jrSrU =r$ )BaseDropout"   a<  Base class for dropout-style transformations.

This class provides common functionality for various dropout techniques,
including applying cutouts to images and masks.

Args:
    fill (tuple[float, ...] | float | Literal["random", "random_uniform", "inpaint_telea", "inpaint_ns"]):
        Value to fill dropped regions.
    fill_mask (tuple[float, ...] | float | None): Value to fill
        dropped regions in the mask. If None, the mask is not modified.
    p (float): Probability of applying the transform.

Targets:
    image, mask, bboxes, keypoints, volume, mask3d

Image types:
    uint8, float32

Examples:
    >>> import numpy as np
    >>> import albumentations as A
    >>>
    >>> # Example of a custom dropout transform inheriting from BaseDropout
    >>> class CustomDropout(A.BaseDropout):
    ...     def __init__(self, num_holes_range=(4, 8), hole_size_range=(10, 20), *args, **kwargs):
    ...         super().__init__(*args, **kwargs)
    ...         self.num_holes_range = num_holes_range
    ...         self.hole_size_range = hole_size_range
    ...
    ...     def get_params_dependent_on_data(self, params, data):
    ...         img = data["image"]
    ...         height, width = img.shape[:2]
    ...
    ...         # Generate random holes
    ...         num_holes = self.py_random.randint(*self.num_holes_range)
    ...         hole_sizes = self.py_random.randint(*self.hole_size_range, size=num_holes)
    ...
    ...         holes = []
    ...         for i in range(num_holes):
    ...             # Random position for each hole
    ...             x1 = self.py_random.randint(0, max(1, width - hole_sizes[i]))
    ...             y1 = self.py_random.randint(0, max(1, height - hole_sizes[i]))
    ...             x2 = min(width, x1 + hole_sizes[i])
    ...             y2 = min(height, y1 + hole_sizes[i])
    ...             holes.append([x1, y1, x2, y2])
    ...
    ...         # Return holes and random seed
    ...         return {
    ...             "holes": np.array(holes) if holes else np.empty((0, 4), dtype=np.int32),
    ...             "seed": self.py_random.integers(0, 100000)
    ...         }
    >>>
    >>> # Prepare sample data
    >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
    >>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
    >>> bboxes = np.array([[0.1, 0.1, 0.4, 0.4], [0.6, 0.6, 0.9, 0.9]])
    >>>
    >>> # Create a transform with custom dropout
    >>> transform = A.Compose([
    ...     CustomDropout(
    ...         num_holes_range=(3, 6),       # Generate 3-6 random holes
    ...         hole_size_range=(5, 15),      # Holes of size 5-15 pixels
    ...         fill=0,                       # Fill holes with black
    ...         fill_mask=1,                  # Fill mask holes with 1
    ...         p=1.0                         # Always apply for this example
    ...     )
    ... ], bbox_params=A.BboxParams(format='yolo', min_visibility=0.3))
    >>>
    >>> # Apply the transform
    >>> transformed = transform(image=image, mask=mask, bboxes=bboxes)
    >>>
    >>> # Get the transformed data
    >>> dropout_image = transformed["image"]      # Image with random holes filled with 0
    >>> dropout_mask = transformed["mask"]        # Mask with same holes filled with 1
    >>> dropout_bboxes = transformed["bboxes"]    # Bboxes filtered by visibility threshold

ztuple[Targets, ...] | Targets_targetsc                  *    \ rS rSr% S\S'   S\S'   Srg)BaseDropout.InitSchemas   ^tuple[float, ...] | float | Literal['random', 'random_uniform', 'inpaint_telea', 'inpaint_ns']fill tuple[float, ...] | float | None	fill_mask N)__name__
__module____qualname____firstlineno____annotations____static_attributes__r#       i/var/www/fran/franai/venv/lib/python3.13/site-packages/albumentations/augmentations/dropout/transforms.py
InitSchemar   s   s    ll33r*   r,   c                8   > [         TU ]  US9  Xl        X l        g N)p)super__init__r    r"   )selfr    r"   r/   	__class__s       r+   r1   BaseDropout.__init__w   s     	1	"r*   c                    UR                   S:X  a  U$ U R                  S;   a  [        U5      nUS;  a  [        S5      e[	        XU R                  [
        R                  R                  U5      5      $ )Nr      
inpaint_nsinpaint_telea         /Inpainting works only for 1 or 3 channel images)sizer    r   
ValueErrorr
   nprandomdefault_rng)r2   imgholesseedparamsnum_channelss         r+   applyBaseDropout.apply   s_    ::?J9977+C0L6) !RSSc$))RYY-B-B4-HIIr*   c                   UR                   S:X  a  U$ U R                  S;   a2  UR                  S:X  a  UR                  S   OSnUS;  a  [	        S5      e[        XU R                  [        R                  R                  U5      5      $ )Nr   r6      r;   r:   r9   r<   )	r=   r    ndimshaper>   r   r?   r@   rA   )r2   imagesrC   rD   rE   rF   s         r+   apply_to_imagesBaseDropout.apply_to_images   sp    ::?M9977.4kkQ.>6<<?AL6) !RSStyy")):O:OPT:UVVr*   c                *    U R                   " XU40 UD6$ )N)rN   )r2   volumerC   rD   rE   s        r+   apply_to_volumeBaseDropout.apply_to_volume   s     ##F4B6BBr*   c                   UR                   S:X  a  U$ U R                  S;   a2  UR                  S:X  a  UR                  S   OSnUS;  a  [	        S5      e[        XU R                  [        R                  R                  U5      5      $ )Nr   r6      rJ   r:   r9   r<   )	r=   r    rK   rL   r>   r   r?   r@   rA   )r2   volumesrC   rD   rE   rF   s         r+   apply_to_volumesBaseDropout.apply_to_volumes   sq    ::?N9977/6||q/@7==+aL6) !RSS BII<Q<QRV<WXXr*   c                    U R                   b  UR                  S:X  a  U$ [        XU R                   [        R                  R                  U5      5      $ Nr   )r"   r=   r   r?   r@   rA   r2   maskrC   rD   rE   s        r+   apply_to_mask3dBaseDropout.apply_to_mask3d   s>    >>!UZZ1_KT^^RYY=R=RSW=XYYr*   c                    U R                   b  UR                  S:X  a  U$ [        XU R                   [        R                  R                  U5      5      $ rZ   )r"   r=   r   r?   r@   rA   r[   s        r+   apply_to_masks3dBaseDropout.apply_to_masks3d   s>    >>!UZZ1_K dnnbii>S>STX>YZZr*   c                    U R                   b  UR                  S:X  a  U$ [        XU R                   [        R                  R                  U5      5      $ rZ   )r"   r=   r
   r?   r@   rA   r[   s        r+   apply_to_maskBaseDropout.apply_to_mask   s=    >>!UZZ1_Kd4>>2993H3H3NOOr*   c           
     
   UR                   S:X  a  U$ [        SU R                  S5      5      nUc  U$ US   S S n[        X5      n[	        [        UUUUR                  R                  UR                  R                  S9U5      $ )Nr   r   bboxesrL      )min_areamin_visibility)	r=   r   get_processorr   r   r   rE   rh   ri   )r2   rf   rC   rE   	processorimage_shapedenormalized_bboxess          r+   apply_to_bboxesBaseDropout.apply_to_bboxes   s     ::?M$*<*<X*FG	MWobq)0E"#"))22(//>> 	
 		
r*   c                    UR                   S:X  a  U$ [        SU R                  S5      5      nUb  UR                  R                  (       d  U$ [        X5      $ )Nr   r   	keypoints)r=   r   rj   rE   remove_invisibler   )r2   rq   rC   rE   rk   s        r+   apply_to_keypointsBaseDropout.apply_to_keypoints   sR     ::?-t/A/A+/NO	I$4$4$E$E(::r*   c                    [        S5      e)Nz&Subclasses must implement this method.)NotImplementedError)r2   rE   datas      r+   get_params_dependent_on_data(BaseDropout.get_params_dependent_on_data   s    !"JKKr*   )r    r"   )r    r   r"   r!   r/   float)
rB   
np.ndarrayrC   r{   rD   intrE   r   returnr{   )
rM   r{   rC   r{   rD   r|   rE   r   r}   r{   )
rQ   r{   rC   r{   rD   r|   rE   r   r}   r{   )
rV   r{   rC   r{   rD   r|   rE   r   r}   r{   )
r\   r{   rC   r{   rD   r|   rE   r   r}   r{   )rf   r{   rC   r{   rE   r   r}   r{   )rq   r{   rC   r{   rE   r   r}   r{   rE   dict[str, Any]rw   r   r}   r   )r$   r%   r&   r'   __doc__r   r   r(   r   r,   r1   rG   rN   rR   rW   r]   r`   rc   rn   rs   rx   r)   __classcell__r3   s   @r+   r   r   "   s    L\ /:H+94, 4#l# 4# 	#JWC
YZ
[
P


 
 	

 

4;; ; 	;
 
;L Lr*   r   c                     ^  \ rS rSrSr " S S\5      r\r     S         SU 4S jjjr	          SS jr
          SS jr        SS jr      SS	 jr      SS
 jrSrU =r$ )r      a  Drops random pixels from the image.

This transform randomly sets pixels in the image to a specified value, effectively "dropping out" those pixels.
It can be applied to both the image and its corresponding mask.

Args:
    dropout_prob (float): Probability of dropping out each pixel. Should be in the range [0, 1].
        Default: 0.01

    per_channel (bool): If True, the dropout mask will be generated independently for each channel.
        If False, the same dropout mask will be applied to all channels.
        Default: False

    drop_value (float | tuple[float, ...] | None): Value to assign to the dropped pixels.
        If None, the value will be randomly sampled for each application:
            - For uint8 images: Random integer in [0, 255]
            - For float32 images: Random float in [0, 1]
        If a single number, that value will be used for all dropped pixels.
        If a sequence, it should contain one value per channel.
        Default: 0

    mask_drop_value (float | tuple[float, ...] | None): Value to assign to dropped pixels in the mask.
        If None, the mask will remain unchanged.
        If a single number, that value will be used for all dropped pixels in the mask.
        If a sequence, it should contain one value per channel.
        Default: None

    p (float): Probability of applying the transform. Should be in the range [0, 1].
        Default: 0.5

Targets:
    image, mask, bboxes, keypoints, volume, mask3d

Image types:
    uint8, float32

Note:
    - When applied to bounding boxes, this transform may cause some boxes to have zero area
      if all pixels within the box are dropped. Such boxes will be removed.
    - When applied to keypoints, keypoints that fall on dropped pixels will be removed if
      the keypoint processor is configured to remove invisible keypoints.

Examples:
    >>> import numpy as np
    >>> import albumentations as A
    >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
    >>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
    >>> transform = A.PixelDropout(dropout_prob=0.1, per_channel=True, p=1.0)
    >>> result = transform(image=image, mask=mask)
    >>> dropped_image, dropped_mask = result['image'], result['mask']

c                  L    \ rS rSr% \" SSS9rS\S'   S\S'   S	\S
'   S	\S'   Srg)PixelDropout.InitSchemai  r   r:   )gelerz   dropout_probboolper_channelr!   
drop_valuemask_drop_valuer#   N)r$   r%   r&   r'   r   r   r(   r)   r#   r*   r+   r,   r     s%    #qQ/e/4499r*   r,   c                P   > [         TU ]  US9  Xl        X l        X0l        X@l        g r.   )r0   r1   r   r   r   r   )r2   r   r   r   r   r/   r3   s         r+   r1   PixelDropout.__init__  s,     	1(&$.r*   c                0    [         R                  " XU5      $ )aK  Apply pixel dropout to the image.

Args:
    img (np.ndarray): The image to apply the transform to.
    drop_mask (np.ndarray): The dropout mask.
    drop_values (np.ndarray): The values to assign to the dropped pixels.
    **params (Any): Additional parameters for the transform.

Returns:
    np.ndarray: The transformed image.

)fpixelpixel_dropout)r2   rB   	drop_maskdrop_valuesrE   s        r+   rG   PixelDropout.apply*  s    & ##CK@@r*   c                N    U R                   c  U$ [        R                  " XU5      $ )at  Apply pixel dropout to the mask.

Args:
    mask (np.ndarray): The mask to apply the transform to.
    mask_drop_mask (np.ndarray): The dropout mask for the mask.
    mask_drop_values (float | np.ndarray): The values to assign to the dropped pixels in the mask.
    **params (Any): Additional parameters for the transform.

Returns:
    np.ndarray: The transformed mask.

)r   r   r   )r2   r\   mask_drop_maskmask_drop_valuesrE   s        r+   rc   PixelDropout.apply_to_mask?  s)    & 'K##D:JKKr*   c                *   Ub  U R                   (       a  U$ [        SU R                  S5      5      nUc  U$ US   SS n[        X5      nU R                   (       ak  [	        UR
                  5      S:  aR  [        R                  " X"R
                  S   S::  a  SOSS	9nUR                  S
:X  a  UR
                  S   S:X  a  US   nOUn[        R                  " UUUUR                  R                  UR                  R                  5      n[        X5      $ )a=  Apply pixel dropout to the bounding boxes.

Args:
    bboxes (np.ndarray): The bounding boxes to apply the transform to.
    drop_mask (np.ndarray | None): The dropout mask for the bounding boxes.
    **params (Any): Additional parameters for the transform.

Returns:
    np.ndarray: The transformed bounding boxes.

Nr   rf   rL   rg   rJ   r   )axisr;   r:   )r   r   rj   r   lenrL   r?   anyrK   fdropoutmask_dropout_bboxesrE   rh   ri   r   )	r2   rf   r   rE   rk   rl   rm   combined_maskresults	            r+   rn   PixelDropout.apply_to_bboxesW  s   "  0 0M$*<*<X*FG	MWobq)0E IOO 4q 8FF99LPQ9Q2WXYM!!Q&=+>+>q+AQ+F -a 0%M--%%++
  44r*   c                    U$ )zApply pixel dropout to the keypoints.

Args:
    keypoints (np.ndarray): The keypoints to apply the transform to.
    **params (Any): Additional parameters for the transform.

Returns:
    np.ndarray: The transformed keypoints.

r#   )r2   rq   rE   s      r+   rs   PixelDropout.apply_to_keypoints  s
     r*   c                V   SU;   a  US   OUS   S   n[         R                  " UR                  U R                  U R                  U R
                  5      n[         R                  " UU R                  U R
                  5      nSnSn[         R                  " U5      nU R                  bp  Ubm  [         R                  " UR                  U R                  U R                  U R
                  5      n[         R                  " UU R                  U R
                  5      nUUUb  UOSUb  US.$ SS.$ )zGenerate parameters for pixel dropout based on input data.

Args:
    params (dict[str, Any]): Transform parameters
    data (dict[str, Any]): Input data dictionary

Returns:
    dict[str, Any]: Dictionary of parameters for applying the transform

imagerM   r   N)r   r   r   r   )
r   get_drop_maskrL   r   r   random_generatorprepare_drop_valuesr   get_mask_arrayr   )	r2   rE   rw   reference_arrayr   r   r   r   r\   s	            r+   rx   )PixelDropout.get_params_dependent_on_data  s8    ,3d?$w-Xq@Q ((!!!!	
	 00OO!!
 $$T*+0@#11

  !!%%	N  &99$$%%  #&0>0JnPT4D4P 0	
 	
 W[	
 	
r*   )r   r   r   r   )g{Gz?Fr   Ng      ?)
r   rz   r   r   r   r!   r   r!   r/   rz   )
rB   r{   r   r{   r   r{   rE   r   r}   r{   )
r\   r{   r   r{   r   zfloat | np.ndarrayrE   r   r}   r{   )rf   r{   r   znp.ndarray | NonerE   r   r}   r{   )rq   r{   rE   r   r}   r{   r~   )r$   r%   r&   r'   r   r   r,   r   r   r1   rG   rc   rn   rs   rx   r)   r   r   s   @r+   r   r      sQ   3j:, : H #!78<@// / 5	/
 :/ / /AA A  	A
 A 
A*LL #L -	L
 L 
L0/5/5 %/5 	/5
 
/5b  
	"4
4
 4
 
	4
 4
r*   )'r   
__future__r   typingr   r   r   numpyr?   albucorer   pydanticr   $albumentations.augmentations.dropoutr	   r   /albumentations.augmentations.dropout.functionalr
   r   r   r   r   "albumentations.augmentations.pixelr   albumentations.core.bbox_utilsr   r   r   #albumentations.core.keypoints_utilsr   (albumentations.core.transforms_interfacer   r   $albumentations.core.type_definitionsr   r   __all__r   r   r#   r*   r+   <module>r      sg    # % %  %  G  D ^ ^ B [ E
yL- yLxo
= o
r*   