a
    yµÿfÆ  ã                   @   s’   d Z ddlZddlmZ ddlZddlZddlZejZ	ej
feedœdd„Zdeejdœdd	„Zeejd
œdd„ZejZejZdd„ Zdd„ ZdS )zDMonkey patches to update/extend functionality of existing functions.é    N)ÚPath©ÚfilenameÚflagsc                 C   s   t  t | tj¡|¡S )zý
    Read an image from a file.

    Args:
        filename (str): Path to the file to read.
        flags (int, optional): Flag that can take values of cv2.IMREAD_*. Defaults to cv2.IMREAD_COLOR.

    Returns:
        (np.ndarray): The read image.
    )Úcv2ZimdecodeÚnpÚfromfileZuint8r   © r	   úU/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/utils/patches.pyÚimread   s    r   )r   Úimgc                 C   s>   z$t  t| ƒj||¡d  | ¡ W dS  ty8   Y dS 0 dS )a2  
    Write an image to a file.

    Args:
        filename (str): Path to the file to write.
        img (np.ndarray): Image to write.
        params (list of ints, optional): Additional parameters. See OpenCV documentation.

    Returns:
        (bool): True if the file was written, False otherwise.
    é   TFN)r   Zimencoder   ÚsuffixÚtofileÚ	Exception)r   r   Úparamsr	   r	   r
   Úimwrite   s
    r   ©ZwinnameÚmatc                 C   s   t |  d¡ ¡ |ƒ dS )z—
    Displays an image in the specified window.

    Args:
        winname (str): Name of the window.
        mat (np.ndarray): Image to be shown.
    Úunicode_escapeN)Ú_imshowÚencodeÚdecoder   r	   r	   r
   Úimshow0   s    r   c                  O   s.   ddl m} |r d|vr d|d< t| i |¤ŽS )aG  
    Load a PyTorch model with updated arguments to avoid warnings.

    This function wraps torch.load and adds the 'weights_only' argument for PyTorch 1.13.0+ to prevent warnings.

    Args:
        *args (Any): Variable length argument list to pass to torch.load.
        **kwargs (Any): Arbitrary keyword arguments to pass to torch.load.

    Returns:
        (Any): The loaded PyTorch object.

    Note:
        For PyTorch versions 2.0 and above, this function automatically sets 'weights_only=False'
        if the argument is not provided, to avoid deprecation warnings.
    r   )Ú
TORCH_1_13Zweights_onlyF)Zultralytics.utils.torch_utilsr   Ú_torch_load)ÚargsÚkwargsr   r	   r	   r
   Ú
torch_load@   s    r   c                  O   sj   t dƒD ]\}zt| i |¤ŽW   S  tyb } z*|dkr<|‚t d| d ¡ W Y d}~qd}~0 0 qdS )a8  
    Optionally use dill to serialize lambda functions where pickle does not, adding robustness with 3 retries and
    exponential standoff in case of save failure.

    Args:
        *args (tuple): Positional arguments to pass to torch.save.
        **kwargs (Any): Keyword arguments to pass to torch.save.
    é   é   é   N)ÚrangeÚ_torch_saveÚRuntimeErrorÚtimeÚsleep)r   r   ÚiÚer	   r	   r
   Ú
torch_saveY   s    	r)   )N)Ú__doc__r%   Úpathlibr   r   Únumpyr   Ztorchr   r   ZIMREAD_COLORÚstrÚintr   Zndarrayr   Úloadr   Úsaver#   r   r)   r	   r	   r	   r
   Ú<module>   s   