
     `i"                         d Z ddlZddlmZmZ ddlZddlmZ ddl	m
Z
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 ddlmZ  G d de          Ze G d de                      ZdgZ dS )z%Fast Image processor class for OWLv2.    N)OptionalUnion)
functional   )BaseImageProcessorFastBatchFeatureDefaultFastImageProcessorKwargs)group_images_by_shapereorder_images)OPENAI_CLIP_MEANOPENAI_CLIP_STDChannelDimension
ImageInputPILImageResamplingSizeDict)Unpack)
TensorTypeauto_docstring   )OwlViTImageProcessorFastc                       e Zd ZdS )Owlv2FastImageProcessorKwargsN)__name__
__module____qualname__     {/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/owlv2/modular_owlv2.pyr   r   -   s          r   r   c                      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%dS )"Owlv2ImageProcessorFasti  )heightwidthgp?TNkwargsc                 *    t          j        | fi | d S N)r   __init__)selfr#   s     r   r&   z Owlv2ImageProcessorFast.__init__?   s!    '7777777r   imagesc                 (    t          j        | |fi |S r%   )r   
preprocess)r'   r(   r#   s      r   r*   z"Owlv2ImageProcessorFast.preprocessB   s    %0vHHHHHr         ?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_imagesF   sc     RSS)65!!F]
5L	aJ/uVW>BBBr   disable_groupingc                     t          ||          \  }}i }|                                D ]!\  }}	|                     |	|          }	|	||<   "t          ||          }
|
S )z
        Unlike the Base class `self.pad` where all images are padded to the maximum image size,
        Owlv2 pads an image to square.
        r;   )r,   )r
   itemsr:   r   )r'   r(   r;   r,   r#   grouped_imagesgrouped_images_indexprocessed_images_groupedr1   stacked_imagesprocessed_imagess              r   r4   zOwlv2ImageProcessorFast.padS   s     0EV^n/o/o/o,,#% %3%9%9%;%; 	= 	=!E>!--- .  N /=$U++)*BDXYYr   imager5   anti_aliasingc                    |j         |j        f}|j        }t          j        |dd                                       |j                  t          j        |                              |j                  z  }|r$||dz
  dz                      d          }nt          j        |          t          j	        |          z  }t          j
        |dk               rt          d          t          j
        |dk    |dk    z            rt          j        d           t          j
        |dk              r|}	nidt          j        d|z                                            z  dz   }
t!          j        ||
d         |
d         f|                                	          }	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.
        r   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)r5   	antialias)r!   r"   r1   torchtensortodeviceclamp
atleast_1d	ones_likeany
ValueErrorwarningswarnceilintr3   gaussian_blurtolistresize)r'   rD   r5   rE   anti_aliasing_sigmar#   output_shapeinput_shapefactorsfilteredkernel_sizesouts               r   rZ   zOwlv2ImageProcessorFast.resizek   s   , TZ0k ,{122//225<@@5<P\C]C]C`C`afamCnCnn 	"*(/!q'8&?&?A&?&F&F##&+&67J&K&Keo^eNfNf&f#901455 $%mnnnY 3a 7GqLIJJ Mq   y,122   5:a2E.E#F#F#J#J#L#LLqP?LO\!_=EXE_E_EaEa  
 Hhxt{DJ&?5QQQ
r   	do_resizeinterpolationzF.InterpolationModedo_pad
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsc           	         t          ||          \  }}i }|                                D ]$\  }}|                     |||d|	|
          }|||<   %t          ||          }|r|                     |d|          }t          ||          \  }}i }|                                D ]/\  }}|r(|                     |||t          j                  }|||<   0t          ||          }t          ||          \  }}i }|                                D ]$\  }}|                     |d|||	|
          }|||<   %t          ||          }|rt          j	        |d          n|}t          d|i|	          S )
Nr=   Fr+   )r,   r;   )rD   r5   rc   input_data_formatr   )dimpixel_values)datatensor_type)r
   r>   rescale_and_normalizer   r4   rZ   r   FIRSTrK   stackr   )r'   r(   rb   r5   rc   rd   re   rf   rg   rh   ri   r;   rj   r#   r?   r@   rA   r1   rB   rC   resized_images_groupedresized_stackresized_imagess                          r   _preprocessz#Owlv2ImageProcessorFast._preprocess   s   " 0EV^n/o/o/o,,#% %3%9%9%;%; 	= 	=!E>!77
NE:y N /=$U++)*BDXYY 	q#xx(8_oxpp/D/?0
 0
 0
,, "$%3%9%9%;%; 	> 	>!E> > $("/&6&<	 !, ! ! 1>&u-'(>@TUU 0E^fv/w/w/w,,#% %3%9%9%;%; 	= 	=!E>!77~|ZQZ N /=$U++)*BDXYYCQg5;'7Q????Wg.2B!CQ_````r   )r+   )TN)&r   r   r   r   BILINEARresampler   rh   r   ri   r5   rf   rb   re   rg   rd   r   valid_kwargs	crop_sizedo_center_cropr   r&   r   r   r*   floatr:   listr   boolr4   r   rZ   r   strr   r   rw   r   r   r   r    r    0   si       !*H!JIC((DNIJLF0LIN8(E!F 8 8 8 8 I Iv>[7\ I I I ^I . % R`    " !$	   ^$  #4.  	  
n	       8 # 6 66 6 	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 >a >a >a >a >ar   r    )!__doc__rT   typingr   r   rK   torchvision.transforms.v2r   r3   image_processing_utils_fastr   r   r	   image_transformsr
   r   image_utilsr   r   r   r   r   r   processing_utilsr   utilsr   r   #owlvit.image_processing_owlvit_fastr   r   r    __all__r   r   r   <module>r      s   , +  " " " " " " " "  5 5 5 5 5 5         
 F E E E E E E E                ' & & & & &        K J J J J J J I I I I$C I I I pa pa pa pa pa6 pa pa paf %
%r   