Aziz-AI98 commited on
Commit
a2a6ee2
·
verified ·
1 Parent(s): 9042be6

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +270 -0
app.py ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gc
3
+ import cv2
4
+ import numpy as np
5
+ import pandas as pd
6
+ import gradio as gr
7
+ from pathlib import Path
8
+ from ultralytics import YOLO
9
+
10
+ # ============================================================
11
+ # COCO Labels (only what matters for driving)
12
+ # ============================================================
13
+ RELEVANT_CLASSES = {
14
+ "person", "bicycle", "car", "motorcycle", "bus", "truck",
15
+ "traffic light", "stop sign",
16
+ }
17
+
18
+ # ============================================================
19
+ # Model Loading (Cached)
20
+ # ============================================================
21
+ model_cache = {}
22
+
23
+ def get_model(model_name: str):
24
+ """Load YOLOv8 model (cached to avoid reloading)."""
25
+ if model_name not in model_cache:
26
+ model_cache[model_name] = YOLO(model_name)
27
+ return model_cache[model_name]
28
+
29
+ # ============================================================
30
+ # Detection (YOLOv8)
31
+ # ============================================================
32
+ def detect_road_objects(frame, model, score_threshold, imgsz=512):
33
+ """Run YOLOv8 on a frame and return filtered detections."""
34
+ results = model.predict(
35
+ source=frame,
36
+ conf=score_threshold,
37
+ imgsz=imgsz,
38
+ verbose=False,
39
+ device="cpu",
40
+ )
41
+ detections = []
42
+ if not results:
43
+ return detections
44
+
45
+ r = results[0]
46
+ if r.boxes is None:
47
+ return detections
48
+
49
+ boxes = r.boxes.xyxy.cpu().numpy()
50
+ confs = r.boxes.conf.cpu().numpy()
51
+ cls_ids = r.boxes.cls.cpu().numpy().astype(int)
52
+ names = r.names
53
+
54
+ for box, conf, cid in zip(boxes, confs, cls_ids):
55
+ label = names.get(cid, str(cid))
56
+ if label not in RELEVANT_CLASSES:
57
+ continue
58
+ x1, y1, x2, y2 = box.astype(int).tolist()
59
+ detections.append({
60
+ "box": [x1, y1, x2, y2],
61
+ "label": label,
62
+ "confidence": float(conf),
63
+ })
64
+ return detections
65
+
66
+ # ============================================================
67
+ # IoU + Tracker
68
+ # ============================================================
69
+ def iou(a, b):
70
+ ax1, ay1, ax2, ay2 = a
71
+ bx1, by1, bx2, by2 = b
72
+ inter = max(0, min(ax2, bx2) - max(ax1, bx1)) * max(0, min(ay2, by2) - max(ay1, by1))
73
+ union = (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1) - inter
74
+ return inter / union if union > 0 else 0.0
75
+
76
+
77
+ class IoUTracker:
78
+ def __init__(self, threshold=0.30, max_missing=12):
79
+ self.threshold = threshold
80
+ self.max_missing = max_missing
81
+ self.tracks = {}
82
+ self.next_id = 1
83
+
84
+ def update(self, detections):
85
+ used = set()
86
+ for track_id, track in list(self.tracks.items()):
87
+ choices = [
88
+ (iou(track["box"], d["box"]), i)
89
+ for i, d in enumerate(detections)
90
+ if i not in used and d["label"] == track["label"]
91
+ ]
92
+ best, idx = max(choices, default=(0, -1))
93
+ if best >= self.threshold:
94
+ self.tracks[track_id].update(box=detections[idx]["box"], missing=0)
95
+ detections[idx]["track_id"] = track_id
96
+ used.add(idx)
97
+ else:
98
+ self.tracks[track_id]["missing"] += 1
99
+
100
+ self.tracks = {k: v for k, v in self.tracks.items() if v["missing"] <= self.max_missing}
101
+
102
+ for i, d in enumerate(detections):
103
+ if i not in used:
104
+ d["track_id"] = self.next_id
105
+ self.tracks[self.next_id] = {"box": d["box"], "label": d["label"], "missing": 0}
106
+ self.next_id += 1
107
+ return detections
108
+
109
+ # ============================================================
110
+ # Risk Assessment
111
+ # ============================================================
112
+ def risk_for_object(box, label, frame_w, frame_h):
113
+ x1, y1, x2, y2 = box
114
+ area_ratio = ((x2 - x1) * (y2 - y1)) / (frame_w * frame_h)
115
+ center_x = (x1 + x2) / 2
116
+ in_lane = 0.30 * frame_w < center_x < 0.70 * frame_w
117
+ important = label in {"person", "bicycle", "car", "motorcycle", "bus", "truck"}
118
+
119
+ if important and in_lane and area_ratio > 0.075:
120
+ return "DANGER", area_ratio
121
+ if important and (in_lane or area_ratio > 0.025):
122
+ return "CAUTION", area_ratio
123
+ return "SAFE", area_ratio
124
+
125
+ # ============================================================
126
+ # Video Processing Pipeline
127
+ # ============================================================
128
+ def process_driving_video(
129
+ input_video_path,
130
+ model_choice,
131
+ score_threshold,
132
+ iou_threshold,
133
+ max_missing,
134
+ skip_frames,
135
+ imgsz
136
+ ):
137
+ if not input_video_path:
138
+ return None, None, "⚠️ الرجاء رفع فيديو أولاً."
139
+
140
+ model = get_model(model_choice)
141
+ cap = cv2.VideoCapture(input_video_path)
142
+ if not cap.isOpened():
143
+ return None, None, "❌ تعذر فتح الفيديو المرفق."
144
+
145
+ fps = cap.get(cv2.CAP_PROP_FPS) or 25
146
+ w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
147
+ h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
148
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
149
+
150
+ output_path = "driving_assistant_output.mp4"
151
+ csv_path = "driving_events.csv"
152
+
153
+ import imageio.v2 as imageio
154
+ writer = imageio.get_writer(
155
+ output_path,
156
+ fps=fps,
157
+ codec="libx264",
158
+ quality=7,
159
+ pixelformat="yuv420p",
160
+ )
161
+
162
+ tracker = IoUTracker(threshold=iou_threshold, max_missing=max_missing)
163
+ events = []
164
+ frame_no = 0
165
+ analyzed = 0
166
+
167
+ rank = {"SAFE": 0, "CAUTION": 1, "DANGER": 2}
168
+ colors = {"SAFE": (0, 200, 0), "CAUTION": (0, 190, 255), "DANGER": (0, 0, 255)}
169
+ last_annotated = None
170
+
171
+ while True:
172
+ ok, frame = cap.read()
173
+ if not ok:
174
+ break
175
+
176
+ frame_no += 1
177
+
178
+ if frame_no % skip_frames != 0 and last_annotated is not None:
179
+ writer.append_data(cv2.cvtColor(last_annotated, cv2.COLOR_BGR2RGB))
180
+ continue
181
+
182
+ analyzed += 1
183
+ frame_risk = "SAFE"
184
+ detections = detect_road_objects(frame, model, score_threshold, imgsz)
185
+ tracked = tracker.update(detections)
186
+
187
+ for d in tracked:
188
+ risk, area = risk_for_object(d["box"], d["label"], w, h)
189
+ if rank[risk] > rank[frame_risk]:
190
+ frame_risk = risk
191
+
192
+ x1, y1, x2, y2 = d["box"]
193
+ color = colors[risk]
194
+ cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
195
+ cv2.putText(
196
+ frame,
197
+ f"#{d['track_id']} {d['label']} {d['confidence']:.2f} {risk}",
198
+ (x1, max(25, y1 - 8)),
199
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2,
200
+ )
201
+ if risk != "SAFE":
202
+ events.append({
203
+ "frame": frame_no,
204
+ "time_seconds": round(frame_no / fps, 2),
205
+ "track_id": d["track_id"],
206
+ "object": d["label"],
207
+ "risk": risk,
208
+ "box_area_ratio": round(area, 4),
209
+ })
210
+
211
+ cv2.rectangle(frame, (0, 0), (w, 60), colors[frame_risk], -1)
212
+ cv2.putText(
213
+ frame, f"RISK: {frame_risk}", (20, 40),
214
+ cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
215
+ )
216
+
217
+ last_annotated = frame.copy()
218
+ writer.append_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
219
+
220
+ cap.release()
221
+ writer.close()
222
+
223
+ events_df = pd.DataFrame(events).drop_duplicates() if events else pd.DataFrame(columns=["frame", "time_seconds", "track_id", "object", "risk", "box_area_ratio"])
224
+ events_df.to_csv(csv_path, index=False)
225
+
226
+ summary_msg = f"✅ تم الانتهاء بنجاح! تم تحليل {analyzed} إطار من أصل {total_frames}."
227
+ return output_path, csv_path, summary_msg
228
+
229
+ # ============================================================
230
+ # Gradio UI Design
231
+ # ============================================================
232
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
233
+ gr.Markdown("# 🚗 Smart Front-View Driving Assistant")
234
+ gr.Markdown(" نظام مساعد القيادة الذكية باستخدام **YOLOv8** وتتبع الأجسام وتقييم المخاطر المرورية.")
235
+
236
+ with gr.Row():
237
+ with gr.Column(scale=1):
238
+ video_input = gr.Video(label="📹 ارفع فيديو كاميرا القيادة (Dashcam)")
239
+
240
+ model_choice = gr.Dropdown(
241
+ choices=["yolov8n.pt", "yolov8s.pt", "yolov8m.pt"],
242
+ value="yolov8s.pt",
243
+ label="اختر نموذج YOLOv8"
244
+ )
245
+
246
+ with gr.Accordion("⚙️ إعدادات متقدمة", open=False):
247
+ skip_frames = gr.Slider(1, 5, value=1, step=1, label="تخطي الإطارات (لزيادة السرعة)")
248
+ imgsz = gr.Slider(320, 640, value=512, step=96, label="حجم صورة المعالجة (Inference Size)")
249
+ score_threshold = gr.Slider(0.1, 0.9, value=0.35, step=0.05, label="عتبة الثقة (Confidence Threshold)")
250
+ iou_threshold = gr.Slider(0.1, 0.8, value=0.30, step=0.05, label="عتبة التتبع (IoU Threshold)")
251
+ max_missing = gr.Slider(1, 30, value=12, step=1, label="أقصى إطارات مفقودة للتتبع")
252
+
253
+ submit_btn = gr.Button("🚀 بدء التحليل", variant="primary")
254
+
255
+ with gr.Column(scale=1):
256
+ video_output = gr.Video(label="🎬 فيديو النتائج النهائي")
257
+ csv_output = gr.File(label="📥 تحميل تقرير الأحداث (CSV)")
258
+ status_output = gr.Textbox(label="حالة العملية التنفيذية")
259
+
260
+ submit_btn.click(
261
+ fn=process_driving_video,
262
+ inputs=[
263
+ video_input, model_choice, score_threshold,
264
+ iou_threshold, max_missing, skip_frames, imgsz
265
+ ],
266
+ outputs=[video_output, csv_output, status_output]
267
+ )
268
+
269
+ if __name__ == "__main__":
270
+ demo.launch()