-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_env.py
73 lines (60 loc) · 3.06 KB
/
random_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os.path as osp
import argparse
import numpy as np
from softgym.registered_env import env_arg_dict, SOFTGYM_ENVS
from softgym.utils.normalized_env import normalize
from softgym.utils.visualization import save_numpy_as_gif
import pyflex
from matplotlib import pyplot as plt
def show_depth():
# render rgb and depth
img, depth = pyflex.render()
img = img.reshape((720, 720, 4))[::-1, :, :3]
depth = depth.reshape((720, 720))[::-1]
# get foreground mask
rgb, depth = pyflex.render_cloth()
depth = depth.reshape(720, 720)[::-1]
# mask = mask[:, :, 3]
# depth[mask == 0] = 0
# show rgb and depth(masked)
depth[depth > 5] = 0
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
axes[0].imshow(img)
axes[1].imshow(depth)
plt.show()
def main():
parser = argparse.ArgumentParser(description='Process some integers.')
# ['PassWater', 'PourWater', 'PourWaterAmount', 'RopeFlatten', 'ClothFold', 'ClothFlatten', 'ClothDrop', 'ClothFoldCrumpled', 'ClothFoldDrop', 'RopeConfiguration']
parser.add_argument('--env_name', type=str, default='ClothFold')
parser.add_argument('--headless', type=int, default=0, help='Whether to run the environment with headless rendering')
parser.add_argument('--num_variations', type=int, default=1, help='Number of environment variations to be generated')
parser.add_argument('--save_video_dir', type=str, default='./data/', help='Path to the saved video')
parser.add_argument('--img_size', type=int, default=720, help='Size of the recorded videos')
parser.add_argument('--test_depth', type=int, default=0, help='If to test the depth rendering by showing it')
args = parser.parse_args()
env_kwargs = env_arg_dict[args.env_name]
# Generate and save the initial states for running this environment for the first time
env_kwargs['use_cached_states'] = False
env_kwargs['save_cached_states'] = False
env_kwargs['num_variations'] = args.num_variations
env_kwargs['render'] = True
env_kwargs['headless'] = args.headless
if not env_kwargs['use_cached_states']:
print('Waiting to generate environment variations. May take 1 minute for each variation...')
env = normalize(SOFTGYM_ENVS[args.env_name](**env_kwargs))
env.reset()
frames = [env.get_image(args.img_size, args.img_size)]
for i in range(env.horizon):
action = env.action_space.sample()
# By default, the environments will apply action repitition. The option of record_continuous_video provides rendering of all
# intermediate frames. Only use this option for visualization as it increases computation.
_, _, _, info = env.step(action, record_continuous_video=True, img_size=args.img_size)
frames.extend(info['flex_env_recorded_frames'])
if args.test_depth:
show_depth()
if args.save_video_dir is not None:
save_name = osp.join(args.save_video_dir, args.env_name + '.gif')
save_numpy_as_gif(np.array(frames), save_name)
print('Video generated and save to {}'.format(save_name))
if __name__ == '__main__':
main()