ó
    ˆŽ¡jmp  ã                   ó  • S r SSKJr  SSKrSSKJr  SSKJr  SSK	J
r
JrJr  SSKJrJrJrJr  SS	KJr  \R(                  " \5      r\" 5       (       a  SSKrS
 rS rS rS rS rS rS r\" SS9\ " S S\5      5       5       rS/r g)z
Processor class for SAM3.
é    )ÚdeepcopyNé   )Ú
ImageInput)ÚProcessorMixin)ÚBatchEncodingÚPreTokenizedInputÚ	TextInput)Ú
TensorTypeÚauto_docstringÚis_torch_availableÚlogging)Úrequiresc                 ó�   • U R                  S5      u  pp4USU-  -
  USU-  -
  USU-  -   USU-  -   /n[        R                  " USS9$ ©Néÿÿÿÿç      à?©Údim©ÚunbindÚtorchÚstack©ÚxÚx_cÚy_cÚwÚhÚbs         Ú¶C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\transformers/models/sam3/processing_sam3.pyÚbox_cxcywh_to_xyxyr!   #   sR   € Ø—X‘X˜b“\�N€CˆaØ
��a‘‰-˜3  q¡™=¨C°#¸±'©M¸SÀ3ÈÁ7¹]ÐL€AÜ�;Š;�q˜bÑ!Ð!ó    c                 óv   • U R                  S5      u  pp4USU-  -
  USU-  -
  X4/n[        R                  " USS9$ r   r   r   s         r    Úbox_cxcywh_to_xywhr$   )   s@   € Ø—X‘X˜b“\�N€CˆaØ
��a‘‰-˜3  q¡™=¨AÐ4€AÜ�;Š;�q˜bÑ!Ð!r"   c                 óf   • U R                  S5      u  pp#XX-   X-   /n[        R                  " USS9$ ©Nr   r   r   ©r   Úyr   r   r   s        r    Úbox_xywh_to_xyxyr)   /   ó4   € Ø—‘˜"“�J€Aˆ!Ø
�A‘E˜Q™UÐ$€AÜ�;Š;�q˜bÑ!Ð!r"   c                 óv   • U R                  S5      u  pp#U SU-  -   USU-  -   X#/n[        R                  " USS9$ r   r   r'   s        r    Úbox_xywh_to_cxcywhr,   5   s@   € Ø—‘˜"“�J€Aˆ!Ø
ˆc�A‰g‰+˜˜S 1™W™¨Ð0€AÜ�;Š;�q˜bÑ!Ð!r"   c                 óf   • U R                  S5      u  pp#XX -
  X1-
  /n[        R                  " USS9$ r&   r   )r   r(   ÚXÚYr   s        r    Úbox_xyxy_to_xywhr0   ;   r*   r"   c                 ó|   • U R                  S5      u  pp4X-   S-  X$-   S-  X1-
  XB-
  /n[        R                  " USS9$ )Nr   é   r   r   )r   Úx0Úy0Úx1Úy1r   s         r    Úbox_xyxy_to_cxcywhr7   A   sB   € Ø—X‘X˜b“\�N€BˆBØ
‰'�Q‰˜™ A™¨©°2±7Ð<€AÜ�;Š;�q˜bÑ!Ð!r"   c                 ó<   • U R                  S5      u  pp4X1-
  XB-
  -  $ )zŸ
Batched version of box area. Boxes should be in [x0, y0, x1, y1] format.

Inputs:
- boxes: Tensor of shape (..., 4)

Returns:
- areas: Tensor of shape (...,)
r   )r   )Úboxesr3   r4   r5   r6   s        r    Úbox_arear:   G   s%   € ð —\‘\ "Ó%�N€BˆBØ‰G˜™Ñ Ð r"   )r   )Úbackendsc                   ó(  ^ • \ rS rSr S"S\S-  S\4U 4S jjjr\       S#S\S-  S\\	-  \
\   -  \
\	   -  S-  S\S-  S	\
\
\
\         \R                  -  S-  S
\
\
\
\         \R                  -  S-  S\
\
\      \R                  -  S-  S\\-  S-  S\4S jj5       rS$S%S jjrS rS&S jrS rS'S jrS(S jrS rS r S'S\R                  \R6                  -  \
-  S\S\S\S\S-  S\
4S jjrS)S jr S*S jrS+S jr   S,S  jr S!r!U =r"$ )-ÚSam3ProcessoréU   NÚtarget_sizeÚpoint_pad_valuec                 ó„   >• [         TU ]  " X40 UD6  X@l        Ub  X0l        gU R                  R                  S   U l        g)z×
target_size (`int`, *optional*):
    The target size (target_size, target_size) to which the image will be resized.
point_pad_value (`int`, *optional*, defaults to -10):
    The value used for padding input boxes.
NÚheight)ÚsuperÚ__init__r@   Úimage_processorÚsizer?   )ÚselfrE   Ú	tokenizerr?   r@   ÚkwargsÚ	__class__s         €r    rD   ÚSam3Processor.__init__X   sA   ø€ ô 	‰Ò˜Ñ>°vÒ>Ø.ÔØ*5Ñ*A˜;ÕÀt×G[ÑG[×G`ÑG`ÐaiÑGjˆÕr"   ÚimagesÚtextÚsegmentation_mapsÚinput_boxesÚinput_boxes_labelsÚoriginal_sizesÚreturn_tensorsÚreturnc                 óî  • Sn	Ub  U R                   " U4UUS.UD6n	O[UbJ  [        U[        R                  5      (       a  UR	                  5       R                  5       n[        SU0US9n	OUb  [        S5      eU R                  X$5      nUb(  U R                  X'SSS9n
U	b  U	R                  U
5        OU
n	UGb=  U	S   nU R                  US	S
SSS9nU R                  USSSS9nUb  Uc  U R                  U5      nUb  U R                  U5      SS nUb  U R                  U5      SS nUb  Ub  WW:w  a  [        S5      eUbi  U R                  UWS/-   5      n[        R                  " U[        R                   S9nU R#                  UUSSS9  [%        U5      nU	R                  SU05        UbH  U R                  UW5      n[        R                  " U[        R&                  S9nU	R                  SU05        U	$ )aÝ  
images (`ImageInput`, *optional*):
    The image(s) to process.
text (`str`, `list[str]`, `list[list[str]]`, *optional*):
    The text to process.
segmentation_maps (`ImageInput`, *optional*):
    The segmentation maps to process.
input_boxes (`list[list[list[float]]]`, `torch.Tensor`, *optional*):
    The bounding boxes to process.
input_boxes_labels (`list[list[int]]`, `torch.Tensor`, *optional*):
    The labels for the bounding boxes.
original_sizes (`list[list[float]]`, `torch.Tensor`, *optional*):
    The original sizes of the images.

Returns:
    A [`BatchEncoding`] with the following fields:
    - `pixel_values` (`torch.Tensor`): The processed image(s).
    - `original_sizes` (`list[list[float]]`): The original sizes of the images.
    - `labels` (`torch.Tensor`): The processed segmentation maps (if provided).
    - `input_boxes_labels` (`torch.Tensor`): The processed labels for the bounding boxes.
    - `input_boxes` (`torch.Tensor`): The processed bounding boxes.
N)rN   rR   rQ   )Útensor_typezKEither images or original_sizes must be provided if input_boxes is not NoneÚ
max_lengthé    )rR   ÚpaddingrV   r   r9   z)[image level, box level, box coordinates]é   )Úexpected_depthÚ
input_nameÚexpected_formatÚexpected_coord_sizer2   Úlabelsz[image level, box level])rZ   r[   r\   zaInput boxes and labels have inconsistent dimensions. Please ensure they have the same dimensions.)ÚdtypeT)Úis_bounding_boxÚpreserve_paddingrO   rP   )rE   Ú
isinstancer   ÚTensorÚcpuÚtolistr   Ú
ValueErrorÚ_resolve_text_promptsrH   ÚupdateÚ_validate_single_inputÚ_generate_default_box_labelsÚ_get_nested_dimensionsÚ_pad_nested_listÚtensorÚfloat32Ú_normalize_tensor_coordinatesr7   Úint64)rG   rL   rM   rN   rO   rP   rQ   rR   rI   ÚencodingÚtext_inputsÚprocessed_boxesÚprocessed_boxes_labelsÚboxes_max_dimsÚboxes_labels_max_dimsÚpadded_boxesÚfinal_boxesÚpadded_boxes_labelsÚfinal_boxes_labelss                      r    Ú__call__ÚSam3Processor.__call__e   sl  € ðD ˆØÑØ×+Ò+Øðà"3Ø-ñð ñ	‰Hð Ñ'Ü˜.¬%¯,©,×7Ñ7Ø!/×!3Ñ!3Ó!5×!<Ñ!<Ó!>�Ü$Ð&6¸Ð%GÐUcÑd‰HØÑ$ÜÐjÓkÐkà×)Ñ)¨$Ó<ˆØÑØŸ.™.¨ÐVbÐoq˜.ÐrˆKØÑ#Ø—‘ Õ,à&�ð Ò"Ø%Ð&6Ñ7ˆNà"×9Ñ9ØØ Ø"Ø KØ$%ð :ð ˆOð &*×%@Ñ%@Ø"Ø Ø#Ø :ð	 &Að &Ð"ð Ñ*Ð/EÑ/MØ)-×)JÑ)JÈ?Ó)[Ð&ð Ñ*Ø!%×!<Ñ!<¸_Ó!MÈbÈqÐ!Q�Ø%Ñ1Ø(,×(CÑ(CÐDZÓ([Ð\^Ð]^Ð(_Ð%ð Ñ*Ð/EÑ/QØ!Ð%:Ó:Ü$Ø{óð ð
 Ñ*Ø#×4Ñ4°_ÀnÐXYÐWZÑFZÓ[�Ü#Ÿlšl¨<¼u¿}¹}ÑM�Ø×2Ñ2Ø ÀÐX\ð 3ñ ô 1°Ó=�Ø—‘ °Ð <Ô=à%Ñ1Ø&*×&;Ñ&;Ð<RÐTiÓ&jÐ#Ü%*§\¢\Ð2EÌUÏ[É[Ñ%YÐ"Ø—‘Ð!5Ð7IÐ JÔKàˆr"   c                 óÒ   • Uu  pE[        U5      R                  5       nU(       a  UR                  SSS5      nUS   U-  US'   US   U-  US'   U(       a  UR                  SS5      nU$ )a³  
Expects a numpy array of length 2 in the final dimension. Requires the original image size in (H, W) format.

Args:
    target_size (`int`):
        The target size of the image.
    coords (`torch.Tensor`):
        The coordinates to be normalized.
    original_size (`tuple`):
        The original size of the image.
    is_bounding_box (`bool`, *optional*, defaults to `False`):
        Whether the coordinates are bounding boxes.
r   r2   ).r   ).é   rY   )r   ÚfloatÚreshape)rG   ÚcoordsÚoriginal_sizer`   Úold_hÚold_ws         r    Ú_normalize_coordinatesÚ$Sam3Processor._normalize_coordinatesÒ   so   € ð %‰ˆÜ˜&Ó!×'Ñ'Ó)ˆæØ—^‘^ B¨¨1Ó-ˆFØ ™¨%Ñ/ˆˆv‰Ø ™¨%Ñ/ˆˆv‰æØ—^‘^ B¨Ó*ˆFàˆr"   c                 óT   • U Vs/ s H  o"c  SOS/[        U5      -  PM     sn$ s  snf )z[Generate default box labels: `point_pad_value` for None (padded) entries, 1 for real boxes.Nr~   )Úlen)rG   rs   Úimage_boxess      r    rj   Ú*Sam3Processor._generate_default_box_labelsí   s0   € á[jÓkÒ[jÈKÑ+‘°!°´s¸;Ó7GÑ1GÒGÑ[jÑkÐkùÒks   …%c           	      ó  • Uc  g[        U[        R                  5      (       ad  X2S-
  :X  d  [        UR                  5      S::  a  UR                  5       R                  5       $ U Vs/ s H  o@R                  XBUS-   5      PM     sn$ [        U[        R                  5      (       aV  X2S-
  :X  d  [        UR                  5      S::  a  UR                  5       $ U Vs/ s H  o@R                  XBUS-   5      PM     sn$ [        U[        5      (       a2  X2:X  a  U$ U Vs/ s H  nUb  U R                  XBUS-   5      OSPM     sn$ [        U[        [        45      (       a  U$ [        S[        U5       35      es  snf s  snf s  snf )af  
Recursively convert various input formats (tensors, numpy arrays, lists) to nested lists.
Preserves None values within lists.

Args:
    data: Input data in any format (may be None or contain None values)
    expected_depth: Expected nesting depth
    current_depth: Current depth in recursion

Returns:
    Nested list representation of the data (or None)
Nr2   r~   zUnsupported data type: )rb   r   rc   rˆ   ÚshapeÚnumpyre   Ú_convert_to_nested_listÚnpÚndarrayÚlistÚintr   rf   Útype)rG   ÚdatarZ   Úcurrent_depthÚitems        r    rŽ   Ú%Sam3Processor._convert_to_nested_listñ   sq  € ð ‰<Øô �dœEŸL™L×)Ñ)Ø°Ñ 2Ó2´c¸$¿*¹*³oÈÓ6JØ—z‘z“|×*Ñ*Ó,Ð,ájnÓoÒjnÐbf×4Ñ4°TÈ=Ð[\ÑK\Ö]ÑjnÑoÐoÜ˜œbŸj™j×)Ñ)Ø°Ñ 2Ó2´c¸$¿*¹*³oÈÓ6JØ—{‘{“}Ð$ájnÓoÒjnÐbf×4Ñ4°TÈ=Ð[\ÑK\Ö]ÑjnÑoÐoÜ˜œd×#Ñ#ØÓ.à�ñ
 !%óâ $˜ð ^bÑ]m�D×0Ñ0°À}ÐWXÑGXÔYÐswÒwÙ $ñð ô ˜œs¤E˜l×+Ñ+ØˆKäÐ6´t¸D³z°lÐCÓDÐDùò' pùò
 pùòs   Á'E8ÃE=Ä$Fc                 óR  • Uc  U(       a  S$ S$ [        U[        [        45      (       d  U$ [        U5      nU(       a<  [        U5      [        U5      :w  a$  [	        S[        U5       S[        U5       S35      e[        U5       H  u  p4Ub  M
  U(       d  M  X#   c  M  SX'   M      U$ )zA
Resolve text prompts by setting defaults based on prompt types.
NÚvisualzEThe number of text prompts must match the number of input boxes. Got z text prompts and z input boxes.)rb   r‘   Útuplerˆ   rf   Ú	enumerate)rG   rM   rO   ÚiÚ
text_values        r    rg   Ú#Sam3Processor._resolve_text_prompts  s«   € ð
 ‰<Þ*�8Ð4°Ð4ä˜$¤¤u ×.Ñ.ØˆKô �D‹zˆæœ3˜t›9¬¨KÓ(8Ó8ÜðÜ˜4“y�kÐ!3´C¸Ó4DÐ3EÀ]ðTóð ô ' tž_‰MˆAØÓ!§k k°k±nÓ6PØ"�“ñ -ð ˆr"   c                 óð  • Uc  / n[        U[        5      (       d  U$ [        U5      S:X  a  UR                  [        U5      5        O[	        US   [        U5      5      US'   [        U5      S:”  a…  U H  nUc  M  [        U[        5      (       d  M  U R                  U5      n[        U5       H@  u  pVUS-   [        U5      :¼  a  UR                  U5        M*  [	        X%S-      U5      X%S-   '   MB     M�     U$ )aH  
Get the maximum dimensions at each level of nesting, skipping None values.

Args:
    nested_list (`list`):
        Nested list structure (may contain None values).
    max_dims (`list`, *optional*):
        Current maximum dimensions (for recursion).

Returns:
    `list`: A list of maximum dimensions for each nesting level.
r   r~   )rb   r‘   rˆ   ÚappendÚmaxrk   r›   )rG   Únested_listÚmax_dimsr–   Úsub_dimsrœ   r   s          r    rk   Ú$Sam3Processor._get_nested_dimensions6  sß   € ð ÑØˆHä˜+¤t×,Ñ,ØˆOäˆx‹=˜AÓØ�O‰OœC Ó,Õ-ä˜h q™k¬3¨{Ó+;Ó<ˆH�Q‰Käˆ{Ó˜aÓÛ#�à‘<ÙÜ˜d¤D×)Ó)Ø#×:Ñ:¸4Ó@�Hä"+¨HÖ"5™˜Ø˜q™5¤C¨£MÓ1Ø$ŸO™O¨CÖ0ä.1°(¸q¹5±/À3Ó.G˜H¨¡U›Oó	 #6ñ $ð ˆr"   c                 ó|  • Uc  U R                   nU[        U5      :¼  a  U$ [        U[        5      (       d  U/n[        U5      nX#   nU[        U5      S-
  :X  a  UR	                  U/Xe-
  -  5        O¿US:”  am  U[        U5      S-
  :  a  X#S-   S nU R                  Xt5      nOU/X#S-      -  nUR	                  [        Xe-
  5       V	s/ s H  n	[        U5      PM     sn	5        OLX#S-   S nU R                  Xt5      nUR	                  [        U5       V	s/ s H  n	[        U5      PM     sn	5        U[        U5      S-
  :  ao  [        [        U5      5       HW  n
X   c  X#S-   S nU R                  Xt5      X'   M$  [        X   [        5      (       d  M=  U R                  X   X#S-   U5      X'   MY     U$ s  sn	f s  sn	f )aË  
Recursively pad a nested list to match target dimensions. Replaces None values with padded structures.

Args:
    nested_list (`list`):
        Nested list to pad (may contain None values).
    target_dims (`list`):
        Target dimensions for each level.
    current_level (`int`, *optional*, defaults to 0):
        Current nesting level.
    pad_value (`int`, *optional*):
        Value to use for padding.

Returns:
    `list`: The padded nested list.
Nr~   r   r2   )	r@   rˆ   rb   r‘   ÚextendÚ_create_empty_nested_structureÚranger   rl   )rG   r¢   Útarget_dimsÚcurrent_levelÚ	pad_valueÚcurrent_sizer?   Útemplate_dimsÚtemplateÚ_rœ   s              r    rl   ÚSam3Processor._pad_nested_list^  sÛ  € ð" ÑØ×,Ñ,ˆIàœC Ó,Ó,ØÐô ˜+¤t×,Ñ,Ø&˜-ˆKô ˜;Ó'ˆØ!Ñ0ˆð œC Ó,¨qÑ0Ó0à×Ñ 	˜{¨kÑ.HÑIÕJð ˜aÓà ¤3 {Ó#3°aÑ#7Ó7à$/ÀÑ0AÐ0CÐ$D�MØ#×BÑBÀ=Ó\‘Hð !*˜{¨[ÈÑ9JÑ-KÑK�Hà×"Ñ"ÄÀkÑF`Ô@aÓ#bÒ@a¸1¤H¨XÖ$6Ñ@aÑ#bÕcð !,¸AÑ,=Ð,?Ð @�Ø×>Ñ>¸}ÓX�Ø×"Ñ"ÄÀkÔ@RÓ#SÒ@R¸1¤H¨XÖ$6Ñ@RÑ#SÔTð œ3˜{Ó+¨aÑ/Ó/Üœ3˜{Ó+Ö,�Ø‘>Ñ)à$/ÀÑ0AÐ0CÐ$D�MØ%)×%HÑ%HÈÓ%b�K“NÜ ¡´×5Ó5Ø%)×%:Ñ%:¸;¹>È;ÐhiÑXiÐktÓ%u�K“Nñ -ð Ðùò# $cùò
 $Ts   ÃF4ÄF9c                 óž   • [        U5      S:X  a	  U/US   -  $ [        US   5       Vs/ s H  o0R                  USS U5      PM     sn$ s  snf )zÞ
Create an empty nested structure with given dimensions filled with pad_value.

Args:
    dims (`list`):
        The dimensions of the nested structure.
    pad_value (`int`):
        The value to fill the structure with.
r~   r   N)rˆ   r©   r¨   )rG   Údimsr¬   r°   s       r    r¨   Ú,Sam3Processor._create_empty_nested_structure   sX   € ô ˆt‹9˜‹>Ø�;  a¡Ñ(Ð(äV[Ð\`ÐabÑ\cÔVdÓeÒVdÐQR×7Ñ7¸¸Q¸R¸À)ÖLÑVdÑeÐeùÒes   ©A
c                 ó  • [        U[        5      (       a3  [        U5      S:X  a  gU H  nUc  M  SU R                  U5      -   s  $    g[        U[        R
                  [        R                  45      (       a  [        UR                  5      $ g)z�
Get the nesting level of a list structure, skipping None values.

Args:
    input_list (`list`):
        The list to get the nesting level of.
r   r~   )	rb   r‘   rˆ   Ú_get_nesting_levelr�   r�   r   rc   rŒ   )rG   Ú
input_listr–   s      r    r¶   Ú Sam3Processor._get_nesting_level¯  sy   € ô �j¤$×'Ñ'Ü�:‹ !Ó#Øã"�ØÓ#Ø˜t×6Ñ6°tÓ<Ñ<Ò<ñ #ð Ü˜
¤R§Z¡Z´·±Ð$>×?Ñ?ä�z×'Ñ'Ó(Ð(Ør"   r”   rZ   r[   r\   r]   c                 ó  • Uc  g[        U[        R                  [        R                  45      (       a{  UR
                  U:w  a"  [        SU SU SU SUR
                   S3	5      eUb5  UR                  S   U:w  a"  [        SU SU SUR                  S    S	35      eU R                  X5      $ [        U[        5      (       a?  U R                  U5      nXb:w  a  [        SU S
U SU SU S3	5      eU R                  X5      $ g)aW  
        Validate a single input by ensuring proper nesting and raising an error if the input is not valid.

        Args:
            data (`torch.Tensor`, `np.ndarray`, or `list`):
                Input data to process.
            expected_depth (`int`):
                Expected nesting depth.
            input_name (`str`):
                Name of the input for error messages.
            expected_format (`str`):
                The expected format of the input.
            expected_coord_size (`int`, *optional*):
                Expected coordinate size (4 for boxes, None for labels).
.
NzInput z must be a tensor/array with z, dimensions. The expected nesting format is z. Got z dimensions.r   z as the last dimension, got Ú.z must be a nested list with z( levels. The expected nesting format is z levels.)rb   r   rc   r�   r�   Úndimrf   rŒ   rŽ   r‘   r¶   )rG   r”   rZ   r[   r\   r]   r•   s          r    ri   Ú$Sam3Processor._validate_single_inputÅ  s   € ð0 ‰<Øô �dœUŸ\™\¬2¯:©:Ð6×7Ñ7à�y‰y˜NÓ*Ü Ø˜Z˜LÐ(EÀnÐEUð  VBð  CRð  BSð  SYð  Z^÷  Zcñ  Zcð  Ydð  dpð  qóð ð %Ñ0Ø—:‘:˜b‘>Ð%8Ó8Ü$Ø   Ð,IÐJ]ÐI^Ð^zÐ{÷  |Fñ  |Fð  GIñ  |Jð  {Kð  KLð  Móð ð ×/Ñ/°ÓEÐEô �dœD×!Ñ!Ø ×3Ñ3°DÓ9ˆMØÓ.Ü Ø˜Z˜LÐ(DÀ^ÐDTÐT|ð  ~Mð  }Nð  NTð  Ubð  Tcð  ckð  lóð ð ×/Ñ/°ÓEÐEð "r"   c                 ó~  • U(       a  XR                   :g  nUR                  SSS9n[        [        U5      5       H€  nXqR                  S   :  d  M  U[        U5      :  a  X'   OUS   nU R                  X   XƒS9n	U(       a4  WU   n
[        R                  " U
R                  X   5      X‘U   5      X'   M|  X‘U'   M‚     g)a¹  
Helper method to normalize coordinates in a tensor across multiple images.

Args:
    tensor (`torch.Tensor`):
        Input tensor with coordinates.
    original_sizes (`list`):
        Original image sizes.
    is_bounding_box (`bool`, *optional*, defaults to `False`):
        Whether coordinates are bounding boxes.
    preserve_padding (`bool`, *optional*, defaults to `False`):
        Whether to preserve padding values (for boxes).
r   T)r   Úkeepdimr   )r`   N)	r@   Úallr©   rˆ   rŒ   r…   r   ÚwhereÚ	expand_as)rG   rm   rQ   r`   ra   ÚmaskÚ
coord_maskÚimg_idxr‚   Únormalized_coordsÚimg_masks              r    ro   Ú+Sam3Processor._normalize_tensor_coordinates÷  sÅ   € ö à×1Ñ1Ñ1ˆDØŸ™ b°$˜Ð7ˆJäœS Ó0Ö1ˆGØŸ™ a™Õ(Ø;BÄSÈÓEXÓ;X Ò 7Ð^lÐmnÑ^o�Ø$(×$?Ñ$?Ø‘O ]ð %@ð %Ð!ö $à)¨'Ñ2�HÜ&+§k¢kØ ×*Ñ*¨6©?Ó;Ð=NÐW^ÑP_ó'�F“Oð '8˜7“Oò 2r"   c                 ó:   • U R                   R                  UUUUS9$ )a¶  
Converts the output of [`Sam3Model`] into semantic segmentation maps.

Args:
    outputs ([`Sam3ImageSegmentationOutput`]):
        Raw outputs of the model containing semantic_seg.
    target_sizes (`list[tuple]` of length `batch_size`, *optional*):
        List of tuples corresponding to the requested final size (height, width) of each prediction. If unset,
        predictions will not be resized.
    threshold (`float`, *optional*, defaults to 0.5):
        Threshold for binarizing the semantic segmentation masks.
    return_segmentation_scores (`bool`, *optional*, defaults to `False`):
        Whether to return segmentation scores alongside the segmentation map.

Returns:
    semantic_segmentation: `list[torch.Tensor]` of length `batch_size`, where each item is a semantic
    segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is
    specified). Each entry is a binary mask (0 or 1).
)Útarget_sizesÚ	thresholdÚreturn_segmentation_scores)rE   Ú"post_process_semantic_segmentation)rG   ÚoutputsrÉ   rÊ   rË   s        r    rÌ   Ú0Sam3Processor.post_process_semantic_segmentation  s/   € ð, ×#Ñ#×FÑFØØ%ØØ'Að	 Gð 
ð 	
r"   c                 ó:   • U R                   R                  XU5      $ )a  
Converts the raw output of [`Sam3Model`] into final bounding boxes in (top_left_x, top_left_y,
bottom_right_x, bottom_right_y) format. This is a convenience wrapper around the image processor method.

Args:
    outputs ([`Sam3ImageSegmentationOutput`]):
        Raw outputs of the model containing pred_boxes, pred_logits, and optionally presence_logits.
    threshold (`float`, *optional*, defaults to 0.3):
        Score threshold to keep object detection predictions.
    target_sizes (`list[tuple[int, int]]`, *optional*):
        List of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the
        batch. If unset, predictions will not be resized.

Returns:
    `list[dict]`: A list of dictionaries, each dictionary containing the following keys:
        - **scores** (`torch.Tensor`): The confidence scores for each predicted box on the image.
        - **boxes** (`torch.Tensor`): Image bounding boxes in (top_left_x, top_left_y, bottom_right_x,
          bottom_right_y) format.

Example:

```python
>>> from transformers import AutoModel, AutoProcessor
>>> from PIL import Image
>>> import httpx
>>> from io import BytesIO

>>> model = AutoModel.from_pretrained("facebook/sam3-base")
>>> processor = AutoProcessor.from_pretrained("facebook/sam3-base")

>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> with httpx.stream("GET", url) as response:
...     image = Image.open(BytesIO(response.read()))
>>> inputs = processor(images=image, text="cat", return_tensors="pt")
>>> outputs = model(**inputs)

>>> # Post-process to get bounding boxes
>>> results = processor.post_process_object_detection(outputs, threshold=0.3, target_sizes=[image.size[::-1]])
>>> boxes = results[0]["boxes"]
>>> scores = results[0]["scores"]
```
)rE   Úpost_process_object_detection)rG   rÍ   rÊ   rÉ   s       r    rÐ   Ú+Sam3Processor.post_process_object_detection7  s   € ðV ×#Ñ#×AÑAÀ'ÐVbÓcÐcr"   c                 ó:   • U R                   R                  XX45      $ )a)  
Converts the raw output of [`Sam3Model`] into instance segmentation predictions with bounding boxes and masks.
This is a convenience wrapper around the image processor method.

Args:
    outputs ([`Sam3ImageSegmentationOutput`]):
        Raw outputs of the model containing pred_boxes, pred_logits, pred_masks, and optionally
        presence_logits.
    threshold (`float`, *optional*, defaults to 0.3):
        Score threshold to keep instance predictions.
    mask_threshold (`float`, *optional*, defaults to 0.5):
        Threshold for binarizing the predicted masks.
    target_sizes (`list[tuple[int, int]]`, *optional*):
        List of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the
        batch. If unset, predictions will not be resized.

Returns:
    `list[dict]`: A list of dictionaries, each dictionary containing the following keys:
        - **scores** (`torch.Tensor`): The confidence scores for each predicted instance on the image.
        - **boxes** (`torch.Tensor`): Image bounding boxes in (top_left_x, top_left_y, bottom_right_x,
          bottom_right_y) format.
        - **masks** (`torch.Tensor`): Binary segmentation masks for each instance, shape (num_instances,
          height, width).

Example:

```python
>>> from transformers import AutoModel, AutoProcessor
>>> from PIL import Image
>>> import httpx
>>> from io import BytesIO

>>> model = AutoModel.from_pretrained("facebook/sam3-base")
>>> processor = AutoProcessor.from_pretrained("facebook/sam3-base")

>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> with httpx.stream("GET", url) as response:
...     image = Image.open(BytesIO(response.read()))
>>> inputs = processor(images=image, text="cat", return_tensors="pt")
>>> outputs = model(**inputs)

>>> # Post-process to get instance segmentation
>>> results = processor.post_process_instance_segmentation(
...     outputs, threshold=0.3, target_sizes=[image.size[::-1]]
... )
>>> masks = results[0]["masks"]
>>> boxes = results[0]["boxes"]
>>> scores = results[0]["scores"]
```
)rE   Ú"post_process_instance_segmentation)rG   rÍ   rÊ   Úmask_thresholdrÉ   s        r    rÓ   Ú0Sam3Processor.post_process_instance_segmentationd  s#   € ðr ×#Ñ#×FÑFØ ó
ð 	
r"   )r@   r?   )Niöÿÿÿ)NNNNNNN)F)r�   útorch.TensorrS   rÖ   )r   )N)r   N)FF)Nr   F)ç333333Ó?N)r×   r   N)#Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r’   rD   r   r   r	   r   r‘   r   r   rc   Ústrr
   r   r{   r…   rj   rŽ   rg   rk   rl   r¨   r¶   r�   r�   ri   ro   rÌ   rÐ   rÓ   Ú__static_attributes__Ú__classcell__)rJ   s   @r    r=   r=   U   sñ  ø† ð beñkØ7:¸T±zðkØ[^÷kð kð ð %)ØaeØ/3ØEIØJNØBFØ26ñjà˜TÑ!ðjð Ð+Ñ+¨d°9©oÑ=ÀÐEVÑ@WÑWÐZ^Ñ^ðjð &¨Ñ,ð	jð
 ˜$˜t E™{Ñ+Ñ,¨u¯|©|Ñ;¸dÑBðjð !  d¨3¡i¡Ñ1°E·L±LÑ@À4ÑGðjð ˜T %™[Ñ)¨E¯L©LÑ8¸4Ñ?ðjð ˜jÑ(¨4Ñ/ðjð 
ôjó ðjöXò6lô(EòTô6&ôP@òDfòð8 +/ñ0Fà�l‰l˜RŸZ™ZÑ'¨$Ñ.ð0Fð ð0Fð ð	0Fð
 ð0Fð ! 4™Zð0Fð 
õ0Fôd!8ðH UZô
ô:+dð` ØØ÷;
ò ;
r"   r=   )!Ú__doc__Úcopyr   r�   r�   Úimage_utilsr   Úprocessing_utilsr   Útokenization_utils_baser   r   r	   Úutilsr
   r   r   r   Úutils.import_utilsr   Ú
get_loggerrØ   Úloggerr   r!   r$   r)   r,   r0   r7   r:   r=   Ú__all__© r"   r    Ú<module>rê      sž   ðñõ ã å %Ý .ß RÑ Rß LÓ LÝ *ð 
×	Ò	˜HÓ	%€á×ÑÛò"ò"ò"ò"ò"ò"ò!ñ 
�:ÑØôH	
�Nó H	
ó ó ðH	
ðV Ð
�r"   