Skip to content

Commit

Permalink
Add function for rendering top-down trajectory from json file (#460)
Browse files Browse the repository at this point in the history
* update

* format

* format

* format

* format

* format

* allow set state

* _replay_done

* foramt

* fix height error

* panda_replay

* now allow show agent name with top down renderere

* replay stop system

* prefer track

* color

* global light

* update

* fix light pois

* lane follow !

* vehicle color

* format

* cam para

* camera dist

* format

* sync main

Co-authored-by: pengzhenghao <pengzh@ie.cuhk.edu.hk>
  • Loading branch information
QuanyiLi and pengzhenghao committed Jun 29, 2021
1 parent fe5df32 commit 3df10ea
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 28 deletions.
1 change: 1 addition & 0 deletions pgdrive/envs/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use_chase_camera=True,
use_chase_camera_follow_lane=False, # If true, then vision would be more stable.
camera_height=1.8,
camera_dist=7,
prefer_track_agent=None,

# ===== Vehicle =====
Expand Down
1 change: 1 addition & 0 deletions pgdrive/envs/marl_envs/marl_bottleneck.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pgdrive.envs.multi_agent_pgdrive import pygame_replay
from pgdrive.utils.math_utils import clip
from pgdrive.envs.marl_envs.marl_inout_roundabout import LidarStateObservationMARound
from pgdrive.envs.multi_agent_pgdrive import MultiAgentPGDrive
from pgdrive.obs import ObservationType
Expand Down
36 changes: 14 additions & 22 deletions pgdrive/envs/marl_envs/marl_intersection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import copy

from pgdrive.envs.multi_agent_pgdrive import pygame_replay, panda_replay
from pgdrive.envs.marl_envs.marl_inout_roundabout import LidarStateObservationMARound
from pgdrive.envs.multi_agent_pgdrive import MultiAgentPGDrive
from pgdrive.obs import ObservationType
Expand Down Expand Up @@ -204,8 +204,8 @@ def _vis():
},
"show_lidar": False,
},
"fast": True,
"use_render": True,
# "fast": True,
# "use_render": True,
"debug": True,
"manual_control": True,
"num_agents": 48,
Expand All @@ -218,7 +218,7 @@ def _vis():
for i in range(1, 100000):
actions = {k: [0.0, 1.0] for k in env.vehicles.keys()}
if len(env.vehicles) == 1:
actions = {k: [-1.0, 1.0] for k in env.vehicles.keys()}
actions = {k: [-0, 1.0] for k in env.vehicles.keys()}
o, r, d, info = env.step(actions)
for r_ in r.values():
total_r += r_
Expand Down Expand Up @@ -350,29 +350,21 @@ def show_map_and_traj():
env.close()


def pygame_replay():
import json
import pygame
env = MultiAgentIntersectionEnv({"use_topdown": True})
with open("metasvodist_inter_best.json", "r") as f:
traj = json.load(f)
o = env.reset(copy.deepcopy(traj))
frame_count = 0
while True:
o, r, d, i = env.step(env.action_space.sample())
env.pg_world.force_fps.toggle()
env.render(mode="top_down", num_stack=50, film_size=(4000, 4000), history_smooth=0)
pygame.image.save(env._top_down_renderer._runtime, "inter_{}.png".format(frame_count))
frame_count += 1
if len(env.scene_manager.replay_system.restore_episode_info) == 0:
env.close()


if __name__ == "__main__":
# _draw()
_vis()
# _vis_debug_respawn()
# _profiwdle()
# _long_run()
# show_map_and_traj()
# pygame_replay("parking", MultiAgentParkingLotEnv, False, other_traj="metasvodist_parking_best.json")
# panda_replay(
# "parking",
# MultiAgentIntersectionEnv,
# False,
# other_traj="metasvodist_inter.json",
# extra_config={"pg_world_config": {
# "global_light": True
# }}
# )
# pygame_replay()
18 changes: 14 additions & 4 deletions pgdrive/envs/marl_envs/marl_parking_lot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging

from pgdrive.envs.marl_envs.marl_inout_roundabout import LidarStateObservationMARound
from pgdrive.envs.multi_agent_pgdrive import MultiAgentPGDrive, pygame_replay
from pgdrive.envs.multi_agent_pgdrive import MultiAgentPGDrive, pygame_replay, panda_replay
from pgdrive.obs import ObservationType
from pgdrive.scene_creator.blocks.first_block import FirstBlock
from pgdrive.scene_creator.blocks.parking_lot import ParkingLot
Expand Down Expand Up @@ -386,7 +386,8 @@ def _vis():
"show_lidar": False,
},
"pg_world_config": {
"debug_static_world": True
"debug_static_world": True,
"global_light": True
},
"fast": True,
"use_render": True,
Expand Down Expand Up @@ -542,8 +543,17 @@ def _long_run():

if __name__ == "__main__":
# _draw()
# _vis()
_vis()
# _vis_debug_respawn()
_profile()
# _long_run()
pygame_replay("parking", MultiAgentParkingLotEnv, True, other_ckpt="metasvodist_parking_3.json")
# pygame_replay("parking", MultiAgentParkingLotEnv, False, other_traj="metasvodist_parking_best.json")
panda_replay(
"parking",
MultiAgentParkingLotEnv,
False,
other_traj="metasvodist_parking_best.json",
extra_config={"pg_world_config": {
"global_light": True
}}
)
1 change: 1 addition & 0 deletions pgdrive/envs/multi_agent_pgdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ def pygame_replay(name, env_class, save=False, other_traj=None, film_size=(1000,
with open(ckpt, "r") as f:
traj = json.load(f)
o = env.reset(copy.deepcopy(traj))
env.main_camera.set_follow_lane(True)
frame_count = 0
while True:
o, r, d, i = env.step(env.action_space.sample())
Expand Down
4 changes: 3 additions & 1 deletion pgdrive/envs/pgdrive_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ def _after_lazy_init(self):

# for manual_control and main camera type
if (self.config["use_render"] or self.config["use_image"]) and self.config["use_chase_camera"]:
self.main_camera = ChaseCamera(self.pg_world.cam, self.config["camera_height"], 7, self.pg_world)
self.main_camera = ChaseCamera(
self.pg_world.cam, self.config["camera_height"], self.config["camera_dist"], self.pg_world
)
self.main_camera.set_follow_lane(self.config["use_chase_camera_follow_lane"])
self.main_camera.track(self.current_track_vehicle, self.pg_world)
self.pg_world.accept("b", self.bird_view_camera)
Expand Down
1 change: 0 additions & 1 deletion pgdrive/scene_creator/vehicle/base_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,6 @@ def overspeed(self):

@property
def replay_done(self):
print(111)
return self._replay_done if hasattr(self, "_replay_done") else (
self.crash_building or self.crash_vehicle or
# self.on_white_continuous_line or
Expand Down

0 comments on commit 3df10ea

Please sign in to comment.