+
    "Ý³jß6  ã                   óD  € ^ RI t ^ RIt^ RIHt ^ RIHtHt ^ RIt^ RI	t	^ RI
Ht ^ RIHt R tR tR R	 ltR
 t ! R R4      tRRR]	P$                  ! R4      3R ltRRR]	P$                  ! R4      3R ltRR]	P$                  ! R4      3R ltR tR tR R ltR R ltR# )é    N)ÚThread)ÚTupleÚUnion)ÚImage)Útqdmc                  óØ  € \         P                  P                  4       '       Ed=   \         P                  P                  ^ 4      P                  ^8  p \         P                  P                  ^ 4      P                  ^8¬  pV'       g   \
        P                  ! R\        ^R7       \        ;QJ d8    . R \         P                  P                  R4      R,           4       F  NK  	  5M1! R \         P                  P                  R4      R,           4       4      pVR
8  d0   \
        P                  ! R\         P                   R2\        ^R7       VR
8  ;'       g    V'       * pMRp R	pRpWV3# )r   zSFlash Attention is disabled as it requires a GPU with Ampere (8.0) CUDA capability.©ÚcategoryÚ
stacklevelc              3   ó8   "  € T F  p\        V4      x € K  	  R # 5i©N)Úint)Ú.0Úvs   & Ú`/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torchao/_models/sam2/utils/misc.pyÚ	<genexpr>Ú$get_sdpa_settings.<locals>.<genexpr>   s   é € ÐQÑ0P¨1¤ A§ Ó0Pùs   ‚Ú.ºNé   NzYou are using PyTorch zw without Flash Attention v2 support. Consider upgrading to PyTorch 2.2+ for Flash Attention v2 (which could be faster).TF)r   r   )ÚtorchÚcudaÚis_availableÚget_device_propertiesÚmajorÚwarningsÚwarnÚUserWarningÚtupleÚ__version__Úsplit)Úold_gpuÚuse_flash_attnÚpytorch_versionÚmath_kernel_ons       r   Úget_sdpa_settingsr&      s  € Ü‡z�z×Ñ× Ó Ü—*‘*×2Ñ2°1Ó5×;Ñ;¸aÑ?ˆäŸ™×9Ñ9¸!Ó<×BÑBÀaÑGˆßÜ�MŠMØeÜ$Øõ÷  œ%ÑQ´×0AÑ0A×0GÑ0GÈÓ0LÈRÖ0PÓQŸ%™%ÑQ´×0AÑ0A×0GÑ0GÈÓ0LÈRÖ0PÓQÓQˆØ˜VÔ#Ü�MŠMØ(¬×):Ñ):Ð(;ð <eð eä$Øõ	ð )¨6Ñ1×GÐG¸Ô5G‰àˆØˆØˆà NÐ2Ð2ó    c                ó†   € ^ RI Hp VP                  V P                  \        P
                  4      P                  4       4      # )aö  
Get the connected components (8-connectivity) of binary masks of shape (N, 1, H, W).

Inputs:
- mask: A binary mask tensor of shape (N, 1, H, W), where 1 is foreground and 0 is
        background.

Outputs:
- labels: A tensor of shape (N, 1, H, W) containing the connected component labels
          for foreground pixels and 0 for background pixels.
- counts: A tensor of shape (N, 1, H, W) containing the area of the connected
          components for foreground pixels and 0 for background pixels.
)Ú_C)Úsam2r)   Úget_connected_componnetsÚtor   Úuint8Ú
contiguous)Úmaskr)   s   & r   Úget_connected_componentsr0   0   s.   € õ à×&Ñ& t§w¡w¬u¯{©{Ó';×'FÑ'FÓ'HÓIÐIr'   c                ó8   € V ^8„  d   QhR\         P                  /# )r   Úmasks)r   ÚTensor)Úformats   "r   Ú__annotate__r5   C   s   € ÷ ñ ”u—|‘|ñ r'   c                ó”  € V P                   w  rr4V P                  p\        P                  ! WE\        P                  R7      p\        P                  ! W5\        P                  R7      p\        P
                  ! WgRR7      w  r‰VR,          P                  V^W44      pV	R,          P                  V^W44      p	\        P                  ! \        P                  ! WV4      P                  R4      RR7      w  r¢\        P                  ! \        P                  ! WR4      P                  R4      RR7      w  r²\        P                  ! \        P                  ! W	V4      P                  R4      RR7      w  rÂ\        P                  ! \        P                  ! W	R4      P                  R4      RR7      w  rÒ\        P                  ! W¬W½3RR7      pV# )zß
compute bounding box given an input mask

Inputs:
- masks: [B, 1, H, W] masks, dtype=torch.Tensor

Returns:
- box_coords: [B, 1, 4], contains (x, y) coordinates of top left and bottom right box corners, dtype=torch.Tensor
)ÚdeviceÚdtypeÚxy)Úindexing©Údim)NN.éþÿÿÿéÿÿÿÿ)Úshaper7   r   ÚarangeÚint32ÚmeshgridÚexpandÚminÚwhereÚflattenÚmaxÚstack)r2   ÚBÚ_ÚhÚwr7   ÚxsÚysÚgrid_xsÚgrid_ysÚmin_xsÚmax_xsÚmin_ysÚmax_ysÚbbox_coordss   &              r   Úmask_to_boxrV   C   sB  € ð —‘�J€Aˆ!Ø�\‰\€FÜ	�Š�a¬e¯k©kÔ	:€BÜ	�Š�a¬e¯k©kÔ	:€BÜ—~’~ b°tÔ<Ñ€GØ�oÕ&×-Ñ-¨a°°AÓ9€GØ�oÕ&×-Ñ-¨a°°AÓ9€GÜ—	’	œ%Ÿ+š+ e°aÓ8×@Ñ@ÀÓDÈ"ÔM�I€FÜ—	’	œ%Ÿ+š+ e°bÓ9×AÑAÀ"ÓEÈ2ÔN�I€FÜ—	’	œ%Ÿ+š+ e°aÓ8×@Ñ@ÀÓDÈ"ÔM�I€FÜ—	’	œ%Ÿ+š+ e°bÓ9×AÑAÀ"ÓEÈ2ÔN�I€FÜ—+’+˜v¨vÐ>ÀBÔG€KàÐr'   c                 ó’  € \         P                  ! V 4      p\        P                  ! VP	                  R 4      P                  W34      4      pVP                  \        P                  8X  d   VR,          pM\        RVP                   RV  24      h\        P                  ! V4      P                  ^^ ^4      pVP                  w  rVWFV3# )ÚRGBç     ào@zUnknown image dtype: z on )r   ÚopenÚnpÚarrayÚconvertÚresizer8   r-   ÚRuntimeErrorr   Ú
from_numpyÚpermuteÚsize)Úimg_pathÚ
image_sizeÚimg_pilÚimg_npÚimgÚvideo_widthÚvideo_heights   &&     r   Ú_load_img_as_tensorrj   ]   s¡   € Ü�jŠj˜Ó"€GÜ�XŠX�g—o‘o eÓ,×3Ñ3°ZÐ4LÓMÓN€FØ‡|�|”r—x‘xÔØ˜%•‰äÐ2°6·<±<°.ÀÀXÀJÐOÓPÐPÜ
×
Ò
˜6Ó
"×
*Ñ
*¨1¨a°Ó
3€CØ '§¡Ñ€KØ˜kÐ)Ð)r'   c                   ó6   a € ] tR t^it o RtR tR tR tRtV t	R# )ÚAsyncVideoFrameLoaderzR
A list of video frames to be load asynchronously without blocking session start.
c                ó<  a € VS n         VS n        VS n        VS n        VS n        R .\        V4      ,          S n        R S n        R S n        R S n	        VS n
        S P                  ^ 4       V 3R lp\        VRR7      S n        S P                  P                  4        R # )Nc                  óÌ   <€  \        \        \        SP                  4      4      R R7       F  p SP	                  V 4       K  	  R#   \
         d   pTSn         Rp?R# Rp?ii ; i)úframe loading (JPEG)©ÚdescN)r   ÚrangeÚlenÚimagesÚ__getitem__Ú	ExceptionÚ	exception)ÚnÚeÚselfs     €r   Ú_load_framesÚ4AsyncVideoFrameLoader.__init__.<locals>._load_framesŠ   sP   ø€ ð#Üœe¤C¨¯©Ó$4Ó5Ð<R×S�AØ×$Ñ$ QÖ'ó Tøäô #Ø!"�—’ûð#ús   ƒAA ÁA#ÁAÁA#T)ÚtargetÚdaemon)Ú	img_pathsrd   Úoffload_video_to_cpuÚimg_meanÚimg_stdrs   rt   rw   ri   rh   Úcompute_deviceru   r   ÚthreadÚstart)rz   r   rd   r€   r�   r‚   rƒ   r{   s   f&&&&&& r   Ú__init__ÚAsyncVideoFrameLoader.__init__n   s�   ø€ ð #ˆŒØ$ˆŒØ$8ˆÔ!Ø ˆŒØˆŒà�fœs 9›~Õ-ˆŒàˆŒà ˆÔØˆÔØ,ˆÔð 	×Ñ˜Ôõ	#ô  L¸Ô>ˆŒØ�‰×ÑÖr'   c                ó®  € V P                   e   \        R4      V P                   hV P                  V,          pVe   V# \        V P                  V,          V P
                  4      w  r#pW0n        W@n        W P                  ,          pW P                  ,          pV P                  '       g   VP                  V P                  RR7      pW P                  V&   V# )NzFailure in frame loading threadT)Únon_blocking)rw   r_   rt   rj   r   rd   ri   rh   r�   r‚   r€   r,   rƒ   )rz   Úindexrg   ri   rh   s   &&   r   ru   Ú!AsyncVideoFrameLoader.__getitem__”   s²   € Ø�>‰>Ò%ÜÐ@ÓAÀtÇ~Á~ÐUà�k‰k˜%Õ ˆØŠ?ØˆJä)<Ø�N‰N˜5Õ! 4§?¡?ó*
Ñ&ˆ˜;ð )ÔØ&Ôà�}‰}ÕˆØ�|‰|ÕˆØ×(×(Ð(Ø—&‘&˜×,Ñ,¸4�&Ó@ˆCØ �‰�EÑØˆ
r'   c                ó,   € \        V P                  4      # r   )rs   rt   )rz   s   &r   Ú__len__ÚAsyncVideoFrameLoader.__len__©   s   € Ü�4—;‘;ÓÐr'   )rƒ   rw   rd   rt   r�   r   r‚   r€   r„   ri   rh   N)
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r†   ru   r�   Ú__static_attributes__Ú__classdictcell__)Ú__classdict__s   @r   rl   rl   i   s   ø‡ € ñò$òL÷* ð  r'   rl   Fr   c           
     ó~  € \        V \        4      p\        V \        4      pT;'       d*    \        P                  P                  V 4      R,          R9   p	V'       g	   V	'       d   \        V VVVVVR7      # V'       d8   \        P                  P                  V 4      '       d   \        V VVVVVVR7      # \        R4      h)z¬
Load the video frames from video_path. The frames are resized to image_size as in
the model and are loaded to GPU if offload_video_to_cpu=False. This is used by the demo.
)Ú
video_pathrd   r€   r�   r‚   rƒ   )r˜   rd   r€   r�   r‚   Úasync_loading_framesrƒ   z;Only MP4 video and JPEG folder are supported at this momentr>   )z.mp4z.MP4)
Ú
isinstanceÚbytesÚstrÚosÚpathÚsplitextÚ!load_video_frames_from_video_fileÚisdirÚ!load_video_frames_from_jpg_imagesÚNotImplementedError)
r˜   rd   r€   r�   r‚   r™   rƒ   Úis_bytesÚis_strÚis_mp4_paths
   &&&&&&&   r   Úload_video_framesr§   ­   s²   € ô ˜*¤eÓ,€HÜ˜
¤CÓ(€FØ×QÐQœRŸW™W×-Ñ-¨jÓ9¸"Õ=ÐAQÑQ€Kß—;Ü0Ø!Ø!Ø!5ØØØ)ô
ð 	
÷ 
”B—G‘G—M‘M *×-Ò-Ü0Ø!Ø!Ø!5ØØØ!5Ø)ô
ð 	
ô "ØIó
ð 	
r'   c                óv  € \        V \        4      '       d)   \        P                  P	                  V 4      '       d   T pM\        R4      h\        P                  ! V4       Uu. uF2  p\        P                  P                  V4      R,          R	9   g   K0  VNK4  	  p	pV	P                  R R7       \        V	4      p
V
^ 8X  d   \        RV 24      hV	 Uu. uF"  p\        P                  P                  W{4      NK$  	  pp\        P                  ! V\        P                  R7      R
,          p\        P                  ! V\        P                  R7      R
,          pV'       d)   \        VVVVVV4      pWÝP                   VP"                  3# \        P$                  ! V
^W\        P                  R7      p\'        \)        VRR7      4       F  w  pp\+        VV4      w  Wï&   ppK  	  V'       g4   VP-                  V4      pVP-                  V4      pVP-                  V4      pWã,          pWä,          pVXX3# u upi u upi )aD  
Load the video frames from a directory of JPEG files ("<frame_index>.jpg" format).

The frames are resized to image_size x image_size and are loaded to GPU if
`offload_video_to_cpu` is `False` and to CPU if `offload_video_to_cpu` is `True`.

You can load a frame asynchronously by setting `async_loading_frames` to `True`.
ak  Only JPEG frames are supported at this moment. For video files, you may use ffmpeg (https://ffmpeg.org/) to extract frames into a folder of JPEG files, such as 
```
ffmpeg -i <your_video>.mp4 -q:v 2 -start_number 0 <output_dir>/'%05d.jpg'
```
where `-q:v` generates high-quality JPEG frames and `-start_number 0` asks ffmpeg to start the JPEG file from 00000.jpg.c                 ó`   € \        \        P                  P                  V 4      ^ ,          4      # )r   )r   r�   rž   rŸ   )Úps   &r   Ú<lambda>Ú3load_video_frames_from_jpg_images.<locals>.<lambda>ù   s   € ¤3¤r§w¡w×'7Ñ'7¸Ó':¸1Õ'=Ô#>r'   )Úkeyzno images found in ©r8   ro   rp   r>   )z.jpgz.jpegz.JPGz.JPEG©ºNNNNN)rš   rœ   r�   rž   r¡   r£   ÚlistdirrŸ   Úsortrs   r_   Újoinr   ÚtensorÚfloat32rl   ri   rh   ÚzerosÚ	enumerater   rj   r,   )r˜   rd   r€   r�   r‚   r™   rƒ   Ú
jpg_folderrª   Úframe_namesÚ
num_framesÚ
frame_namer   Úlazy_imagesrt   rx   rc   ri   rh   s   &&&&&&&            r   r¢   r¢   Ö   sæ  € ô" �*œc×"Ò"¤r§w¡w§}¡}°Z×'@Ò'@Ø‰
ä!ð<ó
ð 	
ô —’˜JÔ'óá'ˆAÜ�7‰7×Ñ˜AÓ˜rÕ"Ð&HÑH÷ 	
ˆÙ'ð ð ð
 ×ÑÑ>ÐÔ?Ü�[Ó!€JØ�Q„ÜÐ0°°Ð=Ó>Ð>ÙHSÓTÉ¸*”—‘—‘˜jÖ5É€IÐTÜ�|Š|˜H¬E¯M©MÔ:¸=ÕI€HÜ�lŠl˜7¬%¯-©-Ô8¸ÕG€GçÜ+ØØØ ØØØó
ˆð ×4Ñ4°k×6MÑ6MÐMÐMä�[Š[˜ Q¨
ÄeÇmÁmÔT€FÜ ¤ iÐ6LÔ!MÖN‰ˆˆ8Ü/BÀ8ÈZÓ/XÑ,ˆ‰	�<¢ñ OçØ—‘˜>Ó*ˆØ—;‘;˜~Ó.ˆØ—*‘*˜^Ó,ˆà
Õ€FØ
Õ€FØ�< Ð,Ð,ùòEùò Us   Á"-H1ÂH1Ã(H6c                óÀ  € ^ RI p\        P                  ! V\        P                  R7      R,          p\        P                  ! V\        P                  R7      R,          pVP                  P                  R4       VP                  V 4      P                  4       P                  w  rxp	. p
VP                  WVR7       F%  pV
P                  VP                  ^^ ^4      4       K'  	  \        P                  ! V
^ R7      P                  4       R,          p
V'       g4   V
P                  V4      p
VP                  V4      pVP                  V4      pW£,          p
W¤,          p
W§V3# )z(Load the video frames from a video file.Nr®   r   )ÚwidthÚheightr;   rY   r¯   )Údecordr   r´   rµ   ÚbridgeÚ
set_bridgeÚVideoReaderÚnextr?   Úappendra   rH   Úfloatr,   )r˜   rd   r€   r�   r‚   rƒ   rÀ   ri   rh   rJ   rt   Úframes   &&&&&&      r   r    r      s  € ó ä�|Š|˜H¬E¯M©MÔ:¸=ÕI€HÜ�lŠl˜7¬%¯-©-Ô8¸ÕG€Gà
‡M�M×Ñ˜WÔ%Ø#)×#5Ñ#5°jÓ#A×#FÑ#FÓ#H×#NÑ#NÑ €L˜qà€FØ×#Ñ# JÈÐ#ÖTˆØ�‰�e—m‘m A q¨!Ó,Ö-ñ Uô �[Š[˜ QÔ'×-Ñ-Ó/°%Õ7€FßØ—‘˜>Ó*ˆØ—;‘;˜~Ó.ˆØ—*‘*˜^Ó,ˆà
Õ€FØ
Õ€FØ Ð,Ð,r'   c                ó
  € V^ 8”  g   Q R4       hT p \        V ^ 8*  4      w  r4V^ 8„  WA8*  ,          p\        P                  ! VRV 4      p V #   \         d.   p\        P
                  ! T R2\        ^R7       Tp  Rp?T # Rp?ii ; i)zQ
A post processor to fill small holes in mask scores with area under `max_area`.
zmax_area must be positivegš™™™™™¹?a*  

Skipping the post-processing step due to the error above. You can still use SAM 2 and it's OK to ignore the error above, although some post-processing functionality may be limited (which doesn't affect the results in most cases; see https://github.com/facebookresearch/sam2/blob/main/INSTALL.md).r	   N)r0   r   rE   rv   r   r   r   )r/   Úmax_areaÚ
input_maskÚlabelsÚareasÚis_holery   s   &&     r   Úfill_holes_in_mask_scoresrÎ   9  s�   € ð �aŒ<Ð4Ð4Ó4ˆ<à€JðÜ0°¸±Ó;‰ˆØ˜A‘: %Ñ"3Õ4ˆä�{Š{˜7 C¨Ó.ˆð €Køô ô 
ä�ŠØˆcð Nð Nô !Øõ	
ð Œà€Kûð
ús   ’6A
 Á
BÁ"A=Á=Bc                ó    € V f   YrCMB\         P                  ! V R,          V.^R7      p\         P                  ! V R,          V.^R7      pRVRV/# )zDAdd new points and labels to previous point inputs (add at the end).Úpoint_coordsr;   Úpoint_labels)r   Úcat)Úold_point_inputsÚ
new_pointsÚ
new_labelsÚpointsrË   s   &&&  r   Úconcat_pointsr×   V  sR   € àÒØ#‘ä—’Ð,¨^Õ<¸jÐIÈqÔQˆÜ—’Ð,¨^Õ<¸jÐIÈqÔQˆà˜F N°FÐ;Ð;r'   c                ón   € V ^8„  d   QhR\         \        P                  \        P                  3,          /# )r   Úimage)r   r[   Úndarrayr   r3   )r4   s   "r   r5   r5   a  s%   € ÷ 
ñ 
œ%¤§
¡
¬E¯L©LÐ 8Õ9ñ 
r'   c                 óH  € \        V \        P                  4      '       d   V P                  R ,          # \        V \        P
                  4      '       d   V P                  w  rpW#3# \        V \        4      '       d   V P                  w  r2W#3# \        R\        V 4       24      h)r   z;Only support np.ndarray, torch.Tensoror PIL Image, but got )
rš   r[   rÚ   r?   r   r3   r   rb   r£   Útype)rÙ   rJ   rK   rL   s   &   r   Úget_image_sizerÝ   a  s€   € Ü�%œŸ™×$Ò$Ø�{‰{˜2�ÐÜ	�Eœ5Ÿ<™<×	(Ò	(Ø—+‘+‰ˆˆaØˆvˆÜ	�Eœ5×	!Ò	!Ø�z‰z‰ˆØˆvˆä!ØIÌ$ÈuË+ÈÐWó
ð 	
r'   c                ó°   € V ^8„  d   QhR\         \        P                  \        P                  3,          R\
        \        \        \        \        3,          /# )r   rÙ   Úcrop_box)r   r[   rÚ   r   r3   r   r   )r4   s   "r   r5   r5   p  s<   € ÷ 
ñ 
Ü”—‘œUŸ\™\Ð)Õ*ð
Ü6;¼CÄÄcÌ3Ð<NÕ6Oñ
r'   c                 óò   € Vw  r#rE\        V \        P                  4      '       d   WV1W$1R 3,          # \        V \        P                  4      '       d   V R W51W$13,          # \        R\        V 4       24      h)r°   zAExpected image to be of type np.ndarray or torch.Tensor, but got )rš   r[   rÚ   r   r3   Ú
ValueErrorrÜ   )rÙ   rß   Úx0Úy0Úx1Úy1s   &&    r   Ú
crop_imageræ   p  sy   € ð �N€BˆBÜ�%œŸ™×$Ò$à˜�U˜B˜E 1�_Õ%Ð%Ü	�Eœ5Ÿ<™<×	(Ò	(à�Q˜˜˜r˜u�_Õ%Ð%äð%Ü%)¨%£[ Mð3ó
ð 	
r'   )g
×£p=
ß?gÉv¾Ÿ/Ý?g–C‹lçûÙ?)gZd;ßOÍ?gyé&1¬Ì?gÍÌÌÌÌÌÌ?)r�   r   Ú	threadingr   Útypingr   r   Únumpyr[   r   ÚPILr   r   r&   r0   rV   rj   rl   r7   r§   r¢   r    rÎ   r×   rÝ   ræ   © r'   r   Ú<module>rì      sª   ðó 
Û Ý ß ã Û Ý Ý ò3ò<Jõ&ò4	*÷A ñ A ðP #Ø!ØØ—<’< Ó'ô&
ðZ #Ø!ØØ—<’< Ó'ô@-ðN #Ø!Ø—<’< Ó'ô-ò@ò:<õ
÷
r'   