diff --git a/metadrive/envs/base_env.py b/metadrive/envs/base_env.py index 05174ec17..590190de3 100644 --- a/metadrive/envs/base_env.py +++ b/metadrive/envs/base_env.py @@ -566,6 +566,8 @@ def reset_sensors(self): for name, sensor in self.engine.sensors.items(): if hasattr(sensor, "track") and name != "main_camera": sensor.track(current_track_agent.origin, [0., 0.8, 1.5], [0, 0.59681, 0]) + # Step the env to avoid the black screen in the first frame. + self.engine.taskMgr.step() def _get_reset_return(self, reset_info): # TODO: figure out how to get the information of the before step diff --git a/metadrive/tests/test_functionality/test_first_frame_depth_cam.py b/metadrive/tests/test_functionality/test_first_frame_depth_cam.py new file mode 100644 index 000000000..259a3790a --- /dev/null +++ b/metadrive/tests/test_functionality/test_first_frame_depth_cam.py @@ -0,0 +1,70 @@ +import tqdm + +from metadrive.component.sensors.depth_camera import DepthCamera +from metadrive.engine.asset_loader import AssetLoader +from metadrive.envs.scenario_env import ScenarioEnv +from metadrive.policy.replay_policy import ReplayEgoCarPolicy + + +def test_first_frame_depth_cam(): + env = ScenarioEnv( + { + # To enable onscreen rendering, set this config to True. + "use_render": False, + + # !!!!! To enable offscreen rendering, set this config to True !!!!! + "image_observation": True, + "render_pipeline": False, + + # ===== The scenario and MetaDrive config ===== + "agent_policy": ReplayEgoCarPolicy, + "no_traffic": False, + "sequential_seed": True, + "reactive_traffic": False, + "start_scenario_index": 0, + "num_scenarios": 10, + "horizon": 1000, + "no_static_vehicles": False, + "vehicle_config": dict( + show_navi_mark=False, + use_special_color=False, + image_source="depth_camera", + lidar=dict(num_lasers=120, distance=50), + lane_line_detector=dict(num_lasers=0, distance=50), + side_detector=dict(num_lasers=12, distance=50) + ), + "data_directory": AssetLoader.file_path("nuscenes", unix_style=False), + + # ===== Set some sensor and visualization configs ===== + "daytime": "08:10", + "window_size": (800, 450), + "camera_dist": 0.8, + "camera_height": 1.5, + "camera_pitch": None, + "camera_fov": 60, + "interface_panel": ["semantic_camera"], + "sensors": dict( + # semantic_camera=(SemanticCamera, 1600, 900), + depth_camera=(DepthCamera, 800, 600), + # rgb_camera=(RGBCamera, 800, 600), + ), + + # ===== Remove useless items in the images ===== + "show_logo": False, + "show_fps": False, + "show_interface": True, + } + ) + + for ep in tqdm.trange(5): + env.reset() + for t in range(10000): + img = env.engine.get_sensor("depth_camera").get_image(env.agent) + assert not (img == 255).all() + if t == 5: + break + env.step([1, 0.88]) + + +if __name__ == '__main__': + test_first_frame_depth_cam()