facebookresearch / facebookresearch/sam2

I gave same prompt input but makes different result in video and single image prediction

Open
#533 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
19.9k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

`_, _, masks = predictor.add_new_points_or_box(inference_state=state, frame_idx=0, obj_id=0, box=input_box)

# Iterate over frames and save masked images
for local_frame_idx, (frame_idx, object_ids, masks) in enumerate(predictor.propagate_in_video(state)):
mask_to_vis = {}
bbox_to_vis = {}

for obj_id, mask in zip(object_ids, masks):
mask = mask[0].cpu().numpy()
mask = mask > 0.0
non_zero_indices = np.argwhere(mask)
if len(non_zero_indices) == 0:
bbox = [0, 0, 0, 0]
else:
y_min, x_min = non_zero_indices.min(axis=0).tolist()
y_max, x_max = non_zero_indices.max(axis=0).tolist()

bbox = [y_min, x_min, y_max, x_max]
bbox_to_vis[obj_id] = bbox
mask_to_vis[obj_id] = mask

# Load the original frame image
original_image_path = os.path.join(current_images_dir, image_names[local_frame_idx])
original_image = cv2.imread(original_image_path)

# Ensure the mask is the same size as the original image
mask_resized = cv2.resize(mask.astype(np.uint8) * 255, (original_image.shape[1], original_image.shape[0]), interpolation=cv2.INTER_NEAREST)

# Apply the mask to the original image
masked_image = cv2.bitwise_and(original_image, original_image, mask=mask_resized)

# Save the masked image
masked_filename = f"masked_frame_{local_frame_idx+400*batch_idx:04d}.png"
cv2.imwrite(os.path.join(output_path, masked_filename), masked_image)`

I'm currently using SAM2 to segment my custom dataset with both videos and single image. When using the same prompt, the segmentation works fine on a single image, just like in the demo, but when applied to a video, the result doesn't come out as expected, as shown in the image below. When I checked the internal mask data, it seems to have values between -0.05 and 0.05, so I thought it might be a confidence value issue. I tried adjusting the threshold, but the result still doesn't come out as high quality as with a single image and instead shows strange results with patterns. I don't know what the cause of the issue is and need help troubleshooting.

![frame_0121](https://github.com/user-attachments/assets/66203e57-c931-42f4-bb4c-47d6d23bdaa1)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.