ó
    qyüi™*  ã                   óè   • S r SSKJr  SSKrSSKJr  SSKJr  SSKJ	r	J
r
Jr  SSKJrJr  SS	KJrJrJr  \(       a  S
SKJr  \R*                  " \5      rS rS rS rS r\ " S S\5      5       rS/rg)z Image processor class for OwlViTé    )ÚTYPE_CHECKINGNé   )ÚTorchvisionBackend)Úcenter_to_corners_format)ÚOPENAI_CLIP_MEANÚOPENAI_CLIP_STDÚPILImageResampling)ÚImagesKwargsÚUnpack)Ú
TensorTypeÚauto_docstringÚloggingé   )ÚOwlViTObjectDetectionOutputc                 ó,  • U R                  5       (       a@  U R                  [        R                  [        R                  4;   a  U $ U R                  5       $ U R                  [        R                  [        R                  4;   a  U $ U R                  5       $ )N)	Úis_floating_pointÚdtypeÚtorchÚfloat32Úfloat64ÚfloatÚint32Úint64Úint)Úts    Úo/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/models/owlvit/image_processing_owlvit.pyÚ_upcastr   "   sc   € à×Ñ×ÑØ—G‘G¤§¡¬u¯}©}Ð=Ó=ˆqÐLÀ1Ç7Á7Ã9ÐLà—G‘G¤§¡¬U¯[©[Ð9Ó9ˆqÐF¸q¿u¹u»wÐFó    c                 óü  • [        U[        [        45      (       aS  [        R                  " U Vs/ s H  o"S   PM	     sn5      n[        R                  " U Vs/ s H  o"S   PM	     sn5      nO>[        U[        R
                  5      (       a  UR                  S5      u  p4O[        S5      e[        R                  " XCXC/SS9nUR                  S5      R                  U R                  5      nX-  n U $ s  snf s  snf )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ÚlistÚtupler   ÚtensorÚTensorÚunbindÚ	TypeErrorÚstackÚ	unsqueezeÚtoÚdevice)ÚboxesÚtarget_sizesÚiÚimage_heightÚimage_widthÚscale_factors         r   Ú_scale_boxesr3   *   sÐ   € ô �,¤¤u ×.Ñ.Ü—|’|±<Ó$@²<¨a q¤T±<Ñ$@ÓAˆÜ—l’l±,Ó#?²,¨Q a¤D±,Ñ#?Ó@‰Ü	�L¤%§,¡,×	/Ñ	/Ø$0×$7Ñ$7¸Ó$:Ñ!ˆ�käÐNÓOÐOä—;’; ¸;ÐUÐ[\Ñ]€LØ×)Ñ)¨!Ó,×/Ñ/°·±Ó=€LØÑ €EØ€Lùò %AùÚ#?s   °C4ÁC9c                 óf   • [        U 5      n U SS2S4   U SS2S4   -
  U SS2S4   U SS2S4   -
  -  $ )a“  
Computes the area of a set of bounding boxes, which are specified by its (x1, y1, x2, y2) coordinates.

Args:
    boxes (`torch.FloatTensor` of shape `(number_of_boxes, 4)`):
        Boxes for which the area will be computed. They are expected to be in (x1, y1, x2, y2) format with `0 <= x1
        < x2` and `0 <= y1 < y2`.
Returns:
    `torch.FloatTensor`: a tensor containing the area for each box.
Né   r   r   r   )r   )r-   s    r   Úbox_arear6   F   sB   € ô �E‹N€EØ’!�Q�$‰K˜%¢ 1 ™+Ñ%¨%²°1°©+¸ºaÀ¸d¹Ñ*CÑDÐDr   c                 óV  • [        U 5      n[        U5      n[        R                  " U S S 2S S S24   US S 2S S24   5      n[        R                  " U S S 2S SS 24   US S 2SS 24   5      nXT-
  R	                  SS9nUS S 2S S 2S4   US S 2S S 2S4   -  nUS S 2S 4   U-   U-
  nXx-  n	X˜4$ )Nr5   r   )Úminr   )r6   r   Úmaxr8   Úclamp)
Úboxes1Úboxes2Úarea1Úarea2Úleft_topÚright_bottomÚwidth_heightÚinterÚunionÚious
             r   Úbox_iourE   U   sÄ   € Ü�VÓ€EÜ�VÓ€Eä�yŠy˜¢ 4¨¨!¨ Ñ,¨f²Q¸¸¸°U©mÓ<€HÜ—9’9˜V¢A t¨Q©R KÑ0°&º¸A¹B¸±-Ó@€Là Ñ+×2Ñ2°qÐ2Ð9€LØššA˜q˜Ñ! L²²A°q°Ñ$9Ñ9€Eà’!�T�'‰N˜UÑ" UÑ*€Eà
‰-€CØˆ:Ðr   c            	       óÂ   ^ • \ rS rSr\R
                  r\r\	r
SSS.rSrSSS.rSrSrSrSrSrS/rS\\   4U 4S jjr  SS
SS\S\\\   -  S	-  4S jjrSS jrSrU =r$ )ÚOwlViTImageProcessorée   i   )ÚheightÚwidthTFÚpixel_valuesÚkwargsc                 ó\   >• SU;   a  UR                  S5      nX!S'   [        TU ]  " S0 UD6  g )NÚrescaleÚ
do_rescale© )ÚpopÚsuperÚ__init__)ÚselfrL   Úrescale_valÚ	__class__s      €r   rS   ÚOwlViTImageProcessor.__init__t   s3   ø€ ð ˜ÓØ Ÿ*™* YÓ/ˆKØ#.�<Ñ ä‰ÒÑ"˜6Ó"r   NÚoutputsr   Ú	thresholdr.   c                 ó°  • UR                   UR                  pT[        U5      nUb  [        U5      U:w  a  [        S5      e[        R
                  " USS9n[        R                  " UR                  5      nUR                  n	[        U5      nUb  [        XS5      n/ n
[        X‰U5       H*  u  p¼nX²:„  nX¾   nXÎ   nXÞ   nU
R                  X¼US.5        M,     U
$ )aÛ  
Converts the raw output of [`OwlViTForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y,
bottom_right_x, bottom_right_y) format.

Args:
    outputs ([`OwlViTObjectDetectionOutput`]):
        Raw outputs of the model.
    threshold (`float`, *optional*, defaults to 0.1):
        Score threshold to keep object detection predictions.
    target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*):
        Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size
        `(height, width)` of each image in the batch. If unset, predictions will not be resized.

Returns:
    `list[Dict]`: A list of dictionaries, each dictionary containing the following keys:
    - "scores": The confidence scores for each predicted box on the image.
    - "labels": Indexes of the classes predicted by the model on the image.
    - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format.
z9Make sure that you pass in as many target sizes as imageséÿÿÿÿr    ©ÚscoresÚlabelsr-   )ÚlogitsÚ
pred_boxesÚlenÚ
ValueErrorr   r9   ÚsigmoidÚvaluesÚindicesr   r3   ÚzipÚappend)rT   rX   rY   r.   Úbatch_logitsÚbatch_boxesÚ
batch_sizeÚbatch_class_logitsÚbatch_scoresÚbatch_labelsÚresultsr]   r^   r-   Úkeeps                  r   Úpost_process_object_detectionÚ2OwlViTImageProcessor.post_process_object_detection~   sß   € ð2 %,§N¡N°G×4FÑ4F�kÜ˜Ó&ˆ
àÑ#¬¨LÓ(9¸ZÓ(GÜÐXÓYÐYô #ŸYšY |¸Ñ<ÐÜ—}’}Ð%7×%>Ñ%>Ó?ˆØ)×1Ñ1ˆô /¨{Ó;ˆð Ñ#Ü& {ÓAˆKàˆÜ%(¨À[Ö%QÑ!ˆF˜EØÑ%ˆDØ‘\ˆFØ‘\ˆFØ‘KˆEØ�N‰N fÈÑOÖPñ &Rð ˆr   c                 ó  • UR                   UR                  peUb#  [        U5      [        U5      :w  a  [        S5      eUb  UR                  S   S:w  a  [        S5      e[
        R                  " USS9n[
        R                  " UR                  5      n[        U5      nUS:  a†  [        UR                  S	   5       Hj  n	[
        R                  " X‰   * 5       HJ  n
X‰   U
   (       d  M  [        Xi   U
SS24   R                  S	5      Xi   5      S	   S	   nS
Xº'   SX‰   X³:„  '   ML     Ml     Ub  [        Xd5      n/ n[
        R                  " U5      n[        UR                  S	   5       HŸ  n	X‰   nUR!                  5       R#                  5       (       d  M,  SXîU:  '   [
        R                  " U5      S-   nXïS-  -
  US-  -  n[
        R$                  " USS5      nUXÙ'   XÙ   S	:„  nXÙ   U   nXi   U   nUR'                  USUS.5        M¡     U$ )a5  
Converts the output of [`OwlViTForObjectDetection.image_guided_detection`] into the format expected by the COCO
api.

Args:
    outputs ([`OwlViTImageGuidedObjectDetectionOutput`]):
        Raw outputs of the model.
    threshold (`float`, *optional*, defaults to 0.0):
        Minimum confidence threshold to use to filter out predicted boxes.
    nms_threshold (`float`, *optional*, defaults to 0.3):
        IoU threshold for non-maximum suppression of overlapping boxes.
    target_sizes (`torch.Tensor`, *optional*):
        Tensor of shape (batch_size, 2) where each entry is the (height, width) of the corresponding image in
        the batch. If set, predicted normalized bounding boxes are rescaled to the target sizes. If left to
        None, predictions will not be unnormalized.

Returns:
    `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image
    in the batch as predicted by the model. All labels are set to None as
    `OwlViTForObjectDetection.image_guided_detection` perform one-shot object detection.
NzTMake sure that you pass in as many target sizes as the batch dimension of the logitsr   r5   zTEach element of target_sizes must contain the size (h, w) of each image of the batchr[   r    g      ð?r   g      ð¿ç        g�íµ ÷Æ°>çš™™™™™¹?gÍÌÌÌÌÌì?r\   )r_   Útarget_pred_boxesra   rb   Úshaper   r9   rc   rd   r   ÚrangeÚargsortrE   r*   r3   Ú
zeros_likeÚnonzeroÚnumelÚcliprg   )rT   rX   rY   Únms_thresholdr.   r_   Útarget_boxesÚprobsr]   Úidxr/   Úiousrn   ÚalphasÚquery_scoresÚ	max_scoreÚquery_alphasÚmaskÚ
box_scoresr-   s                       r   Ú#post_process_image_guided_detectionÚ8OwlViTImageProcessor.post_process_image_guided_detection³   s  € ð,  'Ÿ~™~¨w×/HÑ/H�àÑ#¬¨F«´s¸<Ó7HÓ(HÜÐsÓtÐtØÑ#¨×(:Ñ(:¸1Ñ(=ÀÓ(BÜÐsÓtÐtä—	’	˜& bÑ)ˆÜ—’˜uŸ|™|Ó,ˆô 0°Ó=ˆð ˜3ÓÜ˜\×/Ñ/°Ñ2Ö3�ÜŸš¨© |Ö4�AØ!™; qŸ>Ù ä" <Ñ#4°Qº°TÑ#:×#DÑ#DÀQÓ#GÈÑIZÓ[Ð\]Ñ^Ð_`Ña�DØ"�D‘GØ8;�F‘K Ñ 4Ó5ó 5ñ 4ð Ñ#Ü'¨ÓCˆLð ˆÜ×!Ò! &Ó)ˆä˜×+Ñ+¨AÑ.Ö/ˆCà!™;ˆLØ×'Ñ'Ó)×/Ñ/×1Ñ1Ùð 69ˆL¨	Ñ1Ñ2ô Ÿ	š	 ,Ó/°$Ñ6ˆIØ(¸©OÑ<ÀÈSÁÑQˆLÜ Ÿ:š: l°C¸Ó=ˆLØ&ˆF‰Kà‘; ‘?ˆDØ™ TÑ*ˆJØ Ñ% dÑ+ˆEØ�N‰N j¸DÈ5ÑQÖRñ' 0ð* ˆr   rP   )rt   N)rs   g333333Ó?N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r	   ÚBICUBICÚresampler   Ú
image_meanr   Ú	image_stdÚsizeÚdefault_to_squareÚ	crop_sizeÚ	do_resizeÚdo_center_croprO   Údo_normalizeÚdo_convert_rgbÚmodel_input_namesr   r
   rS   r   r   r#   r$   rp   rˆ   Ú__static_attributes__Ú__classcell__)rV   s   @r   rG   rG   e   s©   ø† à!×)Ñ)€HØ!€JØ€IØ CÑ(€DØÐØ¨Ñ-€IØ€IØ€NØ€JØ€LØ€NØ'Ð(Ðð# ¨Ñ!5÷ #ð Ø8<ñ	3à.ð3ð ð3ð ! 4¨¡;Ñ.°Ñ5õ	3÷jKò Kr   rG   )Ú__doc__Útypingr   r   Úimage_processing_backendsr   Úimage_transformsr   Úimage_utilsr   r   r	   Úprocessing_utilsr
   r   Úutilsr   r   r   Úmodeling_owlvitr   Ú
get_loggerrŠ   Úloggerr   r3   r6   rE   rG   Ú__all__rP   r   r   Ú<module>r§      s   ðñ 'å  ã å ;Ý 8ß PÑ Pß 4ß 8Ñ 8ö Ý<ð 
×	Ò	˜HÓ	%€òGòò8Eòð  ôXÐ-ó Xó ðXðv "Ð
"�r   