
    fTh2                     
   S r SSKJr  SSKJrJrJr  SSK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Jr  SS
KJr  \" 5       (       a  SSKr\" 5       (       a  SSKr " S S\5      r " S S\SS9r " S S\5      rS/rg)z
Processor class for SAM.
    )deepcopy)ListOptionalUnionN   )
ImageInput)ImagesKwargsProcessingKwargsProcessorMixin)
AudioInputBatchEncodingPreTokenizedInput	TextInput)is_tf_availableis_torch_available)
VideoInputc                       \ rS rSr% \\   \S'   \\\\         \S'   \\\\	         \S'   \\\\\            \S'   \\	   \S'   Sr
g)	SamImagesKwargs&   segmentation_mapsinput_pointsinput_labelsinput_boxespoint_pad_value N)__name__
__module____qualname____firstlineno__r   r   __annotations__r   floatint__static_attributes__r       ^/var/www/auris/envauris/lib/python3.13/site-packages/transformers/models/sam/processing_sam.pyr   r   &   sW    
++4U,--4S	?++$tDK0122c]"r$   r   c                   ,    \ rS rSr% \\S'   SSS00rSrg)SamProcessorKwargs.   images_kwargsr   r   N)r   r   r   r   r   r    	_defaultsr#   r   r$   r%   r'   r'   .   s    ""s
Ir$   r'   F)totalc                   $  ^  \ rS rSrSrS/rSr/ SQrU 4S jr SSSSS.S	\	\
   S
\	\\\\\   \\   4      S\	\   S\	\   S\4
S jjjr     SS jrS r SS\S\R.                  S\R.                  4S jjr   SS jr\S 5       rS rSrU =r$ )SamProcessor7   a  
Constructs a SAM processor which wraps a SAM image processor and an 2D points & Bounding boxes processor into a
single processor.

[`SamProcessor`] offers all the functionalities of [`SamImageProcessor`]. See the docstring of
[`~SamImageProcessor.__call__`] for more information.

Args:
    image_processor (`SamImageProcessor`):
        An instance of [`SamImageProcessor`]. The image processor is a required input.
image_processorSamImageProcessor)r   r   r   r   c                 `   > [         TU ]  U5        U R                  R                  S   U l        g )Nlongest_edge)super__init__r0   sizetarget_size)selfr0   	__class__s     r%   r5   SamProcessor.__init__N   s)    )//44^Dr$   N)textaudiovideoimagesr;   r<   r=   returnc          
         U R                   " [        4S0 0UDU R                  " U6 D6nUS   R                  SS5      nUS   R                  SS5      n	US   R                  SS5      n
US   R                  SS5      nU R                  " U40 US   D6nUS   n[        US	5      (       a  UR                  5       nU R                  UU	U
S
9u  pn
U R                  UUUU	U
US   R                  S5      US9nU$ )z
This method uses [`SamImageProcessor.__call__`] method to prepare image(s) for the model. It also prepares 2D
points and bounding boxes for the model if they are provided.
tokenizer_init_kwargsr)   r   Nr   r   r   original_sizesnumpy)r   r   r   common_kwargsreturn_tensors)r   r   r   rE   r   )
_merge_kwargsr'   'prepare_and_validate_optional_call_argspopr0   hasattrrC   _check_and_preprocess_points_normalize_and_convertget)r8   r>   r;   r<   r=   argskwargsoutput_kwargsr   r   r   r   encoding_image_processorrB   s                 r%   __call__SamProcessor.__call__R   sQ   & **
"$
 
 ::DA	
 %_599.$O$_599.$O#O488M'8<<=NPTU#'#7#7$
O,$
  22BC>7+++113N262S2S%%# 3T 3
/K $(#>#>$%%#(9==>NO+ $? $
  ('r$   c                 @  ^ Tb  [        U5      [        T5      :w  a0  T Vs/ s H!  oR                  U R                  XS   5      PM#     snmO;[        TU5       VV	s/ s H!  u  pU R                  U R                  X5      PM#     sn	nm[	        U4S jT 5       5      (       d  Ub  U R                  TXG5      u  mn[        R                  " T5      mUb  [        R                  " U5      nUb  [        U5      [        U5      :w  a/  U V
s/ s H!  n
U R                  U R                  XS   SS9PM#     nn
O8[        XR5       V
V	s/ s H   u  pU R                  U R                  XSS9PM"     nn
n	[        R                  " U5      nUb  US:X  aC  [        R                  " U5      n[        UR                  5      S:w  a  UR                  S5      OUnONUS:X  aH  [        R                  " U5      n[        UR                  5      S:w  a  [        R                  " US5      OUnUR                  S	U05        Tb  US:X  aC  [        R                  " T5      m[        TR                  5      S
:w  a  TR                  S5      OTmONUS:X  aH  [        R                  " T5      m[        TR                  5      S
:w  a  [        R                  " TS5      OTmUR                  ST05        Ub  US:X  aC  [        R                  " U5      n[        UR                  5      S:w  a  UR                  S5      OUnONUS:X  aH  [        R                  " U5      n[        UR                  5      S:w  a  [        R                  " US5      OUnUR                  SU05        U$ s  snf s  sn	nf s  sn
f s  sn	n
f )Nr   c              3   Z   >#    U  H   oR                   TS    R                   :H  v   M"     g7f)r   N)shape).0pointr   s     r%   	<genexpr>6SamProcessor._normalize_and_convert.<locals>.<genexpr>   s"     V{{l1o&;&;;s   (+T)is_bounding_boxptr      tfr      r   r   )len_normalize_coordinatesr7   zipall_pad_points_and_labelsnparraytorch
from_numpyrU   	unsqueezer]   convert_to_tensorexpand_dimsupdate)r8   rP   rB   r   r   r   rE   r   rW   original_sizeboxs      `       r%   rK   #SamProcessor._normalize_and_convert   sj    #>"c,&77iu iu`e//0@0@%XYIZ[iu  14L.0Q 0Q, //0@0@%W0Q 
 VVVV+151L1L$l2.L, 88L1L#88L1L">"c+&66  +* //0@0@#VWGXjn/o*   /2+.N.N* //0@0@#fj/k.N   ((;/K"%#..{;:=k>O>O:PTU:Uk33A6[f4' 22;?@CKDUDU@VZ[@[bnn[!<al$++]K,HI#%$//=<?@R@R<SWX<X|55a8^j4'!33LABElFXFXBY]^B^r~~lA>dp$++^\,JK#%$//=<?@R@R<SWX<X|55a8^j4'!33LABElFXFXBY]^B^r~~lA>dp$++^\,JK''A  $
s   (N
(N(N'Nc           	         [        U Vs/ s H  oDR                  S   PM     sn5      n/ n[        U5       H  u  ptUR                  S   U:w  a[  [        R                  " U[        R
                  " XTR                  S   -
  S45      U-   /SS9n[        R                  " X'   U/5      X''   UR                  U5        M     UnX4$ s  snf )zX
The method pads the 2D points and labels to the maximum number of points in the batch.
r      )axis)maxrU   	enumeraterd   concatenatezerosappend)r8   r   r   r   rW   expected_nb_pointsprocessed_input_pointsis           r%   rc   #SamProcessor._pad_points_and_labels   s     !l!KlU++a.l!KL!#!,/HA{{1~!33BHH&8;;q>&I1%MNQ``ahi #%))LOo=N"O"))%0 0 .)) "Ls   Cr7   coordsc                    Uu  pVU R                   R                  X1S9u  px[        U5      R                  [        5      nU(       a  UR                  SSS5      nUS   X-  -  US'   US   Xu-  -  US'   U(       a  UR                  SS5      nU$ )zn
Expects a numpy array of length 2 in the final dimension. Requires the original image size in (H, W) format.
)r3   rp   ).r   ).r\   r^   )r0   _get_preprocess_shaper   astyper!   reshape)	r8   r7   r{   rl   rZ   old_hold_wnew_hnew_ws	            r%   r`   #SamProcessor._normalize_coordinates   s     %++AA-Aj&!((/^^B1-F5=9v5=9v^^B*Fr$   c                 
   Ub  [        US5      (       a  UR                  5       R                  5       n[        U[        5      (       a  [        US   [        5      (       d  [        S5      eU Vs/ s H  n[        R                  " U5      PM     nnOSnUb  [        US5      (       a  UR                  5       R                  5       n[        U[        5      (       a  [        US   [        5      (       d  [        S5      eU Vs/ s H  n[        R                  " U5      PM     nnOSnUb  [        US5      (       a  UR                  5       R                  5       n[        U[        5      (       a3  [        US   [        5      (       a  [        US   S   [        5      (       d  [        S5      eU Vs/ s H6  n[        R                  " U5      R                  [        R                  5      PM8     nnOSnXU4$ s  snf s  snf s  snf )a  
Check and preprocesses the 2D points, labels and bounding boxes. It checks if the input is valid and if they
are, it converts the coordinates of the points and bounding boxes. If a user passes directly a `torch.Tensor`,
it is converted to a `numpy.ndarray` and then to a `list`.
NrC   r   z7Input points must be a list of list of floating points.z-Input labels must be a list of list integers.z>Input boxes must be a list of list of list of floating points.)
rI   rC   tolist
isinstancelist
ValueErrorrd   re   r   float32)r8   r   r   r   input_pointlabelrm   s          r%   rJ   )SamProcessor._check_and_preprocess_points   s    #|W--+113::<lD11LQROUY9Z9Z !Z[[EQR\kBHH[1\LRLL#|W--+113::<lD11LQROUY9Z9Z !PQQ9EFBHHUOLFLL"{G,,)//188: {D11!+a.$77!+a."3T:: !abbGRS{288C=//

;{KSKK;669 S G Ts   / G6 G;0=H c                 j    U R                   R                  n[        [        R	                  U5      5      $ N)r0   model_input_namesr   dictfromkeys)r8   image_processor_input_namess     r%   r   SamProcessor.model_input_names/  s)    &*&:&:&L&L#DMM"=>??r$   c                 :    U R                   R                  " U0 UD6$ r   )r0   post_process_masks)r8   rM   rN   s      r%   r   SamProcessor.post_process_masks4  s    ##66GGGr$   )r7   r   )NNNr[   r*   )F)NNN)r   r   r   r   __doc__
attributesimage_processor_classoptional_call_argsr5   r   r   r   r   r   r   r   r   r   rQ   rK   rc   r"   rd   ndarrayr`   rJ   propertyr   r   r#   __classcell__)r9   s   @r%   r.   r.   7   s   
 $$J/E (,9( im&*&*9($9( uY(94	?DQbLccde9( 
#9( 
#9( 
9(~ L(\*" TY(*

	. 	-7^ @ @H Hr$   r.   ) r   copyr   typingr   r   r   rC   rd   image_utilsr   processing_utilsr	   r
   r   tokenization_utils_baser   r   r   r   utilsr   r   video_utilsr   rf   
tensorflowr]   r   r'   r.   __all__r   r$   r%   <module>r      s}     ( (  % N N ^ ^ 8 % #l #) ~H> ~HB 
r$   