-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
203 lines (153 loc) · 5.84 KB
/
Copy pathapp.py
File metadata and controls
203 lines (153 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import cv2 #type: ignore
import streamlit as st #type: ignore
import tempfile
import os
import time
from lane_detection import detect_lane_lines, average_lane_lines, draw_adas_overlay
from object_detection import detect_objects, get_object_summary
st.set_page_config(
page_title="AI-Based Lane Detection & ADAS System",
page_icon="🚗",
layout="wide"
)
st.title("AI-Based Lane Detection & ADAS Warning System")
st.markdown(
"""
Upload a road-driving video and view the ADAS lane detection output frame-by-frame.
"""
)
st.sidebar.header("📁 Upload Road Video")
uploaded_video = st.sidebar.file_uploader(
"",
type=["mp4", "avi", "mov", "mkv"]
)
st.sidebar.markdown("---")
left_col, right_col = st.columns([2.2, 1])
with left_col:
st.subheader("🎥 ADAS Output Preview")
video_placeholder = st.empty()
st.subheader("Object Detection")
obj_col1, obj_col2, obj_col3, obj_col4 = st.columns(4)
with obj_col1:
vehicle_box = st.empty()
with obj_col2:
pedestrian_box = st.empty()
with obj_col3:
traffic_light_box = st.empty()
with obj_col4:
objects_box = st.empty()
with right_col:
st.subheader("ADAS Parameters")
frame_count_box = st.empty()
lane_status_box = st.empty()
offset_box = st.empty()
steering_box = st.empty()
fps_box = st.empty()
detection_box = st.empty()
warning_box = st.empty()
def get_lane_metrics(frame, left_lane, right_lane):
height, width, _ = frame.shape
if left_lane is None or right_lane is None:
return {
"lane_detected": False,
"lane_status": "Lane Not Detected",
"offset_pixels": "N/A",
"steering_advice": "No Lane Data",
"risk_level": "Unknown"
}
lx1, ly1, lx2, ly2 = left_lane
rx1, ry1, rx2, ry2 = right_lane
lane_center = (lx1 + rx1) // 2
vehicle_center = width // 2
offset_pixels = vehicle_center - lane_center
if abs(offset_pixels) < 40:
lane_status = "SAFE"
steering_advice = "Keep Center"
risk_level = "Low"
elif offset_pixels > 40:
lane_status = "WARNING"
steering_advice = "Move LEFT"
risk_level = "Medium"
else:
lane_status = "WARNING"
steering_advice = "Move RIGHT"
risk_level = "Medium"
return {
"lane_detected": True,
"lane_status": lane_status,
"offset_pixels": offset_pixels,
"steering_advice": steering_advice,
"risk_level": risk_level
}
def process_video(video_path):
import lane_detection
lane_detection.prev_left_lane = None
lane_detection.prev_right_lane = None
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
st.error("❌ Could not open the video file.")
return
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
video_fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_count = 0
detected_frames = 0
warning_frames = 0
progress_bar = st.progress(0)
start_time = time.time()
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_count += 1
# frame = cv2.resize(frame, (640, 480))
lines = detect_lane_lines(frame)
left_lane, right_lane = average_lane_lines(frame, lines)
metrics = get_lane_metrics(frame, left_lane, right_lane)
output_frame = draw_adas_overlay(frame, left_lane, right_lane)
output_frame, detected_objects = detect_objects(output_frame)
object_summary = get_object_summary(detected_objects)
if metrics["lane_detected"]:
detected_frames += 1
if metrics["lane_status"] == "WARNING":
warning_frames += 1
frame_rgb = cv2.cvtColor(output_frame, cv2.COLOR_BGR2RGB)
video_placeholder.image(
frame_rgb,
channels="RGB",
use_container_width=True
)
elapsed_time = time.time() - start_time
processing_fps = frame_count / elapsed_time if elapsed_time > 0 else 0
detection_rate = (detected_frames / frame_count) * 100 if frame_count > 0 else 0
frame_count_box.metric("Processed Frames", f"{frame_count}/{total_frames}")
lane_status_box.metric("Lane Status", metrics["lane_status"])
offset_box.metric("Offset Pixels", metrics["offset_pixels"])
steering_box.metric("Steering Advice", metrics["steering_advice"])
fps_box.metric("Processing FPS", round(processing_fps, 2))
detection_box.metric("Detection Rate", f"{detection_rate:.2f}%")
warning_box.metric("Warning Frames", warning_frames)
vehicle_box.metric("Vehicles", object_summary["vehicle_count"])
pedestrian_box.metric("Pedestrians", object_summary["pedestrian_count"])
traffic_light_box.metric("Traffic Lights", object_summary["traffic_light_count"])
objects_box.metric("Objects", object_summary["total_objects"])
if total_frames > 0:
progress_bar.progress(min(frame_count / total_frames, 1.0))
cap.release()
st.success("✅ Video processing completed successfully.")
#st.markdown("### 📌 Video Summary")
#s1, s2, s3 = st.columns(3)
#s1.metric("Original FPS", round(video_fps, 2))
#s2.metric("Resolution", f"{width} × {height}")
#s3.metric("Total Frames", frame_count)
if uploaded_video is not None:
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
temp_file.write(uploaded_video.read())
temp_video_path = temp_file.name
st.sidebar.success("✅ Video uploaded successfully")
st.sidebar.markdown("### ▶ Processing Control")
if st.sidebar.button("▶ Start Lane Detection", use_container_width=True):
process_video(temp_video_path)
else:
st.info("<-- Upload a road-driving video from the sidebar to start.")