
    Z j1                         S SK r S SKJs  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Jr  SSKJrJr  SSKJrJrJr  SS	KJr  SS
KJr  \R8                  " \5      r\\" SS9 " S S\5      5       5       rS/r g)    N   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)IMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STDSizeDict)ImagesKwargsUnpack)auto_docstringis_torchdynamo_compilinglogging)
TensorType)requires)torch)backendsc                      ^  \ rS rSrSr\r\rSSS.r	SSS.r
SrSrSrSrSrSSS\S	S4S
 jrS\S   S\S\SSS\S\S\S\S\S\\\   -  S-  S\\\   -  S-  S\S-  S\S-  S\S-  S\\-  S-  S	\4 S jrS\\   4U 4S jjrS rS rS rU =r$ )!SLANeXtImageProcessor&      i   )heightwidthTimageztorch.Tensorsizereturnc                 f
   UR                   u  p4pVUR                  X4-  XV5      nUR                  n[        UR                  UR
                  5      [        XV5      -  n[        XX-  5      n	[        Xh-  5      n
[        R                  " U
[        R                  US9nUS-   [        U5      [        U
5      -  -  S-
  nUR                  5       R                  [        R                  5      nXR                  5       -
  n[        R                  " US:  [        R                  " U5      U5      n[        R                  " US:  [        R                  " U5      U5      n[        R                  " XS-
  :  [        R                   " U5      U5      n[        R                  " XS-
  :  [        R"                  " XS-
  5      U5      nUS-  S-   R                  5       R                  [        R                  5      nSU-
  n[        R                  " U	[        R                  US9nUS-   [        U5      [        U	5      -  -  S-
  nUR                  5       R                  [        R                  5      nUUR                  5       -
  n[        R                  " US:  [        R                  " U5      U5      n[        R                  " US:  [        R                  " U5      U5      n[        R                  " UUS-
  :  [        R                   " U5      U5      n[        R                  " UUS-
  :  [        R"                  " UUS-
  5      U5      nUS-  S-   R                  5       R                  [        R                  5      nSU-
  nUR%                  SS5      R                  [        R&                  5      nUR                  [        R                  5      nUR)                  5       nUS-   R)                  5       nUR)                  5       nUS-   R)                  5       nUS S 2US S 2S 4   US S S 24   4   nUS S 2US S 2S 4   US S S 24   4   nUS S 2US S 2S 4   US S S 24   4   nUS S 2US S 2S 4   US S S 24   4   n UR                  SU	S5      n!UR                  SU	S5      n"UR                  SSU
5      n#UR                  SSU
5      n$U"U$U-  U#U-  -   -  U!U$U-  U#U -  -   -  -   n%U%S-   S	-	  n%U%R%                  SS5      R                  [        R&                  5      n&U&R                  X4X5      R                  UR*                  S
9$ )N)dtypedeviceg      ?r      r   i      i       )r   )shapeviewr   maxr   r   roundr   arangefloat32floatfloortoint32where
zeros_like	ones_like	full_likeclampuint8longr   )'selfr   r   
batch_sizechannelsr   r   r   scaletarget_heighttarget_width
target_colsrc_colsrc_col_floorsrc_col_fracweight_rightweight_left
target_rowsrc_rowsrc_row_floorsrc_row_fracweight_bottom
weight_topimage_uint8image_int32col_left	col_rightrow_top
row_bottompixel_top_leftpixel_top_rightpixel_bottom_leftpixel_bottom_rightweight_bottom_3dweight_top_3dweight_right_3dweight_left_3dinterpresults'                                          څ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/slanext/image_processing_slanext.py_resizeSLANeXtImageProcessor._resize4   s   
 /4kk+
f

:0&@DKK,s6/AAfn-U]+\\,emmFS
#eu\7J(JKcQ**5;;7!4!4!66{{=1#4e6F6F|6TVbcMA$5u7G7G7VXef{{=AI#=u|?\^jkQY&qy(QS`
 %t+c188:==ekkJ\)\\-u}}VT
#fm8L(LMPSS**5;;7!4!4!66{{=1#4e6F6F|6TVbcMA$5u7G7G7VXef{{=FQJ#>P\@]_klVaZ'QR
)SUb
 &,s299;>>u{{KM)
kk!S),,U[[9!nnU[[1 %%'"Q&,,.	$$&#a'--/
$Q4(8(47:K%KL%aD)99T1W;M&MN':ag+>q@Q(QR(Jq$w,?4QR7AS)ST(--aB"=!<&++Aq,?$))!Q=^+o.OO
1B B_WiEi ijk G$+a%((5{{:MPPW\WbWbPcc    images	do_resizeresamplez"tvF.InterpolationMode | int | Nonedo_center_crop	crop_size
do_rescalerescale_factordo_normalize
image_meanN	image_stddo_padpad_sizedisable_groupingreturn_tensorsc           	         Ub$  [        5       (       d  [        R                  S5        [        XS9u  nn0 nUR	                  5        H"  u  nnU(       a  U R                  UUS9nUUU'   M$     [        UU5      n[        UUS9u  nn0 nUR	                  5        H8  u  nnU(       a  U R                  UU5      nU R                  UXxXU5      nUUU'   M:     [        UU5      nU(       a  U R                  UXS9n[        SU0US9$ )Nz&Resampling is not supported in SLANeXt)rf   )r   r   )re   rf   pixel_values)datatensor_type)r   loggerwarning_oncer   itemsrW   r   center_croprescale_and_normalizepadr   )r4   rZ   r[   r   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   kwargsgrouped_imagesgrouped_images_indexresized_images_groupedr#   stacked_imagesresized_imagesprocessed_images_groupedprocessed_imagess                            rV   _preprocess!SLANeXtImageProcessor._preprocessv   s/   & (@(B(B HI 0EV/o,,!#%3%9%9%;!E>!%N!N,:"5) &< ((>@TU 0E^fv/w,,#% %3%9%9%;!E>!%!1!1.)!L!77
LV_N /=$U+ &< **BDXY#xx(88xo.2B!CQ_``rY   rr   c                 F   > [         TU ]  " S0 UD6  U R                  5         g )N )super__init__init_decoder)r4   rr   	__class__s     rV   r   SLANeXtImageProcessor.__init__   s    "6"rY   c                    / SQnU[        S5       Vs/ s H  nSUS-    S3PM     sn-  nU[        S5       Vs/ s H  nSUS-    S3PM     sn-  nSU;  a  UR                  S5        SU;   a  UR                  S5        S	/U-   S
/-   n[        U5       VVs0 s H  u  p#X2_M	     snnU l        Xl        / SQU l        U R                  S	   U l        U R                  S
   U l        gs  snf s  snf s  snnf )a  
Initialize the decoder vocabulary for table structure recognition.

Builds a character dictionary mapping HTML table structure tokens (e.g., `<thead>`, `<tr>`, `<td>`, colspan/
rowspan attributes) to integer indices. The dictionary includes special `"sos"` (start-of-sequence) and
`"eos"` (end-of-sequence) tokens. Merged `<td></td>` tokens are used in place of standalone `<td>` tokens
when applicable.
)
z<thead>z</thead>z<tbody>z</tbody>z<tr>z</tr><td><td>z</td>   z
 colspan="r   "z
 rowspan="	<td></td>r   soseos)r   r   r   N)	rangeappendremove	enumeratedict	charactertd_tokenbos_ideos_id)r4   dict_characterichars       rV   r   "SLANeXtImageProcessor.init_decoder   s    
 	%)D)QZAwa0)DD%)D)QZAwa0)DDn,!!+.^#!!&)>1UG;,5n,EF,ETW,EF	'4ii&ii& ED Gs   C&C+C0c                    UR                   U l        U R                  SS n[        U R                  5      [        U R                  5      /n[        U R                  5      nUR                  SS9nUR                  SS9R                  n/ nUR                  S   n[        U5       H  n/ n	/ n
[        UR                  S   5       H[  n[        XXU4   5      nUS:  a  X:X  a    O@X;   a  M&  U R                  U   nU	R                  U5        U
R                  X(U4   5        M]     UR                  U	5        [        R                  " U
5      R                  5       R                  5       nM     / SQUS   -   / SQ-   nUWS.$ )a  
Post-process the raw model outputs to decode the predicted table structure into an HTML token sequence.

Converts the model's predicted probability distributions over the structure vocabulary into a sequence of
HTML tokens representing the table structure. The decoded tokens are wrapped with `<html>`, `<body>`, and
`<table>` tags to form a complete HTML table structure.

Args:
    outputs ([`SLANeXtForTableRecognitionOutput`]):
        Raw outputs from the SLANeXt model. The `last_hidden_state` field contains the predicted probability
        distributions over the structure vocabulary at each decoding step, with shape
        `(batch_size, max_text_length, num_classes)`.

Returns:
    `dict`: A dictionary containing:
        - **structure** (`list[str]`): The predicted HTML table structure as a list of tokens, wrapped with
          `<html>`, `<body>`, and `<table>` tags.
        - **structure_score** (`float`): The mean confidence score across all predicted tokens.
r   r    r   )dim)z<html>z<body>z<table>)z</table>z</body>z</html>)	structurestructure_score)last_hidden_statepredintr   r   argmaxr%   valuesr#   r   r   r   r   stackmeanitem)r4   outputsstructure_probsignored_tokensend_idxstructure_idxstructure_str_listr5   batch_indexstructure_list
score_listpositionchar_idxtextr   r   s                   rV   post_process_table_recognition4SLANeXtImageProcessor.post_process_table_recognition   sn   ( --	))Aa.dkk*C,<=dkk"'..1.5)--!-4;;"((+
 ,KNJ!-"5"5a"89}(-BCDa<H$7-~~h/%%d+!!/x2G"HI : %%n5#kk*5::<AACO - 46H6KKNpp	&?KKrY   )r   r   r   r   r   r   ) __name__
__module____qualname____firstlineno__r\   r   rb   r	   rc   r   re   do_convert_rgbr[   r_   ra   rd   r
   rW   listboolr)   strr   r   rz   r   r   r   r   r   __static_attributes____classcell__)r   s   @rV   r   r   &   sz    H&J$IC(D,HNIJLF@d@d @d 
	@dD0a^$0a 0a 	0a
 70a 0a 0a 0a 0a 0a DK'$.0a 4;&-0a t0a T/0a +0a  j(4/!0a$ 
%0ad!5 "'H.L .LrY   r   )!r   $torchvision.transforms.v2.functional
transformsv2
functionaltvFimage_processing_backendsr   image_processing_utilsr   image_transformsr   r   image_utilsr   r	   r
   processing_utilsr   r   utilsr   r   r   utils.genericr   utils.import_utilsr   
get_loggerr   rl   r   __all__r}   rY   rV   <module>r      sz   ,  2 2 ; 2 E P P 4 F F ' * 
		H	% 	:VL. VL  VLr #
#rY   