Skip to content

Commit

Permalink
Fix a bug that first frame of depth cam is not properly rendered (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhenghao authored Dec 16, 2024
1 parent 4111ff5 commit d29ef38
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions metadrive/envs/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions metadrive/tests/test_sensors/test_first_frame_depth_cam.py
Original file line number Diff line number Diff line change
@@ -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"],
# "show_interface": True,
"sensors": dict(
# semantic_camera=(SemanticCamera, 1600, 900),
depth_camera=(DepthCamera, 800, 600),
# rgb_camera=(RGBCamera, 800, 600),
),
}
)

for ep in tqdm.trange(5):
env.reset()
for t in range(10000):

img = env.engine.get_sensor("depth_camera").perceive(False)
# 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()

0 comments on commit d29ef38

Please sign in to comment.