IntentDrive / backend /app /legacy /map_renderer.py
sajith-0701
Deploy FastAPI backend to HF Spaces (Docker SDK)
98075af
import matplotlib.pyplot as plt
import json
import os
import numpy as np
DATAROOT = './DataSet'
VERSION = 'v1.0-mini'
def get_map_mask():
"""
Since the vector map expansion (JSON API) is not included in the raw dataset,
we use the actual raw HD Map Raster Masks (PNGs) inherently included in the v1.0-mini dataset.
"""
map_json_path = os.path.join(DATAROOT, VERSION, 'map.json')
try:
with open(map_json_path, 'r') as f:
map_data = json.load(f)
# Grab the first available semantic prior map (binary mask of drivable area)
filename = map_data[0]['filename']
img_path = os.path.join(DATAROOT, filename)
if os.path.exists(img_path):
img = plt.imread(img_path)
return img
else:
print(f"Map image not found at {img_path}")
return None
except Exception as e:
print(f"Error loading map.json: {e}")
return None
def render_map_patch(x_center, y_center, radius=50.0, ax=None):
"""
Simulates extracting an HD map patch by grabbing a corresponding
section of the full-scale dataset map mask and displaying it.
"""
if ax is None:
fig, ax = plt.subplots(figsize=(10, 10))
mask = get_map_mask()
if mask is None:
return ax
# nuScenes standard raster resolution is 10 pixels per meter (0.1m)
pixels_per_meter = 10
# Let's find an interesting visual patch in the massive 20000x20000 map
# We will offset heavily into the image so we don't just see black emptiness
offset_x = 8000
offset_y = 8500
x_min_px = int(offset_x + (x_center - radius) * pixels_per_meter)
x_max_px = int(offset_x + (x_center + radius) * pixels_per_meter)
y_min_px = int(offset_y + (y_center - radius) * pixels_per_meter)
y_max_px = int(offset_y + (y_center + radius) * pixels_per_meter)
# Prevent out of bounds
x_min_px, x_max_px = max(0, x_min_px), min(mask.shape[1], x_max_px)
y_min_px, y_max_px = max(0, y_min_px), min(mask.shape[0], y_max_px)
crop = mask[y_min_px:y_max_px, x_min_px:x_max_px]
# Convert grayscale mask to an RGBA mask to allow custom colors and true transparency in the visual
import numpy as np
# True means drivable area, false is background
colored_mask = np.zeros((crop.shape[0], crop.shape[1], 4), dtype=np.float32)
# Let's paint the drivable area road gray-blue with some opacity (e.g. 0.4)
# The road pixels in the original image are often 1.0 (or close to it)
road_pixels = crop > 0.5
# Paint road pixels (R=0.2, G=0.3, B=0.5, Alpha=0.3 for a technical blueprint look)
colored_mask[road_pixels] = [0.2, 0.3, 0.5, 0.3]
# Background remains perfectly transparent (Alpha=0)
# Use imshow with the explicit RGBA mask
ax.imshow(colored_mask,
extent=[x_center - radius, x_center + radius, y_center - radius, y_center + radius],
origin='lower', zorder=-1) # Z-order ensures map is behind all points
return ax
if __name__ == "__main__":
print("Loading Native HD Map Mask from Raw Dataset...")
fig, ax = plt.subplots(figsize=(10, 10))
# Test rendering a patch at center 0,0
render_map_patch(0, 0, radius=60.0, ax=ax)
# Draw a fake car path on top to prove it works
ax.plot([0, 10, 30],
[0, 5, 15],
'r*-', linewidth=3, markersize=10, label="Vehicle Trajectory")
ax.legend()
plt.grid(True, linestyle='--', alpha=0.5)
plt.title("Phase 3: Dataset-Native HD Map Raster Overlay")
plt.savefig("demo_raw_map.png", bbox_inches='tight')
plt.show()
print("Successfully generated 'demo_raw_map.png' using strictly internal dataset files!")