
    h*                        S r SSKJr  SSKJrJrJr  SSKrSSKr	SSK
Js  Js  Jr  SSKJrJrJr  SSKJr  SSKJr  SSKJrJr  SS	KJr  S
/r " S S
\5      rg)a  Implementation of mask-based dropout augmentation.

This module provides the MaskDropout transform, which identifies objects in a segmentation mask
and drops out random objects completely. This augmentation is particularly useful for instance
segmentation and object detection tasks, as it simulates occlusions or missing objects in a
semantically meaningful way, rather than dropping out random pixels or regions.
    )annotations)AnyLiteralcastN)BboxProcessordenormalize_bboxesnormalize_bboxes)KeypointsProcessor)OnePlusIntRangeType)BaseTransformInitSchemaDualTransform)ALL_TARGETSMaskDropoutc                     ^  \ rS rSrSr\r " S S\5      r    S       SU 4S jjjr	\
SS j5       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      ac  Apply dropout to random objects in a mask, zeroing out the corresponding regions in both the image and mask.

This transform identifies objects in the mask (where each unique non-zero value represents a distinct object),
randomly selects a number of these objects, and sets their corresponding regions to zero in both the image and mask.
It can also handle bounding boxes and keypoints, removing or adjusting them based on the dropout regions.

Args:
    max_objects (int | tuple[int, int]): Maximum number of objects to dropout. If a single int is provided,
        it's treated as the upper bound. If a tuple of two ints is provided, it's treated as a range [min, max].
    fill (float | Literal["inpaint_telea", "inpaint_ns"]): Value to fill dropped out regions in the image.
        Can be one of:
        - float: Constant value to fill the regions (e.g., 0 for black, 255 for white)
        - "inpaint_telea": Use Telea inpainting algorithm (for 3-channel images only)
        - "inpaint_ns": Use Navier-Stokes inpainting algorithm (for 3-channel images only)
    fill_mask (float): Value to fill the dropped out regions in the mask.
    min_area (float): Minimum area (in pixels) of a bounding box that must remain visible after dropout to be kept.
        Only applicable if bounding box augmentation is enabled. Default: 0.0
    min_visibility (float): Minimum visibility ratio (visible area / total area) of a bounding box after dropout
        to be kept. Only applicable if bounding box augmentation is enabled. Default: 0.0
    p (float): Probability of applying the transform. Default: 0.5.

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

Image types:
    uint8, float32

Note:
    - The mask should be a single-channel image where 0 represents the background and non-zero values represent
      different object instances.
    - For bounding box and keypoint augmentation, make sure to set up the corresponding processors in the pipeline.

Examples:
    >>> import numpy as np
    >>> import albumentations as A
    >>>
    >>> # Prepare sample data
    >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
    >>> mask = np.zeros((100, 100), dtype=np.uint8)
    >>> mask[20:40, 20:40] = 1  # Object 1
    >>> mask[60:80, 60:80] = 2  # Object 2
    >>> bboxes = np.array([[20, 20, 40, 40], [60, 60, 80, 80]], dtype=np.float32)
    >>> bbox_labels = [1, 2]
    >>> keypoints = np.array([[30, 30], [70, 70]], dtype=np.float32)
    >>> keypoint_labels = [0, 1]
    >>>
    >>> # Define the transform with tuple for max_objects
    >>> transform = A.Compose(
    ...     transforms=[
    ...         A.MaskDropout(
    ...             max_objects=(1, 2),  # Using tuple to specify min and max objects to drop
    ...             fill=0,  # Fill value for dropped regions in image
    ...             fill_mask=0,  # Fill value for dropped regions in mask
    ...             p=1.0
    ...         ),
    ...     ],
    ...     bbox_params=A.BboxParams(
    ...         format='pascal_voc',
    ...         label_fields=['bbox_labels'],
    ...         min_area=1,
    ...         min_visibility=0.1
    ...     ),
    ...     keypoint_params=A.KeypointParams(
    ...         format='xy',
    ...         label_fields=['keypoint_labels'],
    ...         remove_invisible=True
    ...     )
    ... )
    >>>
    >>> # Apply the transform
    >>> transformed = transform(
    ...     image=image,
    ...     mask=mask,
    ...     bboxes=bboxes,
    ...     bbox_labels=bbox_labels,
    ...     keypoints=keypoints,
    ...     keypoint_labels=keypoint_labels
    ... )
    >>>
    >>> # Get the transformed data
    >>> transformed_image = transformed['image']  # Image with dropped out regions
    >>> transformed_mask = transformed['mask']    # Mask with dropped out regions
    >>> transformed_bboxes = transformed['bboxes']  # Remaining bboxes after dropout
    >>> transformed_bbox_labels = transformed['bbox_labels']  # Labels for remaining bboxes
    >>> transformed_keypoints = transformed['keypoints']  # Remaining keypoints after dropout
    >>> transformed_keypoint_labels = transformed['keypoint_labels']  # Labels for remaining keypoints

c                  4    \ rS rSr% S\S'   S\S'   S\S'   Srg	)
MaskDropout.InitSchemav   r   max_objects.float | Literal['inpaint_telea', 'inpaint_ns']fillfloat	fill_mask N)__name__
__module____qualname____firstlineno____annotations____static_attributes__r       k/var/www/fran/franai/venv/lib/python3.13/site-packages/albumentations/augmentations/dropout/mask_dropout.py
InitSchemar   v   s    ((<<r!   r#   c                Z   > [         TU ]  US9  [        SU5      U l        X l        X0l        g )N)pztuple[int, int])super__init__r   r   r   r   )selfr   r   r   r%   	__class__s        r"   r'   MaskDropout.__init__|   s/     	1 1;?	"r!   c                    S/$ )zTGet targets as parameters.

Returns:
    list[str]: List of targets as parameters.

maskr   )r(   s    r"   targets_as_paramsMaskDropout.targets_as_params   s     xr!   c                   US   n[         R                  " USS9u  pEUS:X  a  SnS	U0$ U R                  R                  " U R                  6 n[        XW5      nXu:X  a	  US:  nS	U0$ U R                  R                  [        SUS-   5      U5      n[        R                  " UR                  SS [        S9nU H
  n	XdU	:H  -  nM     S	U0$ )
zGet parameters dependent on the data.

Args:
    params (dict[str, Any]): Dictionary containing parameters.
    data (dict[str, Any]): Dictionary containing data.

Returns:
    dict[str, Any]: Dictionary with parameters for transformation.

r,   T)
return_numr   N      )dtypedropout_mask)fdropoutlabel	py_randomrandintr   minsamplerangenpzerosshapebool)
r(   paramsdatar,   label_image
num_labelsr4   objects_to_droplabels_indexlabel_indexs
             r"   get_params_dependent_on_data(MaskDropout.get_params_dependent_on_data   s     F|"*..$"G?L -- #nn44d6F6FGO!*>O,#ax --  $~~44U1j1n5M_!xx

2AdC#/K ;$>>L $0 --r!   c           	     f   Uc  U$ U R                   S;   a}  UR                  [        R                  5      n[        R
                  " U5      u    pEn[        S[        XV5      S-  5      n[        R                  " XU[        SU R                   5      5      $ UR                  5       nU R                   X'   U$ )a  Apply dropout to the image.

Args:
    img (np.ndarray): The image to apply the transform to.
    dropout_mask (np.ndarray | None): The dropout mask for the image.
    **params (Any): Additional parameters for the transform.

Returns:
    np.ndarray: The transformed image.

>   
inpaint_nsinpaint_telea   r2   z&Literal['inpaint_telea', 'inpaint_ns'])r   astyper<   uint8cv2boundingRectr9   maxinpaintr   copy)r(   imgr4   r@   _widthheightradiuss           r"   applyMaskDropout.apply   s     J9977'..rxx8L"%"2"2<"@AqC.!34F;;s&$?gimirir:stthhj II
r!   c                f    Ub  U R                   c  U$ UR                  5       nU R                   X'   U$ )a  Apply dropout to the mask.

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

Returns:
    np.ndarray: The transformed mask.

)r   rS   )r(   r,   r4   r@   s       r"   apply_to_maskMaskDropout.apply_to_mask   s4     4>>#9Kyy{!^^r!   c                   Uc  U$ [        SU R                  S5      5      nUc  U$ US   SS n[        X5      n[        R                  " UUUUR
                  R                  UR
                  R                  5      n[        Xu5      $ )a6  Apply dropout to bounding boxes.

Args:
    bboxes (np.ndarray): The bounding boxes to apply the transform to.
    dropout_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   bboxesr>   r2   )	r   get_processorr   r5   mask_dropout_bboxesr@   min_areamin_visibilityr	   )r(   r_   r4   r@   	processorimage_shapedenormalized_bboxesresults           r"   apply_to_bboxesMaskDropout.apply_to_bboxes   s     M$*<*<X*FG	MWobq)0E--%%++
  44r!   c                    Uc  U$ [        SU R                  S5      5      nUb  UR                  R                  (       d  U$ [        R
                  " X5      $ )a%  Apply dropout to keypoints.

Args:
    keypoints (np.ndarray): The keypoints to apply the transform to.
    dropout_mask (np.ndarray | None): The dropout mask for the keypoints.
    **params (Any): Additional parameters for the transform.

Returns:
    np.ndarray: The transformed keypoints.

r
   	keypoints)r   r`   r@   remove_invisibler5   mask_dropout_keypoints)r(   rk   r4   r@   rd   s        r"   apply_to_keypointsMaskDropout.apply_to_keypoints   sS     -t/A/A+/NO	I$4$4$E$E..yGGr!   )r   r   r   ))r1   r1   r   r   g      ?)r   ztuple[int, int] | intr   r   r   r   r%   r   )returnz	list[str])r@   dict[str, Any]rA   rq   rp   rq   )rT   
np.ndarrayr4   np.ndarray | Noner@   r   rp   rr   )r,   rr   r4   rs   r@   r   rp   rr   )r_   rr   r4   rs   r@   r   rp   rr   )rk   rr   r4   rs   r@   r   rp   rr   )r   r   r   r   __doc__r   _targetsr   r#   r'   propertyr-   rG   rY   r\   rh   rn   r    __classcell__)r)   s   @r"   r   r      s    Wr H,  .4?@
#*
# =
# 	
#
 
# 
#  .>4&5BH Hr!   )rt   
__future__r   typingr   r   r   rO   numpyr<   /albumentations.augmentations.dropout.functionalaugmentationsdropout
functionalr5   albumentations.core.bbox_utilsr   r   r	   #albumentations.core.keypoints_utilsr
   albumentations.core.pydanticr   (albumentations.core.transforms_interfacer   r   $albumentations.core.type_definitionsr   __all__r   r   r!   r"   <module>r      sJ    # % % 
  B B ^ ^ B < [ </xH- xHr!   