L i5 ddlZddlmZddlmZmZddlZddlm Z m Z ddl m Z m Z mZddlmZmZddlmZdd lmZej,eZd d gZed Gdde ZddhdhddhdhddhfZdZ d"deededefdZ d#deeeeee e!effdeededede f dZ"de#de$de#fd Z%d!Z&y)$N)Iterable)OptionalUnion) BatchFeatureImageProcessingMixin) center_crop normalizerescale)ChannelDimensionget_image_size)logging)requiresprocessor_classimage_processor_type)vision)backendsceZdZfdZedefdZdefdZdefdZ dde jde de eeefd e eeefde jf d Z dde jd ee ee fd ee ee fde eeefd e eeefde jf d Z dde jdeeefde eeefd e eeefde jf dZfdZxZS)BaseImageProcessorc $t|di|y)N)super__init__)selfkwargs __class__s i/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/image_processing_utils.pyrzBaseImageProcessor.__init__'s "6"returncy)zv `bool`: Whether or not this image processor is a fast processor (backed by PyTorch and TorchVision). Fr)rs ris_fastzBaseImageProcessor.is_fast*s rc (|j|fi|S)z)Preprocess an image or a batch of images.) preprocessrimagesrs r__call__zBaseImageProcessor.__call__1stv000rc td)Nz=Each image processor must implement its own preprocess method)NotImplementedErrorr$s rr#zBaseImageProcessor.preprocess5s!"abbrimagescale data_formatinput_data_formatc "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 )rr)r*r+r,rs rr zBaseImageProcessor.rescale8s<urE{Vgrkqrrrmeanstdc $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 )rr)r.r/r+r,rs rr zBaseImageProcessor.normalizeXs,B  #;Rc gm  rsizec t|}d|vsd|vrtd|jt|f|d|df||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. heightwidthz=The size dictionary must have keys 'height' and 'width'. Got )r1r+r,) get_size_dict ValueErrorkeysr )rr)r1r+r,rs rr zBaseImageProcessor.center_crop}sp8T" 4 7$#6\]a]f]f]h\ijk k  x.$w-0#/     rcHt|}|jdd|S)N_valid_processor_keys)rto_dictpop)r encoder_dictrs rr:zBaseImageProcessor.to_dicts&w( 0$7r)NN)__name__ __module__ __qualname__rpropertyboolr!rr&r#npndarrayfloatrrstrr r rr dictintr r: __classcell__)rs@rrr%s# 1L1clc?CDH szzsseC)9$9:; s $E#/?*?$@A s sJ?CDH # zz# E8E?*+# 5(5/) * # eC)9$9:; # $E#/?*?$@A #  # R?CDH % zz% 38n% eC)9$9:; % $E#/?*?$@A %  % Nrrr3r4 shortest_edge longest_edge max_height max_widthc|t|tsyt|j}tD] }||k(s yy)NFT) isinstancerFsetr7VALID_SIZE_DICT_KEYS) size_dictsize_dict_keys allowed_keyss ris_valid_size_dictrTs? i &)*N, \ ) rmax_sizedefault_to_squareheight_width_ordercZt|tr|r| td||dSt|tr|s d|i}|||d<|St|ttfr |r |d|ddSt|ttfr |s |d|ddS|||r tdd|iStd|) NzLCannot specify both size as an int, with default_to_square=True and max_size)r3r4rIrJrrz7Cannot specify both default_to_square=True and max_sizez+Could not convert size input to size dict: )rNrGr6tuplelist)r1rUrVrWrQs rconvert_to_size_dictr[s$!2  kl l.. D# '8$d+  (0In % D5$- (-?q'DG44 D5$- (1Cq'DG44 (. VW W)) B4&I JJrr1rc t|ts4t||||}tj |dt d|d|dn|}t |s%t|dt d|j|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: ) rNrFr[loggerinforPrTr6r7)r1rUrWrV param_namerQs rr5r5s8 dD !(x9JL^_  lSThSiioptouv&Kq *  i (lGH\G]]cdmdrdrdtcu v   r original_sizepossible_resolutionsc|\}}d}d}td}|D]f\}}t||z ||z } t|| zt|| z} } t| | z||z} ||z| z } | |kDs | |k(sY| |ks_| }| }||f}h|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). Nrinf)rDminrG)rarboriginal_heightoriginal_widthbest_fitmax_effective_resolutionmin_wasted_resolutionr3r4r*downscaled_widthdownscaled_heighteffective_resolutionwasted_resolutions rselect_best_resolutionros"'4#O^H !%L- ' EN*F_,DE.1.52H.I3afOfKg+"#36G#GZiIij"V^/CC ": : $< rsV $"E==9(   H %  ;A-A AJwn%; fjKSMK=AK^bK:AE"#" ) 5hsmT#s(^;< =)sm)) )  )X#%#t#PU#L!r