U
    ,-e                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZmZ d dlZd dlZddlmZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& e% rd d
l'm(Z( e&)e*Z+G dd deZG dd deZ,G dd de,Z-ddhdhddhdhfZ.dd Z/d#ee0 e1e1dddZ2d$ee0e	e0 ee3e0f f ee0 e1e1e4dddZ5e!e,j6e,_6e,j6j7dk	re,j6j7j8dd d!d"e,j6_7dS )%    N)BytesIO)AnyDictIterableListOptionalTupleUnion   )custom_object_save)BatchFeature)center_crop	normalizerescale)ChannelDimension)
IMAGE_PROCESSOR_NAMEPushToHubMixinadd_model_info_to_auto_mapcached_file	copy_funcdownload_urlis_offline_modeis_remote_urlis_vision_availablelogging)Imagec                   @   s   e Zd ZdZdS )r   a  
    Holds the output of the image processor specific `__call__` methods.

    This class is derived from a python dictionary and can be used as a dictionary.

    Args:
        data (`dict`):
            Dictionary of lists/arrays/tensors returned by the __call__ method ('pixel_values', etc.).
        tensor_type (`Union[None, str, TensorType]`, *optional*):
            You can give a tensor_type here to convert the lists of integers in PyTorch/TensorFlow/Numpy Tensors at
            initialization.
    N)__name__
__module____qualname____doc__ r    r    d/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/transformers/image_processing_utils.pyr   4   s   r   c                
   @   s`  e Zd ZdZdZdd ZedddZed)e	ee
jf ee	ee
jf  eeee	eef  ed
ddZd*e	ee
jf edddZee	ee
jf eeeef eeef f dddZeeeef dddZeeef dddZee	ee
jf dddZedddZe	ee
jf ddd Zd!d" Zed+d$d%Ze	eee f d&d'd(ZdS ),ImageProcessingMixinz
    This is an image processor mixin used to provide saving/loading functionality for sequential and image feature
    extractors.
    Nc                 K   sx   | dd| _| D ]\\}}zt| || W q tk
rp } z$td| d| d|   |W 5 d}~X Y qX qdS )z'Set elements of `kwargs` as attributes.processor_classNz
Can't set z with value z for )pop_processor_classitemssetattrAttributeErrorloggererror)selfkwargskeyvalueerrr    r    r!   __init__L   s    zImageProcessingMixin.__init__)r#   c                 C   s
   || _ dS )z%Sets processor class as an attribute.N)r%   )r+   r#   r    r    r!   _set_processor_classX   s    z)ImageProcessingMixin._set_processor_classFmain)pretrained_model_name_or_path	cache_dirforce_downloadlocal_files_onlytokenrevisionc           
      K   s   ||d< ||d< ||d< ||d< | dd}|dk	rTtdt |dk	rPtd|}|dk	rd||d	< | j|f|\}	}| j|	f|S )
a  
        Instantiate a type of [`~image_processing_utils.ImageProcessingMixin`] from an image processor.

        Args:
            pretrained_model_name_or_path (`str` or `os.PathLike`):
                This can be either:

                - a string, the *model id* of a pretrained image_processor hosted inside a model repo on
                  huggingface.co. Valid model ids can be located at the root-level, like `bert-base-uncased`, or
                  namespaced under a user or organization name, like `dbmdz/bert-base-german-cased`.
                - a path to a *directory* containing a image processor file saved using the
                  [`~image_processing_utils.ImageProcessingMixin.save_pretrained`] method, e.g.,
                  `./my_model_directory/`.
                - a path or url to a saved image processor JSON *file*, e.g.,
                  `./my_model_directory/preprocessor_config.json`.
            cache_dir (`str` or `os.PathLike`, *optional*):
                Path to a directory in which a downloaded pretrained model image processor should be cached if the
                standard cache should not be used.
            force_download (`bool`, *optional*, defaults to `False`):
                Whether or not to force to (re-)download the image processor files and override the cached versions if
                they exist.
            resume_download (`bool`, *optional*, defaults to `False`):
                Whether or not to delete incompletely received file. Attempts to resume the download if such a file
                exists.
            proxies (`Dict[str, str]`, *optional*):
                A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
                'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request.
            token (`str` or `bool`, *optional*):
                The token to use as HTTP bearer authorization for remote files. If `True`, or not specified, will use
                the token generated when running `huggingface-cli login` (stored in `~/.huggingface`).
            revision (`str`, *optional*, defaults to `"main"`):
                The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
                git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
                identifier allowed by git.


                <Tip>

                To test a pull request you made on the Hub, you can pass `revision="refs/pr/<pr_number>".

                </Tip>

            return_unused_kwargs (`bool`, *optional*, defaults to `False`):
                If `False`, then this function returns just the final image processor object. If `True`, then this
                functions returns a `Tuple(image_processor, unused_kwargs)` where *unused_kwargs* is a dictionary
                consisting of the key/value pairs whose keys are not image processor attributes: i.e., the part of
                `kwargs` which has not been used to update `image_processor` and is otherwise ignored.
            subfolder (`str`, *optional*, defaults to `""`):
                In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can
                specify the folder name here.
            kwargs (`Dict[str, Any]`, *optional*):
                The values in kwargs of any keys which are image processor attributes will be used to override the
                loaded values. Behavior concerning key/value pairs whose keys are *not* image processor attributes is
                controlled by the `return_unused_kwargs` keyword parameter.

        Returns:
            A image processor of type [`~image_processing_utils.ImageProcessingMixin`].

        Examples:

        ```python
        # We can't instantiate directly the base class *ImageProcessingMixin* so let's show the examples on a
        # derived class: *CLIPImageProcessor*
        image_processor = CLIPImageProcessor.from_pretrained(
            "openai/clip-vit-base-patch32"
        )  # Download image_processing_config from huggingface.co and cache.
        image_processor = CLIPImageProcessor.from_pretrained(
            "./test/saved_model/"
        )  # E.g. image processor (or model) was saved using *save_pretrained('./test/saved_model/')*
        image_processor = CLIPImageProcessor.from_pretrained("./test/saved_model/preprocessor_config.json")
        image_processor = CLIPImageProcessor.from_pretrained(
            "openai/clip-vit-base-patch32", do_normalize=False, foo=False
        )
        assert image_processor.do_normalize is False
        image_processor, unused_kwargs = CLIPImageProcessor.from_pretrained(
            "openai/clip-vit-base-patch32", do_normalize=False, foo=False, return_unused_kwargs=True
        )
        assert image_processor.do_normalize is False
        assert unused_kwargs == {"foo": False}
        ```r4   r5   r6   r8   use_auth_tokenNVThe `use_auth_token` argument is deprecated and will be removed in v5 of Transformers.V`token` and `use_auth_token` are both specified. Please set only the argument `token`.r7   )r$   warningswarnFutureWarning
ValueErrorget_image_processor_dict	from_dict)
clsr3   r4   r5   r6   r7   r8   r,   r9   image_processor_dictr    r    r!   from_pretrained\   s&    [ z$ImageProcessingMixin.from_pretrained)save_directorypush_to_hubc           	      K   s  | dd}|dk	r@tdt |dddk	r8td||d< tj|r\t	d| dtj
|dd	 |r| d
d}| d|tjjd }| j|f|}| |}| jdk	rt| || d tj|t}| | td|  |r| j|||||dd |gS )as  
        Save an image processor object to the directory `save_directory`, so that it can be re-loaded using the
        [`~image_processing_utils.ImageProcessingMixin.from_pretrained`] class method.

        Args:
            save_directory (`str` or `os.PathLike`):
                Directory where the image processor JSON file will be saved (will be created if it does not exist).
            push_to_hub (`bool`, *optional*, defaults to `False`):
                Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the
                repository you want to push to with `repo_id` (will default to the name of `save_directory` in your
                namespace).
            kwargs (`Dict[str, Any]`, *optional*):
                Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method.
        r9   Nr:   r7   r;   zProvided path (z#) should be a directory, not a fileT)exist_okcommit_messagerepo_id)configzImage processor saved in )rH   r7   )r$   r<   r=   r>   getr?   ospathisfileAssertionErrormakedirssplitsepZ_create_repoZ_get_files_timestamps_auto_classr   joinr   to_json_filer)   infoZ_upload_modified_files)	r+   rE   rF   r,   r9   rH   rI   Zfiles_timestampsZoutput_image_processor_filer    r    r!   save_pretrained   sB     


z$ImageProcessingMixin.save_pretrained)r3   returnc                 K   s`  | dd}| dd}| dd}| dd}| dd}| dd}| d	d}	| d
d}
| dd}| dd}| dd}|dk	rtdt |dk	rtd|}d|d}|dk	r||d< t r|	std d}	t|}t	j
|}t	j
|rt	j
|t}t	j
|r&|}d}nt|r>|}t|}npt}z"t|||||||	|||
|d}W nH tk
r|    Y n2 tk
r   td| d| dt dY nX z0t|ddd}| }W 5 Q R X t|}W n( tjk
r   td| dY nX |r td |  ntd | d!|  d"|krX|sXt|d" ||d"< ||fS )#a  
        From a `pretrained_model_name_or_path`, resolve to a dictionary of parameters, to be used for instantiating a
        image processor of type [`~image_processor_utils.ImageProcessingMixin`] using `from_dict`.

        Parameters:
            pretrained_model_name_or_path (`str` or `os.PathLike`):
                The identifier of the pre-trained checkpoint from which we want the dictionary of parameters.
            subfolder (`str`, *optional*, defaults to `""`):
                In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can
                specify the folder name here.

        Returns:
            `Tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the image processor object.
        r4   Nr5   Fresume_downloadproxiesr7   r9   r6   r8   	subfolder Z_from_pipelineZ
_from_autor:   r;   image processor)	file_typefrom_auto_classZusing_pipelinez+Offline mode: forcing local_files_only=TrueT)	r4   r5   r[   rZ   r6   r7   
user_agentr8   r\   z Can't load image processor for 'z'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'z2' is the correct path to a directory containing a z filerutf-8encodingz"It looks like the config file at 'z' is not a valid JSON file.zloading configuration file z from cache at Zauto_map)r$   r<   r=   r>   r?   r   r)   rW   strrM   rN   isdirrU   r   rO   r   r   r   EnvironmentError	ExceptionopenreadjsonloadsJSONDecodeErrorr   )rB   r3   r,   r4   r5   rZ   r[   r7   r9   r6   r8   r\   Zfrom_pipeliner`   ra   is_localZimage_processor_fileZresolved_image_processor_filereadertextrC   r    r    r!   r@   
  s     








 z-ImageProcessingMixin.get_image_processor_dict)rC   c                 K   s   |  }|dd}d|kr2d|kr2|d|d< d|krPd|krP|d|d< | f |}g }| D ](\}}t||rft||| || qf|D ]}||d qtd|  |r||fS |S dS )a  
        Instantiates a type of [`~image_processing_utils.ImageProcessingMixin`] from a Python dictionary of parameters.

        Args:
            image_processor_dict (`Dict[str, Any]`):
                Dictionary that will be used to instantiate the image processor object. Such a dictionary can be
                retrieved from a pretrained checkpoint by leveraging the
                [`~image_processing_utils.ImageProcessingMixin.to_dict`] method.
            kwargs (`Dict[str, Any]`):
                Additional parameters from which to initialize the image processor object.

        Returns:
            [`~image_processing_utils.ImageProcessingMixin`]: The image processor object instantiated from those
            parameters.
        return_unused_kwargsFsizeZ	crop_sizeNzImage processor )copyr$   r&   hasattrr'   appendr)   rW   )rB   rC   r,   rr   Zimage_processorZ	to_remover-   r.   r    r    r!   rA   |  s$    

zImageProcessingMixin.from_dictrY   c                 C   s   t | j}| jj|d< |S )z
        Serializes this instance to a Python dictionary.

        Returns:
            `Dict[str, Any]`: Dictionary of all the attributes that make up this image processor instance.
        Zimage_processor_type)rt   deepcopy__dict__	__class__r   )r+   outputr    r    r!   to_dict  s    zImageProcessingMixin.to_dict)	json_filec              	   C   s6   t |ddd}| }W 5 Q R X t|}| f |S )a  
        Instantiates a image processor of type [`~image_processing_utils.ImageProcessingMixin`] from the path to a JSON
        file of parameters.

        Args:
            json_file (`str` or `os.PathLike`):
                Path to the JSON file containing the parameters.

        Returns:
            A image processor of type [`~image_processing_utils.ImageProcessingMixin`]: The image_processor object
            instantiated from that JSON file.
        rb   rc   rd   )rj   rk   rl   rm   )rB   r}   rp   rq   rC   r    r    r!   from_json_file  s    
z#ImageProcessingMixin.from_json_filec                 C   sb   |   }| D ] \}}t|tjr| ||< q|dd}|dk	rN||d< tj|dddd S )z
        Serializes this instance to a JSON string.

        Returns:
            `str`: String containing all the attributes that make up this feature_extractor instance in JSON format.
        r%   Nr#      T)indent	sort_keys
)	r|   r&   
isinstancenpndarraytolistr$   rl   dumps)r+   
dictionaryr-   r.   r%   r    r    r!   to_json_string  s    z#ImageProcessingMixin.to_json_string)json_file_pathc              	   C   s,   t |ddd}||   W 5 Q R X dS )z
        Save this instance to a JSON file.

        Args:
            json_file_path (`str` or `os.PathLike`):
                Path to the JSON file in which this image_processor instance's parameters will be saved.
        wrc   rd   N)rj   writer   )r+   r   writerr    r    r!   rV     s    z!ImageProcessingMixin.to_json_filec                 C   s   | j j d|   S )N )rz   r   r   r+   r    r    r!   __repr__  s    zImageProcessingMixin.__repr__AutoImageProcessorc                 C   sD   t |ts|j}ddlm  m} t||s:t| d|| _dS )a	  
        Register this class with a given auto class. This should only be used for custom image processors as the ones
        in the library are already mapped with `AutoImageProcessor `.

        <Tip warning={true}>

        This API is experimental and may have some slight breaking changes in the next releases.

        </Tip>

        Args:
            auto_class (`str` or `type`, *optional*, defaults to `"AutoImageProcessor "`):
                The auto class to register this new image processor with.
        r   Nz is not a valid auto class.)	r   rf   r   Ztransformers.models.automodelsautoru   r?   rT   )rB   Z
auto_classZauto_moduler    r    r!   register_for_auto_class  s    

z,ImageProcessingMixin.register_for_auto_class)image_url_or_urlsc                    sl   ddi}t |tr$ fdd|D S t |trVtj|d|d}|  tt|j	S t
dt| dS )	z
        Convert a single or a list of urls into the corresponding `PIL.Image` objects.

        If a single url is passed, the return value will be a single object. If a list is passed a list of objects is
        returned.
        z
User-AgentzuMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36c                    s   g | ]}  |qS r    )fetch_images).0xr   r    r!   
<listcomp>  s     z5ImageProcessingMixin.fetch_images.<locals>.<listcomp>T)streamheadersz=only a single or a list of entries is supported but got type=N)r   listrf   requestsrL   raise_for_statusr   rj   r   contentr?   type)r+   r   r   responser    r   r!   r     s    

z!ImageProcessingMixin.fetch_images)NFFNr2   )F)r   )r   r   r   r   rT   r0   rf   r1   classmethodr	   rM   PathLiker   boolrD   rX   r   r   r   r@   rA   r|   r~   r   rV   r   r   r   r   r    r    r    r!   r"   D   sD        q<q,r"   c                	       s   e Zd Z fddZedddZedddZdeje	e
eeef  e
eeef  ejd	d
dZdejee	ee	 f ee	ee	 f e
eeef  e
eeef  ejdddZdejeeef e
eeef  e
eeef  ejdddZ  ZS )BaseImageProcessorc                    s   t  jf | d S )N)superr0   )r+   r,   rz   r    r!   r0     s    zBaseImageProcessor.__init__rw   c                 K   s   | j |f|S )z)Preprocess an image or a batch of images.)
preprocessr+   Zimagesr,   r    r    r!   __call__   s    zBaseImageProcessor.__call__c                 K   s   t dd S )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_formatrY   c                 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)r   meanstdr   r   rY   c                 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   G  s    !   zBaseImageProcessor.normalize)r   rs   r   r   rY   c                 K   sN   t |}d|ksd|k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 )rs   r   r   )get_size_dictr?   keysr   )r+   r   rs   r   r   r,   r    r    r!   r   l  s    zBaseImageProcessor.center_crop)NN)NN)NN)r   r   r   r0   r   r   r   r   r   floatr   r	   rf   r   r   r   r   r   intr   __classcell__r    r    r   r!   r     s>     %  )  
r   r   r   shortest_edgelongest_edgec                 C   s6   t | tsdS t|  }tD ]}||kr dS qdS )NFT)r   dictsetr   VALID_SIZE_DICT_KEYS)	size_dictZsize_dict_keysZallowed_keysr    r    r!   is_valid_size_dict  s    
r   T)max_sizedefault_to_squareheight_width_orderc                 C   s   t | tr(|r(|d k	rtd| | dS t | trR|sRd| i}|d k	rN||d< |S t | ttfrv|rv| d | d dS t | ttfr|s| d | d dS | d kr|d k	r|rtdd|iS td|  d S )	NzLCannot specify both size as an int, with default_to_square=True and max_size)r   r   r   r   r   r
   z7Cannot specify both default_to_square=True and max_sizez+Could not convert size input to size dict: )r   r   r?   tupler   )rs   r   r   r   r   r    r    r!   convert_to_size_dict  s$    
r   rs   )rs   r   r   r   rY   c              
   C   sh   t | ts<t| |||}t| dt d|  d| d n| }t|sdt| 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   r   r   r)   rW   r   r   r?   r   )rs   r   r   r   
param_namer   r    r    r!   r     s    
r   r^   r   zimage processor file)objectZobject_classZobject_files)NTT)NNTTrs   )9rt   rl   rM   r<   ior   typingr   r   r   r   r   r   r	   numpyr   r   Zdynamic_module_utilsr   Zfeature_extraction_utilsr   ZBaseBatchFeatureZimage_transformsr   r   r   Zimage_utilsr   utilsr   r   r   r   r   r   r   r   r   r   ZPILr   Z
get_loggerr   r)   r"   r   r   r   r   r   r   rf   r   r   rF   r   formatr    r    r    r!   <module>   sd   $0
   [x            ,  