
    h)                        S SK r S SKJr  S SKJr  S SKrS SK	r	S SK
Jr  S SKJr  S SKJr  S SKJr  S SKJr  S SKrSS	 jrSS
 jrS r      S S jr      S!S\S\\	R4                  -  S\	R6                  S\S\S\S\4S jjrSS\	R:                  SSSSS4S\S\S\S\	R6                  S\S\S\S\S\4S jjrS"S jr g)#    N)	Rectangle)Image)SAM2ImagePredictor)SAM2ImageOnnxPredictor)load_sam2_modelFTc           	      x   U(       aJ  [         R                  " [         R                  R                  S5      [         R                  " S/5      /SS9nO[         R                  " / SQ5      nU R                  SS  u  pVU R                  [         R                  5      n U R                  XVS5      UR                  SSS5      -  nU(       a^  SS KnUR                  XR                  UR                  5      u  pU	 Vs/ s H  oR                  US	S
S9PM     n	nUR                  XySSSS9nUR                  U5        g s  snf )N   333333?r   )axis)g?g?g      ?r
      g{Gz?T)epsilonclosed)r   r   r   g      ?   )	thickness)npconcatenaterandomarrayshapeastypeuint8reshapecv2findContoursRETR_EXTERNALCHAIN_APPROX_NONEapproxPolyDPdrawContoursimshow)maskaxrandom_colorborderscolorhw
mask_imager   contours_contours               h/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/models/sam2/sam2_demo.py	show_maskr.      s    		 0 0 3RXXse_EAN>?::bc?DA;;rxx DaA&q!R)@@J&&t->->@U@UVX`aX`W$$Wd4$HX`a%%jBZ[%\
IIj bs   7D7c           
          XS:H     nXS:H     nUR                  US S 2S4   US S 2S4   SSUSSS9  UR                  US S 2S4   US S 2S4   SSUSSS9  g )	Nr   r   green*whiteg      ?)r&   markers	edgecolor	linewidthred)scatter)coordslabelsr#   marker_size
pos_points
neg_pointss         r-   show_pointsr>   &   s    !$J!$JJJ1a4*QT*'#`gsw   JJ1a4*QT*%{^equ      c                 z    U S   U S   p2U S   U S   -
  U S   U S   -
  pTUR                  [        X#4XESSSS95        g )Nr   r   r   r	   r0   )r   r   r   r   )r5   	facecolorlw)	add_patchr   )boxr#   x0y0r(   r'   s         r-   show_boxrG   1   sL    VSVq6CF?CFSVOqLLB8QWYZ[\r?   c	                 N   [        [        XSS95       GH  u  n	u  p[        R                  " SS9  [        R                  " U 5        [        U
[        R                  " 5       US9  Ub$  Uc   e[        X5[        R                  " 5       5        Ub  [        U[        R                  " 5       5        [        U5      S:  a  [        R                  " SU	S-    SUS	 3S
S9  [        R                  " S5        U(       a  U SU	 S3n[        R                  R                  U5      (       a  [        R                  " U5        [        R                   " USSSS9  [#        U[$        5      (       a  UR'                  U5        [        R(                  " SS9  [        R*                  " 5         GM     g )NFstrict
   rL   figsize)r%   r   zMask z	, Score: z.3f   fontsizeoffr+   .pngpngtightr   )formatbbox_inches
pad_inches)block)	enumeratezippltfigurer!   r.   gcar>   rG   lentitler   ospathexistsremovesavefig
isinstancelistappendshowclose)imagemasksscorespoint_coords
box_coordsinput_labelsr%   output_image_file_prefiximage_filesir"   scorefilenames                r-   
show_masksrv   7   s3    &c%&FG=D

8$

5$	73#+++CGGI>!Z+v;?IIa!eWIeC[9BG#231QCt<Hww~~h''		(#KKGPQR+t,,""8,u		/ Hr?   sam2_hiera_largetorch sam2_dirdevicedtypeimage_encoder_onnx_pathimage_decoder_onnx_pathimage_decoder_multi_onnx_pathproviderc	           
      ^    [        XUS9n	US:X  a  [        U	5      n
U
$ [        U	UUUUUUS9n
U
$ )N)r{   rx   )r}   r~   r   r   r{   
onnx_dtype)r   r   r   )rz   r{   r|   
model_typeenginer}   r~   r   r   
sam2_model	predictors              r-   get_predictorr   \   sS     !fEJ&z2	  +$;$;*G
	 r?   r   r   use_gpuenable_batchc	                 	   U(       aC  [         R                  R                  5       (       d   eS[        R                  " 5       ;   d   eSn	OSn	[         R
                  " U(       a  SOS5      n
U(       a{  US:X  au  [         R                  R                  S5      R                  S:  aH  S[         R                  R                  R                  l
        S[         R                  R                  l
        [        R                  R                  S	5        [        R                   " S
5      n[        R"                  " UR%                  S5      5      n['        U U
UUUUUUU	S9	nUR)                  U5        SU S3n[        R"                  " SS//5      n[        R"                  " S/5      nUR+                  UUSS9u  nnn[        R,                  " U5      S S S2   nUU   nUU   nUU   n/ n[/        UUUUUSUS-   US9  [        R"                  " SS/SS//5      n[        R"                  " SS/5      nU[        R0                  " U5      S S 2S S 24   nUR+                  UUUS S S 2S S 24   SS9u  nnn[/        UUUUUUS-   US9  [        R"                  " SS/SS//5      n[        R"                  " SS/5      nU[        R0                  " U5      S S 2S S 24   nUR+                  UUUS S S 2S S 24   SS9u  nnn[/        UUUUUUS-   US9  [        R"                  " / SQ5      nUR+                  S S US S S 24   SS9u  nnn[/        UUUUUS-   US 9  [        R"                  " / SQ5      n[        R"                  " S!S"//5      n[        R"                  " S/5      nUR+                  UUUSS9u  nnn[/        UUUUUUUS#-   US$9  U(       Ga  [        R"                  " / S%Q/ SQ/ S&Q/ S'Q/5      nUR+                  S S USS9u  nnn[2        R4                  " S(S)9  [2        R6                  " U5        U H0  n[9        UR;                  S5      [2        R<                  " 5       SS*9  M2     U H"  n[?        U[2        R<                  " 5       5        M$     [2        R@                  " S+5        [2        RB                  " 5         [2        RD                  " US,-   5        URG                  US,-   5        U$ )-NCUDAExecutionProviderCPUExecutionProvidercudacpurx   r      Tr	   z	truck.jpgRGB)r   
sam2_demo_r+   i  w  r   )rn   point_labelsmultimask_outputr   	multimask)rn   rp   r%   rq   rr   ie  iq  F)rn   r   
mask_inputr   multi_points)rn   rp   rq   rr   background_point)i  iX  i  ik  )rn   r   rD   r   rD   )ro   rq   rr   i?    box_and_point)ro   rn   rp   rq   rr   )K   i  i  iR  )i_  i&  ir  i   )i  i  ix  r   rK   rM   )r$   rR   zbatch_prompt.png)$rx   r   is_availableonnxruntimeget_available_providersr{   get_device_propertiesmajorbackendsmatmul
allow_tf32cudnnr   r   seedr   openr   convertr   	set_imagepredictargsortrv   argmaxr\   r]   r!   r.   squeezer^   rG   r   ri   re   rh   )rz   r   r   r|   r}   r~   r   r   r   r   r{   rk   r   prefixinput_pointinput_labelrl   rm   logits
sorted_indrr   r   r+   	input_boxinput_boxesr"   rD   s                              r-   run_demor   w   s    zz&&((((&+*M*M*OOOO*)\\G&7F6W$)I)I!)L)R)RVW)W04""-*.'IINN1JJ{#EHHU]]5)*E%
I &#F ((S#J<(K((A3-K%--   . E66 F#DbD)J*EJFJFK  !'+!5	 ((S#Js45K((Aq6"K		&)1a/0J ((  dAqj)	 ) E61   !'.!8 ((S#Js45K((Aq6"K		&)1a/0J ((  dAqj)	 ) E61   !'*<!< -.I ((dAg	 ) E61 !'% -.I((S#J<(K((A3-K%--  	 . E66   !'/!9	 hh$$&&	
 %,,"	 - 
vq 	

8$

5Ddll1oswwytD CS#'')$ 
F//06$667r?   c                    [         R                  " S[        U 5      SS9u  p4[        [	        XSS95       GH]  u  nu  pg[
        R                  " U5      n[
        R                  " U5      n	USU4   R                  U5        USU4   R                  UR                  SS5      R                  S	S5      S
S9  USU4   R                  S5        USU4   R                  UR                  S   UR                  S   -  5        USU4   R                  U	5        USU4   R                  UR                  SS5      R                  S	S5      S
S9  USU4   R                  S5        USU4   R                  U	R                  S   U	R                  S   -  5        GM`     [         R                  " 5         [         R                  " SU S	3SSSS9  [         R                  " 5         g )Nr   )g3333333@g%@)nrowsncolsrN   FrI   r   r   ry   rS   rL   rP   rR   r   	sam2_demorT   rU   i  )rV   rW   dpi)r\   subplotsr_   rZ   r[   mpimgimreadr!   	set_titlereplacer   
set_aspectr   tight_layoutre   ri   )
left_imagesright_imagessuffixfigaxesrs   left_img_pathright_img_pathleft_img	right_imgs
             r-   show_all_imagesr   .  s   1C,<nUIC.7K^c8d.e**M<<.LL0	QT
(#QT
]22<DLLVUWXcefQT
QT
hnnQ/(..2CCDQT
)$QT
^33L"EMMfVXYdfgQT
QT
iooa09??13EEF /f KK)F84(GQUVHHJr?   )FT)r   )NNNTNN)rw   rx   ry   ry   ry   r   )ry   )!ra   matplotlib.imagerk   r   matplotlib.pyplotpyplotr\   numpyr   rx   matplotlib.patchesr   PILr   sam2.sam2_image_predictorr   sam2_image_onnx_predictorr   
sam2_utilsr   r   r.   r>   rG   rv   strr{   r|   r   float32boolr   r    r?   r-   <module>r      sG  
 
      (  8 < & $] !"R "#%#%)++%,, ;; ! ! $' : )#%#%)+ttt t ;;	t
 !t !t $'t t tnr?   