Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import numpy as np
|
|
| 6 |
from torchvision import transforms
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
# --- 1. MODEL ARCHITECTURE
|
| 10 |
class LDobjModel(nn.Module):
|
| 11 |
def __init__(self):
|
| 12 |
super(LDobjModel, self).__init__()
|
|
@@ -43,7 +43,7 @@ transform = transforms.Compose([
|
|
| 43 |
transforms.ToTensor()
|
| 44 |
])
|
| 45 |
|
| 46 |
-
# --- 3.
|
| 47 |
def analyze_video(input_video_path):
|
| 48 |
if not input_video_path:
|
| 49 |
return None
|
|
@@ -56,25 +56,21 @@ def analyze_video(input_video_path):
|
|
| 56 |
raw_output = "temp_raw.mp4"
|
| 57 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 58 |
out = cv2.VideoWriter(raw_output, fourcc, fps, (width, height))
|
| 59 |
-
|
| 60 |
morph_kernel = np.ones((5, 5), np.uint8)
|
| 61 |
|
| 62 |
while cap.isOpened():
|
| 63 |
ret, frame = cap.read()
|
| 64 |
if not ret: break
|
| 65 |
|
| 66 |
-
# AI Prediction
|
| 67 |
input_img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 68 |
img_tensor = transform(input_img).unsqueeze(0).to(device)
|
| 69 |
with torch.no_grad():
|
| 70 |
pred = model(img_tensor).squeeze().numpy()
|
| 71 |
|
| 72 |
-
# Mask Cleaning
|
| 73 |
mask = (pred > 0.5).astype(np.uint8)
|
| 74 |
mask_full = cv2.resize(mask, (width, height), interpolation=cv2.INTER_NEAREST)
|
| 75 |
mask_full = cv2.morphologyEx(mask_full, cv2.MORPH_OPEN, morph_kernel)
|
| 76 |
|
| 77 |
-
# Departure Alert Logic
|
| 78 |
moments = cv2.moments(mask_full[int(height*0.75):, :])
|
| 79 |
if moments["m00"] > 0 and abs(int(moments["m10"] / moments["m00"]) - width // 2) > (width * 0.1):
|
| 80 |
overlay = frame.copy()
|
|
@@ -84,41 +80,41 @@ def analyze_video(input_video_path):
|
|
| 84 |
|
| 85 |
out.write(frame)
|
| 86 |
|
| 87 |
-
cap.release()
|
| 88 |
-
out.release()
|
| 89 |
-
|
| 90 |
-
# WEB OPTIMIZATION: Convert to H.264 with FastStart for smooth web playback
|
| 91 |
web_output = "ldobj_final.mp4"
|
| 92 |
os.system(f"ffmpeg -y -i {raw_output} -c:v libx264 -pix_fmt yuv420p -movflags +faststart {web_output}")
|
| 93 |
-
|
| 94 |
return web_output
|
| 95 |
|
| 96 |
-
# --- 4.
|
| 97 |
-
# Custom CSS to lock heights and prevent the "screen flicker" during loading
|
| 98 |
custom_css = """
|
| 99 |
#video-container { min-height: 400px; }
|
| 100 |
.gradio-container { background-color: #f7f9fc; }
|
| 101 |
footer { visibility: hidden; }
|
| 102 |
"""
|
| 103 |
|
| 104 |
-
|
|
|
|
| 105 |
gr.HTML("<h1 style='text-align: center; color: #d32f2f;'>🚗 LDobj Safety Interface</h1>")
|
| 106 |
gr.HTML("<p style='text-align: center;'>AI-Powered Lane Departure Detection & Alert System</p>")
|
| 107 |
|
| 108 |
-
with gr.Group():
|
| 109 |
with gr.Row():
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
-
|
|
|
|
| 112 |
run_btn = gr.Button("START AI ANALYSIS", variant="primary")
|
| 113 |
|
| 114 |
with gr.Column(scale=1):
|
| 115 |
-
# We set interactive=False to make it a dedicated player
|
| 116 |
video_out = gr.Video(label="LDobj Alert Output", interactive=False, autoplay=True)
|
| 117 |
|
| 118 |
gr.Markdown("---")
|
| 119 |
-
gr.Markdown("### How it works\n1. **
|
| 120 |
|
| 121 |
run_btn.click(fn=analyze_video, inputs=video_in, outputs=video_out)
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from torchvision import transforms
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# --- 1. MODEL ARCHITECTURE ---
|
| 10 |
class LDobjModel(nn.Module):
|
| 11 |
def __init__(self):
|
| 12 |
super(LDobjModel, self).__init__()
|
|
|
|
| 43 |
transforms.ToTensor()
|
| 44 |
])
|
| 45 |
|
| 46 |
+
# --- 3. PROCESSING LOGIC ---
|
| 47 |
def analyze_video(input_video_path):
|
| 48 |
if not input_video_path:
|
| 49 |
return None
|
|
|
|
| 56 |
raw_output = "temp_raw.mp4"
|
| 57 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 58 |
out = cv2.VideoWriter(raw_output, fourcc, fps, (width, height))
|
|
|
|
| 59 |
morph_kernel = np.ones((5, 5), np.uint8)
|
| 60 |
|
| 61 |
while cap.isOpened():
|
| 62 |
ret, frame = cap.read()
|
| 63 |
if not ret: break
|
| 64 |
|
|
|
|
| 65 |
input_img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 66 |
img_tensor = transform(input_img).unsqueeze(0).to(device)
|
| 67 |
with torch.no_grad():
|
| 68 |
pred = model(img_tensor).squeeze().numpy()
|
| 69 |
|
|
|
|
| 70 |
mask = (pred > 0.5).astype(np.uint8)
|
| 71 |
mask_full = cv2.resize(mask, (width, height), interpolation=cv2.INTER_NEAREST)
|
| 72 |
mask_full = cv2.morphologyEx(mask_full, cv2.MORPH_OPEN, morph_kernel)
|
| 73 |
|
|
|
|
| 74 |
moments = cv2.moments(mask_full[int(height*0.75):, :])
|
| 75 |
if moments["m00"] > 0 and abs(int(moments["m10"] / moments["m00"]) - width // 2) > (width * 0.1):
|
| 76 |
overlay = frame.copy()
|
|
|
|
| 80 |
|
| 81 |
out.write(frame)
|
| 82 |
|
| 83 |
+
cap.release(); out.release()
|
|
|
|
|
|
|
|
|
|
| 84 |
web_output = "ldobj_final.mp4"
|
| 85 |
os.system(f"ffmpeg -y -i {raw_output} -c:v libx264 -pix_fmt yuv420p -movflags +faststart {web_output}")
|
|
|
|
| 86 |
return web_output
|
| 87 |
|
| 88 |
+
# --- 4. FRONTEND DESIGN (Corrected for Gradio 6.0) ---
|
|
|
|
| 89 |
custom_css = """
|
| 90 |
#video-container { min-height: 400px; }
|
| 91 |
.gradio-container { background-color: #f7f9fc; }
|
| 92 |
footer { visibility: hidden; }
|
| 93 |
"""
|
| 94 |
|
| 95 |
+
# Theme and CSS removed from constructor as per the warning
|
| 96 |
+
with gr.Blocks() as app:
|
| 97 |
gr.HTML("<h1 style='text-align: center; color: #d32f2f;'>🚗 LDobj Safety Interface</h1>")
|
| 98 |
gr.HTML("<p style='text-align: center;'>AI-Powered Lane Departure Detection & Alert System</p>")
|
| 99 |
|
| 100 |
+
with gr.Group():
|
| 101 |
with gr.Row():
|
| 102 |
with gr.Column(scale=1):
|
| 103 |
+
# mirror_webcam argument removed to fix TypeError
|
| 104 |
+
video_in = gr.Video(label="Source Dashcam Feed")
|
| 105 |
run_btn = gr.Button("START AI ANALYSIS", variant="primary")
|
| 106 |
|
| 107 |
with gr.Column(scale=1):
|
|
|
|
| 108 |
video_out = gr.Video(label="LDobj Alert Output", interactive=False, autoplay=True)
|
| 109 |
|
| 110 |
gr.Markdown("---")
|
| 111 |
+
gr.Markdown("### How it works\n1. **Monitor:** System analyzes lanes in the background.\n2. **Active Alert:** Warnings only appear when a lane departure is detected.")
|
| 112 |
|
| 113 |
run_btn.click(fn=analyze_video, inputs=video_in, outputs=video_out)
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
+
# Theme and CSS moved here to comply with Gradio 6.0
|
| 117 |
+
app.launch(
|
| 118 |
+
theme=gr.themes.Default(primary_hue="red"),
|
| 119 |
+
css=custom_css
|
| 120 |
+
)
|