Ë
    ÃÓ^jO$  ã                  óF  — d Z ddlmZ ddl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mZ ddlmZmZ ddlmZ erdd	lmZ g d
¢Z ed«      Z e	dej6                  ¬«      Z e	dedef   ¬«      Zdd„Zdd„Zdd„Z 	 	 	 	 dd„Z!dd„Z"dd„Z# G d„ d«      Z$dd„Z%y)aÕ  Module containing utility functions for augmentation operations.

This module provides a collection of helper functions and utilities used throughout
the augmentation pipeline. It includes functions for image loading, type checking,
error handling, mathematical operations, and decorators that add functionality to
other functions in the codebase. These utilities help ensure consistent behavior
and simplify common operations across different augmentation transforms.
é    )ÚannotationsN©Úwraps)ÚTYPE_CHECKINGÚAnyÚCallableÚTypeVarÚcast)Úis_grayscale_imageÚis_multispectral_imageÚis_rgb_image)ÚConcatenateÚ	ParamSpec)Úangle_to_2pi_range)ÚPath)Úangle_2pi_rangeÚnon_rgb_errorÚread_bgr_imageÚread_grayscaleÚread_rgb_imageÚPÚT)ÚboundÚF.c                ó\   — t        j                  t        | «      t         j                  «      S )zÀRead an image in BGR format from the specified path.

    Args:
        path (str | Path): Path to the image file.

    Returns:
        np.ndarray: Image in BGR format as a numpy array.

    )Úcv2ÚimreadÚstrÚIMREAD_COLOR©Úpaths    úm/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/albumentations/augmentations/utils.pyr   r   ,   s   € ô �:‰:”c˜$“i¤×!1Ñ!1Ó2Ð2ó    c                ó`   — t        | «      }t        j                  |t        j                  «      S )a%  Read an image in RGB format from the specified path.

    This function reads an image in BGR format using OpenCV and then
    converts it to RGB format.

    Args:
        path (str | Path): Path to the image file.

    Returns:
        np.ndarray: Image in RGB format as a numpy array.

    )r   r   ÚcvtColorÚCOLOR_BGR2RGB)r!   Úimages     r"   r   r   9   s%   € ô ˜4Ó €EÜ�<‰<˜œs×0Ñ0Ó1Ð1r#   c                ó\   — t        j                  t        | «      t         j                  «      S )z·Read a grayscale image from the specified path.

    Args:
        path (str | Path): Path to the image file.

    Returns:
        np.ndarray: Grayscale image as a numpy array.

    )r   r   r   ÚIMREAD_GRAYSCALEr    s    r"   r   r   J   s   € ô �:‰:”c˜$“i¤×!5Ñ!5Ó6Ð6r#   c                ó0   ‡ — t        ‰ «      dˆ fd„«       }|S )u$  Decorator to normalize angle values to the range [0, 2Ï€).

    This decorator wraps a function that processes keypoints, ensuring that
    angle values (stored in the 4th column, index 3) are normalized to the
    range [0, 2Ï€) after the wrapped function executes.

    Args:
        func (Callable): Function that processes keypoints and returns a numpy array.
            The function should take a keypoints array as its first parameter.

    Returns:
        Callable: Wrapped function that normalizes angles after processing keypoints.

    c                ó’   •—  ‰| g|¢­i |¤Ž}t        |«      dkD  r+|j                  d   dkD  rt        |d d …df   «      |d d …df<   |S )Nr   é   é   )ÚlenÚshaper   )Ú	keypointsÚargsÚkwargsÚresultÚfuncs       €r"   Úwrapped_functionz)angle_2pi_range.<locals>.wrapped_functioni   sP   ø€ á�iÐ1 $Ò1¨&Ñ1ˆÜˆv‹;˜Š?˜vŸ|™|¨A™°Ò2Ü-¨f²Q¸°T©lÓ;ˆF’1�a�4‰LØˆr#   )r0   ú
np.ndarrayr1   zP.argsr2   zP.kwargsÚreturnr6   r   )r4   r5   s   ` r"   r   r   W   s#   ø€ ô$ ˆ4ƒ[ôó ðð Ðr#   c                ót   — t        | «      s-d}t        | «      r|dz  }t        | «      r|dz  }t        |«      ‚y)aé  Check if the input image is RGB and raise a ValueError if it's not.

    This function is used to ensure that certain transformations are only applied to
    RGB images. It provides helpful error messages for grayscale and multi-spectral images.

    Args:
        image (np.ndarray): The input image to check. Expected to be a numpy array
                            representing an image.

    Raises:
        ValueError: If the input image is not an RGB image (i.e., does not have exactly 3 channels).
                    The error message includes specific instructions for grayscale images
                    and a note about incompatibility with multi-spectral images.

    Note:
        - RGB images are expected to have exactly 3 channels.
        - Grayscale images (1 channel) will trigger an error with conversion instructions.
        - Multi-spectral images (more than 3 channels) will trigger an error stating incompatibility.

    Example:
        >>> import numpy as np
        >>> rgb_image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> non_rgb_error(rgb_image)  # No error raised
        >>>
        >>> grayscale_image = np.random.randint(0, 256, (100, 100), dtype=np.uint8)
        >>> non_rgb_error(grayscale_image)  # Raises ValueError with conversion instructions
        >>>
        >>> multispectral_image = np.random.randint(0, 256, (100, 100, 5), dtype=np.uint8)
        >>> non_rgb_error(multispectral_image)  # Raises ValueError stating incompatibility

    z,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 imagesN)r   r   r   Ú
ValueError)r'   Úmessages     r"   r   r   s   sH   € ô@ ˜ÔØ@ˆÜ˜eÔ$ØÐuÑuˆGÜ! %Ô(ØÐYÑYˆGä˜Ó!Ð!ð r#   c           	     ó’   ‡‡— t        ˆˆfd„| D «       «      st        d|› d‰› d‰› d�«      ‚| d   | d   k  st        |›d| › �«      ‚y	)
a  Checks if the given value is within the specified bounds

    Args:
        value (tuple[float, float]): The value to check and convert. Can be a single float or a tuple of floats.
        lower_bound (float): The lower bound for the range check.
        upper_bound (float): The upper bound for the range check.
        name (str | None): 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  ó>   •K  — | ]  }‰|cxk  xr ‰k  nc –— Œ y ­w©N© )Ú.0ÚxÚlower_boundÚupper_bounds     €€r"   ú	<genexpr>zcheck_range.<locals>.<genexpr>ª   s   øè ø€ Ò>°1ˆ{˜aÖ. ;×.Ð.Ñ>ùs   ƒzAll values in z must be within [z, z] for tuple inputs.r   r,   z2 tuple values must be ordered as (min, max). Got: N)Úallr9   )ÚvaluerA   rB   Únames    `` r"   Úcheck_rangerG   �   sd   ù€ ô Ô>¸Ô>Ô>Ü˜>¨$¨Ð/@ÀÀÈRÐP[È}Ð\oÐpÓqÐqØ�‰8�u˜Q‘xÒÜ˜D˜8Ð#UÐV[ÐU\Ð]Ó^Ð^ð  r#   c                  óF   — e Zd Zd	d
d„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Z	y)ÚPCANc                ó`   — |�|dk  rt        d«      ‚|| _        d | _        d | _        d | _        y )Nr   z/Number of components must be greater than zero.)r9   Ún_componentsÚmeanÚcomponents_Úexplained_variance_)ÚselfrK   s     r"   Ú__init__zPCA.__init__±   s:   € ØÐ#¨¸Ò(9ÜÐNÓOÐOØ(ˆÔØ'+ˆŒ	Ø.2ˆÔØ6:ˆÕ r#   c                ó,  — |j                  t        j                  d¬«      }|j                  \  }}| j                  €t        ||«      | _        t        j                  |d | j                  ¬«      \  | _        }}|| _	        |j                  «       | _        y )NF©Úcopy)rL   ÚmaxComponents)ÚastypeÚnpÚfloat64r/   rK   Úminr   ÚPCACompute2rL   rM   ÚflattenrN   )rO   r@   Ú	n_samplesÚ
n_featuresÚeigenvectorsÚeigenvaluess         r"   ÚfitzPCA.fit¹   s~   € Ø�H‰H”R—Z‘Z eˆHÓ,ˆØ !§¡Ñˆ	�:ð ×ÑÐ$Ü # I¨zÓ :ˆDÔä/2¯©¸qÀtÐ[_×[lÑ[lÔ/mÑ,ˆŒ	�< Ø'ˆÔØ#.×#6Ñ#6Ó#8ˆÕ r#   c                óÈ   — | j                   €t        d«      ‚|j                  t        j                  d¬«      }t        j                  || j                  | j                   «      S )NúgThis PCA instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.FrR   )rM   r9   rU   rV   rW   r   Ú
PCAProjectrL   ©rO   r@   s     r"   Ú	transformzPCA.transformÅ   sV   € Ø×ÑÐ#ÜðUóð ð �H‰H”R—Z‘Z eˆHÓ,ˆÜ�~‰~˜a §¡¨D×,<Ñ,<Ó=Ð=r#   c                óF   — | j                  |«       | j                  |«      S r=   )r_   rd   rc   s     r"   Úfit_transformzPCA.fit_transformÎ   s   € Ø�‰�ŒØ�~‰~˜aÓ Ð r#   c                ó†   — | j                   €t        d«      ‚t        j                  || j                  | j                   «      S )Nra   )rM   r9   r   ÚPCABackProjectrL   rc   s     r"   Úinverse_transformzPCA.inverse_transformÒ   sB   € Ø×ÑÐ#ÜðUóð ô ×!Ñ! ! T§Y¡Y°×0@Ñ0@ÓAÐAr#   c                óŒ   — | j                   €t        d«      ‚t        j                  | j                   «      }| j                   |z  S )NzdThis PCA instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.)rN   r9   rV   Úsum)rO   Útotal_variances     r"   Úexplained_variance_ratiozPCA.explained_variance_ratioÚ   sE   € Ø×#Ñ#Ð+ÜØvóð ô Ÿ™ × 8Ñ 8Ó9ˆØ×'Ñ'¨.Ñ8Ð8r#   c                óH   — t        j                  | j                  «       «      S r=   )rV   Úcumsumrm   )rO   s    r"   Ú#cumulative_explained_variance_ratioz'PCA.cumulative_explained_variance_ratioâ   s   € Ü�y‰y˜×6Ñ6Ó8Ó9Ð9r#   r=   )rK   z
int | Noner7   ÚNone)r@   r6   r7   rq   )r@   r6   r7   r6   )r7   r6   )
Ú__name__Ú
__module__Ú__qualname__rP   r_   rd   rf   ri   rm   rp   r>   r#   r"   rI   rI   °   s&   „ ô;ó
9ó>ó!óBó9ô:r#   rI   c                ó   ‡ — dˆ fd„}|S )Nc                ó\   •‡ — t        j                  ‰ «      dˆ ˆfd„«       }t        d|«      S )Nc                 óŒ   •— t        | «      dkD  r| d   }n‰|v r|‰   }nt        d‰› �«      ‚t        |«      dk(  r|S  ‰| i |¤ŽS )Nr   zMissing required argument: )r.   r9   )r1   r2   Úarrayr4   Ú
param_names      €€r"   Úwrapperz6handle_empty_array.<locals>.decorator.<locals>.wrapperè   s^   ø€ ô �4‹y˜1Š}Ø˜Q™‘à˜vÑ%Ø˜zÑ*‘ä Ð#>¸z¸lÐ!KÓLÐLä�5‹z˜QŠØ�Ù˜Ð( Ñ(Ð(r#   r   )r1   r   r2   r   r7   r   )Ú	functoolsr   r
   )r4   rz   ry   s   ` €r"   Ú	decoratorz%handle_empty_array.<locals>.decoratorç   s-   ù€ Ü	�‰˜Ó	õ	)ó 
ð	)ô �C˜Ó!Ð!r#   )r4   r   r7   r   r>   )ry   r|   s   ` r"   Úhandle_empty_arrayr}   æ   s   ø€ õ"ð$ Ðr#   )r!   z
str | Pathr7   r6   )r4   ú0Callable[Concatenate[np.ndarray, P], np.ndarray]r7   r~   )r'   r6   r7   rq   )
rE   ztuple[float, float]rA   ÚfloatrB   r   rF   z
str | Noner7   rq   )ry   r   r7   zCallable[[F], F])&Ú__doc__Ú
__future__r   r{   r   Útypingr   r   r   r	   r
   r   ÚnumpyrV   Úalbucore.utilsr   r   r   Útyping_extensionsr   r   Ú#albumentations.core.keypoints_utilsr   Úpathlibr   Ú__all__r   Úndarrayr   r   r   r   r   r   r   rG   rI   r}   r>   r#   r"   ú<module>rŠ      s°   ðñõ #ã Ý ß >Õ >ã 
Û ÷ñ ÷
 5å BáÝò€ñ ˆcƒN€ÙˆC�r—z‘zÔ"€ÙˆC�x  S Ñ)Ô*€ó
3ó2ó"
7ðØ
:ðà5óó8'"óT_÷&3:ñ 3:ôlr#   