|
|
import copy |
|
|
import numpy as np |
|
|
from pathlib import Path |
|
|
from trajdata import MapAPI, VectorMap |
|
|
from trajdata.caching.df_cache import DataFrameCache |
|
|
from trajdata.dataset_specific.opendrive import parse_maps |
|
|
from mmdet3d_plugin.datasets.carla.trajdata_rasterizer import rasterize |
|
|
from PIL import Image |
|
|
from trajdata.utils import map_utils |
|
|
|
|
|
patch_size = (102.4, 102.4) |
|
|
canvas_size = (1024, 1024) |
|
|
|
|
|
def carla_init_mapping(): |
|
|
assert canvas_size[0] == canvas_size[1], "not square" |
|
|
assert patch_size[0] == patch_size[1], "not square" |
|
|
pixel_per_meter = canvas_size[0] / patch_size[0] |
|
|
|
|
|
|
|
|
|
|
|
map_data: Dict[str, Dict] = {} |
|
|
map_cache_dir = "./data" |
|
|
town_name_list = [ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"Town05", |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
map_api = MapAPI(map_cache_dir) |
|
|
for town_name in town_name_list: |
|
|
map_id = f"carla:{town_name}" |
|
|
|
|
|
|
|
|
if not (Path(map_cache_dir) / f"carla/maps/{town_name}.pb").exists(): |
|
|
print(f"Attempt to parse CARLA map for {town_name}.") |
|
|
|
|
|
|
|
|
|
|
|
parse_maps.parse_maps( |
|
|
maps_dir_or_file=str( |
|
|
Path(os.environ["CARLA_ROOT"]) |
|
|
/ f"CarlaUE4/Content/Carla/Maps/OpenDrive/{town_name}.xodr" |
|
|
), |
|
|
trajdata_cache_dir=map_cache_dir, |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vec_map_tmp = map_api.get_map( |
|
|
map_id, |
|
|
incl_road_lanes=True, |
|
|
incl_road_areas=True, |
|
|
incl_ped_crosswalks=True, |
|
|
incl_ped_walkways=True, |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
map_img, polylines, raster_from_world = rasterize( |
|
|
vec_map=vec_map_tmp, |
|
|
resolution=pixel_per_meter, |
|
|
return_tf_mat=True, |
|
|
incl_centerlines=False, |
|
|
incl_lane_edges=False, |
|
|
area_color=(255, 255, 255), |
|
|
edge_color=(255, 0, 0), |
|
|
incl_lane_area=True, |
|
|
incl_ego_location=False, |
|
|
incl_ped_walkway=False, |
|
|
incl_ped_crosswalk=False, |
|
|
debug=False, |
|
|
) |
|
|
|
|
|
|
|
|
map_data[map_id] = { |
|
|
"vec_map": vec_map_tmp, |
|
|
"polylines": polylines, |
|
|
"raster_from_world": raster_from_world, |
|
|
"map_img": map_img, |
|
|
} |
|
|
|
|
|
return map_data |
|
|
|
|
|
def get_carla_map_rasterize_semantic(maps, sample_token, input_dict): |
|
|
|
|
|
town_id = sample_token.split('Town')[-1] |
|
|
town_id = town_id.split('_')[0] |
|
|
map_id: str = f"carla:Town{town_id}" |
|
|
map_mask = copy.copy(maps[map_id]["map_img"]) |
|
|
raster_from_world: np.ndarray = maps[map_id][ |
|
|
"raster_from_world" |
|
|
] |
|
|
|
|
|
|
|
|
map_mask = np.max(map_mask, axis=2) |
|
|
map_mask = (map_mask * 255).astype("uint8") |
|
|
map_mask = Image.fromarray(map_mask) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lidar_world: np.ndarray = copy.deepcopy( |
|
|
input_dict["l2g_t"][:, :2].numpy() |
|
|
) |
|
|
lidar_world[0, -1] *= -1 |
|
|
lidar_raster: np.ndarray = map_utils.transform_points( |
|
|
lidar_world, raster_from_world |
|
|
)[ |
|
|
0 |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
map_mask = map_mask.crop( |
|
|
( |
|
|
lidar_raster[0] - int(canvas_size[0] / 2 * 1.5), |
|
|
lidar_raster[1] - int(canvas_size[1] / 2 * 1.5), |
|
|
lidar_raster[0] + int(canvas_size[0] / 2 * 1.5), |
|
|
lidar_raster[1] + int(canvas_size[1] / 2 * 1.5), |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
yaw_angle_ego_global = copy.copy(input_dict["can_bus"][-1]) |
|
|
yaw_angle_ego_global = -1 * yaw_angle_ego_global |
|
|
map_mask = map_mask.rotate(yaw_angle_ego_global) |
|
|
|
|
|
|
|
|
map_mask = map_mask.crop( |
|
|
( |
|
|
int(canvas_size[0] * 0.25), |
|
|
int(canvas_size[1] * 0.25), |
|
|
int(canvas_size[0] * 1.25), |
|
|
int(canvas_size[1] * 1.25), |
|
|
) |
|
|
) |
|
|
map_mask = (np.array(map_mask) / 255).astype("uint8") |
|
|
map_mask = map_mask.reshape( |
|
|
(1, map_mask.shape[0], map_mask.shape[1]) |
|
|
) |
|
|
|
|
|
return map_mask |
|
|
|