
    rhk#                        d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	 ddl
mZmZ ddlmZmZmZmZmZmZ dd	lmZ dd
lmZmZmZmZmZ  e       rddlZ e       rddlmZ n e       rddl mZ  G d de	      Z!e G d de             Z"dgZ#y)z%Fast Image processor class for OWLv2.    N)OptionalUnion)OwlViTImageProcessorFast   )BatchFeatureDefaultFastImageProcessorKwargs)group_images_by_shapereorder_images)OPENAI_CLIP_MEANOPENAI_CLIP_STDChannelDimension
ImageInputPILImageResamplingSizeDict)Unpack)
TensorTypeauto_docstringis_torch_availableis_torchvision_availableis_torchvision_v2_available)
functionalc                   "    e Zd ZU dZee   ed<   y)Owlv2FastImageProcessorKwargsa  
    do_pad (`bool`, *optional*, defaults to `True`):
        Controls whether to pad the image. Can be overridden by the `do_pad` parameter in the `preprocess`
        method. If `True`, padding will be applied to the bottom and right of the image with grey pixels.
    do_padN)__name__
__module____qualname____doc__r   bool__annotations__     z/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/transformers/models/owlv2/modular_owlv2.pyr   r   7   s     TNr"   r   c                   t   e Zd Zej                  ZeZeZ	dddZ
dZdZdZdZdZeZdZdZdee   fdZededee   fd	       Zddd
dedd
fdZ	 dded
   dee   deded
   fdZ	 	 d dd
dededd
fdZ ded
   dededed   dededededee!eee   f      dee!eee   f      dee   dee!e"e#f      de$fdZ%y)!Owlv2ImageProcessorFasti  )heightwidthgp?TNkwargsc                 8     t               j                  di | y )Nr!   )r   __init__)selfr(   s     r#   r*   z Owlv2ImageProcessorFast.__init__P   s    + "++5f5r"   imagesc                 8     t               j                  |fi |S )N)r   
preprocess)r+   r,   r(   s      r#   r.   z"Owlv2ImageProcessorFast.preprocessS   s    4')44VFvFFr"   ztorch.Tensorconstant_valuereturnc                     |j                   dd \  }}t        ||      }||z
  }||z
  }dd||f}t        j                  |||      }	|	S )z<
        Pad an image with zeros to the given size.
        Nr   )fill)shapemaxFpad)
r+   r,   r/   r&   r'   size
pad_bottom	pad_rightpaddingpadded_images
             r#   _pad_imagesz#Owlv2ImageProcessorFast._pad_imagesW   s\     RS)65!F]
5L	aJ/uuVW>Br"   disable_groupingc                     t        ||      \  }}i }|j                         D ]  \  }}| j                  ||      }|||<    t        ||      }	|	S )Nr>   )r/   )r	   itemsr=   r
   )
r+   r,   r>   r/   grouped_imagesgrouped_images_indexprocessed_images_groupedr4   stacked_imagesprocessed_imagess
             r#   r7   zOwlv2ImageProcessorFast.padd   sz     0EV^n/o,,#% %3%9%9%; 	=!E>!--- . N /=$U+	= **BDXYr"   imager8   anti_aliasingc                    |j                   |j                  f}|j                  }t        j                  |dd       j                  |j                        t        j                  |      j                  |j                        z  }|r||dz
  dz  j                  d      }nt        j                  |      t        j                  |      z  }t        j                  |dk        rt        d      t        j                  |dkD  |dk  z        rt        j                  d       t        j                  |dk(        r|}	n]dt        j                  d|z        j                         z  dz   }
t!        j"                  ||
d   |
d   f|j%                         	      }	n|}	t!        j&                  |	|j                   |j                  fd
      }|S )az  
        Resize an image as per the original implementation.

        Args:
            image (`Tensor`):
                Image to resize.
            size (`dict[str, int]`):
                Dictionary containing the height and width to resize the image to.
            anti_aliasing (`bool`, *optional*, defaults to `True`):
                Whether to apply anti-aliasing when downsampling the image.
            anti_aliasing_sigma (`float`, *optional*, defaults to `None`):
                Standard deviation for Gaussian kernel when downsampling the image. If `None`, it will be calculated
                automatically.
           N   r   )minzFAnti-aliasing standard deviation must be greater than or equal to zerozWAnti-aliasing standard deviation greater than zero but not down-sampling along all axesr   )sigmaF)r8   	antialias)r&   r'   r4   torchtensortodeviceclamp
atleast_1d	ones_likeany
ValueErrorwarningswarnceilintr6   gaussian_blurtolistresize)r+   rG   r8   rH   anti_aliasing_sigmar(   output_shapeinput_shapefactorsfilteredkernel_sizesouts               r#   r^   zOwlv2ImageProcessorFast.resizew   s   , TZZ0kk ,,{12/225<<@5<<P\C]C`C`afamamCnn"*(/!q'8&?&?A&?&F#&+&6&67J&Keoo^eNf&f#990145$%mnnYY 3a 7GqLIJMMq yy,12  5::a2E.E#F#J#J#LLqP??LO\!_=EXE_E_Ea
 Hhhxt{{DJJ&?5Q
r"   	do_resizeinterpolationzF.InterpolationModer   
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsc           	         t        ||      \  }}i }|j                         D ]   \  }}| j                  |||d|	|
      }|||<   " t        ||      }|r| j	                  ||      }t        ||      \  }}i }|j                         D ]0  \  }}|s	| j                  |||t        j                        }|||<   2 t        ||      }t        ||      \  }}i }|j                         D ]   \  }}| j                  |d|||	|
      }|||<   " t        ||      }|rt        j                  |d      n|}t        d|i|      S )Nr@   F)rG   r8   rg   input_data_formatr   )dimpixel_values)datatensor_type)r	   rA   rescale_and_normalizer
   r7   r^   r   FIRSTrO   stackr   )r+   r,   rf   r8   rg   r   rh   ri   rj   rk   rl   r>   rm   r(   rB   rC   rD   r4   rE   rF   resized_images_groupedresized_stackresized_imagess                          r#   _preprocessz#Owlv2ImageProcessorFast._preprocess   s   " 0EV^n/o,,#% %3%9%9%; 	=!E>!77
NE:yN /=$U+	= **BDXY#xx(8K[x\/D/?0
,, "$%3%9%9%; 	>!E> $("/&6&<&<	 !, ! 1>&u-	> ((>@TU 0E^fv/w,,#% %3%9%9%; 	=!E>!77~|ZQZN /=$U+	= **BDXYCQ5;;'7Q?Wg.2B!CQ_``r"   )g      ?)TN)&r   r   r   r   BILINEARresampler   rk   r   rl   r8   ri   rf   rh   rj   r   r   valid_kwargs	crop_sizedo_center_cropr   r*   r   r   r.   floatr=   listr   r   r7   r   r^   r   strr   r   rz   r!   r"   r#   r%   r%   A   s   !**H!JIC(DNIJLF0LIN6(E!F 6 G Gv>[7\ G G. % R` " !$	 ^$  #4.  	 
 
n	 . # 66 6 	6 
6p>a^$>a >a 	>a
   56>a >a >a >a >a U5$u+#567>a E%e"456>a #4.>a !sJ!78>a 
>ar"   r%   )$r   rX   typingr   r   7transformers.models.owlvit.image_processing_owlvit_fastr   image_processing_utils_fastr   r   image_transformsr	   r
   image_utilsr   r   r   r   r   r   processing_utilsr   utilsr   r   r   r   r   rO   torchvision.transforms.v2r   r6   torchvision.transformsr   r%   __all__r!   r"   r#   <module>r      s    ,  " \ F  '    96$C  ka6 ka ka\ %
%r"   