o
    h4                     @   s8  d dl Z d dlmZ d dlmZmZ d dlZddlm	Z	m
Z
 ddlmZmZmZ ddlmZmZ ddlmZ eeZd	d
gZG dd de
ZddhdhddhdhddhfZdd Z	d%dee dedefddZ					d&deeee eeef f dee dededef
ddZ de!d e"de!fd!d"Z#d#d$ Z$dS )'    N)Iterable)OptionalUnion   )BatchFeatureImageProcessingMixin)center_crop	normalizerescale)ChannelDimensionget_image_size)loggingprocessor_classimage_processor_typec                       s(  e Zd Z fddZdefddZdefddZ		dd	ejd
e	de
eeef  de
eeef  dejf
ddZ		dd	ejdee	ee	 f dee	ee	 f de
eeef  de
eeef  dejfddZ		dd	ejdeeef de
eeef  de
eeef  dejf
ddZ fddZ  ZS )BaseImageProcessorc                    s   t  jdi | d S )N )super__init__)selfkwargs	__class__r   w/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/image_processing_utils.pyr   %   s   zBaseImageProcessor.__init__returnc                 K   s   | j |fi |S )z)Preprocess an image or a batch of images.)
preprocessr   imagesr   r   r   r   __call__(   s   zBaseImageProcessor.__call__c                 K   s   t d)Nz=Each image processor must implement its own preprocess method)NotImplementedErrorr   r   r   r   r   ,   s   zBaseImageProcessor.preprocessNimagescaledata_formatinput_data_formatc                 K   s   t |f|||d|S )a  
        Rescale an image by a scale factor. image = image * scale.

        Args:
            image (`np.ndarray`):
                Image to rescale.
            scale (`float`):
                The scaling factor to rescale pixel values by.
            data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format for the output image. If unset, the channel dimension format of the input
                image is used. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.

        Returns:
            `np.ndarray`: The rescaled image.
        )r    r!   r"   )r
   )r   r   r    r!   r"   r   r   r   r   r
   /   s   zBaseImageProcessor.rescalemeanstdc                 K   s   t |f||||d|S )aZ  
        Normalize an image. image = (image - image_mean) / image_std.

        Args:
            image (`np.ndarray`):
                Image to normalize.
            mean (`float` or `Iterable[float]`):
                Image mean to use for normalization.
            std (`float` or `Iterable[float]`):
                Image standard deviation to use for normalization.
            data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format for the output image. If unset, the channel dimension format of the input
                image is used. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.

        Returns:
            `np.ndarray`: The normalized image.
        )r#   r$   r!   r"   )r	   )r   r   r#   r$   r!   r"   r   r   r   r   r	   O   s   !zBaseImageProcessor.normalizesizec                 K   sN   t |}d|vsd|vrtd|  t|f|d |d f||d|S )a	  
        Center crop an image to `(size["height"], size["width"])`. If the input size is smaller than `crop_size` along
        any edge, the image is padded with 0's and then center cropped.

        Args:
            image (`np.ndarray`):
                Image to center crop.
            size (`Dict[str, int]`):
                Size of the output image.
            data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format for the output image. If unset, the channel dimension format of the input
                image is used. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
        heightwidthz=The size dictionary must have keys 'height' and 'width'. Got )r%   r!   r"   )get_size_dict
ValueErrorkeysr   )r   r   r%   r!   r"   r   r   r   r   r   t   s   zBaseImageProcessor.center_cropc                    s   t   }|dd  |S )N_valid_processor_keys)r   to_dictpop)r   encoder_dictr   r   r   r,      s   
zBaseImageProcessor.to_dict)NN)__name__
__module____qualname__r   r   r   r   npndarrayfloatr   r   strr   r
   r   r	   dictintr   r,   __classcell__r   r   r   r   r   $   s\    
%
)

'r   r&   r'   shortest_edgelongest_edge
max_height	max_widthc                 C   s6   t | tsdS t|  }tD ]	}||kr dS qdS )NFT)
isinstancer6   setr*   VALID_SIZE_DICT_KEYS)	size_dictsize_dict_keysallowed_keysr   r   r   is_valid_size_dict   s   
rC   Tmax_sizedefault_to_squareheight_width_orderc                 C   s   t | tr|r|d urtd| | dS t | tr)|s)d| i}|d ur'||d< |S t | ttfr;|r;| d | d dS t | ttfrM|sM| d | d dS | d u r_|d ur_|r[tdd|iS td|  )	NzLCannot specify both size as an int, with default_to_square=True and max_size)r&   r'   r9   r:   r   r   z7Cannot specify both default_to_square=True and max_sizez+Could not convert size input to size dict: )r=   r7   r)   tuplelist)r%   rD   rE   rF   r@   r   r   r   convert_to_size_dict   s$   
rI   r%   r   c              
   C   sh   t | tst| |||}t| dt d|  d| d n| }t|s2t| dt d|  |S )a4  
    Converts the old size parameter in the config into the new dict expected in the config. This is to ensure backwards
    compatibility with the old image processor configs and removes ambiguity over whether the tuple is in (height,
    width) or (width, height) format.

    - If `size` is tuple, it is converted to `{"height": size[0], "width": size[1]}` or `{"height": size[1], "width":
    size[0]}` if `height_width_order` is `False`.
    - If `size` is an int, and `default_to_square` is `True`, it is converted to `{"height": size, "width": size}`.
    - If `size` is an int and `default_to_square` is False, it is converted to `{"shortest_edge": size}`. If `max_size`
      is set, it is added to the dict as `{"longest_edge": max_size}`.

    Args:
        size (`Union[int, Iterable[int], Dict[str, int]]`, *optional*):
            The `size` parameter to be cast into a size dictionary.
        max_size (`Optional[int]`, *optional*):
            The `max_size` parameter to be cast into a size dictionary.
        height_width_order (`bool`, *optional*, defaults to `True`):
            If `size` is a tuple, whether it's in (height, width) or (width, height) order.
        default_to_square (`bool`, *optional*, defaults to `True`):
            If `size` is an int, whether to default to a square image or not.
    z9 should be a dictionary on of the following set of keys: z, got z. Converted to .z- must have one of the following set of keys: )	r=   r6   rI   loggerinfor?   rC   r)   r*   )r%   rD   rF   rE   
param_namer@   r   r   r   r(      s   
r(   original_sizepossible_resolutionsc                 C   s   | \}}d}d}t d}|D ]=\}}t|| || }	t||	 t||	 }
}t|
| || }|| | }||ksC||krK||k rK|}|}||f}q|S )a  
    Selects the best resolution from a list of possible resolutions based on the original size.

    This is done by calculating the effective and wasted resolution for each possible resolution.

    The best fit resolution is the one that maximizes the effective resolution and minimizes the wasted resolution.

    Args:
        original_size (tuple):
            The original size of the image in the format (height, width).
        possible_resolutions (list):
            A list of possible resolutions in the format [(height1, width1), (height2, width2), ...].

    Returns:
        tuple: The best fit resolution in the format (height, width).
    Nr   inf)r4   minr7   )rN   rO   original_heightoriginal_widthbest_fitmax_effective_resolutionmin_wasted_resolutionr&   r'   r    downscaled_widthdownscaled_heighteffective_resolutionwasted_resolutionr   r   r   select_best_resolution   s    r[   c                 C   sp   t | |d\}}|\}}|| }|| }||k r(|}	tt|| |}
|
|	fS |}
tt|| |}	|
|	fS )zu
    Given an image and a target resolution, calculate the output size of the image after cropping to the target
    )channel_dim)r   rQ   mathceil)r   target_resolutionr"   rR   rS   target_heighttarget_widthscale_wscale_h	new_width
new_heightr   r   r   get_patch_output_size#  s   rf   )NTT)NNTTr%   )%r]   collections.abcr   typingr   r   numpyr2   image_processing_baser   r   image_transformsr   r	   r
   image_utilsr   r   utilsr   
get_loggerr/   rK   INIT_SERVICE_KWARGSr   r?   rC   r7   boolrI   r6   r5   r(   rG   rH   r[   rf   r   r   r   r   <module>   s\   
~	

,&