+
    Ci0                         ^ RI t ^ RIHt ^ RIHt ^ RIt^ RIt^ RI	t	^ RI
Ht ^ RIHt ^ RIHt ^ RIHt ^RIHt R R	 ltR
 R ltR R ltR tR t ! R R]4      tR# )    N)deepcopy)List)validate)Image)map_coordinates)BaseSessionc                    V ^8  d   QhR\         P                  R\         P                  R\        R\         P                  /# )   imagematrixoutput_shapereturn)npndarraytuple)formats   "P/var/www/html/photoedit/myenv/lib/python3.14/site-packages/rembg/sessions/sam.py__annotate__r      s8     2& 2&::2&!zz2&9>2&ZZ2&    c           	        Vw  r4\         P                  ! V. RO.4      p\         P                  P                  V4      R,          p\         P                  ! V4      p\         P                  ! V4      p\         P
                  ! Wx4      w  rVR,          V	,          VR,          V
,          ,           VR	,          ,           pVR
,          V	,          VR,          V
,          ,           VR,          ,           pV P                  ^8X  d0   \        V P                  \         P                  4      W.^R^ R7      pM\         P                  ! W4V P                  ^,          3\         P                  R7      p\        V P                  ^,          4       FA  p\        V RRV3,          P                  \         P                  4      W.^R^ R7      VRRV3&   KC  	  VP                  V P                  4      # )a  
Apply affine transformation to an image (matching cv2.warpAffine behavior).

cv2.warpAffine maps source coordinates to destination coordinates:
    dst(M @ [x, y, 1]^T) = src(x, y)

So to fill dst(x', y'), we compute the inverse:
    src_coords = M^(-1) @ [x', y', 1]^T

Args:
    image: Input image (H, W) or (H, W, C)
    matrix: 2x3 affine transformation matrix
    output_shape: (height, width) of output

Returns:
    Transformed image
Nr
   Nconstant)ordermodecvaldtypeNNNr   r      )r   r   )r   r    )r   r
   )r    r   )r    r    )r    r
   )r   vstacklinalginvarangemeshgridndimr   astypefloat64zerosshaperanger   )r   r   r   hwM_fullM_invcolsrowsx_coordsy_coordssrc_xsrc_yresultcs   &&&            r   warp_affiner8      sm   ( DA YY	*+FIIMM&!"%E 99Q<D99Q<DT0H $K("U4[8%;;eDkIE$K("U4[8%;;eDkIEzzQ LL$unAJUV
 1Q0

Cu{{1~&A-aAg%%bjj1F1a7O ' ==%%r   c                <    V ^8  d   QhR\         R\         R\         /# )r
   oldholdwlong_side_length)int)r   s   "r   r   r   E   s!      s #  r   c                     VR ,          \        W4      ,          pW,          W,          rT\        VR,           4      p\        VR,           4      pWE3# )g      ?g      ?)maxr=   )r:   r;   r<   scalenewhnewws   &&&   r   get_preprocess_shaperC   E   sB    s"S_4Et|$tcz?Dtcz?D<r   c                8    V ^8  d   QhR\         P                  /# )r
   coords)r   r   )r   s   "r   r   r   N   s     
 
 
r   c                     Vw  r4\        V^ ,          V^,          V4      w  rV\        V 4      P                  \        4      p V R,          Wd,          ,          V R&   V R,          WS,          ,          V R&   V # )r   ).r   ).r    )rC   r   r'   float)rE   original_sizetarget_lengthold_hold_wnew_hnew_ws   &&&    r   apply_coordsrN   N   sj     LE'a-*MLE f$$U+FF^u}5F6NF^u}5F6NMr   c                    . p. pV  F  pVR ,          R8X  d3   VP                  VR,          4       VP                  VR,          4       KC  VR ,          R8X  g   KS  VP                  VR,          ^ ,          VR,          ^,          .4       VP                  VR,          ^,          VR,          ^,          .4       VP                  ^4       VP                  ^4       K  	  \        P                  ! V4      \        P                  ! V4      r!W3# )typepointdatalabel	rectangle)appendr   array)promptpointslabelsmarks   &   r   get_input_pointsr[   [   s    FF<7"MM$v,'MM$w-(&\[(MM4<?DLO<=MM4<?DLO<=MM!MM!  XXf%rxx'7F>r   c           	      b   . p\        V P                  ^ ,          4       Fx  p. p\        V P                  ^,          4       FB  pWV3,          p\        VVR,          V^ ,          V^,          34      pVP                  V4       KD  	  VP                  V4       Kz  	  \        P
                  ! V4      # )r   r   )r+   r*   r8   rU   r   rV   )masksrH   transform_matrixoutput_masksbatchbatch_masksmask_idmasks   &&&     r   transform_masksrd   m   s    Lu{{1~&U[[^,G(D $q!=#34D
 t$ - 	K( ' 88L!!r   c                   h   a  ] tR t^t o RtV 3R lR ltV 3R lR lt]R 4       t]R 4       t	Rt
V tR	# )

SamSessionz
This class represents a session for the Sam model.

Args:
    model_name (str): The name of the model.
    sess_opts (ort.SessionOptions): The session options.
    *args: Variable length argument list.
    **kwargs: Arbitrary keyword arguments.
c                :   < V ^8  d   QhRS[ RS[P                  /# )r
   
model_name	sess_opts)strortSessionOptions)r   __classdict__s   "r   r   SamSession.__annotate__   s%     
 

 %%
r   c                    Wn         V P                  P                  ! V/ VB p\        P                  ! \        V^ ,          4      VR7      V n        \        P                  ! \        V^,          4      VR7      V n        R# )a  
Initialize a new SamSession with the given model name and session options.

Args:
    model_name (str): The name of the model.
    sess_opts (ort.SessionOptions): The session options.
    *args: Variable length argument list.
    **kwargs: Arbitrary keyword arguments.
)sess_optionsN)rh   	__class__download_modelsrk   InferenceSessionrj   encoderdecoder)selfrh   ri   argskwargspathss   &&&*, r   __init__SamSession.__init__   sd      %..??++aM"
 ++aM"
r   c                6   < V ^8  d   QhRS[ RS[S[ ,          /# )r
   imgr   )PILImager   )r   rm   s   "r   r   rn      s'     B4 B4B4
 
hB4r   c                   VP                  RRRR^R\        VP                  ^,          4      \        VP                  ^,          4      ./.4      pRRRRRR	RRR
/RRR/RRRRRR/////p\	        WER7       RpRpV P
                  P                  4       ^ ,          P                  pVP                  R4      p\        P                  ! V4      p	V	P                  R,          p
V^,          V	P                  ^,          ,          pV^ ,          V	P                  ^ ,          ,          p\        W4      p\        P                  ! V^ ^ .^ V^ .. R O.4      p\        V	VR,          V^ ,          V^,          34      p	WP                  \        P                  4      /pV P
                  P!                  RV4      pV^ ,          pRVRV
RV/p\#        V4      w  pp\        P$                  ! V\        P                  ! RR..4      .^ R7      R!,          p\        P$                  ! V\        P                  ! R".4      .^ R7      R#,          P                  \        P                  4      p\'        VWv4      P                  \        P                  4      p\        P$                  ! V\        P(                  ! ^VP                  ^,          ^3\        P                  R7      .^R7      p\        P*                  ! VVP,                  4      pVR$,          P                  \        P                  4      p\        P.                  ! R%\        P                  R7      p\        P.                  ! ^\        P                  R7      pRVRVRVRVRVR\        P                  ! V\        P                  R7      /pV P0                  P!                  RV4      w  p p\        P2                  P5                  V4      p\7        VV
V4      p\        P.                  ! VP                  ^,          VP                  ^,          ^3\        P8                  R7      pVR&,           F  p. R'OVVR8  &   K  	  \:        P<                  ! V4      P                  R4      .# )(a5  
Predict masks for an input image.

This function takes an image as input and performs various preprocessing steps on the image. It then runs the image through an encoder to obtain an image embedding. The function also takes input labels and points as additional arguments. It concatenates the input points and labels with padding and transforms them. It creates an empty mask input and an indicator for no mask. The function then passes the image embedding, point coordinates, point labels, mask input, and has mask input to a decoder. The decoder generates masks based on the input and returns them as a list of images.

Parameters:
    img (PILImage): The input image.
    *args: Additional arguments.
    **kwargs: Additional keyword arguments.

Returns:
    List[PILImage]: A list of masks generated by the decoder.

sam_promptrP   rQ   rS   rR   rV   itemsobject
propertiesstringintegernumber)instanceschema   RGBr   Nimage_embeddingrH   r^   g        )axisr   image_embeddingspoint_coordspoint_labels
mask_inputhas_mask_inputorig_im_sizeL)i  r   r   )Nr   r   )Nr   )r   r   r   )r    r       r   )r   r   r   r   )   r   r   )getr=   widthheightr   rt   
get_inputsnameconvertr   rV   r*   minr8   r'   float32runr[   concatenaterN   onesmatmulTr)   ru   r"   r#   rd   uint8r   	fromarray)rv   r}   rw   rx   rW   r   target_size
input_sizeencoder_input_namecv_imagerH   scale_xscale_yr@   r^   encoder_inputsencoder_outputr   	embeddinginput_pointsinput_labels
onnx_coord
onnx_labelonnx_mask_inputonnx_has_mask_inputdecoder_inputsr]   _inv_transform_matrixrc   ms   &&*,                           r   predictSamSession.predict   s   &  GQSQ/SZZ!^1DE	
 GVX.fi0&(!3

 	&0 
!\\446q9>>kk% 88C= r*Q-(.."33Q-(.."33G%881E1
 R ]JqM*
 

 ;
 ))$?(+ ] 0
	 &6f%="l^^\288c3ZL3I$JQRS

 ^^\288RD>$BK

&
 	 "*jFMMJJ

 ^^J,,Q/32::F 

 YYz+;+=+=>
)00<
((#32::F hhq

; JJ/1BHHZrzzB
 ll&&t^<q!!yy}}-=>}6JKxxQQ;288Lz""A+DSM # %--c233r   c                   VP                  RR4      pVP                  RR4      pV R2pV R2pV'       d   V R2pV R2p\        P                  ! R	V 2R
VV P                  ! V/ VB RR7       \        P                  ! R	V 2R
VV P                  ! V/ VB RR7       VR8X  Edt   \        P
                  P                  \        P
                  P                  V P                  ! V/ VB R4      4      '       Eg    \        4       p\        ^^4       F  p\        P                  ! RV R2R
RV R2V P                  ! V/ VB RR7       \        P
                  P                  V P                  ! V/ VB RV R24      p	VP                  \        V	R4      P                  4       4       \        P                  ! V	4       K  	  \        \        P
                  P                  V P                  ! V/ VB R4      R4      ;_uu_ 4       p
V
P                  V4       R
R
R
4       \        P
                  P                  V P                  ! V/ VB V4      \        P
                  P                  V P                  ! V/ VB V4      3#   + '       g   i     Lp; i)a  
Class method to download ONNX model files.

This method is responsible for downloading two ONNX model files from specified URLs and saving them locally. The downloaded files are saved with the naming convention 'name_encoder.onnx' and 'name_decoder.onnx', where 'name' is the value returned by the 'name' method.

Parameters:
    cls: The class object.
    *args: Variable length argument list.
    **kwargs: Arbitrary keyword arguments.

Returns:
    tuple: A tuple containing the file paths of the downloaded encoder and decoder models.
	sam_modelsam_vit_b_01ec64	sam_quantFz.encoder.onnxz.decoder.onnxz.encoder.quant.onnxz.decoder.quant.onnxz>https://github.com/danielgatis/rembg/releases/download/v0.0.0/NT)fnamepathprogressbarzsam_vit_h_4b8939.encoder.onnxz!sam_vit_h_4b8939.encoder_data.binz\https://github.com/danielgatis/rembg/releases/download/v0.0.0/sam_vit_h_4b8939.encoder_data.z.binzsam_vit_h_4b8939.encoder_data.rbwb)r   poochretrieve
u2net_homeosr   existsjoin	bytearrayr+   extendopenreadremovewrite)clsrw   rx   rh   quantfname_encoderfname_decodercontentifbinfps   &*,        r   rr   SamSession.download_models*  sW    ZZ-?@


;.%,m4%,m4)l*=>M)l*=>ML]O\00	
 	L]O\00	
 ;;BGGNNGGLL//1TE
 E

  kG1a[rstruuyz:1#TB88 $ ww||NND3F34QCt< tD$/4467		$ !  NND3F37   ! GGLL88-HGGLL88-H
 	
 s   >I88J	c                    R# )z
Class method to return a string value.

This method returns the string value 'sam'.

Parameters:
    cls: The class object.
    *args: Variable length argument list.
    **kwargs: Arbitrary keyword arguments.

Returns:
    str: The string value 'sam'.
sam )r   rw   rx   s   &*,r   r   SamSession.namex  s     r   )ru   rt   rh   N)__name__
__module____qualname____firstlineno____doc__rz   r   classmethodrr   r   __static_attributes____classdictcell__)rm   s   @r   rf   rf      sL     
 
8B4 B4H K
 K
Z  r   rf   )r   copyr   typingr   numpyr   onnxruntimerk   r   
jsonschemar   PILr   	PIL.Imager~   scipy.ndimager   baser   r8   rC   rN   r[   rd   rf   r   r   r   <module>r      sN    	        ' ) 2&j
$"$H Hr   