rlx-sam3 0.2.0

SAM 3 for RLX
Documentation
+
2Z
j*���Rt^RIt^RIt^RIt^RIt^RItRRltRRltRt	RRlt
R	R
ltRRltR
Rlt
]R8Xd]!]
!44hR#)aFull SAM3 inference backend used by the Rust `Sam3` wrapper.

This script intentionally delegates the full detector/tracker stack to the
official `facebookresearch/sam3` package. It is the complete execution path
for SAM3 image and base video while native RLX kernels are brought up.
Nc�0�V^8�dQhR\R\/#)��name�return)�str)�formats"�(rlx-models/src/sam3/reference_backend.py�__annotate__r	s����c��c��c��\PPV4pVf5\RV2\P
R7\P!^4V#)Nzmissing env var: ��file)�os�environ�get�print�sys�stderr�exit)r�values& r�envrs?���J�J�N�N�4� �E��}�
�!�$��(�s�z�z�:�������Lr
c��V^8�dQhRR/#)rr�
np.ndarray�)rs"rr	r	s��!�!�,�!r
c�J�\\R44p\\R44p\P!\R4\PR7pW,^,pVP
V8wd\
RVRVP
24hVPW^4#)�RLX_SAM3_INPUT_H�RLX_SAM3_INPUT_W�RLX_SAM3_IMAGE_U8_BIN��dtypezimage expected z bytes, got )�intr�np�fromfile�uint8�size�
ValueError�reshape)�h�w�data�expecteds    r�load_rgbr+s����C�"�#�$�A��C�"�#�$�A�
�;�;�s�2�3�2�8�8�D�D��u�q�y�H��y�y�H���?�8�*�L�����L�M�M��<�<��a� � r
c���VfR#\VR4'd-VP4P4P4p\P
!V\PR7#)N�detachr)�hasattrr-�cpu�numpyr!�asarray�float32)rs&r�tensor_to_numpyr3(sI���}���u�h�������"�"�$�*�*�,��
�:�:�e�2�:�:�.�.r
c�R�V^8�dQhR\R\R\\,/#)r�out_dirrr)r�listr )rs"rr	r	0s%������C��4��9�r
c�D�\V4pVf'\P!R\PR7p\P!V\PR7pVP\PPWR244\VP4#)Nrz.f32)�)r3r!�zerosr2r1�tofiler�path�joinr6�shape)r5rr�arrs&&& r�write_arrayr?0sf��
�%�
 �C�
�{��h�h�t�2�:�:�.��
�*�*�S��
�
�
+�C��J�J�r�w�w�|�|�G�v�T�]�3�4���	�	�?�r
c
�L�V^8�dQhR\R\RRR\R\/#�r�weights�device�rgbrr5r�r�dict)rs"rr	r	9s/����s��C��l��S��T�r
c
��^RIp^RIHp^RIHp^RIHp\PPR4;'gRpVP4;_uu_4VPVRR7;_uu_4V!VR	VRRRR
7p	V!V	4p
V
PVPV44pV
PW�R7pRRR4RRR4\VRXPR44p
\VR
VPR
44p\VRVPR44pRRRV
RVRV/# +'giLv;i +'giL�;i)r8N��Image)�
Sam3Processor)�build_sam3_image_model�RLX_SAM3_TEXT_PROMPT�objectF��device_type�enabledT)rC�	eval_mode�checkpoint_path�load_from_HF�enable_inst_interactivity�compile)�state�prompt�masks�boxes�scores�mode�image�masks_shape�boxes_shape�scores_shape)�torch�PILrI�sam3.model.sam3_image_processorrJ�sam3.model_builderrKrrr�inference_mode�autocast�	set_image�	fromarray�set_text_promptr?)rBrCrDr5r`rIrJrKrW�model�	processorrV�resultr]r^r_s&&&&            r�	run_imagerl9s����=�9�
�Z�Z�^�^�2�
3�
?�
?�x�F�	�	�	�	�	����F�E��!R�!R�&���#��&+��

��"�%�(�	��#�#�E�O�O�C�$8�9���*�*��*�F��"S�	��g�w��
�
�7�0C�D�K��g�w��
�
�7�0C�D�K��w��&�*�*�X�2F�G�L����{��{���	��!"S�!R��	�	�s%�D>�4A	D+	�=D>�+D;
�6D>�>E	c
�L�V^8�dQhR\R\RRR\R\/#rArE)rs"rr	r	Xs/��-�-�s�-�C�-�l�-�S�-�T�-r
c�@�^RIp^RIHp^RIHp\
PPR4;'gRp\P!RR7;_uu_4p\
PPVR4p	VPV4PV	4VP4;_uu_4VPVR	R
7;_uu_4V!VR	VR	R7p
V
P!RR
RV/R7pVPR4pV
P!RRRVR^RV/R7pRRR4RRR4RRR4XPRV4p
\#V
\$4'dV
'dV
^,pM\#V
\&4'dT
pM/p\)VRVPR44p\)VRVPR44p\)VRVPR44pRRRVRVRV/# +'giL�;i +'giL�;i +'giL�;i)r8NrH)�build_sam3_video_predictorrLrM�rlx_sam3_video_)�prefixz	00000.jpgFrN)rRrSrCrU�type�
start_session�
resource_path)�request�
session_id�
add_prompt�frame_index�text�outputsrXrYrZr[�videor]r^r_)r`rarIrcrorrr�tempfile�TemporaryDirectoryr;r<rg�saverdre�handle_request�
isinstancer6rFr?)rBrCrDr5r`rIrorW�
frames_dir�
frame_path�	predictor�responservrz�firstr]r^r_s&&&&              r�	run_videor�Xs�����=�
�Z�Z�^�^�2�
3�
?�
?�x�F�	�	$�	$�,=�	>�	>�*��W�W�\�\�*�k�:�
�
�����!�!�*�-�
�
!�
!�
#�
#�U�^�^��PU�^�%V�%V�2� '�"���	�I�!�/�/���/�:�N�0��H�"���l�3�J� �/�/��L� �*�!�1��F�	�0��H�&W�
#�	
?�0�l�l�9�h�/�G��'�4� � �W���
��	�G�T�	"�	"������g�w��	�	�'�0B�C�K��g�w��	�	�'�0B�C�K��w��%�)�)�H�2E�F�L����{��{���	��=&W�%V��
#�
#��	
?�	>�s>�AH
�2G:	�AG'
�G:	�$H
�'G7�2G:	�:H

�H
�
H	c�$�V^8�dQhR\/#)rr)r )rs"rr	r	�s��
�
�c�
r
c��^RIp\R4p\R4p\PPRR4p\PPRR	4pTR
8Xd>TPP4'g\R\PR7R	pRTPPPn
RTPPn
\P!TR
R7\!4pTR8Xd\#Y%Yc4pM#TR8Xd\%Y%Yc4pM\'RT:24h\)\P*P-TR4RRR7;_uu_4p\.P0!Yx4RRR4^# \d*p\RT2\PR7Rp?^#Rp?ii;i +'gi^#;i)r8Nz/torch must be installed for SAM3 full backend: r�RLX_SAM3_WEIGHTS�RLX_SAM3_OUT_DIR�
RLX_SAM3_MODEr\�RLX_SAM3_DEVICEr/�cudazARLX_SAM3_DEVICE=cuda but CUDA is unavailable; falling back to CPUFT)�exist_okr{zunknown RLX_SAM3_MODE=z	meta.jsonr(zutf-8)�encoding)r`�ImportErrorrrrrrrrr��is_available�backends�matmul�
allow_tf32�cudnn�makedirsr+r�rlr%�openr;r<�json�dump)	r`�excrBr5r[rCrD�meta�fs	         r�mainr��sa����
�$�%�G��$�%�G�
�:�:�>�>�/�7�3�D�
�Z�Z�^�^�-�u�
5�F�
����
�
� 7� 7� 9� 9�
�Q�X[�Xb�Xb�c���,1�E�N�N�����)�&+�E�N�N���#��K�K��$�'�
�*�C��w����#�7��	
�����#�7���1�$��:�;�;�	
�b�g�g�l�l�7�K�0�#��	H�	H�A��	�	�$��
I���1��
�?��u�E�C�J�J�W�����,
I�	H��s#�F�-G�G�F=�=G�G	�__main__)�__doc__r�rrr|r0r!rr+r3r?rlr�r��__name__�
SystemExitrr
r�<module>r�s[����	�
����!�/���>-�`
�<�z��
�T�V�
��r