
    ^j1                         d dl Z d dlmc mc 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 ddlmZmZ ddlmZmZmZ dd	lmZ dd
lmZ  ej8                  e      Ze ed       G d de                    ZdgZ y)    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                        e Zd ZdZeZeZdddZdddZ	dZ
dZdZdZdZdddeddfd	Z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f dZdee   f fdZd Zd Z xZS )SLANeXtImageProcessor   i   )heightwidthTimageztorch.Tensorsizereturnc                 n
   |j                   \  }}}}|j                  ||z  ||      }|j                  }t        |j                  |j
                        t        ||      z  }t        ||z        }	t        ||z        }
t        j                  |
t        j                  |      }|dz   t        |      t        |
      z  z  dz
  }|j                         j                  t        j                        }||j                         z
  }t        j                  |dk  t        j                  |      |      }t        j                  |dk  t        j                  |      |      }t        j                  ||dz
  k\  t        j                   |      |      }t        j                  ||dz
  k\  t        j"                  ||dz
        |      }|dz  dz   j                         j                  t        j                        }d|z
  }t        j                  |	t        j                  |      }|dz   t        |      t        |	      z  z  dz
  }|j                         j                  t        j                        }||j                         z
  }t        j                  |dk  t        j                  |      |      }t        j                  |dk  t        j                  |      |      }t        j                  ||dz
  k\  t        j                   |      |      }t        j                  ||dz
  k\  t        j"                  ||dz
        |      }|dz  dz   j                         j                  t        j                        }d|z
  }|j%                  dd      j                  t        j&                        }|j                  t        j                        }|j)                         }|dz   j)                         }|j)                         }|dz   j)                         }|d d |d d d f   |d d d f   f   }|d d |d d d f   |d d d f   f   }|d d |d d d f   |d d d f   f   }|d d |d d d f   |d d d f   f   } |j                  d|	d      }!|j                  d|	d      }"|j                  dd|
      }#|j                  dd|
      }$|"|$|z  |#|z  z   z  |!|$|z  |#| z  z   z  z   }%|%dz   d	z	  }%|%j%                  dd      j                  t        j&                        }&|&j                  |||	|
      j                  |j*                  
      S )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'                                          /var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/slanext/image_processing_slanext.py_resizezSLANeXtImageProcessor._resize4   s   
 /4kk+
Hfe

:0&%@DKK,s65/AAfun-UU]+\\,emmFS
#eu\7J(JKcQ**5;;7!4!4!66{{=1#4e6F6F|6TVbcMA$5u7G7G7VXef{{=EAI#=u|?\^jkUQY&uqy(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{{:x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           	         |t               st        j                  d       t        ||      \  }}i }|j	                         D ]  \  }}|r| j                  ||      }|||<   ! t        ||      }t        ||      \  }}i }|j	                         D ]4  \  }}|r| j                  ||      }| j                  ||||	|
|      }|||<   6 t        ||      }|r| j                  |||      }t        d|i|      S )Nz&Resampling is not supported in SLANeXt)rd   )r   r   )rc   rd   pixel_values)datatensor_type)r   loggerwarning_oncer   itemsrV   r   center_croprescale_and_normalizepadr   )r3   rX   rY   r   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   kwargsgrouped_imagesgrouped_images_indexresized_images_groupedr"   stacked_imagesresized_imagesprocessed_images_groupedprocessed_imagess                            rU   _preprocessz!SLANeXtImageProcessor._preprocessv   s@   & (@(B HI 0EV^n/o,,!#%3%9%9%; 	;!E>!%N!N,:"5)	; ((>@TU 0E^fv/w,,#% %3%9%9%; 	=!E>!%!1!1.)!L!77
NL*V_N /=$U+	= **BDXY#xx(88^nxo.2B!CQ_``rW   rp   c                 D    t        |   di | | j                          y )N )super__init__init_decoder)r3   rp   	__class__s     rU   r|   zSLANeXtImageProcessor.__init__   s    "6"rW   c                    g d}|t        d      D cg c]  }d|dz    d c}z  }|t        d      D cg c]  }d|dz    d c}z  }d|vr|j                  d       d|v r|j                  d       d	g|z   d
gz   }t        |      D ci c]  \  }}||
 c}}| _        || _        g d| _        | j                  d	   | _        | j                  d
   | _        yc c}w c c}w c c}}w )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)r3   dict_characterichars       rU   r}   z"SLANeXtImageProcessor.init_decoder   s    
 	%)DQZAwa0DD%)DQZAwa0DDn,!!+.^#!!&)>1UG;,5n,EFDT1WF	'4ii&ii& ED Gs   CC!C&c                    |j                   | _        | j                  dd }t        | j                        t        | j                        g}t        | j                        }|j                  d      }|j                  d      j                  }g }|j                  d   }t        |      D ]  }g }	g }
t        |j                  d         D ]Y  }t        |||f         }|dkD  r||k(  r n=||v r$| j                  |   }|	j                  |       |
j                  |||f          [ |j                  |	       t        j                  |
      j                         j                         } g d|d   z   g dz   }|dS )aO  
        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)r3   outputsstructure_probsignored_tokensend_idxstructure_idxstructure_str_listr4   batch_indexstructure_list
score_listpositionchar_idxtextr   r   s                   rU   post_process_table_recognitionz4SLANeXtImageProcessor.post_process_table_recognition   s   ( --	))Aa.dkk*C,<=dkk"'..1.5)--!-4;;"((+
 , 	DKNJ!-"5"5a"89 J}[(-BCDa<H$7~-~~h/%%d+!!/+x2G"HIJ %%n5#kk*5::<AACO	D 46H6KKNpp	&?KKrW   )__name__
__module____qualname__rZ   r   r`   r	   ra   r   rc   do_convert_rgbrY   r]   r_   rb   r
   rV   listboolr(   strr   r   rx   r   r   r|   r}   r   __classcell__)r~   s   @rU   r   r   &   st    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rW   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   rj   r   __all__rz   rW   rU   <module>r      s|   ,  2 2 ; 2 E P P 4 F F ' * 
		H	% 	:VL. VL  VLr #
#rW   