
    i>                     H   d dl Z d dlZd dlZd dlmc mc mZ ddl	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mZmZmZ ddlmZmZmZmZ ddlmZ d	d
lm Z  d	dl!m"Z"  e       rd dl#m$Z% d Z&d Z'd Z(e G d de              Z)e ed       G d de"                    Z*ddgZ+y)    N   )TorchvisionBackend)BatchFeature)group_images_by_shapepadreorder_imagesto_channel_dimension_format)OPENAI_CLIP_MEANOPENAI_CLIP_STDChannelDimensionPILImageResamplingSizeDict)
TensorTypeauto_docstringis_scipy_availablerequires_backends)requires   )OwlViTImageProcessor)OwlViTImageProcessorPil)ndimagec                 P   t        |      }t        |      }| j                  }|| j                  kD  r/|d|| j                  z
  z  z  }t	        j
                  | |      } | |fS || j                  dz
  k(  r|| j                  d   fz   }| |fS || j                  k  rt        d      | |fS )a%  Validate resize output shape according to input image.

    Args:
        image (`np.ndarray`):
         Image to be resized.
        output_shape (`iterable`):
            Size of the generated output image `(rows, cols[, ...][, dim])`. If `dim` is not provided, the number of
            channels is preserved.

    Returns
        image (`np.ndarray`):
            The input image, but with additional singleton dimensions appended in the case where `len(output_shape) >
            input.ndim`.
        output_shape (`Tuple`):
            The output shape converted to tuple.

    Raises ------ ValueError:
        If output_shape length is smaller than the image number of dimensions.

    Notes ----- The input image is reshaped if its number of dimensions is not equal to output_shape_length.

    )   r   zIoutput_shape length cannot be smaller than the image number of dimensions)tuplelenshapendimnpreshape
ValueError)imageoutput_shapeoutput_ndiminput_shapes       x/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/models/owlv2/modular_owlv2.py_preprocess_resize_output_shaper'   3   s    . &Ll#K++KUZZt{UZZ788

5+. , 


Q	&#u{{2&88 , 
uzz	!dee,    c                 *   t        j                  |       }t        j                  |      r)t         j                  }t         j                  } ||       }n t         j                  }t         j
                  } ||       }t        j                  |||      }|S )a  Clip output image to range of values of input image.

    Note that this function modifies the values of *output_image* in-place.

    Taken from:
    https://github.com/scikit-image/scikit-image/blob/b4b521d6f0a105aabeaa31699949f78453ca3511/skimage/transform/_warps.py#L640.

    Args:
        input_image : ndarray
            Input image.
        output_image : ndarray
            Output image, which is modified in-place.
    )r   minisnannanminnanmaxmaxclip)input_imageoutput_imagemin_valmin_funcmax_funcmax_vals         r&   _clip_warp_outputr6   Z   sn     ff[!G	xx9999;'6666{#G77<':Lr(   c                    t        |t        t        f      rMt        j                  |D cg c]  }|d   	 c}      }t        j                  |D cg c]  }|d   	 c}      }n:t        |t        j
                        r|j                  d      \  }}nt        d      t        j                  ||      }t        j                  ||||gd      }|j                  d      j                  | j                        }| |z  } | S c c}w c c}w )a  
    Scale batch of bounding boxes to the target sizes.

    Args:
        boxes (`torch.Tensor` of shape `(batch_size, num_boxes, 4)`):
            Bounding boxes to scale. Each box is expected to be in (x1, y1, x2, y2) format.
        target_sizes (`list[tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`):
            Target sizes to scale the boxes to. Each target size is expected to be in (height, width) format.

    Returns:
        `torch.Tensor` of shape `(batch_size, num_boxes, 4)`: Scaled bounding boxes.
    r   r   z4`target_sizes` must be a list, tuple or torch.Tensor)dim)
isinstancelistr   torchtensorTensorunbind	TypeErrorr.   stack	unsqueezetodevice)boxestarget_sizesiimage_heightimage_widthmax_sizescale_factors          r&   _scale_boxesrK   x   s     ,u.||<$@aQqT$@All,#?QAaD#?@	L%,,	/$0$7$7$:!kNOO yy{3H;;(HhGQOL))!,//=LL EL %A#?s   D D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dZdZdddded	dfd
Z	 dded   dedz  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dde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fdZy)Owlv2ImageProcessor  heightwidthp?TNimagesztorch.Tensorconstant_valuereturnc                     |j                   dd \  }}t        ||      }||z
  }||z
  }dd||f}t        j                  |||      }	|	S )<
        Pad an image with zeros to the given size.
        Nr   )fill)r   r.   tvFr   )
selfrS   rT   rP   rQ   size
pad_bottom	pad_rightpaddingpadded_images
             r&   _pad_imageszOwlv2ImageProcessor._pad_images   s\     RS)65!F]
5L	aJ/wwvw^Dr(   disable_groupingc                     t        ||      \  }}i }|j                         D ]  \  }}	| j                  |	|      }	|	||<    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.
        rb   )rT   )r   itemsra   r   )r[   rS   rb   rT   kwargsgrouped_imagesgrouped_images_indexprocessed_images_groupedr   stacked_imagesprocessed_imagess              r&   r   zOwlv2ImageProcessor.pad   sz     0EV^n/o,,#% %3%9%9%; 	=!E>!--- . N /=$U+	= **BDXYr(   r"   r\   anti_aliasingc                 h   |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(                  |	|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   Nr   r   )r*   FAnti-aliasing standard deviation must be greater than or equal to zeroWAnti-aliasing standard deviation greater than zero but not down-sampling along all axesr   )sigmaF)r\   	antialias)rP   rQ   r   r;   r<   rB   rC   clamp
atleast_1d	ones_likeanyr!   warningswarnceilintrZ   gaussian_blurtolistr   resize)r[   r"   r\   rl   anti_aliasing_sigmarf   r#   r%   factorsfilteredkernel_sizess              r&   r|   zOwlv2ImageProcessor.resize   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!((NNr(   	do_resizeresamplePILImageResampling | Nonedo_pad
do_rescalerescale_factordo_normalize
image_mean	image_stdreturn_tensorsc           	      4   t        ||      \  }}i }|j                         D ]   \  }}| j                  |||d|	|
      }|||<   " t        ||      }|r| j	                  |d|      }t        ||      \  }}i }|j                         D ]!  \  }}|s	| j                  |||      }|||<   # t        ||      }t        ||      \  }}i }|j                         D ]   \  }}| j                  |d|||	|
      }|||<   " t        ||      }t        d|i|      S )Nrd   F        )rT   rb   )r"   r\   r   pixel_valuesdatatensor_type)r   re   rescale_and_normalizer   r   r|   r   )r[   rS   r   r\   r   r   r   r   r   r   r   rb   r   rf   rg   rh   ri   r   rj   rk   resized_images_groupedresized_stackresized_imagess                          r&   _preprocesszOwlv2ImageProcessor._preprocess   s   " 0EV^n/o,,#% %3%9%9%; 	=!E>!77
NE:yN /=$U+	= **BDXY#xx(8_oxp/D/?0
,, "$%3%9%9%; 	>!E> $.tV^ _0=&u-	> ((>@TU 0E^fv/w,,#% %3%9%9%; 	=!E>!77~|ZQZN /=$U+	= **BDXY.2B!CQ_``r(   r   TN)__name__
__module____qualname__r   BILINEARr   r
   r   r   r   r\   r   r   r   r   r   	crop_sizedo_center_cropfloatra   r:   boolr   r   r|   strr   r   r    r(   r&   rM   rM      s   !**H!JIC(DNIJLFIN. % R` " !$	 ^$  +  	  
n	 8 # 4O4O 4O 	4O 
4Ol7a^$7a 7a 	7a
 .7a 7a 7a 7a 7a DK'$.7a 4;&-7a +7a j(4/7a 
7ar(   rM   )r;   )backendsc                   b   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dZdZddej&                  dedej&                  fd	Z	 	 ddej&                  d
eeef   dedej&                  fdZdeej&                     ded
edddededededeee   z  dz  deee   z  dz  deez  dz  defdZy)Owlv2ImageProcessorPilrN   rO   rR   TNr"   rT   rU   c                 ~    |j                   dd \  }}t        ||      }||z
  }||z
  }t        |d|fd|ff|      }|S )rW   rX   Nr   )r"   r_   constant_values)r   r.   r   )r[   r"   rT   rP   rQ   r\   r]   r^   s           r&   r   zOwlv2ImageProcessorPil.padI  s]     BC(65!F]
5L	_q)n5*

 r(   r\   rl   c                     t        | d       |d   |d   f}t        |t        j                        }t	        ||      \  }}|j
                  }t        j                  ||      }d}	d}
d}|r|t        j                  d|dz
  dz        }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                   |||
|	
      }n|}|D cg c]  }d|z  	 }}t        j"                  ||||	|
d      }t%        ||      }t        |t        j&                        }|S c c}w )a~  
        Resize an image as per the original implementation.

        Args:
            image (`np.ndarray`):
                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.
        scipyrP   rQ   mirrorr   r   r   rn   ro   )cvalmodeT)orderr   r   	grid_mode)r   r	   r   LASTr'   r   r   dividemaximumrs   rt   ru   r!   rv   rw   ndigaussian_filterzoomr6   FIRST)r[   r"   r\   rl   r}   rf   r#   r%   r~   ndi_moder   r   r   fzoom_factorsouts                   r&   r|   zOwlv2ImageProcessorPil.resizeX  sq   , 	$(XW6+E3C3H3HI=e\R|kk))K6 "*&(jjWq[A4E&F#&(mm4G&H2<<X_K`&`#66-12$%mnnVV014AFGMMq **52EDW_`HH'./!A//hhxUPT`de!%-+E3C3I3IJ 0s   *E;rS   r   r   r   r   r   r   r   r   r   r   c                     g }|D ]d  }|r| j                  ||      }|r| j                  |      }|r| j                  |||      }|r| j                  ||	|
      }|j	                  |       f t        d|i|      S )Nr   r   )rescaler   r|   	normalizeappendr   )r[   rS   r   r\   r   r   r   r   r   r   r   r   rf   rk   r"   s                  r&   r   z"Owlv2ImageProcessorPil._preprocess  s      		+EUN;E4:uj)D##E*		+ .2B!CQ_``r(   r   r   ) r   r   r   r   r   r   r
   r   r   r   r\   r   r   r   r   r   r   r   r   ndarrayr   r   dictr   ry   r   r|   r:   r   r   r   r   r   r(   r&   r   r   :  sc    "**H!JIC(DNIJLFIN U RZZ & # 7zz7 38n7 	7 
7raRZZ a a 	a
 .a a a a a DK'$.a 4;&-a j(4/a 
ar(   r   ),rv   numpyr   r;   $torchvision.transforms.v2.functional
transformsv2
functionalrZ   image_processing_backendsr   image_processing_utilsr   image_transformsr   r   r   r	   image_utilsr
   r   r   r   r   utilsr   r   r   r   utils.import_utilsr   owlvit.image_processing_owlvitr   "owlvit.image_processing_pil_owlvitr   r   r   r   r'   r6   rK   rM   r   __all__r   r(   r&   <module>r      s       2 2 ; 2    + A H $$N<> _a. _a _aD 	:oa4 oa  oad !":
;r(   