Update scripts/visualize.py
Browse files- scripts/visualize.py +107 -37
scripts/visualize.py
CHANGED
|
@@ -4,30 +4,94 @@ import h5py
|
|
| 4 |
import imageio
|
| 5 |
import argparse
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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 |
|
|
@@ -51,36 +115,43 @@ def resize_with_aspect_ratio(image, width=None, height=None):
|
|
| 51 |
return padded
|
| 52 |
|
| 53 |
|
| 54 |
-
def traj_visualization(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 66 |
-
|
| 67 |
-
px = (
|
|
|
|
|
|
|
|
|
|
| 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 =
|
| 75 |
-
center_v =
|
| 76 |
-
traj_elevation[center_v -
|
| 77 |
-
center_u -
|
| 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 |
|
|
@@ -94,12 +165,11 @@ def traj_visualization(data_path, out_dir, fmt='mp4', fps=30):
|
|
| 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=
|
| 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)
|
|
|
|
| 4 |
import imageio
|
| 5 |
import argparse
|
| 6 |
import numpy as np
|
| 7 |
+
from dataclasses import dataclass
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
MAIN_BGR_H = 384
|
| 11 |
+
MAIN_BGR_W = 480
|
| 12 |
+
ELEVATION_H = 200
|
| 13 |
+
ELEVATION_W = 200
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@dataclass
|
| 17 |
+
class ExcavatorModel:
|
| 18 |
+
name: str = ''
|
| 19 |
+
|
| 20 |
+
# body
|
| 21 |
+
boom_len: float = 0.0
|
| 22 |
+
arm_len: float = 0.0
|
| 23 |
+
operating_arm_height: float = 0.0
|
| 24 |
+
boom_init_angle: float = 0.0
|
| 25 |
+
arm_init_angle: float = 0.0
|
| 26 |
+
arm_offset_x: float = 0.0
|
| 27 |
+
arm_offset_y: float = 0.0
|
| 28 |
+
|
| 29 |
+
# bucket
|
| 30 |
+
bucket_length: float = 0.0
|
| 31 |
+
bucket_width: float = 0.0
|
| 32 |
+
|
| 33 |
+
# elevation resolution
|
| 34 |
+
meter_per_pixel_u: float = 0.0
|
| 35 |
+
meter_per_pixel_v: float = 0.0
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class ExcavatorKinematics(object):
|
| 39 |
+
def __init__(self, excavator: str):
|
| 40 |
+
self.model = self._get_model(excavator)
|
| 41 |
+
|
| 42 |
+
def _get_model(self, excavator: str) -> ExcavatorModel:
|
| 43 |
+
exc = ExcavatorModel(name=excavator)
|
| 44 |
+
if excavator == '75':
|
| 45 |
+
exc.boom_len = 3.6957
|
| 46 |
+
exc.arm_len = 1.62233
|
| 47 |
+
exc.operating_arm_height = 1.4
|
| 48 |
+
exc.boom_init_angle = 1.0
|
| 49 |
+
exc.arm_init_angle = 1.5
|
| 50 |
+
exc.arm_offset_x = 0.0
|
| 51 |
+
exc.arm_offset_y = -0.1
|
| 52 |
+
exc.bucket_length = 0.8
|
| 53 |
+
exc.bucket_width = 0.6
|
| 54 |
+
exc.meter_per_pixel_u = 0.1
|
| 55 |
+
exc.meter_per_pixel_v = 0.1
|
| 56 |
+
|
| 57 |
+
elif excavator in ('490', '306'):
|
| 58 |
+
exc.boom_len = 6.670
|
| 59 |
+
exc.arm_len = 2.90746
|
| 60 |
+
exc.operating_arm_height = 2.46
|
| 61 |
+
exc.boom_init_angle = 1.0
|
| 62 |
+
exc.arm_init_angle = 1.5
|
| 63 |
+
exc.arm_offset_x = 0.0
|
| 64 |
+
exc.arm_offset_y = 0.0
|
| 65 |
+
exc.bucket_length = 1.5
|
| 66 |
+
exc.bucket_width = 1.2
|
| 67 |
+
exc.meter_per_pixel_u = 0.2
|
| 68 |
+
exc.meter_per_pixel_v = 0.2
|
| 69 |
+
|
| 70 |
+
else:
|
| 71 |
+
raise NotImplementedError
|
| 72 |
+
|
| 73 |
+
return exc
|
| 74 |
+
|
| 75 |
+
def fk(self, joints: np.ndarray) -> np.ndarray:
|
| 76 |
+
assert joints.shape[1] == 4
|
| 77 |
+
|
| 78 |
+
boom = joints[:, 0]
|
| 79 |
+
arm = joints[:, 1]
|
| 80 |
+
x = self.model.boom_len * np.sin(boom + self.model.boom_init_angle) + \
|
| 81 |
+
self.model.arm_len * np.sin(np.pi - boom - self.model.boom_init_angle - arm - self.model.arm_init_angle)
|
| 82 |
+
z = self.model.boom_len * np.cos(boom + self.model.boom_init_angle) - \
|
| 83 |
+
self.model.arm_len * np.cos(np.pi - boom - self.model.boom_init_angle - arm - self.model.arm_init_angle)
|
| 84 |
+
y = np.zeros_like(x)
|
| 85 |
+
xyz = np.stack([x, y, z], axis=1)
|
| 86 |
+
|
| 87 |
+
return xyz
|
| 88 |
|
| 89 |
|
| 90 |
+
def resize_with_aspect_ratio(
|
| 91 |
+
image: np.ndarray,
|
| 92 |
+
width: int=None,
|
| 93 |
+
height: int=None
|
| 94 |
+
) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
if width is None and height is None:
|
| 96 |
return image
|
| 97 |
|
|
|
|
| 115 |
return padded
|
| 116 |
|
| 117 |
|
| 118 |
+
def traj_visualization(
|
| 119 |
+
excavator: str,
|
| 120 |
+
data_path: str,
|
| 121 |
+
out_dir: str,
|
| 122 |
+
fmt: str='mp4',
|
| 123 |
+
fps: int=30
|
| 124 |
+
) -> None:
|
| 125 |
# load data
|
| 126 |
with h5py.File(data_path) as f:
|
| 127 |
elevations = np.array(f['observations']['images']['elevation'])
|
| 128 |
joints = np.array(f['observations']['qpos'])
|
| 129 |
mains = np.array(f['observations']['images']['main'])
|
|
|
|
| 130 |
assert elevations.shape[0] == joints.shape[0]
|
|
|
|
| 131 |
|
| 132 |
# forward kinemetics
|
| 133 |
+
exc = ExcavatorKinematics(excavator)
|
| 134 |
+
xyz = exc.fk(joints)
|
| 135 |
+
px = (xyz[:, 0] / exc.model.meter_per_pixel_u).astype(np.int32)
|
| 136 |
+
|
| 137 |
+
bucket_half_u = int(exc.model.bucket_length / 2 / exc.model.meter_per_pixel_u)
|
| 138 |
+
bucket_half_v = int(exc.model.bucket_width / 2 / exc.model.meter_per_pixel_v)
|
| 139 |
|
| 140 |
# visualization
|
| 141 |
frames = []
|
| 142 |
for i in range(elevations.shape[0]):
|
| 143 |
# end pose
|
| 144 |
traj_elevation = elevations[i].copy()
|
| 145 |
+
center_u = ELEVATION_W // 2
|
| 146 |
+
center_v = ELEVATION_H // 2 - px[i]
|
| 147 |
+
traj_elevation[center_v - bucket_half_v: center_v + bucket_half_v,
|
| 148 |
+
center_u - bucket_half_u: center_u + bucket_half_u] = (255, 0, 0)
|
| 149 |
|
| 150 |
# elevation
|
| 151 |
traj_elevation = resize_with_aspect_ratio(traj_elevation, height=MAIN_BGR_H, width=MAIN_BGR_W)
|
| 152 |
|
| 153 |
# main bgr
|
| 154 |
+
traj_main = mains[i].copy()[..., ::-1] # BGR -> RGB
|
| 155 |
traj_main = resize_with_aspect_ratio(traj_main, height=MAIN_BGR_H, width=MAIN_BGR_W)
|
| 156 |
traj_img = np.concatenate([traj_main, traj_elevation], axis=1)
|
| 157 |
|
|
|
|
| 165 |
|
| 166 |
if __name__=="__main__":
|
| 167 |
parser = argparse.ArgumentParser()
|
| 168 |
+
parser.add_argument("--excavator", type=str, required=True, help='excavator name')
|
| 169 |
parser.add_argument("--data_path", type=str, required=True, help='path to the h5 file')
|
| 170 |
parser.add_argument("--out_dir", type=str, required=True, help='path the output directory')
|
| 171 |
parser.add_argument("--format", type=str, required=False, default='mp4', help='format of the output file, mp4 or gif are supported')
|
| 172 |
+
parser.add_argument("--fps", type=int, required=False, default=30, help='FPS of the output file')
|
| 173 |
args = parser.parse_args()
|
| 174 |
|
| 175 |
+
traj_visualization(args.excavator, args.data_path, args.out_dir, args.format, args.fps)
|
|
|
|
|
|