54 KiB
54 KiB
In [ ]:
# Copyright (c) Meta Platforms, Inc. and affiliates.In [ ]:
using_colab = FalseIn [ ]:
if using_colab:
import torch
import torchvision
print("PyTorch version:", torch.__version__)
print("Torchvision version:", torchvision.__version__)
print("CUDA is available:", torch.cuda.is_available())
import sys
!{sys.executable} -m pip install opencv-python matplotlib scikit-learn
!{sys.executable} -m pip install 'git+https://github.com/facebookresearch/sam3.git'In [ ]:
!nvidia-smiIn [ ]:
import os
import sam3
import torch
sam3_root = os.path.join(os.path.dirname(sam3.__file__), "..")
# use all available GPUs on the machine
gpus_to_use = range(torch.cuda.device_count())
# # use only a single GPU
# gpus_to_use = [torch.cuda.current_device()]In [ ]:
from sam3.model_builder import build_sam3_video_predictor
predictor = build_sam3_video_predictor(gpus_to_use=gpus_to_use)In [ ]:
import glob
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from sam3.visualization_utils import (
load_frame,
prepare_masks_for_visualization,
visualize_formatted_frame_output,
)
# font size for axes titles
plt.rcParams["axes.titlesize"] = 12
plt.rcParams["figure.titlesize"] = 12
def propagate_in_video(predictor, session_id):
# we will just propagate from frame 0 to the end of the video
outputs_per_frame = {}
for response in predictor.handle_stream_request(
request=dict(
type="propagate_in_video",
session_id=session_id,
)
):
outputs_per_frame[response["frame_index"]] = response["outputs"]
return outputs_per_frame
def abs_to_rel_coords(coords, IMG_WIDTH, IMG_HEIGHT, coord_type="point"):
"""Convert absolute coordinates to relative coordinates (0-1 range)
Args:
coords: List of coordinates
coord_type: 'point' for [x, y] or 'box' for [x, y, w, h]
"""
if coord_type == "point":
return [[x / IMG_WIDTH, y / IMG_HEIGHT] for x, y in coords]
elif coord_type == "box":
return [
[x / IMG_WIDTH, y / IMG_HEIGHT, w / IMG_WIDTH, h / IMG_HEIGHT]
for x, y, w, h in coords
]
else:
raise ValueError(f"Unknown coord_type: {coord_type}")In [ ]:
# "video_path" needs to be either a JPEG folder or a MP4 video file
video_path = f"{sam3_root}/assets/videos/0001"In [ ]:
# load "video_frames_for_vis" for visualization purposes (they are not used by the model)
if isinstance(video_path, str) and video_path.endswith(".mp4"):
cap = cv2.VideoCapture(video_path)
video_frames_for_vis = []
while True:
ret, frame = cap.read()
if not ret:
break
video_frames_for_vis.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
cap.release()
else:
video_frames_for_vis = glob.glob(os.path.join(video_path, "*.jpg"))
try:
# integer sort instead of string sort (so that e.g. "2.jpg" is before "11.jpg")
video_frames_for_vis.sort(
key=lambda p: int(os.path.splitext(os.path.basename(p))[0])
)
except ValueError:
# fallback to lexicographic sort if the format is not "<frame_index>.jpg"
print(
f'frame names are not in "<frame_index>.jpg" format: {video_frames_for_vis[:5]=}, '
f"falling back to lexicographic sort."
)
video_frames_for_vis.sort()In [ ]:
response = predictor.handle_request(
request=dict(
type="start_session",
resource_path=video_path,
)
)
session_id = response["session_id"]In [ ]:
# note: in case you already ran one text prompt and now want to switch to another text prompt
# it's required to reset the session first (otherwise the results would be wrong)
_ = predictor.handle_request(
request=dict(
type="reset_session",
session_id=session_id,
)
)In [ ]:
prompt_text_str = "person"
frame_idx = 0 # add a text prompt on frame 0
response = predictor.handle_request(
request=dict(
type="add_prompt",
session_id=session_id,
frame_index=frame_idx,
text=prompt_text_str,
)
)
out = response["outputs"]
plt.close("all")
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[prepare_masks_for_visualization({frame_idx: out})],
titles=["SAM 3 Dense Tracking outputs"],
figsize=(6, 4),
)In [ ]:
# now we propagate the outputs from frame 0 to the end of the video and collect all outputs
outputs_per_frame = propagate_in_video(predictor, session_id)
# finally, we reformat the outputs for visualization and plot the outputs every 60 frames
outputs_per_frame = prepare_masks_for_visualization(outputs_per_frame)
vis_frame_stride = 60
plt.close("all")
for frame_idx in range(0, len(outputs_per_frame), vis_frame_stride):
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[outputs_per_frame],
titles=["SAM 3 Dense Tracking outputs"],
figsize=(6, 4),
)In [ ]:
# we pick id 2, which is the dancer in the front
obj_id = 2
response = predictor.handle_request(
request=dict(
type="remove_object",
session_id=session_id,
obj_id=obj_id,
)
)In [ ]:
# now we propagate the outputs from frame 0 to the end of the video and collect all outputs
outputs_per_frame = propagate_in_video(predictor, session_id)
# finally, we reformat the outputs for visualization and plot the outputs every 60 frames
outputs_per_frame = prepare_masks_for_visualization(outputs_per_frame)
vis_frame_stride = 60
plt.close("all")
for frame_idx in range(0, len(outputs_per_frame), vis_frame_stride):
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[outputs_per_frame],
titles=["SAM 3 Dense Tracking outputs"],
figsize=(6, 4),
)In [ ]:
sample_img = Image.fromarray(load_frame(video_frames_for_vis[0]))
IMG_WIDTH, IMG_HEIGHT = sample_img.sizeIn [ ]:
# let's add back the dancer via point prompts.
# we will use a single positive click to add the dancer back.
frame_idx = 0
obj_id = 2
points_abs = np.array(
[
[760, 550], # positive click
]
)
# positive clicks have label 1, while negative clicks have label 0
labels = np.array([1])In [ ]:
# convert points and labels to tensors; also convert to relative coordinates
points_tensor = torch.tensor(
abs_to_rel_coords(points_abs, IMG_WIDTH, IMG_HEIGHT, coord_type="point"),
dtype=torch.float32,
)
points_labels_tensor = torch.tensor(labels, dtype=torch.int32)
response = predictor.handle_request(
request=dict(
type="add_prompt",
session_id=session_id,
frame_index=frame_idx,
points=points_tensor,
point_labels=points_labels_tensor,
obj_id=obj_id,
)
)
out = response["outputs"]
plt.close("all")
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[prepare_masks_for_visualization({frame_idx: out})],
titles=["SAM 3 Dense Tracking outputs"],
figsize=(6, 4),
points_list=[points_abs],
points_labels_list=[labels],
)In [ ]:
# now we propagate the outputs from frame 0 to the end of the video and collect all outputs
outputs_per_frame = propagate_in_video(predictor, session_id)
# finally, we reformat the outputs for visualization and plot the outputs every 60 frames
outputs_per_frame = prepare_masks_for_visualization(outputs_per_frame)
vis_frame_stride = 60
plt.close("all")
for frame_idx in range(0, len(outputs_per_frame), vis_frame_stride):
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[outputs_per_frame],
titles=["SAM 3 Dense Tracking outputs"],
figsize=(6, 4),
)In [ ]:
# For the dancer in the front, suppose now we only want to segment her T-shirt instead of her whole body
# we will use 2 positive clicks and 2 negative clicks to select her shirt.
frame_idx = 0
obj_id = 2
points_abs = np.array(
[
[740, 450], # positive click
[760, 630], # negative click
[840, 640], # negative click
[760, 550], # positive click
]
)
# positive clicks have label 1, while negative clicks have label 0
labels = np.array([1, 0, 0, 1])In [ ]:
# convert points and labels to tensors; also convert to relative coordinates
points_tensor = torch.tensor(
abs_to_rel_coords(points_abs, IMG_WIDTH, IMG_HEIGHT, coord_type="point"),
dtype=torch.float32,
)
points_labels_tensor = torch.tensor(labels, dtype=torch.int32)
response = predictor.handle_request(
request=dict(
type="add_prompt",
session_id=session_id,
frame_index=frame_idx,
points=points_tensor,
point_labels=points_labels_tensor,
obj_id=obj_id,
)
)
out = response["outputs"]
plt.close("all")
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[prepare_masks_for_visualization({frame_idx: out})],
titles=["SAM 3 Dense Tracking outputs"],
figsize=(6, 4),
points_list=[points_abs],
points_labels_list=[labels],
)In [ ]:
# now we propagate the outputs from frame 0 to the end of the video and collect all outputs
outputs_per_frame = propagate_in_video(predictor, session_id)
# finally, we reformat the outputs for visualization and plot the outputs every 60 frames
outputs_per_frame = prepare_masks_for_visualization(outputs_per_frame)
vis_frame_stride = 60
plt.close("all")
for frame_idx in range(0, len(outputs_per_frame), vis_frame_stride):
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[outputs_per_frame],
titles=["SAM 3 Dense Tracking outputs"],
figsize=(6, 4),
)In [ ]:
# finally, close the inference session to free its GPU resources
# (you may start a new session on another video)
_ = predictor.handle_request(
request=dict(
type="close_session",
session_id=session_id,
)
)In [ ]:
# after all inference is done, we can shutdown the predictor
# to free up the multi-GPU process group
predictor.shutdown()In [ ]:
In [ ]: