a
    aDfY                     @   sZ  d dl mZ d dlmZ d dlmZmZmZmZm	Z	 d dl
Z
d dlZd dlmZmZ d dlmZ d dlmZmZmZmZ g dZed	Zed
deddeddeddejdejdejdejdiZeje
jeje
j ej!e
j"eje
j#ej$e
j%ed
e
jede
j ede
j"ede
j#ede
j%i
Z&dZ'dZ(eej Z)e	e*ef ej+dddZ,e	e*ef ej+dddZ-e	e*ef ej+dddZ.eeej+ef ej+f eeej+ef ej+f dddZ/ej+eje0ej+d d!d"Z1e	ej+e2eje3f e2d#d$d%Z4eeeef ef eeeef ef dd&d'Z5eeej+ef ej+f eeej+ef ej+f dd(d)Z6eeej+ef ej+f eeej+ef ej+f dd*d+Z7ej+e2d,d-d.Z8ej+e9d,d/d0Z:ej+e9d,d1d2Z;ej+e9d,d3d4Z<ej+dd,d5d6Z=eeej+ef ej+f eeej+gej+f d7d8d9Z>ee0e0f e0e0ee* dd:d;d<Z?dS )=    )wraps)Path)AnyCallableOptionalTupleUnionN)Concatenate	ParamSpecangle_to_2pi_range)MONO_CHANNEL_DIMENSIONSNUM_MULTI_CHANNEL_DIMENSIONSNUM_RGB_CHANNELSKeypointInternalType)read_bgr_imageread_rgb_imageread_grayscaleMAX_VALUES_BY_DTYPENPDTYPE_TO_OPENCV_DTYPEclippedget_opencv_dtype_from_numpyangle_2pi_rangeclippreserve_channel_dimensure_contiguousis_rgb_imageis_grayscale_imageis_multispectral_imageget_num_channelsnon_rgb_warning_maybe_process_in_chunksPuint8   uint16i  uint32l    float32      ?int32float64      )pathreturnc                 C   s   t t| t jS N)cv2imreadstrZIMREAD_COLORr-    r4   o/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/albumentations/augmentations/utils.pyr   D   s    r   c                 C   s   t | }t|tjS r/   )r   r0   ZcvtColorZCOLOR_BGR2RGB)r-   imager4   r4   r5   r   H   s    r   c                 C   s   t t| t jS r/   )r0   r1   r2   ZIMREAD_GRAYSCALEr3   r4   r4   r5   r   M   s    r   )funcr.   c                    s,   t  tjtjtjtjd fdd}|S )Nimgargskwargsr.   c                    s0   | j }t|d}t | g|R i |||S )Nr(   )dtyper   getr   )r9   r:   r;   r<   maxvalr7   r4   r5   wrapped_functionR   s    z!clipped.<locals>.wrapped_functionr   npndarrayr"   r:   r;   r7   r@   r4   r?   r5   r   Q   s    "r   )r9   r<   r>   r.   c                 C   s   t | d||S )Nr   )rB   r   Zastype)r9   r<   r>   r4   r4   r5   r   [   s    r   )valuer.   c                 C   s   t | tjr| j} t|  S )zReturn a corresponding OpenCV dtype for a numpy's dtype
    :param value: Input dtype of numpy array
    :return: Corresponding dtype for OpenCV
    )
isinstancerB   rC   r<   r   )rE   r4   r4   r5   r   _   s    r   c                    s(   t  ttjtjtd fdd}|S )N)keypointr:   r;   r.   c                    s6    | g|R i |d d \}}}}||t ||fS )Nr,   r   )rG   r:   r;   xyasr?   r4   r5   r@   l   s    &z)angle_2pi_range.<locals>.wrapped_function)r   r   r"   r:   r;   rD   r4   r?   r5   r   i   s    r   c                    s,   t  tjtjtjtjd fdd}|S )zPreserve dummy channel dim.r8   c                    s   | j } | g|R i |}t|tkrP|d dkrPt|j tkrPtj|ddS t|tkrt|j tkr|d d d d df S |S )N   )Zaxisr   )shapelenr   r   rB   expand_dims)r9   r:   r;   rN   resultr?   r4   r5   r@   y   s    

z.preserve_channel_dim.<locals>.wrapped_functionrA   rD   r4   r?   r5   r   t   s    "r   c                    s,   t  tjtjtjtjd fdd}|S )z$Ensure that input img is contiguous.r8   c                    s&   t j| dgd}  | g|R i |S )NZC_CONTIGUOUS)requirements)rB   require)r9   r:   r;   r?   r4   r5   r@      s    z+ensure_contiguous.<locals>.wrapped_functionrA   rD   r4   r?   r5   r      s    "r   )r6   r.   c                 C   s   t | jtkr| jd S dS )Nr+   rM   )rO   rN   r   r6   r4   r4   r5   r      s    r   c                 C   s   t | tkS r/   )r   r   rT   r4   r4   r5   r      s    r   c                 C   s   t | dkS )NrM   r   rT   r4   r4   r5   r      s    r   c                 C   s   t | }|dvS )N>   rM      rU   )r6   num_channelsr4   r4   r5   r      s    r   c                 C   s8   t | s4d}t| r|d7 }t| r,|d7 }t|d S )Nz,This transformation expects 3-channel imagesz[
You can convert your grayscale image to RGB using cv2.cvtColor(image, cv2.COLOR_GRAY2RGB))z?
This transformation cannot be applied to multi-spectral images)r   r   r   
ValueError)r6   messager4   r4   r5   r       s    r    )
process_fnr;   r.   c                    s&   t tjtjd fdd}|S )an  Wrap OpenCV function to enable processing images with more than 4 channels.

    Limitations:
        This wrapper requires image to be the first argument and rest must be sent via named arguments.

    Args:
        process_fn: Transform function (e.g cv2.resize).
        kwargs: Additional parameters.

    Returns:
        numpy.ndarray: Transformed image.

    )r9   r.   c                    s   t | }|tkrg }td|dD ]}|| tkrtdD ]P}| d d d d || || d f }|fi  }t|d}|| q8q | d d d d ||d f }|fi  }|| q t|S | fi  S )Nr   r,   r+   rM   rL   )r   FOURrangeTWOrB   rP   appendZdstack)r9   rW   chunksindexichunkr;   rZ   r4   r5   __process_fn   s    &
z._maybe_process_in_chunks.<locals>.__process_fn)r   rB   rC   )rZ   r;   rd   r4   rc   r5   r!      s    r!   )rE   lower_boundupper_boundnamer.   c                    sZ   t  fdd| D s4td| d  d d| d | d ksVt|d	|  d
S )a  Checks if the given value is within the specified bounds

    Args:
        value: The value to check and convert. Can be a single float or a tuple of floats.
        lower_bound: The lower bound for the range check.
        upper_bound: The upper bound for the range check.
        name: The name of the parameter being checked. Used for error messages.

    Raises:
        ValueError: If the value is outside the bounds or if the tuple values are not ordered correctly.
    c                 3   s&   | ]} |  kokn  V  qd S r/   r4   ).0rH   re   rf   r4   r5   	<genexpr>       zcheck_range.<locals>.<genexpr>zAll values in z must be within [z, z] for tuple inputs.r   rM   z2 tuple values must be ordered as (min, max). Got: N)allrX   )rE   re   rf   rg   r4   ri   r5   check_range   s    rm   )@	functoolsr   pathlibr   typingr   r   r   r   r   r0   numpyrB   Ztyping_extensionsr	   r
   Z#albumentations.core.keypoints_utilsr   Zalbumentations.core.typesr   r   r   r   __all__r"   r<   r#   r%   r&   r'   r   ZCV_8UZCV_16Ur)   ZCV_32SZCV_32Fr*   ZCV_64Fr   r]   r[   ZBIG_INTEGERr2   rC   r   r   r   r   floatr   intobjectr   r   r   r   r   boolr   r   r   r    r!   rm   r4   r4   r4   r5   <module>   sr   




8
 *