Ë
    S^(hW  ã                   óü   — d dl Z d dlmZmZmZmZmZ d dlZddl	m
Z
mZmZ ddlmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZ ddlmZ dej>                  d	e d
eej>                     fd„Z!d„ Z" G d„ de
«      Z#dgZ$y)é    N)ÚIterableÚListÚOptionalÚTupleÚUnioné   )ÚBaseImageProcessorÚBatchFeatureÚselect_best_resolution)ÚPaddingModeÚconvert_to_rgbÚpadÚresizeÚto_channel_dimension_format)	ÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚget_image_sizeÚinfer_channel_dimension_formatÚmake_flat_list_of_imagesÚto_numpy_arrayÚvalid_imagesÚvalidate_preprocess_arguments)Ú
TensorTypeÚimageÚ
patch_sizeÚreturnc                 ó  — g }t        | |¬«      \  }}t        d||«      D ]^  }t        d||«      D ]L  }|t        j                  k(  r| |||z   …|||z   …f   }n| dd…|||z   …|||z   …f   }|j	                  |«       ŒN Œ` |S )a  
    Divides an image into patches of a specified size.

    Args:
        image (`np.array`):
            The input image.
        patch_size (`int`):
            The size of each patch.
        input_data_format (`ChannelDimension` or `str`):
            The channel dimension format of the input image.

    Returns:
        list: A list of np.array representing the patches.
    ©Úchannel_dimr   N)r   Úranger   ÚLASTÚappend)	r   r   Úinput_data_formatÚpatchesÚheightÚwidthÚiÚjÚpatchs	            úl/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/aria/image_processing_aria.pyÚdivide_to_patchesr,   *   s°   € ð €GÜ" 5Ð6GÔH�M€FˆEÜ�1�f˜jÓ)ò "ˆÜ�q˜% Ó,ò 	"ˆAØ Ô$4×$9Ñ$9Ò9Ø˜a ! j¡.Ð0°!°a¸*±nÐ2DÐDÑE‘àša  Q¨¡^Ð!3°Q¸¸Z¹Ð5GÐGÑH�Ø�N‰N˜5Õ!ñ	"ð"ð €Nó    c                 óê   — t        | |¬«      \  }}|\  }}||z  }||z  }||k  r(|}	t        t        j                  ||z  «      |«      }
|
|	fS |}
t        t        j                  ||z  «      |«      }	|
|	fS )Nr   )r   ÚminÚmathÚceil)r   Útarget_resolutionr$   Úoriginal_heightÚoriginal_widthÚtarget_heightÚtarget_widthÚscale_wÚscale_hÚ	new_widthÚ
new_heights              r+   Ú_get_patch_output_sizer;   F   s’   € Ü&4°UÐHYÔ&ZÑ#€O�^Ø"3Ñ€M�<à˜^Ñ+€GØ˜oÑ-€Gà�ÒØ ˆ	ÜœŸ™ ?°WÑ#<Ó=¸}ÓMˆ
ð
 �yÐ Ð ð #ˆ
ÜœŸ	™	 .°7Ñ":Ó;¸\ÓJˆ	à�yÐ Ð r-   c                   ó2  ‡ — e Zd ZdZg d¢Zddddddddej                  f	dee   d	ee   d
e	de	de
eee	e	f         de
e   de
e   de
e   defˆ fd„Zdddddddddej                  dfdeeee   f   de
eeee   f      d	e
eeee   f      d
e
e	   de
e	   de
e   de
e   de
e   dede
eeef      de
e   de
eeef      fd„Zdej,                  dededej,                  fd„Zdej,                  dededej,                  fd„Zej6                  dddfdej8                  dee	ee	e	f   eee	e	f      f   ded eeee   f   de
eeef      de
eeef      dej8                  fd!„Zdej,                  d"eee	e	f      d#e	dedededeej,                     fd$„Zˆ xZ S )%ÚAriaImageProcessoraœ  
    A vision processor for the Aria model that handles image preprocessing.
    Initialize the AriaImageProcessor.

    Args:
        image_mean (`list`, *optional*, defaults to [0.5, 0.5, 0.5]):
            Mean values for normalization.
        image_std (`list`, *optional*, defaults to [0.5, 0.5, 0.5]):
            Standard deviation values for normalization.
        max_image_size (`int`, *optional*, defaults to 980):
            Maximum image size.
        min_image_size (`int`, *optional*, defaults to 336):
            Minimum image size.
        split_resolutions (`list`, *optional*, defaults to a list of optimal,resolutions as tuples):
            The optimal resolutions for splitting the image.
        split_image (`bool`, *optional*, defaults to `False`):
            Whether to split the image.
        do_convert_rgb (`bool`, *optional*, defaults to `True`):
            Whether to convert the image to RGB.
        do_normalize (`bool`, *optional*, defaults to `True`):
            Whether to normalize the image.
        resample (PILImageResampling, *optional*, defaults to `BICUBIC`):
            The resampling filter to use if resizing the image.
    ©Úpixel_valuesÚ
pixel_maskÚ	num_cropsNéÔ  iP  FTÚ
image_meanÚ	image_stdÚmax_image_sizeÚmin_image_sizeÚsplit_resolutionsÚsplit_imageÚdo_convert_rgbÚdo_normalizeÚresamplec
                 ó
  •— t        ‰| �  di |
¤Ž |€g d¢}|€g d¢}|| _        || _        || _        || _        || _        |€!g d¢}|D �cg c]  }|d   dz  |d   dz  f‘Œ }}|| _        || _        || _	        |	| _
        y c c}w )N)ç      à?rM   rM   ))é   é   )rN   r   )rN   é   )rN   é   )rN   é   )rN   é   )rN   é   )rO   rP   )rO   r   )rO   rO   )rO   rN   )r   rN   )r   rO   )rP   rN   )rP   rO   )rQ   rN   )rR   rN   )rS   rN   )rT   rN   r   éê  rN   © )ÚsuperÚ__init__rE   rF   rC   rD   rH   rG   rI   rJ   rK   )ÚselfrC   rD   rE   rF   rG   rH   rI   rJ   rK   ÚkwargsÚelÚ	__class__s               €r+   rX   zAriaImageProcessor.__init__s   s®   ø€ ô 	‰ÑÑ"˜6Ò"àÐÚ(ˆJØÐÚ'ˆIØ,ˆÔØ,ˆÔØ$ˆŒØ"ˆŒØ&ˆÔØÐ$ò !yÐØFWÖ XÀ " Q¡%¨#¡+¨r°!©u°s©{Ò!;Ð XÐÐ XØ!2ˆÔØ,ˆÔØ(ˆÔØ ˆ�ùò	 !Ys   Á
B ÚptÚimagesÚreturn_tensorsÚdata_formatr$   c           	      óz  — |�|n| j                   }|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j
                  }|�|n| j                  }|	�|	n| j                  }	|dvrt        d«      ‚t        |«      }t        |«      st        d«      ‚t        ||||	¬«       |r|D �cg c]  }t        |«      ‘Œ }}|D �cg c]  }t        |«      ‘Œ }}|€t        |d   «      }g }g }d}|D �]j  }|r"| j                  || j                   ||	||¬«      }n|g}|�t#        |«      |kD  rt#        |«      }|D �]  }t%        |«      \  }}|t'        ||«      z  }||k\  rt'        t)        ||z  «      |«      |f}n|t'        t)        ||z  «      |«      f}t+        |||	||¬«      }||d   z
  ||d	   z
  }}t-        |d|fd|ff||¬«      }t/        j0                  ||ft2        ¬
«      }d	|d|d   …d|d	   …f<   |j5                  |«       |r>| j7                  |dz  | j                   | j                  ||¬«      }|�t9        |||«      n|}|j5                  |«       �Œ" �Œm t;        t/        j<                  |d¬«      t/        j<                  |d¬«      |dœ|
¬«      S c c}w c c}w )a!  
        Process a list of images.

        Args:
            images (ImageInput or list of ImageInput):
                The input image or a list of images.
            image_mean (`list`, *optional*, defaults to [0.5, 0.5, 0.5]):
                Mean values for normalization.
            image_std (`list`, *optional*, defaults to [0.5, 0.5, 0.5]):
                Standard deviation values for normalization.
            max_image_size (`int`, *optional*, defaults to `self.max_image_size` (980)):
                Maximum image size.
            min_image_size (`int`, *optional*, defaults to `self.min_image_size` (336)):
                Minimum image size.
            split_image (`bool`, *optional*, defaults to `self.split_image` (False)):
                Whether to split the image.
            do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb` (True)):
                Whether to convert the image to RGB.
            do_normalize (`bool`, *optional*, defaults to `self.do_normalize` (True)):
                Whether to normalize the image.
            resample (PILImageResampling, *optional*, defaults to `self.resample` (BICUBIC)):
                The resampling filter to use if resizing the image.
            return_tensors (`str` or `TensorType`, *optional*, defaults to "pt"):
                The type of tensor to return.
            data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format for the output 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.
                If unset, will use same as the input image.
            input_data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format for 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.
                If unset, will use the inferred format of the input image.

        Returns:
            BatchFeature:
                A BatchFeature object containing:
                - 'pixel_values':
                    Tensor of processed image pixel values.
                - 'pixel_mask':
                    Boolean pixel mask. This mask is a 2D tensor of shape (max_image_size, max_image_size) where:
                    - True (1) values indicate pixels that belong to the original resized image.
                    - False (0) values indicate pixels that are part of the padding.
                  The mask helps distinguish between actual image content and padded areas in subsequent processing steps.
                - 'num_crops':
                    The maximum number of crops across all images.
        N)rU   rB   z(max_image_size must be either 490 or 980zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)rJ   rC   rD   rK   r   )r`   r$   )rK   r`   r$   rN   )Údtypeg     ào@)Úaxisr>   )ÚdataÚtensor_type)rC   rD   rE   rF   rH   rI   rJ   rK   Ú
ValueErrorr   r   r   r   r   r   Úget_image_patchesrG   Úlenr   ÚmaxÚintr   r   ÚnpÚzerosÚboolr#   Ú	normalizer   r
   Ústack)rY   r^   rC   rD   rE   rF   rH   rI   rJ   rK   r_   r`   r$   r   r?   Úpixel_masksrA   Úcrop_imagesÚ
crop_imageÚhÚwÚscaleÚnew_sizeÚcrop_image_resizedÚpadding_bottomÚpadding_rightÚcrop_image_paddedr@   s                               r+   Ú
preprocesszAriaImageProcessor.preprocess“   sP  € ðF $.Ð#9‘Z¸t¿¹ˆ
Ø!*Ð!6‘I¸D¿N¹Nˆ	Ø+9Ð+E™È4×K^ÑK^ˆØ+9Ð+E™È4×K^ÑK^ˆØ%0Ð%<‘kÀ$×BRÑBRˆØ+9Ð+E™È4×K^ÑK^ˆØ'3Ð'?‘|ÀT×EVÑEVˆØ'Ð3‘8¸¿¹ˆà Ñ+ÜÐGÓHÐHä)¨&Ó1ˆä˜FÔ#Üð:óð ô
 	&Ø%Ø!ØØõ		
ñ Ø9?Ö@°”n UÕ+Ð@ˆFÐ@ð 6<Ö<¨E”. Õ'Ð<ˆÐ<àÐ$ä >¸vÀa¹yÓ IÐàˆØˆØˆ	àó ;	7ˆEÙØ"×4Ñ4ØØ×*Ñ*Ø"ØØ 1Ø&7ð 5ó ‘ð  %˜g�ØÐ ¤C¨Ó$4°yÒ$@Ü Ó,�	à)ó ,7�
ä% jÓ1‘��1Ø&¬¨Q°«Ñ2�Ø˜’6Ü #¤C¨¨E©	£N°NÓ CÀ^ÐT‘Hà .´´C¸¸E¹	³NÀNÓ0SÐT�Hä%+ØØØ%Ø 1Ø&7ô&Ð"ð 1?ÀÈ!ÁÑ0LÈnÐ_gÐhiÑ_jÑNj �Ü$'Ø&Ø˜Ð(¨1¨mÐ*<Ð=Ø 1Ø&7ô	%Ð!ô  ŸX™X ~°~Ð&FÌdÔS�
Ø;<�
˜=˜X a™[˜=¨-¨H°Q©K¨-Ð7Ñ8Ø×"Ñ" :Ô.áØ(,¯©Ø)¨EÑ1ØŸ™ØŸ™Ø$5Ø*;ð )7ó )Ð%ð 'Ð2ô 4Ð4EÀ{ÐTeÔfà.ð &ð ×#Ñ#Ð$5Ö6òY,7ð;	7ôx ä "§¡¨¸AÔ >Ü Ÿh™h {¸Ô;Ø&ñð
 'ô
ð 	
ùòS Aùò =s   ÃJ3ÃJ8r   r2   r   c                 óH   — t        |||«      \  }}t        |||f||¬«      }|S )aC  
        Resizes an image to a target resolution while maintaining aspect ratio.

        Args:
            image (np.array):
                The input image.
            target_resolution (tuple):
                The target resolution (height, width) of the image.
            resample (`PILImageResampling`):
                Resampling filter to use if resizing the image.
            input_data_format (`ChannelDimension` or `str`):
                The channel dimension format of the input image.

        Returns:
            np.array: The resized and padded image.
        ©rK   r$   )r;   r   )rY   r   r2   rK   r$   r:   r9   Úresized_images           r+   Ú_resize_for_patchingz'AriaImageProcessor._resize_for_patchingD  s7   € ô& !7°uÐ>OÐQbÓ cÑˆ
�Iô ˜u z°9Ð&=ÈÐduÔvˆàÐr-   c                 ó‚   — |\  }}t        |||«      \  }}||z
  dz  }||z
  dz  }	| j                  ||	|	f||ff¬«      }
|
S )zU
        Pad an image to a target resolution while maintaining aspect ratio.
        rO   )Úpadding)r;   r   )rY   r   r2   r$   r5   r6   r:   r9   Úpaste_xÚpaste_yÚpadded_images              r+   Ú_pad_for_patchingz$AriaImageProcessor._pad_for_patching^  sh   € ð '8Ñ#ˆ�|Ü 6°uÐ>OÐQbÓ cÑˆ
�Ià )Ñ+°Ñ1ˆØ  :Ñ-°!Ñ3ˆà—x‘x °¸'Ð0BÀWÈgÐDVÐ/W�xÓXˆàÐr-   g        r�   ÚmodeÚconstant_valuesc                 ó^  — t        |t        «      st        |«      dk7  rt        ||||||«      S |€t	        |«      }t
        j                  dt
        j                  dt
        j                  dt
        j                  di}t        j                  ||||   |¬«      }|�t        |||«      }|S |}|S )a­	  
        Pads the `image` with the specified `padding` and `mode`. Padding can be in the (`height`, `width`)
        dimension of in the (`num_patches`) dimension. In the second case an iterable if tuples is expected
        as input.

        Args:
            image (`np.ndarray`):
                The image to pad.
            padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`):
                Padding to apply to the edges of the height, width axes. Can be one of three formats:
                - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis.
                - `((before, after),)` yields same before and after pad for height and width.
                - `(pad,)` or int is a shortcut for before = after = pad width for all axes.
            mode (`PaddingMode`):
                The padding mode to use. Can be one of:
                    - `"constant"`: pads with a constant value.
                    - `"reflect"`: pads with the reflection of the vector mirrored on the first and last values of the
                    vector along each axis.
                    - `"replicate"`: pads with the replication of the last value on the edge of the array along each axis.
                    - `"symmetric"`: pads with the reflection of the vector mirrored along the edge of the array.
            constant_values (`float` or `Iterable[float]`, *optional*):
                The value to use for the padding if `mode` is `"constant"`.
            data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format for the output 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.
                If unset, will use same as the input image.
            input_data_format (`str` or `ChannelDimension`, *optional*):
                The channel dimension format for 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.
                If unset, will use the inferred format of the input image.

        Returns:
            `np.ndarray`: The padded image.

        rP   ÚconstantÚreflectÚedgeÚ	symmetric)r†   r‡   )Ú
isinstancerj   rh   r   r   r   ÚCONSTANTÚREFLECTÚ	REPLICATEÚ	SYMMETRICrk   r   )rY   r   r�   r†   r‡   r`   r$   Úpadding_mode_mappings           r+   r   zAriaImageProcessor.padn  sÆ   € ô` �gœsÔ#¤s¨7£|°qÒ'8Ü�u˜g t¨_¸kÐK\Ó]Ð]àÐ$Ü >¸uÓ EÐô × Ñ  *Ü×Ñ Ü×!Ñ! 6Ü×!Ñ! ;ð	 
Ðô —‘�u˜gÐ,@ÀÑ,FÐXgÔhˆàR]ÐRiÔ'¨¨{Ð<MÓNð 	ð ˆð puð 	ð ˆr-   Úgrid_pinpointsr   c                 ó   — t        |t        «      st        d«      ‚|}t        ||¬«      }t	        ||«      }	| j                  ||	||¬«      }
| j                  |
|	|¬«      }t        |||¬«      }|D �cg c]  }t        |||¬«      ‘Œ }}|S c c}w )aY  
        Process an image with variable resolutions by dividing it into patches.

        Args:
            image (`np.array`):
                The input image to be processed.
            grid_pinpoints (List[Tuple[int, int]]):
                A list of possible resolutions as tuples.
            patch_size (`int`):
                Size of the patches to divide the image into.
            resample (`PILImageResampling`):
                Resampling filter to use if resizing the image.
            data_format (`ChannelDimension` or `str`):
                The channel dimension format for the output image.
            input_data_format (`ChannelDimension` or `str`):
                The channel dimension format of the input image.

        Returns:
            `List[np.array]`: A list of NumPy arrays containing the processed image patches.
        z6grid_pinpoints must be a list of possible resolutions.r   r}   )r$   )r   r$   )r    Úinput_channel_dim)	r�   ÚlistÚ	TypeErrorr   r   r   r…   r,   r   )rY   r   r“   r   rK   r`   r$   Úpossible_resolutionsÚ
image_sizeÚbest_resolutionr~   r„   r%   r*   s                 r+   rg   z$AriaImageProcessor.get_image_patches°  s·   € ô: ˜.¬$Ô/ÜÐTÓUÐUà-Ðä# EÐ7HÔIˆ
Ü0°Ð=QÓRˆØ×1Ñ1Ø�?¨XÐIZð 2ó 
ˆð ×-Ñ-¨m¸_Ð`qÐ-Órˆä# L¸ZÐ[lÔmˆð
 !ö
àô (¨¸;ÐZkÖlð
ˆð 
ð ˆùò	
s   Á2B)!Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr   ÚBICUBICr   Úfloatrj   r   r   rm   rX   r   ÚFIRSTr   r   Ústrr   r{   rk   ÚarrayÚtupler   r…   r   rŽ   Úndarrayr   r   rg   Ú__classcell__)r\   s   @r+   r=   r=   W   sP  ø„ ñò2 DÐð #'Ø!%Ø!Ø!Ø=AØ&+Ø)-Ø'+Ø'9×'AÑ'Añ!à˜‘Kð!ð ˜‘;ð!ð ð	!ð
 ð!ð $ D¨¨s°C¨x©Ñ$9Ñ:ð!ð ˜d‘^ð!ð ! ™ð!ð ˜t‘nð!ð %õ!ðF ;?Ø9=Ø(,Ø(,Ø&*Ø)-Ø'+Ø'+Ø;?Ø2B×2HÑ2HØDHño
à�j $ zÑ"2Ð2Ñ3ðo
ð ˜U 5¨$¨u©+Ð#5Ñ6Ñ7ðo
ð ˜E %¨¨e©Ð"4Ñ5Ñ6ð	o
ð
 ! ™ðo
ð ! ™ðo
ð ˜d‘^ðo
ð ! ™ðo
ð ˜t‘nðo
ð %ðo
ð !  s¨J Ñ!7Ñ8ðo
ð Ð.Ñ/ðo
ð $ E¨#Ð/?Ð*?Ñ$@ÑAóo
ðbØ—X‘XðØ27ðØVfðà	�‰óð4Ø—X‘XðØ27ðØL\ðà	�‰óð( (×0Ñ0Ø9<Ø>BØDHñ@à�z‰zð@ð �s˜E # s (™O¨X°e¸CÀ¸H±oÑ-FÐFÑGð@ð ð	@ð
 ˜u h¨u¡oÐ5Ñ6ð@ð ˜e CÐ)9Ð$9Ñ:Ñ;ð@ð $ E¨#Ð/?Ð*?Ñ$@ÑAð@ð 
�‰ó@ðD0à�x‰xð0ð ˜U 3¨ 8™_Ñ-ð0ð ð	0ð
 %ð0ð &ð0ð ,ð0ð 
ˆb�h‰h‰÷0r-   r=   )%r0   Útypingr   r   r   r   r   Únumpyrk   Úimage_processing_utilsr	   r
   r   Úimage_transformsr   r   r   r   r   Úimage_utilsr   r   r   r   r   r   r   r   r   Úutilsr   r¤   rj   r,   r;   r=   Ú__all__rV   r-   r+   ú<module>r¯      s{   ðó* ß 9Õ 9ã ç ^Ñ ^ß eÕ e÷
÷ 
õ 
õ  ð˜RŸX™Xð °3ð ÈdÐSU×S[ÑS[Énó ò8!ô"IÐ+ô IðX  Ð
 �r-   