Skip to content

Commit dc4be79

Browse files
committed
get stereo image directly
1 parent d6f44e8 commit dc4be79

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

examples/manipulation/grasp_eval.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ def load_rl_policy(env, train_cfg, log_dir):
3535
if not checkpoint_files:
3636
raise FileNotFoundError(f"No checkpoint files found in {log_dir}")
3737

38-
last_ckpt = sorted(checkpoint_files)[-1]
38+
try:
39+
*_, last_ckpt = sorted(checkpoint_files)
40+
except ValueError as e:
41+
raise FileNotFoundError(f"No checkpoint files found in {log_dir}") from e
3942
runner.load(last_ckpt)
4043
print(f"Loaded RL checkpoint from {last_ckpt}")
4144

@@ -52,7 +55,10 @@ def load_bc_policy(env, bc_cfg, log_dir):
5255
if not checkpoint_files:
5356
raise FileNotFoundError(f"No checkpoint files found in {log_dir}")
5457

55-
last_ckpt = sorted(checkpoint_files)[-1]
58+
try:
59+
*_, last_ckpt = sorted(checkpoint_files)
60+
except ValueError as e:
61+
raise FileNotFoundError(f"No checkpoint files found in {log_dir}") from e
5662
print(f"Loaded BC checkpoint from {last_ckpt}")
5763
bc_runner.load(last_ckpt)
5864

@@ -61,34 +67,26 @@ def load_bc_policy(env, bc_cfg, log_dir):
6167

6268
def get_stereo_frame(env, step_count):
6369
"""Get stereo frame as numpy array."""
64-
# Get individual camera images
65-
rgb_left, _, _, _ = env.left_cam.render(rgb=True, depth=False)
66-
rgb_right, _, _, _ = env.right_cam.render(rgb=True, depth=False)
67-
68-
# Convert to numpy arrays and format for OpenCV
69-
left_img = rgb_left[0].cpu().numpy() # Take first environment
70-
right_img = rgb_right[0].cpu().numpy() # Take first environment
70+
# Get stacked stereo rgb image (B, 6, H, W)
71+
stacked_stereo_rgb = env.get_stereo_rgb_images(normalize=False).cpu().numpy()[0]
72+
stacked_stereo_rgb = stacked_stereo_rgb.transpose(1, 2, 0)
7173

72-
# Convert from RGB to BGR for OpenCV
74+
# Split stacked stereo rgb image into left and right images
75+
left_img, right_img = np.split(stacked_stereo_rgb, 2, axis=2)
7376
left_img = cv2.cvtColor(left_img, cv2.COLOR_RGB2BGR)
7477
right_img = cv2.cvtColor(right_img, cv2.COLOR_RGB2BGR)
78+
stereo_rgb_img = np.concatenate([left_img, right_img], axis=1)
7579

76-
# Resize images for better display
77-
scale = 2.0
78-
left_img = cv2.resize(left_img, (int(left_img.shape[1] * scale), int(left_img.shape[0] * scale)))
79-
right_img = cv2.resize(right_img, (int(right_img.shape[1] * scale), int(right_img.shape[0] * scale)))
80-
# Concatenate images horizontally (side by side)
81-
stereo_img = np.hstack([left_img, right_img])
8280
# Add a vertical line separator between the two images
8381
separator_x = left_img.shape[1]
8482
cv2.line(
85-
img=stereo_img,
83+
img=stereo_rgb_img,
8684
pt1=(separator_x, 0),
87-
pt2=(separator_x, stereo_img.shape[0]),
85+
pt2=(separator_x, stereo_rgb_img.shape[0]),
8886
color=(255, 255, 255),
8987
thickness=2,
9088
)
91-
return stereo_img
89+
return stereo_rgb_img
9290

9391

9492
def display_stereo_images(env, step_count):

examples/manipulation/grasp_train.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ def load_teacher_policy(env, rl_train_cfg, exp_name):
153153
log_dir = Path("logs") / f"{exp_name + '_' + 'rl'}"
154154
assert log_dir.exists(), f"Log directory {log_dir} does not exist"
155155
checkpoint_files = [f for f in log_dir.iterdir() if re.match(r"model_\d+\.pt", f.name)]
156-
last_ckpt = sorted(checkpoint_files)[-1]
156+
try:
157+
*_, last_ckpt = sorted(checkpoint_files)
158+
except ValueError as e:
159+
raise FileNotFoundError(f"No checkpoint files found in {log_dir}") from e
157160
assert last_ckpt is not None, f"No checkpoint found in {log_dir}"
158161
runner = OnPolicyRunner(env, rl_train_cfg, log_dir, device=gs.device)
159162
runner.load(last_ckpt)

0 commit comments

Comments
 (0)