
    Z j                         S SK r S SKJs  Jr  SSKJr  SSKJr  SSK	J
r
Jr  SSKJrJr  SSKJr  SSKJr  SS	KJr  \\" S
S9 " S S\5      5       5       rS/rg)    N   )BatchFeature)TorchvisionBackend)group_images_by_shapereorder_images)PILImageResamplingSizeDict)auto_docstring)
TensorType)requires)torch)backendsc                   "   \ rS rSrSrSrSSS.r\R                  r	S\
S   S\S	\S
\S\S\S\\
\   -  S-  S\\
\   -  S-  S\S-  S\\-  S-  S\4S jr SS\R&                  S\
\R&                     S\S\
\\\R&                  4      4S jjrSrg)UVDocImageProcessor"   Ti  i  )heightwidthimagesztorch.Tensor	do_resizesize
do_rescalerescale_factordo_normalize
image_meanN	image_stddisable_groupingreturn_tensorsreturnc           	         [        XS9u  p0 nUR                  5        H/  u  nnU R                  UXEXgU5      nUS S 2/ SQS S 2S S 24   nUX'   M1     [        X5      nUR	                  5       n[        UU	S9u  p0 nUR                  5        H?  u  nnU(       a-  [
        R                  " UUR                  UR                  4SSS9nUUU'   MA     [        UU5      n[        UUS.U
S/S9$ )	N)r   )      r   bilinearTr   modealign_corners)pixel_valuesoriginal_imagesr'   )datatensor_typeskip_tensor_conversion)
r   itemsrescale_and_normalizer   copyFinterpolater   r   r   )selfr   r   r   r   r   r   r   r   r   r   kwargsgrouped_imagesgrouped_images_indexprocessed_images_groupedshapestacked_imagesrescale_and_normalize_imagesr'   interpolated_images_groupedr&   s                        ځ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/uvdoc/image_processing_uvdoc.py_preprocessUVDocImageProcessor._preprocess*   s    0EV/o,#% %3%9%9%;!E>!77
LV_N ,Ay!Q,>?N.<$+ &< (66N'e$6;;=/D(;K0
, ')#%3%9%9%;!E>!""$++tzz)Bcg" 2@'. &< &&ACWX".?S&$5#6
 	
    
predictionr'   scalec                    [        U5      n[        R                  " [        U5      UR                  S9n/ n[        U5       H  u  pgUR                  S:X  a  UR                  S5      nUR                  UR                  5      nUR                  SS u  p[        R                  " XUS-    X4SSS	9n
U
R                  SSSS5      n[        R                  " X{SS
9nUR                  S5      R                  SSS5      nX-  nUR                  S/S9R                  [        R                   SSS9nUR#                  SU05        GM     U$ )aN  
Post-process document rectification predictions to convert them into rectified images.

Args:
    prediction: Predicted 2D Bezier mesh coordinates, shape (B, 2, H, W)
    original_images: List of original input tensors, each of shape (C, H_i, W_i). Images may have different sizes.
    scale: Scaling factor for output images (default: 255.0)

Returns:
    List of dictionaries containing rectified images. Each dictionary has:
        - "images": Rectified image tensor of shape (H, W, 3) with dtype torch.uint8
                  and BGR channel order (suitable for OpenCV visualization)
)devicer   r   r    Nr!   r"   Tr#   )r%   )dimsF)dtypenon_blockingr-   r   )listr   tensorfloatr@   	enumeratendim	unsqueezetor5   r.   r/   permutegrid_samplesqueezeflipuint8append)r0   r=   r'   r>   
image_listresultsioriginal_imageoriginal_heightoriginal_widthupsampled_meshrearranged_mesh	rectifiedimages                 r9   #post_process_document_rectification7UVDocImageProcessor.post_process_document_rectification[   sF   & /*
U5\*2C2CD!*:!6A""a'!/!9!9!!<+..z/@/@AN.<.B.B12.F+O ]]q1u%%6"	N -44Q1a@O nUYZI %%a(00Aq9E MEJJRDJ),,5;;TX],^ENNHe,-9 "7< r<    )g     o@)__name__
__module____qualname____firstlineno__r   r   r   r   BILINEARresamplerE   boolr	   rG   strr   r   r:   r   Tensordictr\   __static_attributes__r^   r<   r9   r   r   "   s    JIC(D!**H/
^$/
 /
 	/

 /
 /
 /
 DK'$./
 4;&-/
 +/
 j(4//
 
/
j 	5LL5 ell+5 	5
 
d3$%	&5 5r<   r   )r   torch.nn.functionalnn
functionalr.   feature_extraction_utilsr   image_processing_backendsr   image_transformsr   r   image_utilsr   r	   utilsr
   utils.genericr   utils.import_utilsr   r   __all__r^   r<   r9   <module>ru      s\   ,    4 ; E 7 # ' * 	:l, l  l^ !
!r<   