o
    i)                     @   s4  d dl Z d dlmZ d dlmZ d dlZd dl	Z	d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlZd'd	d
Zd(ddZdd Z						d)ddZ						d*dedee	jB de	jdedededefddZdde	jdddddfdeded ede	jdededed!ed"efd#d$Zd+d%d&Z dS ),    N)	Rectangle)Image)SAM2ImagePredictor)SAM2ImageOnnxPredictor)load_sam2_modelFTc           
         s   |rt jt jdt dggdd}nt g d}| jdd  \}}| t j} | ||d|ddd }|r\dd l  	|  j
 j\}}	 fd	d
|D } j||dddd}|| d S )N   333333?r   )axis)g?g?g      ?r      c                    s   g | ]
} j |d ddqS )g{Gz?T)epsilonclosed)ZapproxPolyDP).0Zcontourcv2 h/home/kim/smarthome/.venv/lib/python3.10/site-packages/onnxruntime/transformers/models/sam2/sam2_demo.py
<listcomp>!   s    zshow_mask.<locals>.<listcomp>)r   r   r   g      ?   )Z	thickness)npZconcatenaterandomarrayshapeZastypeZuint8Zreshaper   ZfindContoursZRETR_EXTERNALZCHAIN_APPROX_NONEZdrawContoursimshow)
maskaxrandom_colorborderscolorhwZ
mask_imageZcontours_r   r   r   	show_mask   s   $r#   w  c              	   C   s|   | |dk }| |dk }|j |d d df |d d df dd|ddd |j |d d df |d d df dd|ddd d S )	Nr   r   green*whiteg      ?)r   markers	edgecolorZ	linewidthred)Zscatter)Zcoordslabelsr   Zmarker_sizeZ
pos_pointsZ
neg_pointsr   r   r   show_points&   s   &&
r-   c              
   C   sV   | d | d }}| d | d  | d | d  }}| t||f||dddd d S )Nr   r   r   r   r%   )r   r   r   r   )r*   Z	facecolorZlw)Z	add_patchr   )boxr   Zx0Zy0r!   r    r   r   r   show_box1   s   ""r/   c	                 C   s   t t||ddD ]\}	\}
}tjdd t|  t|
t |d |d ur5|d us-J t||t  |d ur@t|t  t	|dkrVtj
d|	d  d|d	d
d td |r| d|	 d}tj|rpt| tj|dddd t|tr|| tjdd t  q	d S )NFstrict
   r3   figsize)r   r   zMask z	, Score: z.3f   Zfontsizeoffr"   .pngpngtightr   )formatbbox_inchesZ
pad_inches)block)	enumeratezippltfigurer   r#   gcar-   r/   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   Zscorefilenamer   r   r   
show_masks7   s,   
 




rZ   sam2_hiera_largetorch CUDAExecutionProvidersam2_dirdevicedtypeimage_encoder_onnx_pathimage_decoder_onnx_pathimage_decoder_multi_onnx_pathproviderc	              	   C   s<   t | ||d}	|dkrt|	}
|
S t|	||||||d}
|
S )N)r`   r\   )rb   rc   rd   re   r`   Z
onnx_dtype)r   r   r   )r_   r`   ra   
model_typeenginerb   rc   rd   re   Z
sam2_model	predictorr   r   r   get_predictor\   s   	ri   rf   rg   use_gpuenable_batchc	                 C   s  |rt j s	J dt v sJ d}	nd}	t |rdnd}
|r9|dkr9t jdjdkr9dt jjj	_
dt jj_
tjd	 td
}t|d}t| |
|||||||	d	}|| d| d}tddgg}tdg}|j||dd\}}}t|d d d }|| }|| }|| }g }t|||||d|d |d tddgddgg}tddg}|t|d d d d f }|j|||d d d d d f dd\}}}t||||||d |d tddgddgg}tddg}|t|d d d d f }|j|||d d d d d f dd\}}}t||||||d |d tg d}|jd d |d d d f dd\}}}t|||||d |d  tg d}td!d"gg}tdg}|j|||dd\}}}t|||||||d# |d$ |rtg d%g dg d&g d'g}|jd d |dd\}}}tjd(d) t| |D ]}t|dt dd* q|D ]
}t|t  qt d+ t!  t"|d,  |#|d,  |S )-Nr^   ZCPUExecutionProvidercudacpur\   r      Tr   z	truck.jpgRGB)re   
sam2_demo_r"   i  r$   r   )rS   point_labelsmultimask_outputr   Z	multimask)rS   rU   r   rV   rW   ie  iq  F)rS   rq   
mask_inputrr   Zmulti_points)rS   rU   rV   rW   Zbackground_point)i  iX  i  ik  )rS   rq   r.   rr   r.   )rT   rV   rW   i?    Zbox_and_point)rT   rS   rU   rV   rW   )K   i  i  iR  )i_  i&  ir  i   )i  i  ix  rt   r2   r4   )r   r8   zbatch_prompt.png)$r\   rl   Zis_availableonnxruntimeZget_available_providersr`   Zget_device_propertiesmajorbackendsmatmulZ
allow_tf32Zcudnnr   r   seedr   openr   convertri   Z	set_imageZpredictZargsortrZ   ZargmaxrA   rB   r   r#   ZsqueezerC   r/   r	   rN   rJ   rM   )r_   rf   rg   ra   rb   rc   rd   rj   rk   re   r`   rP   rh   prefixZinput_pointZinput_labelrQ   rR   ZlogitsZ
sorted_indrW   rs   r"   Z	input_boxZinput_boxesr   r.   r   r   r   run_demow   s  





r~   c           
      C   sR  t jdt| dd\}}tt| |ddD ]|\}\}}t|}t|}	|d|f | |d|f j|	dd	d	dd
d |d|f 
d |d|f |jd |jd   |d|f |	 |d|f j|	dd	d	dd
d |d|f 
d |d|f |	jd |	jd   qt   t jd| d	dddd t   d S )Nr   )g3333333@g%@)ZnrowsZncolsr5   Fr0   r   rp   r]   r9   r3   r7   r8   r   Z	sam2_demor:   r;   i  )r<   r=   Zdpi)rA   ZsubplotsrD   r?   r@   mpimgZimreadr   	set_titlereplacer	   Z
set_aspectr   Ztight_layoutrJ   rN   )
Zleft_imagesZright_imagessuffixZfigZaxesrX   Zleft_img_pathZright_img_pathZleft_imgZ	right_imgr   r   r   show_all_images.  s   

&"&$r   )FT)r$   )NNNTNN)r[   r\   r]   r]   r]   r^   )r]   )!rF   Zmatplotlib.imagerP   r   Zmatplotlib.pyplotZpyplotrA   numpyr   r\   Zmatplotlib.patchesr   ZPILr   Zsam2.sam2_image_predictorr   Zsam2_image_onnx_predictorr   Z
sam2_utilsr   rv   r#   r-   r/   rZ   strr`   ra   ri   Zfloat32boolr~   r   r   r   r   r   <module>   s   



)	
	
 8