Upload visualize.py
Browse files- scripts/visualize.py +105 -0
scripts/visualize.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import h5py
|
| 4 |
+
import imageio
|
| 5 |
+
import argparse
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
METER_PER_PIXEL_X = 0.2
|
| 10 |
+
METER_PER_PIXEL_Y = 0.2
|
| 11 |
+
BUCKET_HALF_X = 2
|
| 12 |
+
BUCKET_HALF_Y = 2
|
| 13 |
+
MAIN_BGR_H = 384
|
| 14 |
+
MAIN_BGR_W = 480
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def excavator_kinematic(qpos, k=6.670, n=2.90746):
|
| 18 |
+
'''根据关节角度、大小臂长度计算末端位姿'''
|
| 19 |
+
# 490: k=6.670, n=2.90746
|
| 20 |
+
boom = qpos[:, 0]
|
| 21 |
+
arm = qpos[:, 1]
|
| 22 |
+
bucket = qpos[:, 2]
|
| 23 |
+
swing = qpos[:, 3]
|
| 24 |
+
x = k * np.sin(boom + 1) + n * np.sin(np.pi - boom - 1 - arm - 1.5)
|
| 25 |
+
z = k * np.cos(boom + 1) - n * np.cos(np.pi - boom - 1 - arm - 1.5)
|
| 26 |
+
|
| 27 |
+
return x, z, swing, bucket
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def resize_with_aspect_ratio(image, width=None, height=None):
|
| 31 |
+
if width is None and height is None:
|
| 32 |
+
return image
|
| 33 |
+
|
| 34 |
+
h, w = image.shape[:2]
|
| 35 |
+
ratio = min(height / h, width / w)
|
| 36 |
+
new_h, new_w = int(h * ratio), int(w * ratio)
|
| 37 |
+
|
| 38 |
+
# keep ratio resize
|
| 39 |
+
resized = cv2.resize(image, (new_w, new_h))
|
| 40 |
+
|
| 41 |
+
# pad size
|
| 42 |
+
pad_top = (height - new_h) // 2
|
| 43 |
+
pad_bottom = height - new_h - pad_top
|
| 44 |
+
pad_left = (width - new_w) // 2
|
| 45 |
+
pad_right = width - new_w - pad_left
|
| 46 |
+
|
| 47 |
+
# pad with zero
|
| 48 |
+
padded = cv2.copyMakeBorder(
|
| 49 |
+
resized, pad_top, pad_bottom, pad_left, pad_right, cv2.BORDER_CONSTANT, value=[0, 0, 0])
|
| 50 |
+
|
| 51 |
+
return padded
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def traj_visualization(data_path, out_dir, fmt='mp4', fps=30):
|
| 55 |
+
# load data
|
| 56 |
+
with h5py.File(data_path) as f:
|
| 57 |
+
elevations = np.array(f['observations']['images']['elevation'])
|
| 58 |
+
joints = np.array(f['observations']['qpos'])
|
| 59 |
+
mains = np.array(f['observations']['images']['main'])
|
| 60 |
+
|
| 61 |
+
assert elevations.shape[0] == joints.shape[0]
|
| 62 |
+
H, W = elevations.shape[1:3]
|
| 63 |
+
|
| 64 |
+
# forward kinemetics
|
| 65 |
+
x = excavator_kinematic(joints, k=6.670, n=2.90746)[0]
|
| 66 |
+
# x = excavator_kinematic(joints, k=3.6957, n=1.62233)[0]
|
| 67 |
+
px = (x / METER_PER_PIXEL_X).astype(np.int32)
|
| 68 |
+
|
| 69 |
+
# visualization
|
| 70 |
+
frames = []
|
| 71 |
+
for i in range(elevations.shape[0]):
|
| 72 |
+
# end pose
|
| 73 |
+
traj_elevation = elevations[i].copy()
|
| 74 |
+
center_u = W // 2
|
| 75 |
+
center_v = H // 2 - px[i]
|
| 76 |
+
traj_elevation[center_v - BUCKET_HALF_X: center_v + BUCKET_HALF_X,
|
| 77 |
+
center_u - BUCKET_HALF_Y: center_u + BUCKET_HALF_Y] = (255, 0, 0)
|
| 78 |
+
|
| 79 |
+
# elevation
|
| 80 |
+
traj_elevation = resize_with_aspect_ratio(traj_elevation, height=MAIN_BGR_H, width=MAIN_BGR_W)
|
| 81 |
+
|
| 82 |
+
# main bgr
|
| 83 |
+
traj_main = mains[i].copy()[..., ::-1]
|
| 84 |
+
traj_main = resize_with_aspect_ratio(traj_main, height=MAIN_BGR_H, width=MAIN_BGR_W)
|
| 85 |
+
traj_img = np.concatenate([traj_main, traj_elevation], axis=1)
|
| 86 |
+
|
| 87 |
+
frames.append(traj_img)
|
| 88 |
+
|
| 89 |
+
# save
|
| 90 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 91 |
+
imageio.mimsave(f"{out_dir}/{data_path.split('/')[-1].split('.')[0]}.{fmt}", frames, fps=fps)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
if __name__=="__main__":
|
| 96 |
+
parser = argparse.ArgumentParser()
|
| 97 |
+
parser.add_argument("--data_path", type=str, required=True, help='path to the h5 file')
|
| 98 |
+
parser.add_argument("--out_dir", type=str, required=True, help='path the output directory')
|
| 99 |
+
parser.add_argument("--format", type=str, required=False, default='mp4', help='format of the output file, mp4 or gif are supported')
|
| 100 |
+
parser.add_argument("--fps", type=str, required=False, default=30, help='FPS of the output file')
|
| 101 |
+
args = parser.parse_args()
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
"/home/netease/data/excavator_motion/490/robot_data_20250430_123028.h5"
|
| 105 |
+
traj_visualization(args.data_path, args.out_dir, args.format)
|