asrcoddeploy commited on
Commit
d9db32e
·
verified ·
1 Parent(s): 6850888

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from ultralytics import YOLO
5
+
6
+ # Load the model
7
+ model = YOLO('best.pt')
8
+
9
+ def get_intelligence(frame, box, label):
10
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
11
+ w_px = x2 - x1
12
+ h_px = y2 - y1
13
+
14
+ # Distance estimation logic
15
+ dist_m = (2.5 * 700) / w_px
16
+ dist_cat = "Near" if dist_m < 15 else "Medium" if dist_m < 30 else "Far"
17
+
18
+ # Aspect ratio logic for direction
19
+ aspect_ratio = w_px / h_px
20
+ direction = "Front/Rear" if aspect_ratio < 1.2 else "Side View"
21
+
22
+ return dist_cat, direction
23
+
24
+ def predict(img):
25
+ results = model(img, conf=0.5)
26
+ annotated_img = img.copy()
27
+
28
+ status_reports = []
29
+
30
+ for r in results:
31
+ for box in r.boxes:
32
+ cls_id = int(box.cls[0])
33
+ label = r.names[cls_id]
34
+
35
+ dist, direction = get_intelligence(img, box, label)
36
+
37
+ # Create a label for the report
38
+ report = f"Detected {label} ({direction}) - Distance: {dist}"
39
+ status_reports.append(report)
40
+
41
+ # Draw on image
42
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
43
+ cv2.rectangle(annotated_img, (x1, y1), (x2, y2), (0, 255, 0), 3)
44
+ cv2.putText(annotated_img, f"{label} {dist}", (x1, y1 - 10),
45
+ cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
46
+
47
+ return annotated_img, "\n".join(status_reports) if status_reports else "No emergency vehicles detected."
48
+
49
+ # Build the Gradio UI
50
+ demo = gr.Interface(
51
+ fn=predict,
52
+ inputs=gr.Image(type="numpy"),
53
+ outputs=[gr.Image(type="numpy"), gr.Textbox(label="Intelligence Report")],
54
+ title="EVobj: Emergency Vehicle Detection System",
55
+ description="Upload an image of traffic to detect emergency vehicles and estimate proximity."
56
+ )
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()