text
stringlengths
0
11.8k
if len(red_cube_masks) > 0:
red_mask = red_cube_masks[0]["mask"]
print("Red cube mask found")
else:
red_mask = None
print("No red cube mask found")
if len(green_cube_masks) > 0:
green_mask = green_cube_masks[0]["mask"]
print("Green cube mask found")
else:
green_mask = None
print("No green cube mask found")
# Now we need to compute 3D positions of these objects
def get_3d_position_from_mask(mask, depth, intrinsics):
"""Get the 3D position from a segmentation mask and depth image"""
if mask is None or not np.any(mask):
return None
# Get pixels where mask is True
ys, xs = np.where(mask)
if len(xs) == 0:
return None
# Get corresponding depth values
depths = depth[ys, xs]
# Filter out invalid depth values
valid = (depths > 0) & np.isfinite(depths)
if not np.any(valid):
return None
xs_valid = xs[valid]
ys_valid = ys[valid]
depths_valid = depths[valid]
# Convert pixel coordinates to camera coordinates
fx = intrinsics[0, 0]
fy = intrinsics[1, 1]
cx = intrinsics[0, 2]
# Code block 1
import numpy as np
# Get the current observation
obs = get_observation()
rgb_image = obs["robot0_robotview"]["images"]["rgb"]
depth_image = obs["robot0_robotview"]["images"]["depth"]
intrinsics = obs["robot0_robotview"]["intrinsics"]
pose_mat = obs["robot0_robotview"]["pose_mat"]
print("Image shape:", rgb_image.shape)
print("Depth image shape:", depth_image.shape)
# Segment each cube
blue_cube_masks = segment_sam3_text_prompt(rgb_image, "blue cube")
red_cube_masks = segment_sam3_text_prompt(rgb_image, "red cube")
green_cube_masks = segment_sam3_text_prompt(rgb_image, "green cube")
print(f"Blue masks: {len(blue_cube_masks)}")
print(f"Red masks: {len(red_cube_masks)}")
print(f"Green masks: {len(green_cube_masks)}")
# Get the best mask for each object (highest score)
def get_best_mask(masks):
if not masks:
return None
# Sort by score descending and take the first
sorted_masks = sorted(masks, key=lambda x: x.get("score", 0), reverse=True)
return sorted_masks[0]["mask"]
blue_mask = get_best_mask(blue_cube_masks)
red_mask = get_best_mask(red_cube_masks)
green_mask = get_best_mask(green_cube_masks)
print(f"Blue mask: {'found' if blue_mask is not None else 'not found'}")
print(f"Red mask: {'found' if red_mask is not None else 'not found'}")
print(f"Green mask: {'found' if green_mask is not None else 'not found'}")
# Function to get 3D position from mask
def get_3d_position_from_mask(mask, depth, intrinsics):
"""Get the 3D center position from a segmentation mask and depth image"""
if mask is None or not np.any(mask):
return None
ys, xs = np.where(mask)
if len(xs) == 0:
return None
depths = depth[ys, xs]
valid = (depths > 0) & np.isfinite(depths)
if not np.any(valid):
return None
xs_valid = xs[valid].astype(float)