
    cCi.                         S r SSKJr  SSKJrJr  SSKrSSKJ	r	  SSK
JrJrJrJr  SSKJrJrJrJr  SS	KJr  SS
KJ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 SAMHQ.
    )deepcopy)OptionalUnionN   )
ImageInput)ImagesKwargsProcessingKwargsProcessorMixinUnpack)
AudioInputBatchEncodingPreTokenizedInput	TextInput)is_torch_available)
VideoInputc                       \ rS rSr% \\   \S'   \\\\         \S'   \\\\	         \S'   \\\\\            \S'   \\	   \S'   Sr
g)	SamHQImagesKwargs#   segmentation_mapsinput_pointsinput_labelsinput_boxespoint_pad_value N)__name__
__module____qualname____firstlineno__r   r   __annotations__listfloatint__static_attributes__r       e/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/sam_hq/processing_samhq.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)SamHQProcessorKwargs+   images_kwargsr   Nr   )r   r   r   r   r   r   	_defaultsr#   r   r$   r%   r'   r'   +   s    $$t
Ir$   r'   F)totalc                   :  ^  \ rS rSrSrS/rSrU 4S jr    SS\\	   S\\
\\\\   \\   4      S\\   S	\\   S
\\   S\4S jjr     SS jrS r SS\S\R0                  S\R0                  4S jjrSS jr   SS jr\S 5       rS rS rSS jr Sr!U =r"$ )SamHQProcessor4   a  
Constructs a SAM HQ processor which wraps a SAM  image processor and an 2D points & Bounding boxes processor into a
single processor.

[`SamHQProcessor`] 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SamImageProcessorc                    > [         TU ]  U5        [        U S5      (       d  [        S5      e[        U R                  S5      (       d  [        S5      eU R                  R
                  S   U l        g )Nr/   z,image_processor was not properly initializedsizezimage_processor.size is not setlongest_edge)super__init__hasattr
ValueErrorr/   r2   target_size)selfr/   	__class__s     r%   r5   SamHQProcessor.__init__D   sb    )t.//KLLt++V44>??//44^Dr$   imagestextaudiovideokwargsreturnc                    U R                   " [        4S0 0U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 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  pxn	U R                  U
UUUU	US
   R                  S5      US   R                  S5      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   original_sizesnumpy)r   r   r   common_kwargsreturn_tensorsr   )r   r   r   rG   r   )	_merge_kwargsr'   popr/   r6   rE   _check_and_preprocess_points_normalize_and_convertget)r9   r<   r=   r>   r?   r@   output_kwargsr   r   r   encoding_image_processorrD   s               r%   __call__SamHQProcessor.__call__M   s4    ** 
"$
 
 %_599.$O$_599.$O#O488M#'#7#7$
O,$
 
 22BC>7+++113N262S2S%%# 3T 3
/K $(#>#>$%%#(9==>NO)/:>>?PQ $? $
  ('r$   c                   ^ TbZ  U R                  TU5      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 R                  XRSS9n[        R                  " U5      nUb  U R                  USU5      US'   Tb  U R                  TSU5      US'   Ub  U R                  USU5      US'   U$ )	zJ
Normalize and convert the image processor output to the expected format.
c              3   Z   >#    U  H   oR                   TS    R                   :H  v   M"     g7f)r   N)shape).0pointr   s     r%   	<genexpr>8SamHQProcessor._normalize_and_convert.<locals>.<genexpr>   s"     V{{l1o&;&;;s   (+Tis_bounding_boxr   r      r   r   )_normalize_batch_coordinatesall_pad_points_and_labelsnparray
_to_tensor)r9   rN   rD   r   r   r   rG   r   s      `    r%   rK   %SamHQProcessor._normalize_and_convert   s    #<<\>ZLVVVV+151L1L$l2.L, 88L1L #88L1L ";;Kim;nK((;/K "6:ookSTVd6e$]3#7;|UVXf7g$^4#7;|UVXf7g$^4''r$   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)maxrS   	enumerater^   concatenatezerosappend)r9   r   r   r   rU   expected_nb_pointsprocessed_input_pointsis           r%   r]   %SamHQProcessor._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r8   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$ )zm
Expects a numpy array of length 2 in the final dimension. Requires the original image size in (H,W) format.
)r3   rc   ).r   ).   rZ   )r/   _get_preprocess_shaper   astyper!   reshape)	r9   r8   rn   original_sizerY   old_hold_wnew_hnew_ws	            r%   _normalize_coordinates%SamHQProcessor._normalize_coordinates   s     %++AA-Aj&!((/^^B1-F5=9v5=9v^^B*Fr$   c                    Uc  g[        US5      (       a  UR                  5       R                  5       n[        U[        5      nUn[        U5       H=  nU(       a  U(       d    O.U=(       a    [        US   [        5      nU(       a  US   OSnM?     U(       d  [        U5      eU Vs/ s H  n[        R                  " XS9PM     sn$ s  snf )a|  
Preprocess input by converting torch tensors to numpy arrays and validating structure.

Args:
    inputs: The input to process
    error_message: Error message if validation fails
    expected_nesting: Expected nesting level (1 for points/labels, 2 for boxes)
    dtype: Optional data type for numpy array conversion

Returns:
    Processed input as list of numpy arrays or None
NrE   r   )dtype)	r6   rE   tolist
isinstancer    ranger7   r^   r_   )	r9   inputserror_messageexpected_nestingr}   validcurrent_items	            r%   _preprocess_input SamHQProcessor._preprocess_input   s     > 67##\\^**,F 64('(A:jT:E$+gajG	 ) ]++ 9??+???s   (C	c                     U R                  US5      nU R                  US5      nU R                  USS[        R                  S9nXU4$ )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`.
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.rc   )r   r}   )r   r^   float32)r9   r   r   r   s       r%   rJ   +SamHQProcessor._check_and_preprocess_points   s\     --l<uv--l<kl,,L**	 - 
 ;66r$   c                 N    U R                   R                  n[        USS/-   5      $ )NrD   reshaped_input_sizes)r/   model_input_namesr    )r9   image_processor_input_namess     r%   r    SamHQProcessor.model_input_names  s,    &*&:&:&L&L#/3CE[2\\]]r$   c                 :    U R                   R                  " U0 UD6$ )N)r/   post_process_masks)r9   argsr@   s      r%   r   !SamHQProcessor.post_process_masks  s    ##66GGGr$   c                     US:X  a9  [         R                  " U5      nUR                  U:  a  UR                  S5      $ U$ U$ )aG  
Convert numpy array to tensor and ensure proper dimensionality.
Args:
    array: The numpy array to convert
    min_dim: The minimum number of dimensions the result should have
    return_tensors: The type of tensors to return (e.g., "pt" for PyTorch tensors)
Returns:
    The converted array or tensor with proper dimensions
ptrq   )torch
from_numpyndim	unsqueeze)r9   r_   min_dimrG   s       r%   r`   SamHQProcessor._to_tensor  s@     T!$$U+E).g)=5??1%H5Hr$   c                    [        U5      [        U5      :w  a/  U Vs/ s H!  nU R                  U R                  XBS   US9PM#     sn$ [        X5       VVs/ s H   u  pEU R                  U R                  XEUS9PM"     snn$ s  snf s  snnf )z
Normalize coordinates based on original sizes.
Args:
    inputs: List of coordinate arrays
    original_sizes: Original sizes of the images
    is_bounding_box: Whether inputs are bounding boxes
Returns:
    Normalized coordinates as list
r   rX   )lenrz   r8   zip)r9   r   rD   rY   r   r2   s         r%   r[   +SamHQProcessor._normalize_batch_coordinates$  s     ~#f+- #"D ++D,<,<dSTDUgv+w"  #&f"="=JD ++D,<,<dZi+j"= s   (B'B)r8   )NNNN)NNNr   i)F)rq   N)NNN)#r   r   r   r   __doc__
attributesimage_processor_classr5   r   r   r   r   r   r    r   r   r   r'   r   rO   rK   r]   r"   r^   ndarrayrz   r   rJ   propertyr   r   r`   r[   r#   __classcell__)r:   s   @r%   r-   r-   4   s)   
 $$J/E (,hl&*&*0($0( uY(94	?DQbLccde0( 
#	0(
 
#0( -.0( 
0(l *(X*" TY(*

	*"@L 	72 ^ ^H r$   r-   )r   copyr   typingr   r   rE   r^   image_utilsr   processing_utilsr   r	   r
   r   tokenization_utils_baser   r   r   r   utilsr   video_utilsr   r   r   r'   r-   __all__r   r$   r%   <module>r      sl     "  % V V ^ ^ ' % # #+5 E^ EP 
r$   