99import pickle
1010from pathlib import Path
1111
12+ from scipy .spatial .transform import Rotation
13+
1214from UIB .utils .logger import StateLogger , ActionLogger
1315from UIB .utils .functions import output_path
1416
@@ -18,19 +20,40 @@ def natural_sort(l):
1820 alphanum_key = lambda key : [convert (c ) for c in re .split ('([0-9]+)' , key )]
1921 return sorted (l , key = alphanum_key )
2022
21- def grab_pip_image (env , ocular_img_type = "inset" ):
23+ def grab_pip_image (env , ocular_img_type = "inset" , dynamic_camera = False ):
2224 # Grab an image from both 'for_testing' camera and 'oculomotor' camera, and display them 'picture-in-picture'
2325
26+ # Define camera to use for agent perception
27+ eye_camera_name = 'oculomotor'
28+
29+ # Define camera to use for visualization
30+ main_camera_name = ('dynamicview' if dynamic_camera else 'backview' ) if 'driving' in env .spec .id else 'for_testing' #for_testing #front #rightview #oculomotor #backview
31+
2432 # Define image size
2533 width , height = env .metadata ["imagesize" ]
2634
2735 # Visualise target plane
2836 if hasattr (env , "target_plane_geom_idx" ):
2937 env .model .geom_rgba [env .target_plane_geom_idx ][- 1 ] = 0.1 #0 #.25 #0.1
3038
39+ # Dynamic camera mode
40+ if dynamic_camera :
41+ if not hasattr (env , "camera_angle" ):
42+ env .camera_angle = - np .pi / 2
43+ env .camera_angle_step = 0
44+ env .camera_pos = np .array ([0 , 1.75 , 0 ])
45+ env .camera_angle_vertical = - np .pi / 2
46+ env .camera_angle -= (0.02 ) - 0.02 * (min ((1 , env .camera_angle_step / 400 ))) #last term: dynamic speed change
47+ env .camera_angle_vertical += 0.005 if 200 <= env .camera_angle_step < 300 else 0 #dynamically adjust vertical camera angle
48+ env .camera_pos [1 ] -= 0.004 if 100 <= env .camera_angle_step < 300 else 0 #dynamically adjust camera position relative to torso
49+ env .camera_pos [2 ] -= 0.001 if 200 <= env .camera_angle_step < 300 else 0 #dynamically adjust camera position relative to torso
50+ env .camera_angle_step += 1
51+ env .sim .model .body_quat [env .sim .model ._body_name2id [main_camera_name ]][:] = Rotation .from_euler ("xyz" , (1.57 , env .camera_angle_vertical , env .camera_angle )).as_quat ()[[3 , 0 , 1 , 2 ]]
52+ env .sim .model .cam_pos [env .sim .model ._camera_name2id [main_camera_name ]][:] = env .camera_pos
53+
3154 # Grab images
32- img = np .flipud (env .sim .render (height = height , width = width , camera_name = 'backview' if 'driving' in env . spec . id else 'for_testing' )) #for_testing #front #rightview #oculomotor #backview
33- ocular_img = np .flipud (env .sim .render (height = env .ocular_image_height , width = env .ocular_image_width , camera_name = 'oculomotor' ))
55+ img = np .flipud (env .sim .render (height = height , width = width , camera_name = main_camera_name ))
56+ ocular_img = np .flipud (env .sim .render (height = env .ocular_image_height , width = env .ocular_image_width , camera_name = eye_camera_name ))
3457
3558 # Disable target plane
3659 if hasattr (env , "target_plane_geom_idx" ):
0 commit comments