
    sg-              
       B   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	m
Z
 ddlmZmZ  e       rd dlmZ  e       rd dlZ ed	       G d
 d             Z G d de
      Zdeeef   dededeeef   fdZddddee   ddfdZdee   dee   fdZded   dee   fdZy)    N)	dataclass)AnyIterableListOptionalTuple   )BaseImageProcessor)is_torch_availableis_torchvision_available)ComposeT)frozenc                   l    e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   dZ
eed<   dZeed<   d	 Zy)
SizeDictz>
    Hashable dictionary to store image size information.
    Nheightwidthlongest_edgeshortest_edge
max_height	max_widthc                 P    t        | |      rt        | |      S t        d| d      )NzKey z not found in SizeDict.)hasattrgetattrKeyError)selfkeys     [/var/www/html/venv/lib/python3.12/site-packages/transformers/image_processing_utils_fast.py__getitem__zSizeDict.__getitem__,   s.    44%%cU"9:;;    )__name__
__module____qualname____doc__r   int__annotations__r   r   r   r   r   r    r   r   r   r      sJ     FCE3L#M3JIs<r   r   c                   f     e Zd ZdZddZd	dZ ej                  d      dd       Z fdZ	 xZ
S )
BaseImageProcessorFastNc                     t         )zV
        Given the input settings e.g. do_resize, build the image transforms.
        )NotImplementedErrorr   kwargss     r   _build_transformsz(BaseImageProcessorFast._build_transforms5   s
     "!r   c                 t    |j                         D ]%  \  }}|| j                  vst        d| d| d       y )NzInvalid transform parameter =.)items_transform_params
ValueError)r   r,   kvs       r   _validate_paramsz'BaseImageProcessorFast._validate_params;   sF    LLN 	JDAq... #?s!A3a!HII	Jr   r	   )maxsizec                 J     | j                   di |  | j                  di |S )Nr&   )r6   r-   r+   s     r   get_transformsz%BaseImageProcessorFast.get_transforms@   s+    ''%t%%///r   c                 H    t         |          }|j                  dd        |S )Nr2   )superto_dictpop)r   encoder_dict	__class__s     r   r<   zBaseImageProcessorFast.to_dictE   s&    w(,d3r   )returnr   )r@   N)r    r!   r"   r2   r-   r6   	functools	lru_cacher9   r<   __classcell__)r?   s   @r   r(   r(   2   s?    "J
 Y#0 $0 r   r(   
image_sizer   r   r@   c                 x    | \  }}||z  }||z  }t        ||      }t        ||z        }t        ||z        }	||	fS )a  
    Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio.
    Important, even if image_height < max_height and image_width < max_width, the image will be resized
    to at least one of the edges be equal to max_height or max_width.

    For example:
        - input_size: (100, 200), max_height: 50, max_width: 50 -> output_size: (25, 50)
        - input_size: (100, 200), max_height: 200, max_width: 500 -> output_size: (200, 400)

    Args:
        image_size (`Tuple[int, int]`):
            The image to resize.
        max_height (`int`):
            The maximum allowed height.
        max_width (`int`):
            The maximum allowed width.
    )minr$   )
rD   r   r   r   r   height_scalewidth_scale	min_scale
new_height	new_widths
             r   #get_image_size_for_max_height_widthrL   K   sV    , MFE&Le#KL+.IVi'(JEI%&Iy  r   tensorztorch.Tensoraxisc                 n    || j                         S 	 | j                  |      S # t        $ r | cY S w xY w)zF
    Squeezes a tensor, but only if the axis specified has dim 1.
    )rN   )squeezer3   )rM   rN   s     r   safe_squeezerQ   j   s@     |~~~~4~(( s   & 44valuesc                 J    t        |  D cg c]  }t        |       c}S c c}w )zO
    Return the maximum value across all indices of an iterable of values.
    )zipmax)rR   values_is     r   max_across_indicesrW   w   s      +.v,7hCM777s    imagesc                 b    t        | D cg c]  }|j                   c}      \  }}}||fS c c}w )zH
    Get the maximum height and width across all images in a batch.
    )rW   shape)rX   img_r   r   s        r   get_max_height_widthr]   ~   s5    
  22O3992OPAz9	"" 3Ps   ,)N)rA   dataclassesr   typingr   r   r   r   r   image_processing_utilsr
   utils.import_utilsr   r   torchvision.transformsr   torchr   r(   r$   rL   rQ   rW   r]   r&   r   r   <module>rd      s      ! 7 7 6 L . $< < <$/ 2!c3h!! ! 38_	!>
 
x} 
 
8x} 8c 8#n!5 #%* #r   