|
|
import os |
|
|
|
|
|
os.environ["TF_USE_LEGACY_KERAS"] = "1" |
|
|
|
|
|
import gradio as gr |
|
|
import tensorflow as tf |
|
|
import tf_keras as keras |
|
|
import numpy as np |
|
|
import cv2 |
|
|
from PIL import Image |
|
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
|
|
|
REPO_ID = "mediaportal/Roadsegmentation" |
|
|
MODEL_FILENAME = "trained_model_33_cpu.h5" |
|
|
|
|
|
|
|
|
COLOR_DICT = { |
|
|
0: (128, 128, 128), |
|
|
1: (230, 230, 50), |
|
|
8: (50, 150, 50), |
|
|
10: (128, 180, 255), |
|
|
11: (255, 0, 0), |
|
|
13: (0, 0, 255), |
|
|
19: (0, 0, 0) |
|
|
} |
|
|
|
|
|
hf_token = os.getenv("HF_TOKEN") |
|
|
model = None |
|
|
|
|
|
def load_model(): |
|
|
global model |
|
|
try: |
|
|
path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME, token=hf_token) |
|
|
|
|
|
model = keras.models.load_model(path, compile=False) |
|
|
return "β
Road Segmentation Model Loaded" |
|
|
except Exception as e: |
|
|
return f"β Error: {str(e)}" |
|
|
|
|
|
def segment_road(img): |
|
|
if model is None: |
|
|
return None, None |
|
|
|
|
|
|
|
|
h_orig, w_orig = img.shape[:2] |
|
|
|
|
|
|
|
|
img_resized = cv2.resize(img, (256, 192)) |
|
|
img_array = img_resized.astype('float32') / 255.0 |
|
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
|
|
|
|
|
prediction = model.predict(img_array)[0] |
|
|
|
|
|
|
|
|
mask = np.argmax(prediction, axis=-1).astype(np.uint8) |
|
|
|
|
|
|
|
|
|
|
|
full_mask_color = np.zeros((192, 256, 3), dtype=np.uint8) |
|
|
for class_idx, color in COLOR_DICT.items(): |
|
|
full_mask_color[mask == class_idx] = color |
|
|
|
|
|
|
|
|
road_mask = (mask == 0).astype(np.uint8) * 255 |
|
|
road_mask_resized = cv2.resize(road_mask, (w_orig, h_orig), interpolation=cv2.INTER_NEAREST) |
|
|
|
|
|
overlay = img.copy() |
|
|
overlay[road_mask_resized > 0] = [0, 255, 0] |
|
|
|
|
|
|
|
|
highlighted_road = cv2.addWeighted(img, 0.7, overlay, 0.3, 0) |
|
|
|
|
|
|
|
|
full_mask_resized = cv2.resize(full_mask_color, (w_orig, h_orig), interpolation=cv2.INTER_NEAREST) |
|
|
|
|
|
return highlighted_road, full_mask_resized |
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
|
gr.Markdown("# π ADAS Road & Scene Segmentation") |
|
|
gr.Markdown("Upload a dashboard image to identify the drivable road surface and other objects.") |
|
|
|
|
|
status = gr.Markdown("β³ Initializing system...") |
|
|
|
|
|
with gr.Row(): |
|
|
input_img = gr.Image(label="Input Dashboard Image", type="numpy") |
|
|
output_overlay = gr.Image(label="Drivable Road (Green Highlight)") |
|
|
output_full = gr.Image(label="Full Semantic Map") |
|
|
|
|
|
btn = gr.Button("Analyze Scene", variant="primary") |
|
|
|
|
|
demo.load(load_model, outputs=status) |
|
|
btn.click(fn=segment_road, inputs=input_img, outputs=[output_overlay, output_full]) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.queue().launch() |