
    ^j                     (   d Z ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddl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mZ ddlmZmZmZ 	 	 	 ddeej<                  df   dededede eef   f
dZ! G d ded      Z"e G d de
             Z#dgZ$y)z&Image processor class for BridgeTower.    )UnionN)
functional   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)OPENAI_CLIP_MEANOPENAI_CLIP_STDChannelDimensionPILImageResamplingSizeDictget_image_size)ImagesKwargsUnpack)
TensorTypeauto_docstringis_torch_availableinput_imagetorch.Tensorshorterlongersize_divisorreturnc                    t               r-t        | t        j                        r| j                  dd \  }}nt        | t        j                        \  }}||}}|t        ||      z  }||k  r|}	||z  }
n||z  }	|}
t        |	|
      |kD  r|t        |	|
      z  }||	z  }	||
z  }
t        |	dz         t        |
dz         }
}	|	|z  |z  }	|
|z  |z  }
|	|
fS )z7Get output image size after resizing with size_divisor.N)channel_dimg      ?)r   
isinstancetorchTensorshaper   r   FIRSTminmaxint)r   r   r   r   input_heightinput_widthmin_sizemax_sizescale
new_height	new_widths              /var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/bridgetower/image_processing_bridgetower.pyget_resize_output_image_sizer.   %   s     
; E$/$5$5bc$:!k$2;L\LbLb$c!k &hHs<55Ek!
K'	\)
	
:y!H,3z955Z'
I%	
S 013y33G	J|+l:J\)L8Iy      c                       e Zd ZU dZeed<   y)BridgeTowerImageProcessorKwargsz
    size_divisor (`int`, *optional*, defaults to `self.size_divisor`):
        The size by which to make sure both the height and width can be divided.
    r   N)__name__
__module____qualname____doc__r%   __annotations__ r/   r-   r1   r1   G   s    
 r/   r1   F)totalc            #       B    e Zd ZdZeZddgZej                  Z	e
ZeZddiZdZddiZdZdZdZdZdZdZd	ee   f fd
Z	 d"dddedddeddf
 fdZ	 d"ded   dededdde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dz  dedz  d ee z  dz  dede!f"d!Z" xZ#S )#BridgeTowerImageProcessorzGTorchvision backend for BridgeTower with custom resize and center_crop.pixel_values
pixel_maskshortest_edgei   FT    kwargsc                 $    t        |   di | y )Nr7   )super__init__)selfr?   	__class__s     r-   rB   z"BridgeTowerImageProcessor.__init__d   s    "6"r/   imager   sizeresamplez7PILImageResampling | tvF.InterpolationMode | int | Noner   r   c                     |j                   st        d|j                                |j                   }t        d|z        }t	        ||||      \  }}	t        
|   d|t        ||	      |d|S )z!Resize with size_divisor support.z@The `size` dictionary must contain the key `shortest_edge`. Got g)\?)r   r   r   heightwidth)rE   rF   rG   r7   )r=   
ValueErrorkeysr%   r.   rA   resizer   )rC   rE   rF   rG   r   r?   r   r   output_heightoutput_widthrD   s             r-   rN   z BridgeTowerImageProcessor.resizeg   s     !!_`d`i`i`k_lmnn$$Z')*&B76'
#| w~ 
lC
 	
 	
r/   images	do_resizedo_center_crop	crop_size
do_rescalerescale_factordo_normalize
image_meanN	image_stddo_padpad_sizedisable_groupingreturn_tensorsc           	         t        ||      \  }}i }|j                         D ]!  \  }}|r| j                  ||||      }|||<   # t        ||      }t        ||      \  }}i }|j                         D ]T  \  }}|r2| j	                  |t        |j                  |j                              }| j                  ||||	|
|      }|||<   V t        ||      }i }|r| j                  |d|      \  }}||d<   ||d<   t        ||	      S )
z%Custom preprocessing for BridgeTower.)r\   )rF   rG   r   rI   )rF   T)return_maskr\   r<   r;   )datatensor_type)
r   itemsrN   r	   center_cropr   r=   rescale_and_normalizepadr   )rC   rQ   rR   rF   rG   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r   r?   grouped_imagesgrouped_images_indexresized_images_groupedr!   stacked_imagesresized_imagesprocessed_images_groupedprocessed_imagesr`   processed_maskss                               r-   _preprocessz%BridgeTowerImageProcessor._preprocess~   se   , 0EV^n/o,,!#%3%9%9%; 	;!E>!%^$QYht!u,:"5)	; ((>@TU 0E^fv/w,,#% %3%9%9%; 		=!E>!%!1!1"9P9PXaXoXo)p "2 " "77
NL*V_N /=$U+		= **BDXY04 dEU 19 1-o "1D/^>BBr/   )r>   )$r2   r3   r4   r5   r1   valid_kwargsmodel_input_namesr   BICUBICrG   r
   rX   r   rY   rF   default_to_squarerT   rR   rS   rU   rW   rZ   r   r   rB   r   r%   rN   listboolfloatstrr   r   rn   __classcell__)rD   s   @r-   r:   r:   P   s   Q2L'6!))H!JIS!D #&IINJLFL#(G!H # 

 
 L	

 
 

P #7C^$7C 7C 	7C
 L7C 7C 7C 7C 7C 7C DK'$.7C 4;&-7C t7C T/7C +7C  j(4/!7C" #7C& 
'7Cr/   r:   )i   i5  r>   )%r5   typingr   numpynpr   torchvision.transforms.v2r   tvFimage_processing_backendsr   image_processing_utilsr   image_transformsr   r	   image_utilsr
   r   r   r   r   r   processing_utilsr   r   utilsr   r   r   ndarrayr%   tupler.   r1   r:   __all__r7   r/   r-   <module>r      s    -    7 ; 2 E  5 C C
 	!rzz>12!! ! 	!
 38_!Dl%  dC 2 dC dCN '
'r/   