
    ^j(                         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 ddlmZmZ dd	lmZmZ  G d
 ded      Ze G d de             ZdgZy)    N)
functional   )TorchvisionBackend)BatchFeatureget_patch_output_sizeselect_best_resolution)divide_to_patches)ChannelDimensionPILImageResamplingSizeDictget_image_size)ImagesKwargsUnpack)
TensorTypeauto_docstringc                   F    e Zd ZU dZeed<   eed<   eee      ed<   eed<   y)AriaImageProcessorKwargsa  
    max_image_size (`int`, *optional*, defaults to `self.max_image_size`):
        Maximum image size. Must be either 490 or 980.
    min_image_size (`int`, *optional*, defaults to `self.min_image_size`):
        Minimum image size. Images smaller than this in any dimension will be scaled up.
    split_resolutions (`list[list[int]]`, *optional*, defaults to `self.split_resolutions`):
        A list of possible resolutions as (height, width) pairs for splitting high-resolution images into patches.
    split_image (`bool`, *optional*, defaults to `self.split_image`):
        Whether to split the image into patches using the best matching resolution from `split_resolutions`.
    max_image_sizemin_image_sizesplit_resolutionssplit_imageN)__name__
__module____qualname____doc__int__annotations__listbool     y/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/aria/image_processing_aria.pyr   r   !   s)    	 DI&r!   r   F)totalc                       e Zd Zg dZeZej                  Zg dZ	g dZ
dZdZdZdZdZdZdZdee   f fd	Zd
ededee   fdZdddeddddfdZdddeddfdZdddeee      deddded   f
dZ	 	 	 	 	 d'ded   dedededeee   z  dz  deee   z  dz  dedz  deez  dz  ded ed!eee      dz  d"edddefd#Z d(d$ed%efd&Z! xZ"S ))AriaImageProcessorpixel_values
pixel_mask	num_crops)      ?r*   r*     P  FNTkwargsc                     |j                  d      %g d}|D cg c]  }|d   dz  |d   dz  g c}|d<   t        |   di | y c c}w )Nr   ))      )r/   r   )r/      )r/      )r/      )r/      )r/      )r0   r1   )r0   r   )r0   r0   )r0   r/   )r   r/   )r   r0   )r1   r/   )r1   r0   )r2   r/   )r3   r/   )r4   r/   )r5   r/   r     r/   r    )getsuper__init__)selfr-   default_resolutionsel	__class__s       r"   r9   zAriaImageProcessor.__init__C   s]    ::)*2 #{Pc*d"BqECKA+E*dF&'"6" +es   Aoriginal_resolutiontarget_resolutionreturnc                 v    |\  }}|\  }}t        ||z
  d      \  }}t        ||z
  d      \  }	}
||	||z   |	|
z   gS )zNGet padding size for patching, returns [left, top, right, bottom] for tvF.pad.r0   )divmod)r:   r>   r?   original_heightoriginal_widthtarget_heighttarget_widthpaste_xr_xpaste_yr_ys              r"   _get_padding_sizez$AriaImageProcessor._get_padding_sizeI   sW    *='&7#|l^;Q?mo=qA'C-3??r!   imageztorch.Tensorresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                 |    t        ||t        j                        \  }}| j                  |t	        ||      |      S )zFResize an image to a target resolution while maintaining aspect ratio.input_data_formatheightwidth)r   r
   FIRSTresizer   )r:   rL   r?   rM   
new_height	new_widths         r"   _resize_for_patchingz'AriaImageProcessor._resize_for_patchingQ   s?     !6$8H8N8N!

I {{5(*I"NPXYYr!   c                     t        ||t        j                        }| j                  ||      }t	        j
                  ||      S )zCPad an image to a target resolution while maintaining aspect ratio.rO   )padding)r   r
   rT   rK   tvFpad)r:   rL   r?   new_resolutionrZ   s        r"   _pad_for_patchingz$AriaImageProcessor._pad_for_patching]   s>     /u6G[k[q[qr((9JKwwug..r!   grid_pinpoints
patch_sizec                     t        |t              st        d      t        |t        j
                        }t        ||      }| j                  |||      }| j                  ||      }t        ||      }	|	S )a  
        Process an image with variable resolutions by dividing it into patches.

        Args:
            image (`torch.Tensor`):
                The input image to be processed (channels-first format).
            grid_pinpoints (`list[list[int]]`):
                A list of possible resolutions as (height, width) pairs.
            patch_size (`int`):
                Size of each square patch to divide the image into.
            resample (`PILImageResampling | tvF.InterpolationMode | int | None`):
                Resampling filter to use when resizing.

        Returns:
            `list[torch.Tensor]`: A list of image patches in channels-first format.
        z6grid_pinpoints must be a list of possible resolutions.)channel_dim)r`   )

isinstancer   	TypeErrorr   r
   rT   r   rX   r^   r	   )
r:   rL   r_   r`   rM   
image_sizebest_resolutionresized_imagepadded_imagepatchess
             r"   get_image_patchesz$AriaImageProcessor.get_image_patchesg   sp    . .$/TUU#E7G7M7MN
0^L11%(S--m_M#LZHr!   images
do_rescalerescale_factordo_normalize
image_mean	image_stddisable_groupingreturn_tensorsr   r   r   r   c           	      ^   |	dvrt        d      g }g }d }|D ]>  }|r| j                  |||	|      }n|g}|t        |      |kD  rt        |      }|D ]   }|j                  d   |j                  d   }}|	t	        ||      z  }||k\  rt	        t        ||z        |
      }|	}n|	}t	        t        ||z        |
      }| j                  |t        ||      |      }|	|z
  }|	|z
  }t        j                  |dd||g      }t        j                  |	|	ft        j                        }d|d |d |f<   |j                  |       |j                  |        A t        j                  |d	      }| j                  ||||||      }t        j                  |d	      }t!        |||d
|      S )N)r6   r+   z(max_image_size must be either 490 or 980rQ   r   )dtypeT)dimr&   )datatensor_type)
ValueErrorrj   lenshapemaxr   rU   r   r[   r\   torchzerosr   appendstackrescale_and_normalizer   )r:   rk   rl   rm   rn   ro   rp   rq   rr   r   r   r   r   rM   r-   pixel_masksprocessed_cropsr)   rL   crop_images
crop_imagehwscalenew_hnew_wpadding_bottompadding_rightr(   stacked_imagesstacked_maskss                                  r"   _preprocesszAriaImageProcessor._preprocess   s   " +GHH	 	3E"44U<M~_gh$g C$4y$@,	) 3
!''+Z-=-=b-A1&Q26AI?E*E*EAI?E![[XURW5XZbc
!/%!7 . 6 WWZ!Q~1VW
"[[..)IQVQ[Q[\
-1
6E66E6>*"":.&&z2'3	3< _!<33JjR[
 KQ7 .+&
 '
 	
r!   rR   rS   c                 >   |j                  d| j                        }|j                  d| j                        }|j                  d| j                        }t	        ||f|      \  }}|sd}	|	S t        j                  ||z        t        j                  ||z        z  }	|	S )a  
        A utility that returns number of image patches for a given image size.

        Args:
            height (`int`):
                Height of the input image.
            width (`int`):
                Width of the input image.
            images_kwargs (`dict`, *optional*):
                Any kwargs to override defaults of the image processor.

        Returns:
            `int`: Number of patches per image.
        r   r   r   r/   )r7   r   r   r   r   mathceil)
r:   rR   rS   images_kwargsr   r   r   resized_heightresized_widthnum_patchess
             r"   get_number_of_image_patchesz.AriaImageProcessor.get_number_of_image_patches   s     $''t7G7GH&**+;T=P=PQ)--.A4CYCYZ(>Pa(b%   	
  >N:;diiXfHf>gg 	
 r!   )r+   r,   NFN)N)#r   r   r   model_input_namesr   valid_kwargsr   BICUBICrM   ro   rp   r   r   r   r   do_convert_rgbrl   rn   r   r9   tupler   r   rK   rX   r^   rj   r   floatstrr   r   r   r   __classcell__)r=   s   @r"   r%   r%   3   s   C+L!))H JINNKNJL#(@!A #@U @u @Y]^aYb @
Z
Z !
Z L	
Z
 

Z// !/ 
	/ T#Y 	
 L 
n	V "!48!NRC
^$C
 C
 	C

 C
 DK'$.C
 4;&-C
 +C
 j(4/C
 C
 C
  S	?T1C
 C
 LC
  
!C
J# c r!   r%   )r   r~   torchvision.transforms.v2r   r[   image_processing_backendsr   image_processing_utilsr   r   r   image_transformsr	   image_utilsr
   r   r   r   processing_utilsr   r   utilsr   r   r   r%   __all__r    r!   r"   <module>r      s`   (   7 ; a a 1 Y Y 4 /|5 $ r+ r rj  
 r!   