
    ^jVB                        d Z ddlZddlZddlmZ ddlZddlmc mc m	Z
 ddlmZmZmZ ddlmZ ddlmZmZ ddlmZ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  ddl!m"Z" ddl#m$Z$ dZ% G d ded      Z&	 	 	 	 	 	 	 	 	 d"de'de(de'de'de(de(de(de(de)dz  de'dz  dej                  fdZ*ejV                  jX                  d        Z-e  G d  d!e             Z.d!gZ/y)#z%Image processor class for Pix2Struct.    N)Union)Image	ImageDraw	ImageFont   )TorchvisionBackend)BatchFeatureget_size_dict)group_images_by_shapereorder_images)ChannelDimension
ImageInputSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstring)hf_api)requires_backendszybelkada/fontsc                   V    e Zd ZU dZeed<   eeef   ed<   eed<   e	e   ez  dz  ed<   y)Pix2StructImageProcessorKwargsa  
    max_patches (`int`, *optional*):
        Maximum number of patches to extract.
    patch_size (`dict[str, int]`, *optional*, defaults to `{"height": 16, "width": 16}`):
        The patch size to use for the image. According to Pix2Struct paper and code, the patch size is 16x16.
    is_vqa (`bool`, *optional*, defaults to `False`):
        Whether or not the image processor is for the VQA task. If `True` and `header_text` is passed in, text is
        rendered onto the input images.
    header_text (`Union[list[str], str]`, *optional*):
        Text to render as a header. Only has an effect if `image_processor.is_vqa` is `True`.
    max_patches
patch_sizeis_vqaNheader_text)
__name__
__module____qualname____doc__int__annotations__dictstrboollist     /var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/pix2struct/image_processing_pix2struct.pyr   r   %   s5    
 S#XLcS4''r'   r   F)totaltext	text_size
text_colorbackground_colorleft_paddingright_paddingtop_paddingbottom_padding
font_bytes	font_pathreturnc
                 t   t        t        d       t        j                  d      }
|
j	                  |       }dj                  |      }||	t        j                  |      }n#|	|	}nt               j                  t        d      }t        j                  |d|      }t        j                  d	d
      }t        j                   |      }|j#                  d||      \  }}}}||z   |z   }||z   |z   }t        j                  d	||f|      }t        j                   |      }|j%                  ||f|||       |S )a  
    Render text. This script is entirely adapted from the original script that can be found here:
    https://github.com/google-research/pix2struct/blob/main/pix2struct/preprocessing/preprocessing_utils.py

    Args:
        text (`str`, *optional*, defaults to ):
            Text to render.
        text_size (`int`, *optional*, defaults to 36):
            Size of the text.
        text_color (`str`, *optional*, defaults to `"black"`):
            Color of the text.
        background_color (`str`, *optional*, defaults to `"white"`):
            Color of the background.
        left_padding (`int`, *optional*, defaults to 5):
            Padding on the left.
        right_padding (`int`, *optional*, defaults to 5):
            Padding on the right.
        top_padding (`int`, *optional*, defaults to 5):
            Padding on the top.
        bottom_padding (`int`, *optional*, defaults to 5):
            Padding on the bottom.
        font_bytes (`bytes`, *optional*):
            Bytes of the font to use. If `None`, the default font will be used.
        font_path (`str`, *optional*):
            Path to the font to use. If `None`, the default font will be used.
    visionP   )width)r*   
z	Arial.TTFzUTF-8)encodingsizeRGB)   r=   r   r   )font)fillr?   )r   render_texttextwrapTextWrapperwrapjoinioBytesIOr   hf_hub_downloadDEFAULT_FONT_PATHr   truetyper   newr   Drawtextbboxr*   )r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   wrapperlineswrapped_textr?   temp_img	temp_draw_wh
text_widthtext_heightimgdraws                          r(   rA   rA   9   s.   L k8, "",GLLdL#E99U#L)"3zz*%		x''(9;GdW9ED yy'Hx(I##FLt#DJAq!Q\!M1Jk/N2K ))EJ46F
GC>>#DII|[)<jtITJr'   c                    | j                   \  }}}}t        j                  j                  j	                  | ||f||f      }|j                  ||||d      }|j                  ddddd      j                  |||z  ||z  ||z  |z        }|S )a  
    Extract patches from image tensor. Returns tensor of shape (batch, rows, columns, patch_height*patch_width*channels).

    Args:
        image_tensor (`torch.Tensor`):
            Image tensor of shape (batch, channels, height, width).
        patch_height (`int`):
            Height of patches to extract.
        patch_width (`int`):
            Width of patches to extract.
    )strider         r   r=   )shapetorchnn
functionalunfoldreshapepermute)image_tensorpatch_heightpatch_width
batch_sizechannelsheightr8   patchess           r(   torch_extract_patchesrm      s     +7*<*<'J&%hh!!((k7R\hju[v(wGooj(L+rRGooaAq!,44Fl*E[,@(\BY\gBgG Nr'   c                       e Zd ZdZdZdZdddZdZdZe	Z
ddgZ	 d#d	eeef   ez  dz  d
ef fdZd Z	 	 d$dddededz  dedz  d
df
dZd%dZddded	ed
dfdZe	 d#dedeee   z  dz  dee	   d
ef fd       Zddej:                  dfdedeee   z  dz  dededeedf   dz  dee	   d
efdZ ded   deded	ed ee!z  dz  d!ed
efd"Z" xZ#S )&Pix2StructImageProcessorNT   rk   r8   i   Fflattened_patchesattention_maskr   r4   c                     t        |   di |}|+t        |t              st        di t	        |d      |d<   |S ||d<   |S )zc
        Process custom Pix2Struct kwargs, specifically converting patch_size to SizeDict.
        r   )r;   
param_namer&   )super_standardize_kwargs
isinstancer   r
   )selfr   kwargs	__class__s      r(   rw   z,Pix2StructImageProcessor._standardize_kwargs   sX     ,6v6!*Z*J#+#fmXd.e#fF<   $.F< r'   c                      y)zS
        Skip standard validation as Pix2Struct uses custom preprocessing.
        Nr&   )ry   rz   s     r(   _validate_preprocess_kwargsz4Pix2StructImageProcessor._validate_preprocess_kwargs   s     	r'   imagetorch.Tensorheaderr2   r3   c                 v   |j                   }|j                  }|j                  t        j                  k(  rt	        j
                  |      }nG|dz  j                  dd      j                  t        j                        }t	        j
                  |      }t        |||      }	t        |	j                  |j                        }
t        |j                  |
|j                  z  z        }t        |	j                  |
|	j                  z  z        }t        j                  d|
||z   fd      }|j                  |	j!                  |
|f      d       |j                  |j!                  |
|f      d|f       t	        j"                  |      j                  |      }|t        j                  k7  r|j%                         dz  }|S )a  
        Render header text on image using torch tensors.

        Args:
            image (`torch.Tensor`):
                Image tensor in channel-first format (C, H, W).
            header (`str`):
                Header text to render.
            font_bytes (`bytes`, *optional*):
                Font bytes to use for rendering.
            font_path (`str`, *optional*):
                Path to font file to use for rendering.

        Returns:
            `torch.Tensor`: Image with header in channel-first format (C, H, W).
           r   r2   r3   r<   whiter>   g     o@)devicedtyper`   uint8tvFto_pil_imageclamptorA   maxr8   r    rk   r   rK   pasteresizepil_to_tensorfloat)ry   r~   r   r2   r3   r   r   	image_pilimage_uint8header_image	new_width
new_heightnew_header_height	new_imageresults                  r(   render_headerz&Pix2StructImageProcessor.render_header   sy   .  ;;%++%((/I !3;--a588EK((5I #6jIV **IOO<	))Y-HIJ
 3 3y<CUCU7U VW IIei>O1O%PRYZ	++Y8I,JKVT	(()Z)@AAGXCYZ ""9-008 EKK\\^e+Fr'   imagesc                 6   |j                  dd      }|j                  dd      }|j                  d   |j                  d   z  |j                  d   z  }d|dz  z  }t        j                  |t        j
                  ||j                  	            }||z
  |z  S )
a$  
        Normalize batched images using per-image mean and standard deviation.

        Args:
            images (`torch.Tensor`):
                Batched float image tensor of shape (B, C, H, W).

        Returns:
            `torch.Tensor`: Normalized images of shape (B, C, H, W).
        )r=   r^   r   T)dimkeepdimr=   r^   r   g      ?      ?r   )meanstdr_   r`   maximumtensorr   )ry   r   r   r   num_elements_per_imagemin_stdadjusted_stddevs          r(   	normalizez"Pix2StructImageProcessor.normalize   s     {{y${7jjYj5!'a6<<?!BV\\RS_!T.33--U\\'#**-UV00r'   r   c           	         |j                   |j                  }}|j                  \  }}}}	|||z  z  ||	z  z  dz  }
t        t	        t        |
|z  |z        |      d      }t        t	        t        |
|	z  |z        |      d      }t        ||z  d      }t        ||z  d      }t        ||      }| j                  ||t        j                  j                  d      }t        |||      }|j                  \  }}}}|j                  |||z  |      }t        j                  ||j                        j                  |d      j!                  d|      j                  d||z  d      }t        j                  ||j                        j                  d|      j!                  |d      j                  d||z  d      }|j#                  |dd      }|j#                  |dd      }|dz   j%                         }|dz   j%                         }t        j&                  |||gd      }t        j(                  j*                  j-                  |d	d	d	|||z  z
  g      j%                         }|S )
a  
        Extract flattened patches from a batch of images.

        Args:
            images (`torch.Tensor`):
                Batched images tensor of shape (batch, channels, height, width).
            max_patches (`int`):
                Maximum number of patches to extract.
            patch_size (`SizeDict`):
                Dictionary containing patch height and width.

        Returns:
            `torch.Tensor`: Batched flattened patches with row/column IDs of shape (batch, max_patches, patch_dim).
        r   r=   rq   T)r~   r;   resample	antialiasr   r\   r   r   )rk   r8   r_   r   minr    r   r   r   InterpolationModeBILINEARrm   rd   r`   aranger   repeatexpandr   catra   rb   pad)ry   r   r   r   rg   rh   ri   rj   image_heightimage_widthscalenum_feasible_rowsnum_feasible_colsresized_heightresized_widthresize_sizerl   rowscolumnsdepthrow_idscol_idsr   s                          r(   extract_flattened_patchesz2Pix2StructImageProcessor.extract_flattened_patches  sU   ( %/$5$5z7G7Gk:@,,7
HlK | ;<k@YZ_bbC(<|(K$Lk Z\]^C(;k(I$JK XZ[\.=qA-;Q? nMJ6cF[F[FdFdptu (kJ+2==(
D'5 //*dWneD LLfmm4<<T1ELLQPWX``abdhkrdrtuv 	 LL7WQ VD!_WQw*	 	 ..R4..R4 Q;%%'Q;%%' GWg6B? $$((!Q;$QX.;Y1Z[aacr'   r   rz   c                 (    t        |   |fd|i|S )z
        header_text (`Union[str, list[str]]`, *optional*):
            Text to render as a header. Only has an effect if `image_processor.is_vqa` is `True`.
        r   )rv   
preprocess)ry   r   r   rz   r{   s       r(   r   z#Pix2StructImageProcessor.preprocessK  s     w!&LkLVLLr'   do_convert_rgbinput_data_formatr   ztorch.devicec           
         | j                  ||||      }|j                  d| j                        }|r|t        d      |j	                  dd      }|j	                  dd      }	t        |t              r|gt        |      z  }t        |      D 
cg c]  \  }
}| j                  |||
   ||	       }}
} | j                  |fi |S c c}}
w )z3
        Preprocess images for Pix2Struct.
        )r   r   r   r   r   Nz.A header text must be provided for VQA models.r2   r3   r   )_prepare_image_like_inputsgetr   
ValueErrorpoprx   r#   len	enumerater   _preprocess)ry   r   r   r   r   r   rz   r   r2   r3   ir~   s               r(   _preprocess_image_like_inputsz6Pix2StructImageProcessor._preprocess_image_like_inputsX  s     00)/	 1 
 Hdkk2" !QRRL$7J

;5I+s+*mc&k9
 !*& 1Au ""5+a.Z[d"eF 
  t1&11s   "C
do_normalizereturn_tensorsdisable_groupingc                    t        ||      \  }}	i }
i }|j                         D ]  \  }}|j                  t        j                  k(  r|j                         }|r| j                  |      }| j                  |||      }|j                  d      dk7  j                         }||
|<   |||<    t        |
|	      }t        ||	      }|r.t        j                  |d      }t        j                  |d      }t        ||d|      S )zA
        Preprocess images to extract flattened patches.
        )r   )r   r   r   r\   r   r   )rr   rs   )datatensor_type)r   itemsr   r`   r   r   r   r   sumr   stackr	   )ry   r   r   r   r   r   r   rz   grouped_imagesgrouped_images_indexflattened_patches_groupedattention_masks_groupedr_   stacked_imagesrl   masksrr   attention_maskss                     r(   r   z$Pix2StructImageProcessor._preprocess  s$    0EV^n/o,,$&!"$%3%9%9%; 	3!E>##u{{2!/!5!5!7 !%!?44%;: 5 G [[R[(A-446E/6%e,-2#E*	3" ++DFZ[()@BVW  %,=1 E#kk/qAO'8O\&
 	
r'   )N)NN)r   r   r4   r   )$r   r   r   rescale_factorr   r   r   r   r   r   valid_kwargsmodel_input_namesr"   r#   r    r   rw   r}   bytesr   r   r   r   r   r%   r   r	   r   r   FIRSTr$   r   r   r   r   __classcell__)r{   s   @r(   ro   ro      s   NLN,JKF1L,.>? 8<cNX-4 
	  $( $66 6 DL	6
 :6 
6p1*CC C 	C
 
CJ  /3
M
M 49_t+
M 78	
M
 

M 
M /3#.>.D.D48&2&2 49_t+&2 	&2
 ,&2 c>)*T1&2 78&2 
&2P/
^$/
 /
 	/

 /
 j(4//
 /
 
/
r'   ro   )	$   blackr      r   r   r   NN)0r   rF   rB   typingr   r`   $torchvision.transforms.v2.functional
transformsv2rb   r   PILr   r   r   image_processing_backendsr   image_processing_utilsr	   r
   image_transformsr   r   image_utilsr   r   r   processing_utilsr   r   utilsr   r   	utils.hubr   utils.import_utilsr   rI   r   r#   r    r   rA   compilerdisablerm   ro   __all__r&   r'   r(   <module>r      s@   , 	    2 2 + + ; A E A A 4 /  3 % (\ (, ## C
CC C 	C
 C C C C C TzC [[CN  * X
1 X
 X
v &
&r'   