Ë
    ÜÓ^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½  € ð
 /4¯k©kÑ+ˆ
�H˜f eØ—
‘
˜:¨Ñ0°&¸%Ó@ˆà—‘ˆä�D—K‘K §¡Ó,¬s°6¸5Ó/AÑAˆÜ˜f u™nÓ-ˆÜ˜U U™]Ó+ˆä—\‘\ ,´e·m±mÈFÔSˆ
Ø Ñ#¬¨e«´u¸\Ó7JÑ(JÑKÈcÑQˆØŸ™›×*Ñ*¬5¯;©;Ó7ˆØ ×!4Ñ!4Ó!6Ñ6ˆä—{‘{ =°1Ñ#4´e×6FÑ6FÀ|Ó6TÐVbÓcˆÜŸ™ M°AÑ$5´u×7GÑ7GÈÓ7VÐXeÓfˆÜ—{‘{ =°E¸A±IÑ#=¼u¿¹È|Ó?\Ð^jÓkˆÜŸ™Ø˜U Q™YÑ&¬¯©¸ÀuÈqÁyÓ(QÐS`ó
ˆð % tÑ+¨cÑ1×8Ñ8Ó:×=Ñ=¼e¿k¹kÓJˆØ˜\Ñ)ˆä—\‘\ -´u·}±}ÈVÔTˆ
Ø Ñ#¬¨f«¼¸mÓ8LÑ(LÑMÐPSÑSˆØŸ™›×*Ñ*¬5¯;©;Ó7ˆØ ×!4Ñ!4Ó!6Ñ6ˆÜ—{‘{ =°1Ñ#4´e×6FÑ6FÀ|Ó6TÐVbÓcˆÜŸ™ M°AÑ$5´u×7GÑ7GÈÓ7VÐXeÓfˆÜ—{‘{ =°F¸Q±JÑ#>ÄÇÁÐP\Ó@]Ð_kÓlˆÜŸ™Ø˜V a™ZÑ'¬¯©¸ÈÐQRÉ
Ó)SÐUbó
ˆð &¨Ñ,¨sÑ2×9Ñ9Ó;×>Ñ>¼u¿{¹{ÓKˆØ˜MÑ)ˆ
à—k‘k ! SÓ)×,Ñ,¬U¯[©[Ó9ˆØ!—n‘n¤U§[¡[Ó1ˆØ ×%Ñ%Ó'ˆØ" QÑ&×,Ñ,Ó.ˆ	Ø×$Ñ$Ó&ˆØ# aÑ'×-Ñ-Ó/ˆ
à$¢Q¨²°4°Ñ(8¸(À4ÊÀ7Ñ:KÐ%KÑLˆØ%¢a¨²°D°Ñ)9¸9ÀTÊ1ÀWÑ;MÐ&MÑNˆØ'ª¨:²a¸°gÑ+>ÀÈÊqÈÑ@QÐ(QÑRÐØ(ª¨J²q¸$°wÑ,?ÀÈ4ÒQRÈ7ÑASÐ)SÑTÐà(×-Ñ-¨a°ÀÓBÐØ"Ÿ™¨¨=¸!Ó<ˆØ&×+Ñ+¨A¨q°,Ó?ˆØ$×)Ñ)¨!¨Q°Ó=ˆØØ˜^Ñ+¨oÀÑ.OÑOñ
à Ð1BÑ BÀ_ÐWiÑEiÑ iÑjñkˆð ˜GÑ$¨Ñ+ˆØ—‘˜a Ó%×(Ñ(¬¯©Ó5ˆà�{‰{˜: x°ÀÓM×PÑPÐW\×WbÑWbÐ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Ø 
¨N¸LÈ*ÐV_óˆNð /=Ð$ UÒ+ð	=ô *Ð*BÐDXÓYÐáØ#Ÿx™xÐ(8À8Ð^n˜xÓoÐä .Ð2BÐ!CÐQ_Ô`Ð`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ò   € ò
ˆð 	¼%À»)ÖD°Q˜Z¨¨A© w¨aÒ0ÒDÑDˆØ¼%À»)ÖD°Q˜Z¨¨A© w¨aÒ0ÒDÑDˆà˜nÑ,Ø×!Ñ! +Ô.Ø�^Ñ#Ø×!Ñ! &Ô)à˜ >Ñ1°U°GÑ;ˆÜ,5°nÓ,E×F¡  D�T˜1‘WÓFˆŒ	Ø'ˆŒÚ4ˆŒØ—i‘i Ñ&ˆŒØ—i‘i Ñ&ˆ�ùò 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„  € ð( ×-Ñ-ˆŒ	ØŸ)™) A a˜.ˆÜ˜dŸk™kÓ*¬C°·±Ó,<Ð=ˆÜ�d—k‘kÓ"ˆà'×.Ñ.°1Ð.Ó5ˆØ)×-Ñ-°!Ð-Ó4×;Ñ;ˆàÐØ"×(Ñ(¨Ñ+ˆ
Ü  Ó,ò 	DˆKØˆNØˆJÜ! -×"5Ñ"5°aÑ"8Ó9ò J�Ü˜}¨[¸(Ð-BÑCÓD�Ø˜a’< H°Ò$7ÙØ˜~Ñ-ØØ—~‘~ hÑ/�Ø×%Ñ% dÔ+Ø×!Ñ! /°+¸xÐ2GÑ"HÕIðJð ×%Ñ% nÔ5Ü#Ÿk™k¨*Ó5×:Ñ:Ó<×AÑAÓC‰Oð	Dò 4Ð6HÈÑ6KÑKÒNpÑ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   