U
    h                     @  s  d dl mZ d dlZd dlZd dlmZmZmZmZ d dl	m
Z
 d dlZd dlZd dlmZmZmZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZmZmZmZm Z m!Z! d d
l"m#Z#m$Z$ d dl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/mZ0 dddddddddddgZ1G dd de2Z3G dd de#Z4G dd de$Z5G dd de5Z6G dd de5Z7G d d de5Z8G d!d de5Z9G d"d# d#e#Z:G d$d% d%e$Z;G d&d de;Z<G d'd de;Z=G d(d de5Z>G d)d de5Z?G d*d de?Z@G d+d de$ZAG d,d de5ZBdS )-    )annotationsN)AnySequenceTuplecast)warn)AfterValidatorFieldfield_validatormodel_validator)	AnnotatedSelf)
functional)union_of_bboxes)BorderModeTypeInterpolationTypeOnePlusIntRangeTypeZeroOneRangeTypecheck_0pluscheck_01)BaseTransformInitSchemaDualTransform)	NUM_MULTI_CHANNEL_DIMENSIONSPAIR	ColorTypePercentTypePxType
ScalarTypeScaleFloatTypeScaleIntTypeTargets   
RandomCrop
CenterCropCropCropNonEmptyMaskIfExistsRandomSizedCropRandomResizedCropRandomCropNearBBoxRandomSizedBBoxSafeCrop
CropAndPadRandomCropFromBordersBBoxSafeRandomCropc                   @  s   e Zd ZdS )CropSizeErrorN__name__
__module____qualname__ r2   r2   Q/tmp/pip-unpacked-wheel-e8onvpoz/albumentations/augmentations/crops/transforms.pyr-   5   s   r-   c                   @  s2   e Zd ZU eddZded< eddZded< dS )CropInitSchemar!   )ge
int | NoneheightwidthN)r/   r0   r1   r	   r7   __annotations__r8   r2   r2   r2   r3   r4   9   s   
r4   c                      s   e Zd ZejejejejfZdddd fddZ	dd	d
ddddZ
dd	d
ddddZdd	d
ddddZed	dd	dddZ  ZS )BaseCrop      ?Nfloatbool | Nonepalways_applyc                   s   t  j||d d S Nr>   )super__init__)selfr?   r@   	__class__r2   r3   rC   A   s    zBaseCrop.__init__
np.ndarraytuple[int, int, int, int]r   imgcrop_coordsparamsreturnc                 K  s4   |d }|d }|d }|d }t j|||||dS )Nr   r!         x_miny_minx_maxy_max)fcropscrop)rD   rJ   rK   rL   rQ   rR   rS   rT   r2   r2   r3   applyD   s
    zBaseCrop.applybboxesrK   rL   rM   c                 K  s   t |||d S NshaperU   Zcrop_bboxes_by_coordsrD   rY   rK   rL   r2   r2   r3   apply_to_bboxesK   s    zBaseCrop.apply_to_bboxes	keypointsrK   rL   rM   c                 K  s   t ||S N)rU   crop_keypoints_by_coords)rD   r`   rK   rL   r2   r2   r3   apply_to_keypointsS   s    zBaseCrop.apply_to_keypointstuple[int, int])bboximage_shaperM   c                 C  s`   |d d \}}| \}}}}t |d|}t |d|}t |||}t |||}||||fS )NrN   r   )npclip)re   rf   r7   r8   rQ   rR   rS   rT   r2   r2   r3   
_clip_bbox[   s    zBaseCrop._clip_bbox)r;   N)r/   r0   r1   r    IMAGEMASKBBOXES	KEYPOINTS_targetsrC   rW   r^   rc   staticmethodri   __classcell__r2   r2   rE   r3   r:   >   s   r:   c                      s^   e Zd ZdZG dd deZdddddd	 fd
dZddddddZddddZ  Z	S )r"   a  Crop a random part of the input.

    Args:
        height: height of the crop.
        width: width of the crop.
        p: probability of applying the transform. Default: 1.

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    c                   @  s   e Zd ZdS )zRandomCrop.InitSchemaNr.   r2   r2   r2   r3   
InitSchemaw   s   rq   r;   Nintr<   r=   r7   r8   r?   r@   c                   s    t  j||d || _|| _d S rA   rB   rC   r7   r8   rD   r7   r8   r?   r@   rE   r2   r3   rC   z   s    zRandomCrop.__init__dict[str, Any]$dict[str, tuple[int, int, int, int]]rL   datarM   c           	      C  s   |d d d }|\}}| j |ks,| j|krPtd| j | jf d|d d  t }t }t|| j | jf||}d|iS )Nr[   rN   zDCrop size (height, width) exceeds image dimensions (height, width): z vs rK   )r7   r8   r-   randomrU   get_crop_coords)	rD   rL   ry   rf   image_heightimage_widthh_startw_startrK   r2   r2   r3   get_params_dependent_on_data   s    z'RandomCrop.get_params_dependent_on_datatuple[str, ...]rM   c                 C  s   dS N)r7   r8   r2   rD   r2   r2   r3   get_transform_init_args_names   s    z(RandomCrop.get_transform_init_args_names)r;   N)
r/   r0   r1   __doc__r4   rq   rC   r   r   rp   r2   r2   rE   r3   r"   g   s
   c                      s^   e Zd ZdZG dd deZdddddd	 fd
dZddddZddddddZ  Z	S )r#   a  Crop the central part of the input.

    This transform crops the center of the input image, mask, bounding boxes, and keypoints to the specified dimensions.
    It's useful when you want to focus on the central region of the input, discarding peripheral information.

    Args:
        height (int): The height of the crop. Must be greater than 0.
        width (int): The width of the crop. Must be greater than 0.
        p (float): Probability of applying the transform. Default: 1.0.

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - If the specified crop size is larger than the input image in either dimension,
          it will raise a CropSizeError.
        - For bounding boxes and keypoints, only those that fall within the cropped area are kept,
          and their coordinates are adjusted to the new image size.

    Example:
        >>> import numpy as np
        >>> import albumentations as A
        >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> transform = A.Compose([
        ...     A.CenterCrop(height=80, width=80, p=1.0),
        ... ])
        >>> transformed = transform(image=image)
        >>> transformed_image = transformed['image']  # 80x80 center crop of the original image
    c                   @  s   e Zd ZdS )zCenterCrop.InitSchemaNr.   r2   r2   r2   r3   rq      s   rq   r;   Nrr   r<   r=   rs   c                   s    t  j||d || _|| _d S rA   rt   ru   rE   r2   r3   rC      s    zCenterCrop.__init__r   r   c                 C  s   dS r   r2   r   r2   r2   r3   r      s    z(CenterCrop.get_transform_init_args_namesrv   rw   rx   c                 C  s,   |d d d }t || j| jf}d|iS Nr[   rN   rK   )rU   get_center_crop_coordsr7   r8   )rD   rL   ry   rf   rK   r2   r2   r3   r      s    z'CenterCrop.get_params_dependent_on_data)r;   N)
r/   r0   r1   r   r4   rq   rC   r   r   rp   r2   r2   rE   r3   r#      s
   !c                      sb   e Zd ZdZG dd deZdddddd	d
d fddZddddZddddddZ  Z	S )r$   a  Crop a specific region from the input image.

    This transform crops a rectangular region from the input image, mask, bounding boxes, and keypoints
    based on specified coordinates. It's useful when you want to extract a specific area of interest
    from your inputs.

    Args:
        x_min (int): Minimum x-coordinate of the crop region (left edge). Must be >= 0. Default: 0.
        y_min (int): Minimum y-coordinate of the crop region (top edge). Must be >= 0. Default: 0.
        x_max (int): Maximum x-coordinate of the crop region (right edge). Must be > x_min. Default: 1024.
        y_max (int): Maximum y-coordinate of the crop region (bottom edge). Must be > y_min. Default: 1024.
        always_apply (bool, optional): If set to True, the transform will be always applied. Default: None.
        p (float): Probability of applying the transform. Default: 1.0.

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - The crop coordinates are applied as follows: x_min <= x < x_max and y_min <= y < y_max.
        - If the specified crop region extends beyond the image boundaries, it will be clipped to fit within the image.
        - For bounding boxes and keypoints, only those that fall within the cropped region are kept,
          and their coordinates are adjusted relative to the new image size.

    Example:
        >>> import numpy as np
        >>> import albumentations as A
        >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> transform = A.Compose([
        ...     A.Crop(x_min=10, y_min=20, x_max=80, y_max=90, p=1.0),
        ... ])
        >>> transformed = transform(image=image)
        >>> transformed_image = transformed['image']  # 70x70 crop of the original image
    c                   @  sF   e Zd ZU ded< ded< ded< ded< eddd	d
ddZdS )zCrop.InitSchemazAnnotated[int, Field(ge=0)]rQ   rR   zAnnotated[int, Field(gt=0)]rS   rT   aftermoder   r   c                 C  s4   | j | jk sd}t|| j| jk s0d}t|| S )Nz x_max must be greater than x_minz y_max must be greater than y_min)rQ   rS   
ValueErrorrR   rT   rD   msgr2   r2   r3   validate_coordinates   s    z$Crop.InitSchema.validate_coordinatesN)r/   r0   r1   r9   r   r   r2   r2   r2   r3   rq      s   
rq   r      Nr;   rr   r=   r<   )rQ   rR   rS   rT   r@   r?   c                   s,   t  j||d || _|| _|| _|| _d S rA   )rB   rC   rQ   rR   rS   rT   )rD   rQ   rR   rS   rT   r@   r?   rE   r2   r3   rC     s
    	zCrop.__init__r   r   c                 C  s   dS )NrP   r2   r   r2   r2   r3   r     s    z"Crop.get_transform_init_args_namesrv   rw   rx   c                 C  s   d| j | j| j| jfiS )NrK   rP   )rD   rL   ry   r2   r2   r3   r     s    z!Crop.get_params_dependent_on_data)r   r   r   r   Nr;   )
r/   r0   r1   r   r   rq   rC   r   r   rp   r2   r2   rE   r3   r$      s   %      c                      sv   e Zd ZdZG dd deZddddddd	d
 fddZdddddZdddd fddZddddZ	  Z
S )r%   a  Crop area with mask if mask is non-empty, else make random crop.

    This transform attempts to crop a region containing a mask (non-zero pixels). If the mask is empty or not provided,
    it falls back to a random crop. This is particularly useful for segmentation tasks where you want to focus on
    regions of interest defined by the mask.

    Args:
        height (int): Vertical size of crop in pixels. Must be > 0.
        width (int): Horizontal size of crop in pixels. Must be > 0.
        ignore_values (list of int, optional): Values to ignore in mask, `0` values are always ignored.
            For example, if background value is 5, set `ignore_values=[5]` to ignore it. Default: None.
        ignore_channels (list of int, optional): Channels to ignore in mask.
            For example, if background is the first channel, set `ignore_channels=[0]` to ignore it. Default: None.
        p (float): Probability of applying the transform. Default: 1.0.

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - If a mask is provided, the transform will try to crop an area containing non-zero (or non-ignored) pixels.
        - If no suitable area is found in the mask or no mask is provided, it will perform a random crop.
        - The crop size (height, width) must not exceed the original image dimensions.
        - Bounding boxes and keypoints are also cropped along with the image and mask.

    Raises:
        ValueError: If the specified crop size is larger than the input image dimensions.

    Example:
        >>> import numpy as np
        >>> import albumentations as A
        >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> mask = np.zeros((100, 100), dtype=np.uint8)
        >>> mask[25:75, 25:75] = 1  # Create a non-empty region in the mask
        >>> transform = A.Compose([
        ...     A.CropNonEmptyMaskIfExists(height=50, width=50, p=1.0),
        ... ])
        >>> transformed = transform(image=image, mask=mask)
        >>> transformed_image = transformed['image']
        >>> transformed_mask = transformed['mask']
        # The resulting crop will likely include part of the non-zero region in the mask
    c                   @  s   e Zd ZU ded< ded< dS )z#CropNonEmptyMaskIfExists.InitSchemalist[int] | Noneignore_valuesignore_channelsNr/   r0   r1   r9   r2   r2   r2   r3   rq   M  s   
rq   Nr;   rr   r   r=   r<   )r7   r8   r   r   r@   r?   c                   s,   t  j||d || _|| _|| _|| _d S rA   )rB   rC   r7   r8   r   r   )rD   r7   r8   r   r   r@   r?   rE   r2   r3   rC   Q  s
    	z!CropNonEmptyMaskIfExists.__init__rG   )maskrM   c              
     s   |j d d \}} jd k	r>t j}tt||d|}|jtkr jd k	rt fddt	|j d D }tj
||dd} j|ks j|krtd j d j d	| d| d
	|S )NrN   r   c                   s   g | ]}| j kr|qS r2   )r   ).0chr   r2   r3   
<listcomp>i  s     
 z=CropNonEmptyMaskIfExists._preprocess_mask.<locals>.<listcomp>ZaxiszCrop size (,z) is larger than image ())r[   r   rg   arraywhereisinndimr   r   rangeZtaker7   r8   r   )rD   r   mask_height
mask_widthZignore_values_npZtarget_channelsr2   r   r3   _preprocess_maska  s    
" z)CropNonEmptyMaskIfExists._preprocess_maskrv   r   )rL   kwargsrM   c                   sz  t  j|f| d|kr(| |d }n^d|krzt|d rz|d }| t|d }|dd  D ]}|| |O }qdnd}t||jd d \}}| r*|j	t
kr|jddn|}t|}	t|	\}
}|td| jd  }|
td| jd  }t|d|| j }t|d|| j }n$td|| j }td|| j }|| j }|| j }||||f}||d	< |S )
Nr   masksr   r!   z.Can not find mask for CropNonEmptyMaskIfExistsrN   r   r   rK   )rB   update_paramsr   lenrg   copyRuntimeErrorr[   anyr   r   sumZargwhererz   choicerandintr8   r7   rh   )rD   rL   r   r   r   mr   r   r   Znon_zero_yxyxrQ   rR   rS   rT   rK   rE   r2   r3   r   s  s4    



z&CropNonEmptyMaskIfExists.update_paramsr   r   c                 C  s   dS )N)r7   r8   r   r   r2   r   r2   r2   r3   r     s    z6CropNonEmptyMaskIfExists.get_transform_init_args_names)NNNr;   )r/   r0   r1   r   r4   rq   rC   r   r   r   rp   r2   r2   rE   r3   r%     s   -    #c                   @  s2   e Zd ZU ded< ededddddZdS )BaseRandomSizedCropInitSchemard   size)valuerM   c                 C  s   t dd |D rtd|S )Nc                 s  s   | ]}|d kV  qdS )r   Nr2   )r   r   r2   r2   r3   	<genexpr>  s     z;BaseRandomSizedCropInitSchema.check_size.<locals>.<genexpr>z1All elements of 'size' must be positive integers.)r   r   )clsr   r2   r2   r3   
check_size  s    z(BaseRandomSizedCropInitSchema.check_sizeN)r/   r0   r1   r9   r
   classmethodr   r2   r2   r2   r3   r     s   
r   c                      s   e Zd ZG dd deZejddfddddd	 fd
dZddddddddZdddddddZ	dddddddZ
ddddZ  ZS )_BaseRandomSizedCropc                   @  s   e Zd ZU ejZded< dS )z_BaseRandomSizedCrop.InitSchemar   interpolationN)r/   r0   r1   cv2INTER_LINEARr   r9   r2   r2   r2   r3   rq     s   
rq   Nr;   rd   rr   r=   r<   )r   r   r@   r?   c                   s   t  || || _|| _d S ra   )rB   rC   r   r   )rD   r   r   r@   r?   rE   r2   r3   rC     s    z_BaseRandomSizedCrop.__init__rG   rH   r   )rJ   rK   r   rL   rM   c                 K  s    t j|f| }t|| j|S ra   )rU   rV   
fgeometricresizer   )rD   rJ   rK   r   rL   rV   r2   r2   r3   rW     s    z_BaseRandomSizedCrop.applyrX   c                 K  s   t |||d S rZ   r\   r]   r2   r2   r3   r^     s    z$_BaseRandomSizedCrop.apply_to_bboxesr_   c           	      K  sV   t ||}|d |d  }|d |d  }| jd | }| jd | }t|||S )NrO   r!   rN   r   )rU   rb   r   r   keypoints_scale)	rD   r`   rK   rL   Zcropped_keypointscrop_height
crop_widthscale_xscale_yr2   r2   r3   rc     s    z'_BaseRandomSizedCrop.apply_to_keypointsr   r   c                 C  s   dS )N)r   r   r2   r   r2   r2   r3   r     s    z2_BaseRandomSizedCrop.get_transform_init_args_names)r/   r0   r1   r   rq   r   r   rC   rW   r^   rc   r   rp   r2   r2   rE   r3   r     s   
r   c                      s   e Zd ZdZejejejejfZ	G dd de
Zddejdddddd	d	d
ddd
d fddZddddddZdd fddZ  ZS )r&   a
  Crop a random part of the input and rescale it to a specific size.

    This transform first crops a random portion of the input and then resizes it to a specified size.
    The size of the random crop is controlled by the 'min_max_height' parameter.

    Args:
        min_max_height (tuple[int, int]): Minimum and maximum height of the crop in pixels.
        size (tuple[int, int]): Target size for the output image, i.e. (height, width) after crop and resize.
        w2h_ratio (float): Aspect ratio (width/height) of crop. Default: 1.0
        interpolation (OpenCV flag): Flag that is used to specify the interpolation algorithm. Should be one of:
            cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4.
            Default: cv2.INTER_LINEAR.
        p (float): Probability of applying the transform. Default: 1.0

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - The crop size is randomly selected for each execution within the range specified by 'min_max_height'.
        - The aspect ratio of the crop is determined by the 'w2h_ratio' parameter.
        - After cropping, the result is resized to the specified 'size'.
        - Bounding boxes that end up fully outside the cropped area will be removed.
        - Keypoints that end up outside the cropped area will be removed.
        - This transform differs from RandomResizedCrop in that it allows more control over the crop size
          through the 'min_max_height' parameter, rather than using a scale parameter.

    Mathematical Details:
        1. A random crop height h is sampled from the range [min_max_height[0], min_max_height[1]].
        2. The crop width w is calculated as: w = h * w2h_ratio
        3. A random location for the crop is selected within the input image.
        4. The image is cropped to the size (h, w).
        5. The crop is then resized to the specified 'size'.

    Example:
        >>> import numpy as np
        >>> import albumentations as A
        >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> transform = A.RandomSizedCrop(
        ...     min_max_height=(50, 80),
        ...     size=(64, 64),
        ...     w2h_ratio=1.0,
        ...     interpolation=cv2.INTER_LINEAR,
        ...     p=1.0
        ... )
        >>> result = transform(image=image)
        >>> transformed_image = result['image']
        # transformed_image will be a 64x64 image, resulting from a crop with height
        # between 50 and 80 pixels, and the same aspect ratio as specified by w2h_ratio,
        # taken from a random location in the original image and then resized.
    c                   @  sn   e Zd ZU ded< ded< ded< eddd	Zd
ed< eddd	Zd
ed< ded< eddddddZdS )zRandomSizedCrop.InitSchemar   r   r   min_max_heightzAnnotated[float, Field(gt=0)]	w2h_ratioNzInitializing with 'size' as an integer and a separate 'width' is deprecated. Please use a tuple (height, width) for the 'size' argument.
deprecatedr6   r8   zqInitializing with 'height' and 'width' is deprecated. Please use a tuple (height, width) for the 'size' argument.r7   ScaleIntType | Noner   r   r   r   r   c                 C  sp   t | jtr4t | jtr(| j| jf| _nd}t|| jd krl| jd ksR| jd kr^d}t|| j| jf| _| S Nz:If size is an integer, width as integer must be specified.zGIf 'size' is not provided, both 'height' and 'width' must be specified.
isinstancer   rr   r8   	TypeErrorr7   r   rD   r   messager2   r2   r3   process-  s    
z"RandomSizedCrop.InitSchema.process	r/   r0   r1   r9   r	   r8   r7   r   r   r2   r2   r2   r3   rq     s   
rq   Nr;   )r   r   r@   r?   rd   r   r6   r<   rr   r=   )r   r   r8   r7   r   r   r@   r?   c          	        s2   t  jttttf ||||d || _|| _d S N)r   r   r?   r@   )rB   rC   r   r   rr   r   r   )	rD   r   r   r8   r7   r   r   r@   r?   rE   r2   r3   rC   =  s    "zRandomSizedCrop.__init__rv   rw   rx   c           
      C  sZ   |d d d }t j| j }t|| j }||f}t   }t   }t||||}	d|	iS r   )rz   r   r   rr   r   rU   r{   )
rD   rL   ry   rf   r   r   
crop_shaper~   r   rK   r2   r2   r3   r   N  s    z,RandomSizedCrop.get_params_dependent_on_datar   r   c                   s   t   dS )Nr   r   )r   r   rB   r   r   rE   r2   r3   r   a  s    z-RandomSizedCrop.get_transform_init_args_names)NNNr/   r0   r1   r   r    rj   rk   rl   rm   rn   r   rq   r   r   rC   r   r   rp   r2   r2   rE   r3   r&     s   6(   $c                      s   e Zd ZdZejejejejfZ	G dd de
Zdddejdddd	d
d
dddddd fddZddddddZddddZ  ZS )r'   a  Crop a random part of the input and rescale it to a specified size.

    This transform first crops a random portion of the input image (or mask, bounding boxes, keypoints)
    and then resizes the crop to a specified size. It's particularly useful for training neural networks
    on images of varying sizes and aspect ratios.

    Args:
        size (tuple[int, int]): Target size for the output image, i.e. (height, width) after crop and resize.
        scale (tuple[float, float]): Range of the random size of the crop relative to the input size.
            For example, (0.08, 1.0) means the crop size will be between 8% and 100% of the input size.
            Default: (0.08, 1.0)
        ratio (tuple[float, float]): Range of aspect ratios of the random crop.
            For example, (0.75, 1.3333) allows crop aspect ratios from 3:4 to 4:3.
            Default: (0.75, 1.3333333333333333)
        interpolation (OpenCV flag): Flag that is used to specify the interpolation algorithm. Should be one of:
            cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4.
            Default: cv2.INTER_LINEAR
        p (float): Probability of applying the transform. Default: 1.0

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - This transform attempts to crop a random area with an aspect ratio and relative size
          specified by 'ratio' and 'scale' parameters. If it fails to find a suitable crop after
          10 attempts, it will return a crop from the center of the image.
        - The crop's aspect ratio is defined as width / height.
        - Bounding boxes that end up fully outside the cropped area will be removed.
        - Keypoints that end up outside the cropped area will be removed.
        - After cropping, the result is resized to the specified size.

    Mathematical Details:
        1. A target area A is sampled from the range [scale[0] * input_area, scale[1] * input_area].
        2. A target aspect ratio r is sampled from the range [ratio[0], ratio[1]].
        3. The crop width and height are computed as:
           w = sqrt(A * r)
           h = sqrt(A / r)
        4. If w and h are within the input image dimensions, the crop is accepted.
           Otherwise, steps 1-3 are repeated (up to 10 times).
        5. If no valid crop is found after 10 attempts, a centered crop is taken.
        6. The crop is then resized to the specified size.

    Example:
        >>> import numpy as np
        >>> import albumentations as A
        >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> transform = A.RandomResizedCrop(size=80, scale=(0.5, 1.0), ratio=(0.75, 1.33), p=1.0)
        >>> result = transform(image=image)
        >>> transformed_image = result['image']
        # transformed_image will be a 80x80 crop from a random location in the original image,
        # with the crop's size between 50% and 100% of the original image size,
        # and the crop's aspect ratio between 3:4 and 4:3.
    c                   @  sn   e Zd ZU ded< ded< edddZded	< edddZded
< ded< ded< eddddddZdS )zRandomResizedCrop.InitSchemaz8Annotated[tuple[float, float], AfterValidator(check_01)]scalez;Annotated[tuple[float, float], AfterValidator(check_0plus)]ratioNzGInitializing with 'height' and 'width' is deprecated. Use size instead.r   r6   r8   r7   r   r   r   r   r   r   r   r   c                 C  sp   t | jtr4t | jtr(| j| jf| _nd}t|| jd krl| jd ksR| jd kr^d}t|| j| jf| _| S r   r   r   r2   r2   r3   r     s    
z$RandomResizedCrop.InitSchema.processr   r2   r2   r2   r3   rq     s   
rq   N)g{Gz?r;   )g      ?gUUUUUU?r;   )r   r   r   r@   r?   r   r6   ztuple[float, float]rr   r=   r<   )r   r8   r7   r   r   r   r@   r?   c          	        s2   t  jttttf ||||d || _|| _d S r   )rB   rC   r   r   rr   r   r   )	rD   r   r8   r7   r   r   r   r@   r?   rE   r2   r3   rC     s    "zRandomResizedCrop.__init__rv   rw   rx   c                 C  s   |d d d }|\}}|| }t dD ]}tj| j | }t| jd t| jd f}	ttj|	 }
tt	t
||
 }tt	t
||
 }d|  k r|kr(n q(d|  k r|kr(n q(td|| }td|| }|d || d  }|d || d  }||f}t||||}d|i  S q(|| }|t| jk rl|}tt	|t| j }n4|t| jkr|}tt	|t| j }n|}|}|| d }|| d }|d || d  }|d || d  }||f}t||||}d|iS )	Nr[   rN   
   r   r!   r;   g|=rK   )r   rz   uniformr   mathlogr   exprr   roundsqrtr   rU   r{   minmax)rD   rL   ry   rf   r|   r}   area_Ztarget_areaZ	log_ratioZaspect_ratior8   r7   ijr~   r   r   rK   Zin_ratior2   r2   r3   r     sB     0z.RandomResizedCrop.get_params_dependent_on_datar   r   c                 C  s   dS )N)r   r   r   r   r2   r   r2   r2   r3   r     s    z/RandomResizedCrop.get_transform_init_args_names)NNNr   r2   r2   rE   r3   r'   e  s   9"   $7c                      s   e Zd ZdZejejejejfZ	G dd de
Zddd	d
ddd fddZddddddZeddddZddddZ  ZS )r(   a  Crop bbox from image with random shift by x,y coordinates

    Args:
        max_part_shift (float, (float, float)): Max shift in `height` and `width` dimensions relative
            to `cropping_bbox` dimension.
            If max_part_shift is a single float, the range will be (0, max_part_shift).
            Default (0, 0.3).
        cropping_bbox_key (str): Additional target key for cropping box. Default `cropping_bbox`.
        cropping_box_key (str): [Deprecated] Use `cropping_bbox_key` instead.
        p (float): probability of applying the transform. Default: 1.

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Examples:
        >>> aug = Compose([RandomCropNearBBox(max_part_shift=(0.1, 0.5), cropping_bbox_key='test_bbox')],
        >>>              bbox_params=BboxParams("pascal_voc"))
        >>> result = aug(image=image, bboxes=bboxes, test_bbox=[0, 5, 10, 20])

    c                   @  s   e Zd ZU ded< ded< dS )zRandomCropNearBBox.InitSchemar   max_part_shiftstrcropping_bbox_keyNr   r2   r2   r2   r3   rq   '  s   
rq   r   g333333?cropping_bboxNr;   r   r   z
str | Noner=   r<   )r   r   cropping_box_keyr@   r?   c                   sH   t  j||d |d k	r*tdtdd |}ttttf || _|| _d S )Nr>   zwThe parameter 'cropping_box_key' is deprecated and will be removed in future versions. Use 'cropping_bbox_key' instead.rN   )
stacklevel)	rB   rC   r   DeprecationWarningr   r   r<   r   r   )rD   r   r   r   r@   r?   rE   r2   r3   rC   +  s    zRandomCropNearBBox.__init__rv   zdict[str, tuple[float, ...]]rx   c                 C  s$  || j  }|d d d }| ||}t|d |d  | jd  }t|d |d  | jd  }|d t| | }|d t| | }|d t| | }	|d t| | }
| ||	||
f|}|d |d ks|d |d kr|d |d  |d |d  f}t||}d|iS )Nr[   rN   rO   r!   r   rK   )r   ri   r   r   rz   r   rU   r   )rD   rL   ry   re   rf   Zh_max_shiftZw_max_shiftrQ   rS   rR   rT   rK   r   r2   r2   r3   r   B  s    
" z/RandomCropNearBBox.get_params_dependent_on_data	list[str]r   c                 C  s   | j gS ra   )r   r   r2   r2   r3   targets_as_params^  s    z$RandomCropNearBBox.targets_as_paramsr   c                 C  s   dS )N)r   r   r2   r   r2   r2   r3   r   b  s    z0RandomCropNearBBox.get_transform_init_args_names)r   r   NNr;   )r/   r0   r1   r   r    rj   rk   rl   rm   rn   r   rq   rC   r   propertyr   r   rp   r2   r2   rE   r3   r(     s        c                      s   e Zd ZdZejejejejfZ	G dd de
Zddddd	 fd
dZdddddZddddddZeddddZddddZ  ZS )r,   a  Crop a random part of the input without loss of bounding boxes.

    This transform performs a random crop of the input image while ensuring that all bounding boxes remain within
    the cropped area. It's particularly useful for object detection tasks where preserving all objects in the image
    is crucial.

    Args:
        erosion_rate (float): A value between 0.0 and 1.0 that determines the minimum allowable size of the crop
            as a fraction of the original image size. For example, an erosion_rate of 0.2 means the crop will be
            at least 80% of the original image height. Default: 0.0 (no minimum size).
        p (float): Probability of applying the transform. Default: 1.0.

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        This transform ensures that all bounding boxes in the original image are fully contained within the
        cropped area. If it's not possible to find such a crop (e.g., when bounding boxes are too spread out),
        it will default to cropping the entire image.

    Example:
        >>> import numpy as np
        >>> import albumentations as A
        >>> image = np.ones((300, 300, 3), dtype=np.uint8)
        >>> bboxes = [(10, 10, 50, 50), (100, 100, 150, 150)]
        >>> transform = A.Compose([
        ...     A.BBoxSafeRandomCrop(erosion_rate=0.2, p=1.0),
        ... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['labels']))
        >>> transformed = transform(image=image, bboxes=bboxes, labels=['cat', 'dog'])
        >>> transformed_image = transformed['image']
        >>> transformed_bboxes = transformed['bboxes']
    c                   @  s"   e Zd ZU edddZded< dS )zBBoxSafeRandomCrop.InitSchema        r;   r5   ler<   erosion_rateNr/   r0   r1   r	   r   r9   r2   r2   r2   r3   rq     s   
rq   r   r;   Nr<   r=   r   r?   r@   c                   s   t  j||d || _d S rA   )rB   rC   r   )rD   r   r?   r@   rE   r2   r3   rC     s    zBBoxSafeRandomCrop.__init__rd   rH   )rf   rM   c           
      C  sj   |\}}t |d| j  }||kr&|n
t||}t || | }t }t }||f}	t||	||S )Nr;   )rr   r   rz   r   rU   r{   )
rD   rf   r|   r}   Z	erosive_hr   r   r~   r   r   r2   r2   r3   _get_coords_no_bbox  s    z&BBoxSafeRandomCrop._get_coords_no_bboxrv   rw   rx   c                 C  s"  |d d d }t |d dkr2| |}d|iS t|d | jd}|d kr^| |}d|iS |\}}}}	t|dd}t|dd}t||d}t|	|d}	|\}
}t|t  | }t|t  |
 }|d| t   }|	d|	 t   }t|| }t||
 }d||||fiS )Nr[   rN   rY   r   rK   )rY   r   r!   )r   r   r   r   rg   rh   rr   rz   )rD   rL   ry   rf   rK   Z
bbox_unionrQ   rR   rS   rT   r|   r}   Z
crop_x_minZ
crop_y_minZ	bbox_xmaxZ	bbox_ymaxZ
crop_x_maxZ
crop_y_maxr2   r2   r3   r     s*    

z/BBoxSafeRandomCrop.get_params_dependent_on_datar   r   c                 C  s   dgS )NrY   r2   r   r2   r2   r3   r     s    z$BBoxSafeRandomCrop.targets_as_paramsr   c                 C  s   dS )N)r   r2   r   r2   r2   r3   r     s    z0BBoxSafeRandomCrop.get_transform_init_args_names)r   r;   N)r/   r0   r1   r   r    rj   rk   rl   rm   rn   r   rq   rC   r   r   r   r   r   rp   r2   r2   rE   r3   r,   f  s   $$c                      s   e Zd ZdZejejejejfZ	G dd de
Zdejddfddddd	dd
 fddZdddddddZdddddddZdd fddZ  ZS )r)   a  Crop a random part of the input and rescale it to a specific size without loss of bounding boxes.

    This transform first attempts to crop a random portion of the input image while ensuring that all bounding boxes
    remain within the cropped area. It then resizes the crop to the specified size. This is particularly useful for
    object detection tasks where preserving all objects in the image is crucial while also standardizing the image size.

    Args:
        height (int): Height of the output image after resizing.
        width (int): Width of the output image after resizing.
        erosion_rate (float): A value between 0.0 and 1.0 that determines the minimum allowable size of the crop
            as a fraction of the original image size. For example, an erosion_rate of 0.2 means the crop will be
            at least 80% of the original image height and width. Default: 0.0 (no minimum size).
        interpolation (OpenCV flag): Flag that is used to specify the interpolation algorithm. Should be one of:
            cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.AREA, cv2.INTER_LANCZOS4.
            Default: cv2.INTER_LINEAR.
        p (float): Probability of applying the transform. Default: 1.0.

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - This transform ensures that all bounding boxes in the original image are fully contained within the
          cropped area. If it's not possible to find such a crop (e.g., when bounding boxes are too spread out),
          it will default to cropping the entire image.
        - After cropping, the result is resized to the specified (height, width) size.
        - Bounding box coordinates are adjusted to match the new image size.
        - Keypoints are moved along with the crop and scaled to the new image size.
        - If there are no bounding boxes in the image, it will fall back to a random crop.

    Mathematical Details:
        1. A crop region is selected that includes all bounding boxes.
        2. The crop size is determined by the erosion_rate:
           min_crop_size = (1 - erosion_rate) * original_size
        3. If the selected crop is smaller than min_crop_size, it's expanded to meet this requirement.
        4. The crop is then resized to the specified (height, width) size.
        5. Bounding box coordinates are transformed to match the new image size:
           new_coord = (old_coord - crop_start) * (new_size / crop_size)

    Example:
        >>> import numpy as np
        >>> import albumentations as A
        >>> image = np.random.randint(0, 256, (300, 300, 3), dtype=np.uint8)
        >>> bboxes = [(10, 10, 50, 50), (100, 100, 150, 150)]
        >>> transform = A.Compose([
        ...     A.RandomSizedBBoxSafeCrop(height=224, width=224, erosion_rate=0.2, p=1.0),
        ... ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['labels']))
        >>> transformed = transform(image=image, bboxes=bboxes, labels=['cat', 'dog'])
        >>> transformed_image = transformed['image']
        >>> transformed_bboxes = transformed['bboxes']
        # transformed_image will be a 224x224 image containing all original bounding boxes,
        # with their coordinates adjusted to the new image size.
    c                   @  s*   e Zd ZU edddZded< ded< dS )	z"RandomSizedBBoxSafeCrop.InitSchemar   r;   r   r<   r   r   r   Nr   r2   r2   r2   r3   rq     s
   
rq   r   Nr;   rr   r<   r=   )r7   r8   r   r   r@   r?   c                   s(   t  j|||d || _|| _|| _d S )Nr   )rB   rC   r7   r8   r   )rD   r7   r8   r   r   r@   r?   rE   r2   r3   rC     s    	z RandomSizedBBoxSafeCrop.__init__rG   rH   r   rI   c                 K  s(   t j|f| }t|| j| jf| jS ra   )rU   rV   r   r   r7   r8   r   )rD   rJ   rK   rL   rV   r2   r2   r3   rW   "  s    zRandomSizedBBoxSafeCrop.applyr_   c                 K  sP   t ||}|d |d  }|d |d  }| j| }| j| }tj|||dS )NrO   r!   rN   r   )r   r   )rU   rb   r7   r8   r   r   )rD   r`   rK   rL   r   r   r   r   r2   r2   r3   apply_to_keypoint+  s    

z)RandomSizedBBoxSafeCrop.apply_to_keypointr   r   c                   s   t   dS )Nr7   r8   r   )r7   r8   r   r   r   rE   r2   r3   r   :  s    z5RandomSizedBBoxSafeCrop.get_transform_init_args_names)r/   r0   r1   r   r    rj   rk   rl   rm   rn   r4   rq   r   r   rC   rW   r   r   rp   r2   r2   rE   r3   r)     s   8	c                      sZ  e Zd ZdZejejejejfZ	G dd de
Zddejddddejddf
dd	d
ddddd
ddd
 fddZddddd
dddddZddddd
dddddZddddddddd Zddddddd!d"d#Zed
d
d
dd$d%d&Zed'd
d
d'd(d)d*Zd+d+d+d,d-d.Zd'd/d0d1Zd2d/d3d4Zedd5d6d7d8Zd9d/d:d;Z  ZS )<r*   au  Crop and pad images by pixel amounts or fractions of image sizes.

    This transform allows for simultaneous cropping and padding of images. Cropping removes pixels from the sides
    (i.e., extracts a subimage), while padding adds pixels to the sides (e.g., black pixels). The amount of
    cropping/padding can be specified either in absolute pixels or as a fraction of the image size.

    Args:
        px (int, tuple of int, tuple of tuples of int, or None):
            The number of pixels to crop (negative values) or pad (positive values) on each side of the image.
            Either this or the parameter `percent` may be set, not both at the same time.
            - If int: crop/pad all sides by this value.
            - If tuple of 2 ints: crop/pad by (top/bottom, left/right).
            - If tuple of 4 ints: crop/pad by (top, right, bottom, left).
            - Each int can also be a tuple of 2 ints for a range, or a list of ints for discrete choices.
            Default: None.

        percent (float, tuple of float, tuple of tuples of float, or None):
            The fraction of the image size to crop (negative values) or pad (positive values) on each side.
            Either this or the parameter `px` may be set, not both at the same time.
            - If float: crop/pad all sides by this fraction.
            - If tuple of 2 floats: crop/pad by (top/bottom, left/right) fractions.
            - If tuple of 4 floats: crop/pad by (top, right, bottom, left) fractions.
            - Each float can also be a tuple of 2 floats for a range, or a list of floats for discrete choices.
            Default: None.

        pad_mode (int):
            OpenCV border mode used for padding. Default: cv2.BORDER_CONSTANT.

        pad_cval (number, tuple of number, or list of number):
            The constant value to use for padding if pad_mode is cv2.BORDER_CONSTANT.
            - If number: use this value for all channels.
            - If tuple of 2 numbers: use uniform random value between these numbers.
            - If list of numbers: use random choice from this list.
            Default: 0.

        pad_cval_mask (number, tuple of number, or list of number):
            Same as pad_cval but used for mask padding. Default: 0.

        keep_size (bool):
            If True, the output image will be resized to the input image size after cropping/padding.
            Default: True.

        sample_independently (bool):
            If True and ranges are used for px/percent, sample a value for each side independently.
            If False, sample one value and use it for all sides. Default: True.

        interpolation (int):
            OpenCV interpolation flag used for resizing if keep_size is True.
            Default: cv2.INTER_LINEAR.

        p (float):
            Probability of applying the transform. Default: 1.0.

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - This transform will never crop images below a height or width of 1.
        - When using pixel values (px), the image will be cropped/padded by exactly that many pixels.
        - When using percentages (percent), the amount of crop/pad will be calculated based on the image size.
        - Bounding boxes that end up fully outside the image after cropping will be removed.
        - Keypoints that end up outside the image after cropping will be removed.

    Example:
        >>> import albumentations as A
        >>> transform = A.Compose([
        ...     A.CropAndPad(px=(-10, 20, 30, -40), pad_mode=cv2.BORDER_REFLECT, p=1.0),
        ... ])
        >>> transformed = transform(image=image, mask=mask, bboxes=bboxes, keypoints=keypoints)
        >>> transformed_image = transformed['image']
        >>> transformed_mask = transformed['mask']
        >>> transformed_bboxes = transformed['bboxes']
        >>> transformed_keypoints = transformed['keypoints']
    c                   @  sf   e Zd ZU ded< ded< ded< ded< ded	< d
ed< d
ed< ded< eddddddZdS )zCropAndPad.InitSchemazPxType | NonepxzPercentType | Nonepercentr   pad_mode=ScalarType | tuple[ScalarType, ScalarType] | list[ScalarType]pad_cvalpad_cval_maskbool	keep_sizesample_independentlyr   r   r   r   r   r   c                 C  sD   | j d kr | jd kr d}t|| j d k	r@| jd k	r@d}t|| S )Nz=Both px and percent parameters cannot be None simultaneously.zOnly px or percent may be set!)r   r   r   r   r2   r2   r3   check_px_percent  s    z&CropAndPad.InitSchema.check_px_percentN)r/   r0   r1   r9   r   r   r2   r2   r2   r3   rq     s   
rq   Nr   Tr;   zint | list[int] | Nonezfloat | list[float] | Nonerr   r   r   r=   r<   )
r   r   r   r   r   r   r   r   r@   r?   c                   sD   t  j|
|	d || _|| _|| _|| _|| _|| _|| _|| _	d S rA   )
rB   rC   r   r   r   r   r   r   r   r   )rD   r   r   r   r   r   r   r   r   r@   r?   rE   r2   r3   rC     s    zCropAndPad.__init__rG   zSequence[int]r   r   )rJ   crop_params
pad_params	pad_valuer   rL   rM   c              
   K  s(   t |||||d d d || j| jS Nr[   rN   rU   Zcrop_and_padr   r   )rD   rJ   r  r  r  r   rL   r2   r2   r3   rW     s    	zCropAndPad.apply)r   r  r  pad_value_maskr   rL   rM   c              
   K  s(   t |||||d d d || j| jS r  r  )rD   r   r  r  r  r   rL   r2   r2   r3   apply_to_mask  s    	zCropAndPad.apply_to_maskrH   rd   )rY   r  r  result_shaperL   rM   c                 K  s   t ||||d d d |S r  )rU   Zcrop_and_pad_bboxes)rD   rY   r  r  r  rL   r2   r2   r3   r^     s    zCropAndPad.apply_to_bboxes)r`   r  r  r  rL   rM   c                 K  s"   t ||||d d d || jS r  )rU   Zcrop_and_pad_keypointsr   )rD   r`   r  r  r  rL   r2   r2   r3   rc     s    zCropAndPad.apply_to_keypoints)val1val2max_valrM   c                 C  sz   t |d }|d }|d }|| |k r0|d7 }|| krN||  }| }||7 }n||krj|| }|}||7 }| | || fS )Nr!   rN   )abs)r	  r
  r  ZregainZregain1Zregain2Zdiffr2   r2   r3   Z__prevent_zero  s    
zCropAndPad.__prevent_zeroz	list[int])r  r7   r8   rM   c           	      C  s|   | \}}}}|||  }|||  }|dk r>t |||\}}|dk rXt |||\}}t|dt|dt|dt|dgS )Nr!   r   )r*   _CropAndPad__prevent_zeror   )	r  r7   r8   toprightbottomleftZremaining_heightZremaining_widthr2   r2   r3   _prevent_zero  s    zCropAndPad._prevent_zerorv   rx   c                 C  sr  |d d d \}}| j d k	r(|  }nD|  }t|d | t|d | t|d | t|d | g}dd |D }| dd |D ||}|\}	}
}}||	||
 || g}|d |d  }|d |d  }||kr||krg }|\}	}
}}|	|||
g}t|r"||	| 7 }|||
 7 }ng }|p.d |p6d |d krDd n
| | j|d kr\d n
| | j||fd	S )
Nr[   rN   r   r!   rO   c                 S  s   g | ]}t |d qS r   )r   r   r   r2   r2   r3   r   0  s     z;CropAndPad.get_params_dependent_on_data.<locals>.<listcomp>c                 S  s   g | ]}t |d  qS r  )r   r  r2   r2   r3   r   2  s     )r  r  r  r  r  )	r   _get_px_params_get_percent_paramsrr   r  r   _get_pad_valuer   r   )rD   rL   ry   r7   r8   
new_paramsZpercent_paramsr  r  r  r  r  r  Zresult_rowsZresult_colsr2   r2   r3   r   "  s:    


z'CropAndPad.get_params_dependent_on_datar   c                   s    j d krd}t|t j tr0 j gd }nt j tkrt jr\ fddtdD }qtj	 j  }|gd }nLt j d tr j }n4t j d tkrdd  j D }ndd  j D }|S )Nzpx is not set   c                   s   g | ]}t j j qS r2   )rz   	randranger   r   r   r   r2   r3   r   T  s     z-CropAndPad._get_px_params.<locals>.<listcomp>r   c                 S  s   g | ]}t j| qS r2   )rz   r  r  r2   r2   r3   r   [  s     c                 S  s   g | ]}t |qS r2   rz   r   r  r2   r2   r3   r   ]  s     )
r   r   r   rr   r   r   r   r   rz   r  rD   r   rL   r   r2   r   r3   r  K  s     
zCropAndPad._get_px_paramszlist[float]c                   s    j d krd}t|t j tr0 j gd }nt j tkrt jr\ fddtdD }qtj	 j  }|gd }nPt j d t
tfr j }n4t j d tkrdd  j D }ndd  j D }|S )Nzpercent is not setr  c                   s   g | ]}t j j qS r2   )rz   r   r   r  r   r2   r3   r   j  s     z2CropAndPad._get_percent_params.<locals>.<listcomp>r   c                 S  s   g | ]}t j| qS r2   )rz   r   r  r2   r2   r3   r   q  s     c                 S  s   g | ]}t |qS r2   r  r  r2   r2   r3   r   s  s     )r   r   r   r<   r   r   r   r   rz   r   rr   r  r2   r   r3   r  a  s     
zCropAndPad._get_percent_paramsr   )r  rM   c                 C  s\   t | ttfr| S t| tkrR| \}}t |trFt |trFt||S t||S t| S ra   )	r   rr   r<   r   r   rz   r   r   r   )r  abr2   r2   r3   r  w  s    zCropAndPad._get_pad_valuer   c                 C  s   dS )N)r   r   r   r   r   r   r   r   r2   r   r2   r2   r3   r     s    z(CropAndPad.get_transform_init_args_names)r/   r0   r1   r   r    rj   rk   rl   rm   rn   r   rq   r   ZBORDER_CONSTANTr   rC   rW   r  r^   rc   ro   r  r  r   r  r  r  r   rp   r2   r2   rE   r3   r*   >  s8   N&
)c                      sv   e Zd ZdZejejejejfZ	G dd de
Zdddddddd	 fd
dZddddddZddddZ  ZS )r+   a/	  Randomly crops the input from its borders without resizing.

    This transform randomly crops parts of the input (image, mask, bounding boxes, or keypoints)
    from each of its borders. The amount of cropping is specified as a fraction of the input's
    dimensions for each side independently.

    Args:
        crop_left (float): The maximum fraction of width to crop from the left side.
            Must be in the range [0.0, 1.0]. Default: 0.1
        crop_right (float): The maximum fraction of width to crop from the right side.
            Must be in the range [0.0, 1.0]. Default: 0.1
        crop_top (float): The maximum fraction of height to crop from the top.
            Must be in the range [0.0, 1.0]. Default: 0.1
        crop_bottom (float): The maximum fraction of height to crop from the bottom.
            Must be in the range [0.0, 1.0]. Default: 0.1
        p (float): Probability of applying the transform. Default: 1.0

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - The actual amount of cropping for each side is randomly chosen between 0 and
          the specified maximum for each application of the transform.
        - The sum of crop_left and crop_right must not exceed 1.0, and the sum of
          crop_top and crop_bottom must not exceed 1.0. Otherwise, a ValueError will be raised.
        - This transform does not resize the input after cropping, so the output dimensions
          will be smaller than the input dimensions.
        - Bounding boxes that end up fully outside the cropped area will be removed.
        - Keypoints that end up outside the cropped area will be removed.

    Example:
        >>> import numpy as np
        >>> import albumentations as A
        >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> transform = A.RandomCropFromBorders(
        ...     crop_left=0.1, crop_right=0.2, crop_top=0.2, crop_bottom=0.1, p=1.0
        ... )
        >>> result = transform(image=image)
        >>> transformed_image = result['image']
        # The resulting image will have random crops from each border, with the maximum
        # possible crops being 10% from the left, 20% from the right, 20% from the top,
        # and 10% from the bottom. The image size will be reduced accordingly.
    c                   @  sv   e Zd ZU edddZded< edddZded< edddZded< edddZded< e	d	d
ddddZ
dS )z RandomCropFromBorders.InitSchemar   r;   r   r<   	crop_left
crop_rightcrop_topcrop_bottomr   r   r   r   c                 C  s<   | j | j dkrd}t|| j| j dkr8d}t|| S )Nr;   z1The sum of crop_left and crop_right must be <= 1.z1The sum of crop_top and crop_bottom must be <= 1.)r   r!  r   r"  r#  r   r2   r2   r3   validate_crop_values  s    z5RandomCropFromBorders.InitSchema.validate_crop_valuesN)r/   r0   r1   r	   r   r9   r!  r"  r#  r   r$  r2   r2   r2   r3   rq     s$   
rq   皙?Nr;   r<   r=   )r   r!  r"  r#  r@   r?   c                   s,   t  j||d || _|| _|| _|| _d S rA   )rB   rC   r   r!  r"  r#  )rD   r   r!  r"  r#  r@   r?   rE   r2   r3   rC     s
    	zRandomCropFromBorders.__init__rv   rw   rx   c           
      C  s   |d d d \}}t dt| j| }t t|d td| j | |}t dt| j| }t t|d td| j | |}||||f}	d|	iS )Nr[   rN   r   r!   rK   )rz   r   rr   r   r   r!  r"  r#  )
rD   rL   ry   r7   r8   rQ   rS   rR   rT   rK   r2   r2   r3   r     s    $$z2RandomCropFromBorders.get_params_dependent_on_datar   r   c                 C  s   dS )N)r   r!  r"  r#  r2   r   r2   r2   r3   r     s    z3RandomCropFromBorders.get_transform_init_args_names)r%  r%  r%  r%  Nr;   )r/   r0   r1   r   r    rj   rk   rl   rm   rn   r   rq   rC   r   r   rp   r2   r2   rE   r3   r+     s   /      )C
__future__r   r   rz   typingr   r   r   r   warningsr   r   Znumpyrg   Zpydanticr   r	   r
   r   Ztyping_extensionsr   r   Z&albumentations.augmentations.geometricr   r   Zalbumentations.core.bbox_utilsr   Zalbumentations.core.pydanticr   r   r   r   r   r   Z(albumentations.core.transforms_interfacer   r   Zalbumentations.core.typesr   r   r   r   r   r   r   r   r     rU   __all__	Exceptionr-   r4   r:   r"   r#   r$   r%   r   r   r&   r'   r(   r,   r)   r*   r+   r2   r2   r2   r3   <module>   s\    ,)08P{;  (Zll  X